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>
25 #include <linux/sched/signal.h>
31 #include <trace/events/f2fs.h>
33 static bool __is_cp_guaranteed(struct page
*page
)
35 struct address_space
*mapping
= page
->mapping
;
37 struct f2fs_sb_info
*sbi
;
42 inode
= mapping
->host
;
43 sbi
= F2FS_I_SB(inode
);
45 if (inode
->i_ino
== F2FS_META_INO(sbi
) ||
46 inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
47 S_ISDIR(inode
->i_mode
) ||
53 static void f2fs_read_end_io(struct bio
*bio
)
58 #ifdef CONFIG_F2FS_FAULT_INJECTION
59 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio
)), FAULT_IO
)) {
60 f2fs_show_injection_info(FAULT_IO
);
61 bio
->bi_status
= BLK_STS_IOERR
;
65 if (f2fs_bio_encrypted(bio
)) {
67 fscrypt_release_ctx(bio
->bi_private
);
69 fscrypt_decrypt_bio_pages(bio
->bi_private
, bio
);
74 bio_for_each_segment_all(bvec
, bio
, i
) {
75 struct page
*page
= bvec
->bv_page
;
77 if (!bio
->bi_status
) {
78 if (!PageUptodate(page
))
79 SetPageUptodate(page
);
81 ClearPageUptodate(page
);
89 static void f2fs_write_end_io(struct bio
*bio
)
91 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
95 bio_for_each_segment_all(bvec
, bio
, i
) {
96 struct page
*page
= bvec
->bv_page
;
97 enum count_type type
= WB_DATA_TYPE(page
);
99 if (IS_DUMMY_WRITTEN_PAGE(page
)) {
100 set_page_private(page
, (unsigned long)NULL
);
101 ClearPagePrivate(page
);
103 mempool_free(page
, sbi
->write_io_dummy
);
105 if (unlikely(bio
->bi_status
))
106 f2fs_stop_checkpoint(sbi
, true);
110 fscrypt_pullback_bio_page(&page
, true);
112 if (unlikely(bio
->bi_status
)) {
113 mapping_set_error(page
->mapping
, -EIO
);
114 if (type
== F2FS_WB_CP_DATA
)
115 f2fs_stop_checkpoint(sbi
, true);
118 f2fs_bug_on(sbi
, page
->mapping
== NODE_MAPPING(sbi
) &&
119 page
->index
!= nid_of_node(page
));
121 dec_page_count(sbi
, type
);
122 clear_cold_data(page
);
123 end_page_writeback(page
);
125 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
126 wq_has_sleeper(&sbi
->cp_wait
))
127 wake_up(&sbi
->cp_wait
);
133 * Return true, if pre_bio's bdev is same as its target device.
135 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
136 block_t blk_addr
, struct bio
*bio
)
138 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
141 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
142 if (FDEV(i
).start_blk
<= blk_addr
&&
143 FDEV(i
).end_blk
>= blk_addr
) {
144 blk_addr
-= FDEV(i
).start_blk
;
150 bio_set_dev(bio
, bdev
);
151 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
156 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
160 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
161 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
166 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
167 block_t blk_addr
, struct bio
*bio
)
169 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
170 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
174 * Low-level block read/write IO operations.
176 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
177 struct writeback_control
*wbc
,
178 int npages
, bool is_read
)
182 bio
= f2fs_bio_alloc(sbi
, npages
, true);
184 f2fs_target_device(sbi
, blk_addr
, bio
);
185 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
186 bio
->bi_private
= is_read
? NULL
: sbi
;
188 wbc_init_bio(wbc
, bio
);
193 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
194 struct bio
*bio
, enum page_type type
)
196 if (!is_read_io(bio_op(bio
))) {
199 if (f2fs_sb_mounted_blkzoned(sbi
->sb
) &&
200 current
->plug
&& (type
== DATA
|| type
== NODE
))
201 blk_finish_plug(current
->plug
);
203 if (type
!= DATA
&& type
!= NODE
)
206 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
207 start
%= F2FS_IO_SIZE(sbi
);
212 /* fill dummy pages */
213 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
215 mempool_alloc(sbi
->write_io_dummy
,
216 GFP_NOIO
| __GFP_ZERO
| __GFP_NOFAIL
);
217 f2fs_bug_on(sbi
, !page
);
219 SetPagePrivate(page
);
220 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
222 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
226 * In the NODE case, we lose next block address chain. So, we
227 * need to do checkpoint in f2fs_sync_file.
230 set_sbi_flag(sbi
, SBI_NEED_CP
);
233 if (is_read_io(bio_op(bio
)))
234 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
236 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
240 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
242 struct f2fs_io_info
*fio
= &io
->fio
;
247 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
249 if (is_read_io(fio
->op
))
250 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
252 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
254 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
258 static bool __has_merged_page(struct f2fs_bio_info
*io
,
259 struct inode
*inode
, nid_t ino
, pgoff_t idx
)
261 struct bio_vec
*bvec
;
271 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
273 if (bvec
->bv_page
->mapping
)
274 target
= bvec
->bv_page
;
276 target
= fscrypt_control_page(bvec
->bv_page
);
278 if (idx
!= target
->index
)
281 if (inode
&& inode
== target
->mapping
->host
)
283 if (ino
&& ino
== ino_of_node(target
))
290 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
291 nid_t ino
, pgoff_t idx
, enum page_type type
)
293 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
295 struct f2fs_bio_info
*io
;
298 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
299 io
= sbi
->write_io
[btype
] + temp
;
301 down_read(&io
->io_rwsem
);
302 ret
= __has_merged_page(io
, inode
, ino
, idx
);
303 up_read(&io
->io_rwsem
);
305 /* TODO: use HOT temp only for meta pages now. */
306 if (ret
|| btype
== META
)
312 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
313 enum page_type type
, enum temp_type temp
)
315 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
316 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
318 down_write(&io
->io_rwsem
);
320 /* change META to META_FLUSH in the checkpoint procedure */
321 if (type
>= META_FLUSH
) {
322 io
->fio
.type
= META_FLUSH
;
323 io
->fio
.op
= REQ_OP_WRITE
;
324 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
325 if (!test_opt(sbi
, NOBARRIER
))
326 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
328 __submit_merged_bio(io
);
329 up_write(&io
->io_rwsem
);
332 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
333 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
334 enum page_type type
, bool force
)
338 if (!force
&& !has_merged_page(sbi
, inode
, ino
, idx
, type
))
341 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
343 __f2fs_submit_merged_write(sbi
, type
, temp
);
345 /* TODO: use HOT temp only for meta pages now. */
351 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
353 __submit_merged_write_cond(sbi
, NULL
, 0, 0, type
, true);
356 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
357 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
360 __submit_merged_write_cond(sbi
, inode
, ino
, idx
, type
, false);
363 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
365 f2fs_submit_merged_write(sbi
, DATA
);
366 f2fs_submit_merged_write(sbi
, NODE
);
367 f2fs_submit_merged_write(sbi
, META
);
371 * Fill the locked page with data located in the block address.
372 * A caller needs to unlock the page on failure.
374 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
377 struct page
*page
= fio
->encrypted_page
?
378 fio
->encrypted_page
: fio
->page
;
380 trace_f2fs_submit_page_bio(page
, fio
);
381 f2fs_trace_ios(fio
, 0);
383 /* Allocate a new bio */
384 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
385 1, is_read_io(fio
->op
));
387 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
391 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
393 __submit_bio(fio
->sbi
, bio
, fio
->type
);
395 if (!is_read_io(fio
->op
))
396 inc_page_count(fio
->sbi
, WB_DATA_TYPE(fio
->page
));
400 int f2fs_submit_page_write(struct f2fs_io_info
*fio
)
402 struct f2fs_sb_info
*sbi
= fio
->sbi
;
403 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
404 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
405 struct page
*bio_page
;
408 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
410 down_write(&io
->io_rwsem
);
413 spin_lock(&io
->io_lock
);
414 if (list_empty(&io
->io_list
)) {
415 spin_unlock(&io
->io_lock
);
418 fio
= list_first_entry(&io
->io_list
,
419 struct f2fs_io_info
, list
);
420 list_del(&fio
->list
);
421 spin_unlock(&io
->io_lock
);
424 if (fio
->old_blkaddr
!= NEW_ADDR
)
425 verify_block_addr(sbi
, fio
->old_blkaddr
);
426 verify_block_addr(sbi
, fio
->new_blkaddr
);
428 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
430 /* set submitted = true as a return value */
431 fio
->submitted
= true;
433 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
435 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
436 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
) ||
437 !__same_bdev(sbi
, fio
->new_blkaddr
, io
->bio
)))
438 __submit_merged_bio(io
);
440 if (io
->bio
== NULL
) {
441 if ((fio
->type
== DATA
|| fio
->type
== NODE
) &&
442 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
444 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
447 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
448 BIO_MAX_PAGES
, false);
452 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
453 __submit_merged_bio(io
);
458 wbc_account_io(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
460 io
->last_block_in_bio
= fio
->new_blkaddr
;
461 f2fs_trace_ios(fio
, 0);
463 trace_f2fs_submit_page_write(fio
->page
, fio
);
468 up_write(&io
->io_rwsem
);
472 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
475 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
476 struct fscrypt_ctx
*ctx
= NULL
;
479 if (f2fs_encrypted_file(inode
)) {
480 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
482 return ERR_CAST(ctx
);
484 /* wait the page to be moved by cleaning */
485 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
488 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
491 fscrypt_release_ctx(ctx
);
492 return ERR_PTR(-ENOMEM
);
494 f2fs_target_device(sbi
, blkaddr
, bio
);
495 bio
->bi_end_io
= f2fs_read_end_io
;
496 bio
->bi_private
= ctx
;
497 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
502 /* This can handle encryption stuffs */
503 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
506 struct bio
*bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1);
511 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
515 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
519 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
521 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
525 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
526 base
= get_extra_isize(dn
->inode
);
528 /* Get physical address of data block */
529 addr_array
= blkaddr_in_node(rn
);
530 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
534 * Lock ordering for the change of data block address:
537 * update block addresses in the node page
539 void set_data_blkaddr(struct dnode_of_data
*dn
)
541 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
542 __set_data_blkaddr(dn
);
543 if (set_page_dirty(dn
->node_page
))
544 dn
->node_changed
= true;
547 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
549 dn
->data_blkaddr
= blkaddr
;
550 set_data_blkaddr(dn
);
551 f2fs_update_extent_cache(dn
);
554 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
555 int reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
557 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
563 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
565 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
568 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
569 dn
->ofs_in_node
, count
);
571 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
573 for (; count
> 0; dn
->ofs_in_node
++) {
574 block_t blkaddr
= datablock_addr(dn
->inode
,
575 dn
->node_page
, dn
->ofs_in_node
);
576 if (blkaddr
== NULL_ADDR
) {
577 dn
->data_blkaddr
= NEW_ADDR
;
578 __set_data_blkaddr(dn
);
583 if (set_page_dirty(dn
->node_page
))
584 dn
->node_changed
= true;
588 /* Should keep dn->ofs_in_node unchanged */
589 int reserve_new_block(struct dnode_of_data
*dn
)
591 unsigned int ofs_in_node
= dn
->ofs_in_node
;
594 ret
= reserve_new_blocks(dn
, 1);
595 dn
->ofs_in_node
= ofs_in_node
;
599 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
601 bool need_put
= dn
->inode_page
? false : true;
604 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
608 if (dn
->data_blkaddr
== NULL_ADDR
)
609 err
= reserve_new_block(dn
);
615 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
617 struct extent_info ei
= {0,0,0};
618 struct inode
*inode
= dn
->inode
;
620 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
621 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
625 return f2fs_reserve_block(dn
, index
);
628 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
629 int op_flags
, bool for_write
)
631 struct address_space
*mapping
= inode
->i_mapping
;
632 struct dnode_of_data dn
;
634 struct extent_info ei
= {0,0,0};
637 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
639 return ERR_PTR(-ENOMEM
);
641 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
642 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
646 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
647 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
652 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
657 if (PageUptodate(page
)) {
663 * A new dentry page is allocated but not able to be written, since its
664 * new inode page couldn't be allocated due to -ENOSPC.
665 * In such the case, its blkaddr can be remained as NEW_ADDR.
666 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
668 if (dn
.data_blkaddr
== NEW_ADDR
) {
669 zero_user_segment(page
, 0, PAGE_SIZE
);
670 if (!PageUptodate(page
))
671 SetPageUptodate(page
);
676 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
682 f2fs_put_page(page
, 1);
686 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
688 struct address_space
*mapping
= inode
->i_mapping
;
691 page
= find_get_page(mapping
, index
);
692 if (page
&& PageUptodate(page
))
694 f2fs_put_page(page
, 0);
696 page
= get_read_data_page(inode
, index
, 0, false);
700 if (PageUptodate(page
))
703 wait_on_page_locked(page
);
704 if (unlikely(!PageUptodate(page
))) {
705 f2fs_put_page(page
, 0);
706 return ERR_PTR(-EIO
);
712 * If it tries to access a hole, return an error.
713 * Because, the callers, functions in dir.c and GC, should be able to know
714 * whether this page exists or not.
716 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
719 struct address_space
*mapping
= inode
->i_mapping
;
722 page
= get_read_data_page(inode
, index
, 0, for_write
);
726 /* wait for read completion */
728 if (unlikely(page
->mapping
!= mapping
)) {
729 f2fs_put_page(page
, 1);
732 if (unlikely(!PageUptodate(page
))) {
733 f2fs_put_page(page
, 1);
734 return ERR_PTR(-EIO
);
740 * Caller ensures that this data page is never allocated.
741 * A new zero-filled data page is allocated in the page cache.
743 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
745 * Note that, ipage is set only by make_empty_dir, and if any error occur,
746 * ipage should be released by this function.
748 struct page
*get_new_data_page(struct inode
*inode
,
749 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
751 struct address_space
*mapping
= inode
->i_mapping
;
753 struct dnode_of_data dn
;
756 page
= f2fs_grab_cache_page(mapping
, index
, true);
759 * before exiting, we should make sure ipage will be released
760 * if any error occur.
762 f2fs_put_page(ipage
, 1);
763 return ERR_PTR(-ENOMEM
);
766 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
767 err
= f2fs_reserve_block(&dn
, index
);
769 f2fs_put_page(page
, 1);
775 if (PageUptodate(page
))
778 if (dn
.data_blkaddr
== NEW_ADDR
) {
779 zero_user_segment(page
, 0, PAGE_SIZE
);
780 if (!PageUptodate(page
))
781 SetPageUptodate(page
);
783 f2fs_put_page(page
, 1);
785 /* if ipage exists, blkaddr should be NEW_ADDR */
786 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
787 page
= get_lock_data_page(inode
, index
, true);
792 if (new_i_size
&& i_size_read(inode
) <
793 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
794 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
798 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
800 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
801 struct f2fs_summary sum
;
807 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
810 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
811 dn
->node_page
, dn
->ofs_in_node
);
812 if (dn
->data_blkaddr
== NEW_ADDR
)
815 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
819 get_node_info(sbi
, dn
->nid
, &ni
);
820 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
822 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
823 &sum
, seg_type
, NULL
, false);
824 set_data_blkaddr(dn
);
827 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
829 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
830 f2fs_i_size_write(dn
->inode
,
831 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
835 static inline bool __force_buffered_io(struct inode
*inode
, int rw
)
837 return (f2fs_encrypted_file(inode
) ||
838 (rw
== WRITE
&& test_opt(F2FS_I_SB(inode
), LFS
)) ||
839 F2FS_I_SB(inode
)->s_ndevs
);
842 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
844 struct inode
*inode
= file_inode(iocb
->ki_filp
);
845 struct f2fs_map_blocks map
;
848 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
850 /* convert inline data for Direct I/O*/
852 err
= f2fs_convert_inline_inode(inode
);
857 if (is_inode_flag_set(inode
, FI_NO_PREALLOC
))
860 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
861 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
862 if (map
.m_len
> map
.m_lblk
)
863 map
.m_len
-= map
.m_lblk
;
867 map
.m_next_pgofs
= NULL
;
868 map
.m_next_extent
= NULL
;
869 map
.m_seg_type
= NO_CHECK_TYPE
;
872 map
.m_seg_type
= rw_hint_to_seg_type(iocb
->ki_hint
);
873 flag
= __force_buffered_io(inode
, WRITE
) ?
874 F2FS_GET_BLOCK_PRE_AIO
:
875 F2FS_GET_BLOCK_PRE_DIO
;
878 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
879 err
= f2fs_convert_inline_inode(inode
);
883 if (f2fs_has_inline_data(inode
))
886 flag
= F2FS_GET_BLOCK_PRE_AIO
;
889 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
890 if (map
.m_len
> 0 && err
== -ENOSPC
) {
892 set_inode_flag(inode
, FI_NO_PREALLOC
);
898 static inline void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
900 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
902 down_read(&sbi
->node_change
);
904 up_read(&sbi
->node_change
);
914 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
915 * f2fs_map_blocks structure.
916 * If original data blocks are allocated, then give them to blockdev.
918 * a. preallocate requested block addresses
919 * b. do not use extent cache for better performance
920 * c. give the block addresses to blockdev
922 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
923 int create
, int flag
)
925 unsigned int maxblocks
= map
->m_len
;
926 struct dnode_of_data dn
;
927 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
928 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
929 pgoff_t pgofs
, end_offset
, end
;
930 int err
= 0, ofs
= 1;
931 unsigned int ofs_in_node
, last_ofs_in_node
;
933 struct extent_info ei
= {0,0,0};
935 unsigned int start_pgofs
;
943 /* it only supports block size == page size */
944 pgofs
= (pgoff_t
)map
->m_lblk
;
945 end
= pgofs
+ maxblocks
;
947 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
948 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
949 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
950 map
->m_flags
= F2FS_MAP_MAPPED
;
951 if (map
->m_next_extent
)
952 *map
->m_next_extent
= pgofs
+ map
->m_len
;
958 __do_map_lock(sbi
, flag
, true);
960 /* When reading holes, we need its node page */
961 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
962 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
964 if (flag
== F2FS_GET_BLOCK_BMAP
)
966 if (err
== -ENOENT
) {
968 if (map
->m_next_pgofs
)
970 get_next_page_offset(&dn
, pgofs
);
971 if (map
->m_next_extent
)
972 *map
->m_next_extent
=
973 get_next_page_offset(&dn
, pgofs
);
980 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
981 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
984 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
986 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
988 if (unlikely(f2fs_cp_error(sbi
))) {
992 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
993 if (blkaddr
== NULL_ADDR
) {
995 last_ofs_in_node
= dn
.ofs_in_node
;
998 err
= __allocate_data_block(&dn
,
1001 set_inode_flag(inode
, FI_APPEND_WRITE
);
1005 map
->m_flags
|= F2FS_MAP_NEW
;
1006 blkaddr
= dn
.data_blkaddr
;
1008 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1012 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1014 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1015 blkaddr
== NULL_ADDR
) {
1016 if (map
->m_next_pgofs
)
1017 *map
->m_next_pgofs
= pgofs
+ 1;
1020 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1021 /* for defragment case */
1022 if (map
->m_next_pgofs
)
1023 *map
->m_next_pgofs
= pgofs
+ 1;
1029 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1032 if (map
->m_len
== 0) {
1033 /* preallocated unwritten block should be mapped for fiemap. */
1034 if (blkaddr
== NEW_ADDR
)
1035 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1036 map
->m_flags
|= F2FS_MAP_MAPPED
;
1038 map
->m_pblk
= blkaddr
;
1040 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1041 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1042 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1043 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1054 /* preallocate blocks in batch for one dnode page */
1055 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1056 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1058 dn
.ofs_in_node
= ofs_in_node
;
1059 err
= reserve_new_blocks(&dn
, prealloc
);
1063 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1064 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1068 dn
.ofs_in_node
= end_offset
;
1073 else if (dn
.ofs_in_node
< end_offset
)
1076 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1077 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1078 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1080 f2fs_update_extent_cache_range(&dn
,
1081 start_pgofs
, map
->m_pblk
+ ofs
,
1086 f2fs_put_dnode(&dn
);
1089 __do_map_lock(sbi
, flag
, false);
1090 f2fs_balance_fs(sbi
, dn
.node_changed
);
1095 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1096 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1097 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1099 f2fs_update_extent_cache_range(&dn
,
1100 start_pgofs
, map
->m_pblk
+ ofs
,
1103 if (map
->m_next_extent
)
1104 *map
->m_next_extent
= pgofs
+ 1;
1106 f2fs_put_dnode(&dn
);
1109 __do_map_lock(sbi
, flag
, false);
1110 f2fs_balance_fs(sbi
, dn
.node_changed
);
1113 trace_f2fs_map_blocks(inode
, map
, err
);
1117 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1118 struct buffer_head
*bh
, int create
, int flag
,
1119 pgoff_t
*next_pgofs
, int seg_type
)
1121 struct f2fs_map_blocks map
;
1124 map
.m_lblk
= iblock
;
1125 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1126 map
.m_next_pgofs
= next_pgofs
;
1127 map
.m_next_extent
= NULL
;
1128 map
.m_seg_type
= seg_type
;
1130 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1132 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1133 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1134 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1139 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1140 struct buffer_head
*bh_result
, int create
, int flag
,
1141 pgoff_t
*next_pgofs
)
1143 return __get_data_block(inode
, iblock
, bh_result
, create
,
1148 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1149 struct buffer_head
*bh_result
, int create
)
1151 return __get_data_block(inode
, iblock
, bh_result
, create
,
1152 F2FS_GET_BLOCK_DEFAULT
, NULL
,
1153 rw_hint_to_seg_type(
1154 inode
->i_write_hint
));
1157 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1158 struct buffer_head
*bh_result
, int create
)
1160 /* Block number less than F2FS MAX BLOCKS */
1161 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1164 return __get_data_block(inode
, iblock
, bh_result
, create
,
1165 F2FS_GET_BLOCK_BMAP
, NULL
,
1169 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1171 return (offset
>> inode
->i_blkbits
);
1174 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1176 return (blk
<< inode
->i_blkbits
);
1179 static int f2fs_xattr_fiemap(struct inode
*inode
,
1180 struct fiemap_extent_info
*fieinfo
)
1182 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1184 struct node_info ni
;
1185 __u64 phys
= 0, len
;
1187 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1190 if (f2fs_has_inline_xattr(inode
)) {
1193 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1194 inode
->i_ino
, false);
1198 get_node_info(sbi
, inode
->i_ino
, &ni
);
1200 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1201 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1202 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1203 get_inline_xattr_addrs(inode
));
1206 len
= inline_xattr_size(inode
);
1208 f2fs_put_page(page
, 1);
1210 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1213 flags
|= FIEMAP_EXTENT_LAST
;
1215 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1216 if (err
|| err
== 1)
1221 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1225 get_node_info(sbi
, xnid
, &ni
);
1227 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1228 len
= inode
->i_sb
->s_blocksize
;
1230 f2fs_put_page(page
, 1);
1232 flags
= FIEMAP_EXTENT_LAST
;
1236 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1238 return (err
< 0 ? err
: 0);
1241 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1244 struct buffer_head map_bh
;
1245 sector_t start_blk
, last_blk
;
1247 u64 logical
= 0, phys
= 0, size
= 0;
1251 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1252 ret
= f2fs_precache_extents(inode
);
1257 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1263 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1264 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1268 if (f2fs_has_inline_data(inode
)) {
1269 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1274 if (logical_to_blk(inode
, len
) == 0)
1275 len
= blk_to_logical(inode
, 1);
1277 start_blk
= logical_to_blk(inode
, start
);
1278 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1281 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1282 map_bh
.b_size
= len
;
1284 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1285 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1290 if (!buffer_mapped(&map_bh
)) {
1291 start_blk
= next_pgofs
;
1293 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1294 F2FS_I_SB(inode
)->max_file_blocks
))
1297 flags
|= FIEMAP_EXTENT_LAST
;
1301 if (f2fs_encrypted_inode(inode
))
1302 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1304 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1308 if (start_blk
> last_blk
|| ret
)
1311 logical
= blk_to_logical(inode
, start_blk
);
1312 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1313 size
= map_bh
.b_size
;
1315 if (buffer_unwritten(&map_bh
))
1316 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1318 start_blk
+= logical_to_blk(inode
, size
);
1322 if (fatal_signal_pending(current
))
1330 inode_unlock(inode
);
1335 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1336 * Major change was from block_size == page_size in f2fs by default.
1338 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1339 struct list_head
*pages
, struct page
*page
,
1342 struct bio
*bio
= NULL
;
1343 sector_t last_block_in_bio
= 0;
1344 struct inode
*inode
= mapping
->host
;
1345 const unsigned blkbits
= inode
->i_blkbits
;
1346 const unsigned blocksize
= 1 << blkbits
;
1347 sector_t block_in_file
;
1348 sector_t last_block
;
1349 sector_t last_block_in_file
;
1351 struct f2fs_map_blocks map
;
1357 map
.m_next_pgofs
= NULL
;
1358 map
.m_next_extent
= NULL
;
1359 map
.m_seg_type
= NO_CHECK_TYPE
;
1361 for (; nr_pages
; nr_pages
--) {
1363 page
= list_last_entry(pages
, struct page
, lru
);
1365 prefetchw(&page
->flags
);
1366 list_del(&page
->lru
);
1367 if (add_to_page_cache_lru(page
, mapping
,
1369 readahead_gfp_mask(mapping
)))
1373 block_in_file
= (sector_t
)page
->index
;
1374 last_block
= block_in_file
+ nr_pages
;
1375 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1377 if (last_block
> last_block_in_file
)
1378 last_block
= last_block_in_file
;
1381 * Map blocks using the previous result first.
1383 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1384 block_in_file
> map
.m_lblk
&&
1385 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1389 * Then do more f2fs_map_blocks() calls until we are
1390 * done with this page.
1394 if (block_in_file
< last_block
) {
1395 map
.m_lblk
= block_in_file
;
1396 map
.m_len
= last_block
- block_in_file
;
1398 if (f2fs_map_blocks(inode
, &map
, 0,
1399 F2FS_GET_BLOCK_DEFAULT
))
1400 goto set_error_page
;
1403 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1404 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1405 SetPageMappedToDisk(page
);
1407 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1408 SetPageUptodate(page
);
1412 zero_user_segment(page
, 0, PAGE_SIZE
);
1413 if (!PageUptodate(page
))
1414 SetPageUptodate(page
);
1420 * This page will go to BIO. Do we need to send this
1423 if (bio
&& (last_block_in_bio
!= block_nr
- 1 ||
1424 !__same_bdev(F2FS_I_SB(inode
), block_nr
, bio
))) {
1426 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1430 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
);
1433 goto set_error_page
;
1437 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1438 goto submit_and_realloc
;
1440 last_block_in_bio
= block_nr
;
1444 zero_user_segment(page
, 0, PAGE_SIZE
);
1449 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1457 BUG_ON(pages
&& !list_empty(pages
));
1459 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1463 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1465 struct inode
*inode
= page
->mapping
->host
;
1468 trace_f2fs_readpage(page
, DATA
);
1470 /* If the file has inline data, try to read it directly */
1471 if (f2fs_has_inline_data(inode
))
1472 ret
= f2fs_read_inline_data(inode
, page
);
1474 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1478 static int f2fs_read_data_pages(struct file
*file
,
1479 struct address_space
*mapping
,
1480 struct list_head
*pages
, unsigned nr_pages
)
1482 struct inode
*inode
= mapping
->host
;
1483 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
1485 trace_f2fs_readpages(inode
, page
, nr_pages
);
1487 /* If the file has inline data, skip readpages */
1488 if (f2fs_has_inline_data(inode
))
1491 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1494 static int encrypt_one_page(struct f2fs_io_info
*fio
)
1496 struct inode
*inode
= fio
->page
->mapping
->host
;
1497 gfp_t gfp_flags
= GFP_NOFS
;
1499 if (!f2fs_encrypted_file(inode
))
1502 /* wait for GCed encrypted page writeback */
1503 f2fs_wait_on_block_writeback(fio
->sbi
, fio
->old_blkaddr
);
1506 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1507 PAGE_SIZE
, 0, fio
->page
->index
, gfp_flags
);
1508 if (!IS_ERR(fio
->encrypted_page
))
1511 /* flush pending IOs and wait for a while in the ENOMEM case */
1512 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
1513 f2fs_flush_merged_writes(fio
->sbi
);
1514 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1515 gfp_flags
|= __GFP_NOFAIL
;
1518 return PTR_ERR(fio
->encrypted_page
);
1521 static inline bool check_inplace_update_policy(struct inode
*inode
,
1522 struct f2fs_io_info
*fio
)
1524 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1525 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
1527 if (policy
& (0x1 << F2FS_IPU_FORCE
))
1529 if (policy
& (0x1 << F2FS_IPU_SSR
) && need_SSR(sbi
))
1531 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
1532 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1534 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && need_SSR(sbi
) &&
1535 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1539 * IPU for rewrite async pages
1541 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
1542 fio
&& fio
->op
== REQ_OP_WRITE
&&
1543 !(fio
->op_flags
& REQ_SYNC
) &&
1544 !f2fs_encrypted_inode(inode
))
1547 /* this is only set during fdatasync */
1548 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
1549 is_inode_flag_set(inode
, FI_NEED_IPU
))
1555 bool should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1557 if (f2fs_is_pinned_file(inode
))
1560 /* if this is cold file, we should overwrite to avoid fragmentation */
1561 if (file_is_cold(inode
))
1564 return check_inplace_update_policy(inode
, fio
);
1567 bool should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1569 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1571 if (test_opt(sbi
, LFS
))
1573 if (S_ISDIR(inode
->i_mode
))
1575 if (f2fs_is_atomic_file(inode
))
1578 if (is_cold_data(fio
->page
))
1580 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
1586 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
1588 struct inode
*inode
= fio
->page
->mapping
->host
;
1590 if (should_update_outplace(inode
, fio
))
1593 return should_update_inplace(inode
, fio
);
1596 static inline bool valid_ipu_blkaddr(struct f2fs_io_info
*fio
)
1598 if (fio
->old_blkaddr
== NEW_ADDR
)
1600 if (fio
->old_blkaddr
== NULL_ADDR
)
1605 int do_write_data_page(struct f2fs_io_info
*fio
)
1607 struct page
*page
= fio
->page
;
1608 struct inode
*inode
= page
->mapping
->host
;
1609 struct dnode_of_data dn
;
1610 struct extent_info ei
= {0,0,0};
1611 bool ipu_force
= false;
1614 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1615 if (need_inplace_update(fio
) &&
1616 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
1617 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
1619 if (valid_ipu_blkaddr(fio
)) {
1621 fio
->need_lock
= LOCK_DONE
;
1626 /* Deadlock due to between page->lock and f2fs_lock_op */
1627 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
1630 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1634 fio
->old_blkaddr
= dn
.data_blkaddr
;
1636 /* This page is already truncated */
1637 if (fio
->old_blkaddr
== NULL_ADDR
) {
1638 ClearPageUptodate(page
);
1643 * If current allocation needs SSR,
1644 * it had better in-place writes for updated data.
1646 if (ipu_force
|| (valid_ipu_blkaddr(fio
) && need_inplace_update(fio
))) {
1647 err
= encrypt_one_page(fio
);
1651 set_page_writeback(page
);
1652 f2fs_put_dnode(&dn
);
1653 if (fio
->need_lock
== LOCK_REQ
)
1654 f2fs_unlock_op(fio
->sbi
);
1655 err
= rewrite_data_page(fio
);
1656 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
1657 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1661 if (fio
->need_lock
== LOCK_RETRY
) {
1662 if (!f2fs_trylock_op(fio
->sbi
)) {
1666 fio
->need_lock
= LOCK_REQ
;
1669 err
= encrypt_one_page(fio
);
1673 set_page_writeback(page
);
1675 /* LFS mode write path */
1676 write_data_page(&dn
, fio
);
1677 trace_f2fs_do_write_data_page(page
, OPU
);
1678 set_inode_flag(inode
, FI_APPEND_WRITE
);
1679 if (page
->index
== 0)
1680 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1682 f2fs_put_dnode(&dn
);
1684 if (fio
->need_lock
== LOCK_REQ
)
1685 f2fs_unlock_op(fio
->sbi
);
1689 static int __write_data_page(struct page
*page
, bool *submitted
,
1690 struct writeback_control
*wbc
,
1691 enum iostat_type io_type
)
1693 struct inode
*inode
= page
->mapping
->host
;
1694 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1695 loff_t i_size
= i_size_read(inode
);
1696 const pgoff_t end_index
= ((unsigned long long) i_size
)
1698 loff_t psize
= (page
->index
+ 1) << PAGE_SHIFT
;
1699 unsigned offset
= 0;
1700 bool need_balance_fs
= false;
1702 struct f2fs_io_info fio
= {
1704 .ino
= inode
->i_ino
,
1707 .op_flags
= wbc_to_write_flags(wbc
),
1708 .old_blkaddr
= NULL_ADDR
,
1710 .encrypted_page
= NULL
,
1712 .need_lock
= LOCK_RETRY
,
1717 trace_f2fs_writepage(page
, DATA
);
1719 /* we should bypass data pages to proceed the kworkder jobs */
1720 if (unlikely(f2fs_cp_error(sbi
))) {
1721 mapping_set_error(page
->mapping
, -EIO
);
1725 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1728 if (page
->index
< end_index
)
1732 * If the offset is out-of-range of file size,
1733 * this page does not have to be written to disk.
1735 offset
= i_size
& (PAGE_SIZE
- 1);
1736 if ((page
->index
>= end_index
+ 1) || !offset
)
1739 zero_user_segment(page
, offset
, PAGE_SIZE
);
1741 if (f2fs_is_drop_cache(inode
))
1743 /* we should not write 0'th page having journal header */
1744 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1745 (!wbc
->for_reclaim
&&
1746 available_free_memory(sbi
, BASE_CHECK
))))
1749 /* Dentry blocks are controlled by checkpoint */
1750 if (S_ISDIR(inode
->i_mode
)) {
1751 fio
.need_lock
= LOCK_DONE
;
1752 err
= do_write_data_page(&fio
);
1756 if (!wbc
->for_reclaim
)
1757 need_balance_fs
= true;
1758 else if (has_not_enough_free_secs(sbi
, 0, 0))
1761 set_inode_flag(inode
, FI_HOT_DATA
);
1764 if (f2fs_has_inline_data(inode
)) {
1765 err
= f2fs_write_inline_data(inode
, page
);
1770 if (err
== -EAGAIN
) {
1771 err
= do_write_data_page(&fio
);
1772 if (err
== -EAGAIN
) {
1773 fio
.need_lock
= LOCK_REQ
;
1774 err
= do_write_data_page(&fio
);
1779 file_set_keep_isize(inode
);
1781 down_write(&F2FS_I(inode
)->i_sem
);
1782 if (F2FS_I(inode
)->last_disk_size
< psize
)
1783 F2FS_I(inode
)->last_disk_size
= psize
;
1784 up_write(&F2FS_I(inode
)->i_sem
);
1788 if (err
&& err
!= -ENOENT
)
1792 inode_dec_dirty_pages(inode
);
1794 ClearPageUptodate(page
);
1796 if (wbc
->for_reclaim
) {
1797 f2fs_submit_merged_write_cond(sbi
, inode
, 0, page
->index
, DATA
);
1798 clear_inode_flag(inode
, FI_HOT_DATA
);
1799 remove_dirty_inode(inode
);
1804 if (!S_ISDIR(inode
->i_mode
))
1805 f2fs_balance_fs(sbi
, need_balance_fs
);
1807 if (unlikely(f2fs_cp_error(sbi
))) {
1808 f2fs_submit_merged_write(sbi
, DATA
);
1813 *submitted
= fio
.submitted
;
1818 redirty_page_for_writepage(wbc
, page
);
1820 return AOP_WRITEPAGE_ACTIVATE
;
1825 static int f2fs_write_data_page(struct page
*page
,
1826 struct writeback_control
*wbc
)
1828 return __write_data_page(page
, NULL
, wbc
, FS_DATA_IO
);
1832 * This function was copied from write_cche_pages from mm/page-writeback.c.
1833 * The major change is making write step of cold data page separately from
1834 * warm/hot data page.
1836 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1837 struct writeback_control
*wbc
,
1838 enum iostat_type io_type
)
1842 struct pagevec pvec
;
1844 pgoff_t
uninitialized_var(writeback_index
);
1846 pgoff_t end
; /* Inclusive */
1848 pgoff_t last_idx
= ULONG_MAX
;
1850 int range_whole
= 0;
1853 pagevec_init(&pvec
);
1855 if (get_dirty_pages(mapping
->host
) <=
1856 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
1857 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
1859 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
1861 if (wbc
->range_cyclic
) {
1862 writeback_index
= mapping
->writeback_index
; /* prev offset */
1863 index
= writeback_index
;
1870 index
= wbc
->range_start
>> PAGE_SHIFT
;
1871 end
= wbc
->range_end
>> PAGE_SHIFT
;
1872 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1874 cycled
= 1; /* ignore range_cyclic tests */
1876 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1877 tag
= PAGECACHE_TAG_TOWRITE
;
1879 tag
= PAGECACHE_TAG_DIRTY
;
1881 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1882 tag_pages_for_writeback(mapping
, index
, end
);
1884 while (!done
&& (index
<= end
)) {
1887 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
1892 for (i
= 0; i
< nr_pages
; i
++) {
1893 struct page
*page
= pvec
.pages
[i
];
1894 bool submitted
= false;
1896 done_index
= page
->index
;
1900 if (unlikely(page
->mapping
!= mapping
)) {
1906 if (!PageDirty(page
)) {
1907 /* someone wrote it for us */
1908 goto continue_unlock
;
1911 if (PageWriteback(page
)) {
1912 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1913 f2fs_wait_on_page_writeback(page
,
1916 goto continue_unlock
;
1919 BUG_ON(PageWriteback(page
));
1920 if (!clear_page_dirty_for_io(page
))
1921 goto continue_unlock
;
1923 ret
= __write_data_page(page
, &submitted
, wbc
, io_type
);
1924 if (unlikely(ret
)) {
1926 * keep nr_to_write, since vfs uses this to
1927 * get # of written pages.
1929 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
1933 } else if (ret
== -EAGAIN
) {
1935 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
1937 congestion_wait(BLK_RW_ASYNC
,
1943 done_index
= page
->index
+ 1;
1946 } else if (submitted
) {
1947 last_idx
= page
->index
;
1950 /* give a priority to WB_SYNC threads */
1951 if ((atomic_read(&F2FS_M_SB(mapping
)->wb_sync_req
) ||
1952 --wbc
->nr_to_write
<= 0) &&
1953 wbc
->sync_mode
== WB_SYNC_NONE
) {
1958 pagevec_release(&pvec
);
1962 if (!cycled
&& !done
) {
1965 end
= writeback_index
- 1;
1968 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1969 mapping
->writeback_index
= done_index
;
1971 if (last_idx
!= ULONG_MAX
)
1972 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
1978 int __f2fs_write_data_pages(struct address_space
*mapping
,
1979 struct writeback_control
*wbc
,
1980 enum iostat_type io_type
)
1982 struct inode
*inode
= mapping
->host
;
1983 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1984 struct blk_plug plug
;
1987 /* deal with chardevs and other special file */
1988 if (!mapping
->a_ops
->writepage
)
1991 /* skip writing if there is no dirty page in this inode */
1992 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1995 /* during POR, we don't need to trigger writepage at all. */
1996 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1999 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
2000 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
2001 available_free_memory(sbi
, DIRTY_DENTS
))
2004 /* skip writing during file defragment */
2005 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
2008 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2010 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2011 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2012 atomic_inc(&sbi
->wb_sync_req
);
2013 else if (atomic_read(&sbi
->wb_sync_req
))
2016 blk_start_plug(&plug
);
2017 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
2018 blk_finish_plug(&plug
);
2020 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2021 atomic_dec(&sbi
->wb_sync_req
);
2023 * if some pages were truncated, we cannot guarantee its mapping->host
2024 * to detect pending bios.
2027 remove_dirty_inode(inode
);
2031 wbc
->pages_skipped
+= get_dirty_pages(inode
);
2032 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2036 static int f2fs_write_data_pages(struct address_space
*mapping
,
2037 struct writeback_control
*wbc
)
2039 struct inode
*inode
= mapping
->host
;
2041 return __f2fs_write_data_pages(mapping
, wbc
,
2042 F2FS_I(inode
)->cp_task
== current
?
2043 FS_CP_DATA_IO
: FS_DATA_IO
);
2046 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
2048 struct inode
*inode
= mapping
->host
;
2049 loff_t i_size
= i_size_read(inode
);
2052 down_write(&F2FS_I(inode
)->i_mmap_sem
);
2053 truncate_pagecache(inode
, i_size
);
2054 truncate_blocks(inode
, i_size
, true);
2055 up_write(&F2FS_I(inode
)->i_mmap_sem
);
2059 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
2060 struct page
*page
, loff_t pos
, unsigned len
,
2061 block_t
*blk_addr
, bool *node_changed
)
2063 struct inode
*inode
= page
->mapping
->host
;
2064 pgoff_t index
= page
->index
;
2065 struct dnode_of_data dn
;
2067 bool locked
= false;
2068 struct extent_info ei
= {0,0,0};
2072 * we already allocated all the blocks, so we don't need to get
2073 * the block addresses when there is no need to fill the page.
2075 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
2076 !is_inode_flag_set(inode
, FI_NO_PREALLOC
))
2079 if (f2fs_has_inline_data(inode
) ||
2080 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
2081 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, true);
2085 /* check inline_data */
2086 ipage
= get_node_page(sbi
, inode
->i_ino
);
2087 if (IS_ERR(ipage
)) {
2088 err
= PTR_ERR(ipage
);
2092 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
2094 if (f2fs_has_inline_data(inode
)) {
2095 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
2096 read_inline_data(page
, ipage
);
2097 set_inode_flag(inode
, FI_DATA_EXIST
);
2099 set_inline_node(ipage
);
2101 err
= f2fs_convert_inline_page(&dn
, page
);
2104 if (dn
.data_blkaddr
== NULL_ADDR
)
2105 err
= f2fs_get_block(&dn
, index
);
2107 } else if (locked
) {
2108 err
= f2fs_get_block(&dn
, index
);
2110 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
2111 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
2114 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
2115 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
2116 f2fs_put_dnode(&dn
);
2117 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
2125 /* convert_inline_page can make node_changed */
2126 *blk_addr
= dn
.data_blkaddr
;
2127 *node_changed
= dn
.node_changed
;
2129 f2fs_put_dnode(&dn
);
2132 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, false);
2136 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
2137 loff_t pos
, unsigned len
, unsigned flags
,
2138 struct page
**pagep
, void **fsdata
)
2140 struct inode
*inode
= mapping
->host
;
2141 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2142 struct page
*page
= NULL
;
2143 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
2144 bool need_balance
= false, drop_atomic
= false;
2145 block_t blkaddr
= NULL_ADDR
;
2148 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
2150 if (f2fs_is_atomic_file(inode
) &&
2151 !available_free_memory(sbi
, INMEM_PAGES
)) {
2158 * We should check this at this moment to avoid deadlock on inode page
2159 * and #0 page. The locking rule for inline_data conversion should be:
2160 * lock_page(page #0) -> lock_page(inode_page)
2163 err
= f2fs_convert_inline_inode(inode
);
2169 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2170 * wait_for_stable_page. Will wait that below with our IO control.
2172 page
= f2fs_pagecache_get_page(mapping
, index
,
2173 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
2181 err
= prepare_write_begin(sbi
, page
, pos
, len
,
2182 &blkaddr
, &need_balance
);
2186 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
2188 f2fs_balance_fs(sbi
, true);
2190 if (page
->mapping
!= mapping
) {
2191 /* The page got truncated from under us */
2192 f2fs_put_page(page
, 1);
2197 f2fs_wait_on_page_writeback(page
, DATA
, false);
2199 /* wait for GCed encrypted page writeback */
2200 if (f2fs_encrypted_file(inode
))
2201 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
2203 if (len
== PAGE_SIZE
|| PageUptodate(page
))
2206 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
)) {
2207 zero_user_segment(page
, len
, PAGE_SIZE
);
2211 if (blkaddr
== NEW_ADDR
) {
2212 zero_user_segment(page
, 0, PAGE_SIZE
);
2213 SetPageUptodate(page
);
2215 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
2220 if (unlikely(page
->mapping
!= mapping
)) {
2221 f2fs_put_page(page
, 1);
2224 if (unlikely(!PageUptodate(page
))) {
2232 f2fs_put_page(page
, 1);
2233 f2fs_write_failed(mapping
, pos
+ len
);
2235 drop_inmem_pages_all(sbi
);
2239 static int f2fs_write_end(struct file
*file
,
2240 struct address_space
*mapping
,
2241 loff_t pos
, unsigned len
, unsigned copied
,
2242 struct page
*page
, void *fsdata
)
2244 struct inode
*inode
= page
->mapping
->host
;
2246 trace_f2fs_write_end(inode
, pos
, len
, copied
);
2249 * This should be come from len == PAGE_SIZE, and we expect copied
2250 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2251 * let generic_perform_write() try to copy data again through copied=0.
2253 if (!PageUptodate(page
)) {
2254 if (unlikely(copied
!= len
))
2257 SetPageUptodate(page
);
2262 set_page_dirty(page
);
2264 if (pos
+ copied
> i_size_read(inode
))
2265 f2fs_i_size_write(inode
, pos
+ copied
);
2267 f2fs_put_page(page
, 1);
2268 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2272 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
2275 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
2277 if (offset
& blocksize_mask
)
2280 if (iov_iter_alignment(iter
) & blocksize_mask
)
2286 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2288 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
2289 struct inode
*inode
= mapping
->host
;
2290 size_t count
= iov_iter_count(iter
);
2291 loff_t offset
= iocb
->ki_pos
;
2292 int rw
= iov_iter_rw(iter
);
2295 err
= check_direct_IO(inode
, iter
, offset
);
2299 if (__force_buffered_io(inode
, rw
))
2302 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
2304 down_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
2305 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
2306 up_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
2310 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
2312 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2313 } else if (err
< 0) {
2314 f2fs_write_failed(mapping
, offset
+ count
);
2318 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
2323 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
2324 unsigned int length
)
2326 struct inode
*inode
= page
->mapping
->host
;
2327 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2329 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
2330 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
2333 if (PageDirty(page
)) {
2334 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
2335 dec_page_count(sbi
, F2FS_DIRTY_META
);
2336 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
2337 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
2339 inode_dec_dirty_pages(inode
);
2340 remove_dirty_inode(inode
);
2344 /* This is atomic written page, keep Private */
2345 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2346 return drop_inmem_page(inode
, page
);
2348 set_page_private(page
, 0);
2349 ClearPagePrivate(page
);
2352 int f2fs_release_page(struct page
*page
, gfp_t wait
)
2354 /* If this is dirty page, keep PagePrivate */
2355 if (PageDirty(page
))
2358 /* This is atomic written page, keep Private */
2359 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2362 set_page_private(page
, 0);
2363 ClearPagePrivate(page
);
2368 * This was copied from __set_page_dirty_buffers which gives higher performance
2369 * in very high speed storages. (e.g., pmem)
2371 void f2fs_set_page_dirty_nobuffers(struct page
*page
)
2373 struct address_space
*mapping
= page
->mapping
;
2374 unsigned long flags
;
2376 if (unlikely(!mapping
))
2379 spin_lock(&mapping
->private_lock
);
2380 lock_page_memcg(page
);
2382 spin_unlock(&mapping
->private_lock
);
2384 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
2385 WARN_ON_ONCE(!PageUptodate(page
));
2386 account_page_dirtied(page
, mapping
);
2387 radix_tree_tag_set(&mapping
->page_tree
,
2388 page_index(page
), PAGECACHE_TAG_DIRTY
);
2389 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
2390 unlock_page_memcg(page
);
2392 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
2396 static int f2fs_set_data_page_dirty(struct page
*page
)
2398 struct address_space
*mapping
= page
->mapping
;
2399 struct inode
*inode
= mapping
->host
;
2401 trace_f2fs_set_page_dirty(page
, DATA
);
2403 if (!PageUptodate(page
))
2404 SetPageUptodate(page
);
2406 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
2407 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
2408 register_inmem_page(inode
, page
);
2412 * Previously, this page has been registered, we just
2418 if (!PageDirty(page
)) {
2419 f2fs_set_page_dirty_nobuffers(page
);
2420 update_dirty_page(inode
, page
);
2426 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
2428 struct inode
*inode
= mapping
->host
;
2430 if (f2fs_has_inline_data(inode
))
2433 /* make sure allocating whole blocks */
2434 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2435 filemap_write_and_wait(mapping
);
2437 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
2440 #ifdef CONFIG_MIGRATION
2441 #include <linux/migrate.h>
2443 int f2fs_migrate_page(struct address_space
*mapping
,
2444 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
2446 int rc
, extra_count
;
2447 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
2448 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
2450 BUG_ON(PageWriteback(page
));
2452 /* migrating an atomic written page is safe with the inmem_lock hold */
2453 if (atomic_written
) {
2454 if (mode
!= MIGRATE_SYNC
)
2456 if (!mutex_trylock(&fi
->inmem_lock
))
2461 * A reference is expected if PagePrivate set when move mapping,
2462 * however F2FS breaks this for maintaining dirty page counts when
2463 * truncating pages. So here adjusting the 'extra_count' make it work.
2465 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
2466 rc
= migrate_page_move_mapping(mapping
, newpage
,
2467 page
, NULL
, mode
, extra_count
);
2468 if (rc
!= MIGRATEPAGE_SUCCESS
) {
2470 mutex_unlock(&fi
->inmem_lock
);
2474 if (atomic_written
) {
2475 struct inmem_pages
*cur
;
2476 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
2477 if (cur
->page
== page
) {
2478 cur
->page
= newpage
;
2481 mutex_unlock(&fi
->inmem_lock
);
2486 if (PagePrivate(page
))
2487 SetPagePrivate(newpage
);
2488 set_page_private(newpage
, page_private(page
));
2490 if (mode
!= MIGRATE_SYNC_NO_COPY
)
2491 migrate_page_copy(newpage
, page
);
2493 migrate_page_states(newpage
, page
);
2495 return MIGRATEPAGE_SUCCESS
;
2499 const struct address_space_operations f2fs_dblock_aops
= {
2500 .readpage
= f2fs_read_data_page
,
2501 .readpages
= f2fs_read_data_pages
,
2502 .writepage
= f2fs_write_data_page
,
2503 .writepages
= f2fs_write_data_pages
,
2504 .write_begin
= f2fs_write_begin
,
2505 .write_end
= f2fs_write_end
,
2506 .set_page_dirty
= f2fs_set_data_page_dirty
,
2507 .invalidatepage
= f2fs_invalidate_page
,
2508 .releasepage
= f2fs_release_page
,
2509 .direct_IO
= f2fs_direct_IO
,
2511 #ifdef CONFIG_MIGRATION
2512 .migratepage
= f2fs_migrate_page
,