4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/pagevec.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
21 #include <linux/uio.h>
22 #include <linux/cleancache.h>
23 #include <linux/sched/signal.h>
29 #include <trace/events/f2fs.h>
30 #include <trace/events/android_fs.h>
32 #define NUM_PREALLOC_POST_READ_CTXS 128
34 static struct kmem_cache
*bio_post_read_ctx_cache
;
35 static mempool_t
*bio_post_read_ctx_pool
;
37 static bool __is_cp_guaranteed(struct page
*page
)
39 struct address_space
*mapping
= page
->mapping
;
41 struct f2fs_sb_info
*sbi
;
46 inode
= mapping
->host
;
47 sbi
= F2FS_I_SB(inode
);
49 if (inode
->i_ino
== F2FS_META_INO(sbi
) ||
50 inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
51 S_ISDIR(inode
->i_mode
) ||
57 /* postprocessing steps for read bios */
58 enum bio_post_read_step
{
63 struct bio_post_read_ctx
{
65 struct work_struct work
;
66 unsigned int cur_step
;
67 unsigned int enabled_steps
;
70 static void __read_end_io(struct bio
*bio
)
76 bio_for_each_segment_all(bv
, bio
, i
) {
79 /* PG_error was set if any post_read step failed */
80 if (bio
->bi_status
|| PageError(page
)) {
81 ClearPageUptodate(page
);
84 SetPageUptodate(page
);
89 mempool_free(bio
->bi_private
, bio_post_read_ctx_pool
);
93 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
);
95 static void decrypt_work(struct work_struct
*work
)
97 struct bio_post_read_ctx
*ctx
=
98 container_of(work
, struct bio_post_read_ctx
, work
);
100 fscrypt_decrypt_bio(ctx
->bio
);
102 bio_post_read_processing(ctx
);
105 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
)
107 switch (++ctx
->cur_step
) {
109 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
)) {
110 INIT_WORK(&ctx
->work
, decrypt_work
);
111 fscrypt_enqueue_decrypt_work(&ctx
->work
);
117 __read_end_io(ctx
->bio
);
121 static bool f2fs_bio_post_read_required(struct bio
*bio
)
123 return bio
->bi_private
&& !bio
->bi_status
;
126 static void f2fs_read_end_io(struct bio
*bio
)
128 #ifdef CONFIG_F2FS_FAULT_INJECTION
129 if (time_to_inject(F2FS_P_SB(bio
->bi_io_vec
->bv_page
), FAULT_IO
)) {
130 f2fs_show_injection_info(FAULT_IO
);
131 bio
->bi_status
= BLK_STS_IOERR
;
135 if (f2fs_bio_post_read_required(bio
)) {
136 struct bio_post_read_ctx
*ctx
= bio
->bi_private
;
138 ctx
->cur_step
= STEP_INITIAL
;
139 bio_post_read_processing(ctx
);
146 static void f2fs_write_end_io(struct bio
*bio
)
148 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
149 struct bio_vec
*bvec
;
152 bio_for_each_segment_all(bvec
, bio
, i
) {
153 struct page
*page
= bvec
->bv_page
;
154 enum count_type type
= WB_DATA_TYPE(page
);
156 if (IS_DUMMY_WRITTEN_PAGE(page
)) {
157 set_page_private(page
, (unsigned long)NULL
);
158 ClearPagePrivate(page
);
160 mempool_free(page
, sbi
->write_io_dummy
);
162 if (unlikely(bio
->bi_status
))
163 f2fs_stop_checkpoint(sbi
, true);
167 fscrypt_pullback_bio_page(&page
, true);
169 if (unlikely(bio
->bi_status
)) {
170 mapping_set_error(page
->mapping
, -EIO
);
171 if (type
== F2FS_WB_CP_DATA
)
172 f2fs_stop_checkpoint(sbi
, true);
175 f2fs_bug_on(sbi
, page
->mapping
== NODE_MAPPING(sbi
) &&
176 page
->index
!= nid_of_node(page
));
178 dec_page_count(sbi
, type
);
179 clear_cold_data(page
);
180 end_page_writeback(page
);
182 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
183 wq_has_sleeper(&sbi
->cp_wait
))
184 wake_up(&sbi
->cp_wait
);
190 * Return true, if pre_bio's bdev is same as its target device.
192 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
193 block_t blk_addr
, struct bio
*bio
)
195 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
198 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
199 if (FDEV(i
).start_blk
<= blk_addr
&&
200 FDEV(i
).end_blk
>= blk_addr
) {
201 blk_addr
-= FDEV(i
).start_blk
;
207 bio_set_dev(bio
, bdev
);
208 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
213 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
217 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
218 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
223 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
224 block_t blk_addr
, struct bio
*bio
)
226 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
227 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
231 * Low-level block read/write IO operations.
233 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
234 struct writeback_control
*wbc
,
235 int npages
, bool is_read
,
236 enum page_type type
, enum temp_type temp
)
240 bio
= f2fs_bio_alloc(sbi
, npages
, true);
242 f2fs_target_device(sbi
, blk_addr
, bio
);
244 bio
->bi_end_io
= f2fs_read_end_io
;
245 bio
->bi_private
= NULL
;
247 bio
->bi_end_io
= f2fs_write_end_io
;
248 bio
->bi_private
= sbi
;
249 bio
->bi_write_hint
= io_type_to_rw_hint(sbi
, type
, temp
);
252 wbc_init_bio(wbc
, bio
);
257 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
258 struct bio
*bio
, enum page_type type
)
260 if (!is_read_io(bio_op(bio
))) {
263 if (type
!= DATA
&& type
!= NODE
)
266 if (f2fs_sb_has_blkzoned(sbi
->sb
) && current
->plug
)
267 blk_finish_plug(current
->plug
);
269 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
270 start
%= F2FS_IO_SIZE(sbi
);
275 /* fill dummy pages */
276 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
278 mempool_alloc(sbi
->write_io_dummy
,
279 GFP_NOIO
| __GFP_ZERO
| __GFP_NOFAIL
);
280 f2fs_bug_on(sbi
, !page
);
282 SetPagePrivate(page
);
283 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
285 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
289 * In the NODE case, we lose next block address chain. So, we
290 * need to do checkpoint in f2fs_sync_file.
293 set_sbi_flag(sbi
, SBI_NEED_CP
);
296 if (is_read_io(bio_op(bio
)))
297 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
299 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
303 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
305 struct f2fs_io_info
*fio
= &io
->fio
;
310 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
312 if (is_read_io(fio
->op
))
313 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
315 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
317 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
321 static bool __has_merged_page(struct f2fs_bio_info
*io
,
322 struct inode
*inode
, nid_t ino
, pgoff_t idx
)
324 struct bio_vec
*bvec
;
334 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
336 if (bvec
->bv_page
->mapping
)
337 target
= bvec
->bv_page
;
339 target
= fscrypt_control_page(bvec
->bv_page
);
341 if (idx
!= target
->index
)
344 if (inode
&& inode
== target
->mapping
->host
)
346 if (ino
&& ino
== ino_of_node(target
))
353 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
354 nid_t ino
, pgoff_t idx
, enum page_type type
)
356 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
358 struct f2fs_bio_info
*io
;
361 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
362 io
= sbi
->write_io
[btype
] + temp
;
364 down_read(&io
->io_rwsem
);
365 ret
= __has_merged_page(io
, inode
, ino
, idx
);
366 up_read(&io
->io_rwsem
);
368 /* TODO: use HOT temp only for meta pages now. */
369 if (ret
|| btype
== META
)
375 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
376 enum page_type type
, enum temp_type temp
)
378 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
379 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
381 down_write(&io
->io_rwsem
);
383 /* change META to META_FLUSH in the checkpoint procedure */
384 if (type
>= META_FLUSH
) {
385 io
->fio
.type
= META_FLUSH
;
386 io
->fio
.op
= REQ_OP_WRITE
;
387 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
388 if (!test_opt(sbi
, NOBARRIER
))
389 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
391 __submit_merged_bio(io
);
392 up_write(&io
->io_rwsem
);
395 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
396 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
397 enum page_type type
, bool force
)
401 if (!force
&& !has_merged_page(sbi
, inode
, ino
, idx
, type
))
404 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
406 __f2fs_submit_merged_write(sbi
, type
, temp
);
408 /* TODO: use HOT temp only for meta pages now. */
414 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
416 __submit_merged_write_cond(sbi
, NULL
, 0, 0, type
, true);
419 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
420 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
423 __submit_merged_write_cond(sbi
, inode
, ino
, idx
, type
, false);
426 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
428 f2fs_submit_merged_write(sbi
, DATA
);
429 f2fs_submit_merged_write(sbi
, NODE
);
430 f2fs_submit_merged_write(sbi
, META
);
434 * Fill the locked page with data located in the block address.
435 * A caller needs to unlock the page on failure.
437 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
440 struct page
*page
= fio
->encrypted_page
?
441 fio
->encrypted_page
: fio
->page
;
443 verify_block_addr(fio
, fio
->new_blkaddr
);
444 trace_f2fs_submit_page_bio(page
, fio
);
445 f2fs_trace_ios(fio
, 0);
447 /* Allocate a new bio */
448 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
449 1, is_read_io(fio
->op
), fio
->type
, fio
->temp
);
451 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
455 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
457 __submit_bio(fio
->sbi
, bio
, fio
->type
);
459 if (!is_read_io(fio
->op
))
460 inc_page_count(fio
->sbi
, WB_DATA_TYPE(fio
->page
));
464 int f2fs_submit_page_write(struct f2fs_io_info
*fio
)
466 struct f2fs_sb_info
*sbi
= fio
->sbi
;
467 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
468 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
469 struct page
*bio_page
;
472 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
474 down_write(&io
->io_rwsem
);
477 spin_lock(&io
->io_lock
);
478 if (list_empty(&io
->io_list
)) {
479 spin_unlock(&io
->io_lock
);
482 fio
= list_first_entry(&io
->io_list
,
483 struct f2fs_io_info
, list
);
484 list_del(&fio
->list
);
485 spin_unlock(&io
->io_lock
);
488 if (fio
->old_blkaddr
!= NEW_ADDR
)
489 verify_block_addr(fio
, fio
->old_blkaddr
);
490 verify_block_addr(fio
, fio
->new_blkaddr
);
492 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
494 /* set submitted = true as a return value */
495 fio
->submitted
= true;
497 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
499 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
500 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
) ||
501 !__same_bdev(sbi
, fio
->new_blkaddr
, io
->bio
)))
502 __submit_merged_bio(io
);
504 if (io
->bio
== NULL
) {
505 if ((fio
->type
== DATA
|| fio
->type
== NODE
) &&
506 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
508 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
511 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
512 BIO_MAX_PAGES
, false,
513 fio
->type
, fio
->temp
);
517 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
518 __submit_merged_bio(io
);
523 wbc_account_io(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
525 io
->last_block_in_bio
= fio
->new_blkaddr
;
526 f2fs_trace_ios(fio
, 0);
528 trace_f2fs_submit_page_write(fio
->page
, fio
);
533 up_write(&io
->io_rwsem
);
537 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
540 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
542 struct bio_post_read_ctx
*ctx
;
543 unsigned int post_read_steps
= 0;
545 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
547 return ERR_PTR(-ENOMEM
);
548 f2fs_target_device(sbi
, blkaddr
, bio
);
549 bio
->bi_end_io
= f2fs_read_end_io
;
550 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
552 if (f2fs_encrypted_file(inode
))
553 post_read_steps
|= 1 << STEP_DECRYPT
;
554 if (post_read_steps
) {
555 ctx
= mempool_alloc(bio_post_read_ctx_pool
, GFP_NOFS
);
558 return ERR_PTR(-ENOMEM
);
561 ctx
->enabled_steps
= post_read_steps
;
562 bio
->bi_private
= ctx
;
564 /* wait the page to be moved by cleaning */
565 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
571 /* This can handle encryption stuffs */
572 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
575 struct bio
*bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1);
580 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
584 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
588 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
590 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
594 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
595 base
= get_extra_isize(dn
->inode
);
597 /* Get physical address of data block */
598 addr_array
= blkaddr_in_node(rn
);
599 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
603 * Lock ordering for the change of data block address:
606 * update block addresses in the node page
608 void set_data_blkaddr(struct dnode_of_data
*dn
)
610 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
611 __set_data_blkaddr(dn
);
612 if (set_page_dirty(dn
->node_page
))
613 dn
->node_changed
= true;
616 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
618 dn
->data_blkaddr
= blkaddr
;
619 set_data_blkaddr(dn
);
620 f2fs_update_extent_cache(dn
);
623 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
624 int reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
626 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
632 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
634 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
637 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
638 dn
->ofs_in_node
, count
);
640 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
642 for (; count
> 0; dn
->ofs_in_node
++) {
643 block_t blkaddr
= datablock_addr(dn
->inode
,
644 dn
->node_page
, dn
->ofs_in_node
);
645 if (blkaddr
== NULL_ADDR
) {
646 dn
->data_blkaddr
= NEW_ADDR
;
647 __set_data_blkaddr(dn
);
652 if (set_page_dirty(dn
->node_page
))
653 dn
->node_changed
= true;
657 /* Should keep dn->ofs_in_node unchanged */
658 int reserve_new_block(struct dnode_of_data
*dn
)
660 unsigned int ofs_in_node
= dn
->ofs_in_node
;
663 ret
= reserve_new_blocks(dn
, 1);
664 dn
->ofs_in_node
= ofs_in_node
;
668 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
670 bool need_put
= dn
->inode_page
? false : true;
673 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
677 if (dn
->data_blkaddr
== NULL_ADDR
)
678 err
= reserve_new_block(dn
);
684 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
686 struct extent_info ei
= {0,0,0};
687 struct inode
*inode
= dn
->inode
;
689 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
690 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
694 return f2fs_reserve_block(dn
, index
);
697 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
698 int op_flags
, bool for_write
)
700 struct address_space
*mapping
= inode
->i_mapping
;
701 struct dnode_of_data dn
;
703 struct extent_info ei
= {0,0,0};
706 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
708 return ERR_PTR(-ENOMEM
);
710 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
711 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
715 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
716 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
721 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
726 if (PageUptodate(page
)) {
732 * A new dentry page is allocated but not able to be written, since its
733 * new inode page couldn't be allocated due to -ENOSPC.
734 * In such the case, its blkaddr can be remained as NEW_ADDR.
735 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
737 if (dn
.data_blkaddr
== NEW_ADDR
) {
738 zero_user_segment(page
, 0, PAGE_SIZE
);
739 if (!PageUptodate(page
))
740 SetPageUptodate(page
);
745 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
751 f2fs_put_page(page
, 1);
755 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
757 struct address_space
*mapping
= inode
->i_mapping
;
760 page
= find_get_page(mapping
, index
);
761 if (page
&& PageUptodate(page
))
763 f2fs_put_page(page
, 0);
765 page
= get_read_data_page(inode
, index
, 0, false);
769 if (PageUptodate(page
))
772 wait_on_page_locked(page
);
773 if (unlikely(!PageUptodate(page
))) {
774 f2fs_put_page(page
, 0);
775 return ERR_PTR(-EIO
);
781 * If it tries to access a hole, return an error.
782 * Because, the callers, functions in dir.c and GC, should be able to know
783 * whether this page exists or not.
785 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
788 struct address_space
*mapping
= inode
->i_mapping
;
791 page
= get_read_data_page(inode
, index
, 0, for_write
);
795 /* wait for read completion */
797 if (unlikely(page
->mapping
!= mapping
)) {
798 f2fs_put_page(page
, 1);
801 if (unlikely(!PageUptodate(page
))) {
802 f2fs_put_page(page
, 1);
803 return ERR_PTR(-EIO
);
809 * Caller ensures that this data page is never allocated.
810 * A new zero-filled data page is allocated in the page cache.
812 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
814 * Note that, ipage is set only by make_empty_dir, and if any error occur,
815 * ipage should be released by this function.
817 struct page
*get_new_data_page(struct inode
*inode
,
818 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
820 struct address_space
*mapping
= inode
->i_mapping
;
822 struct dnode_of_data dn
;
825 page
= f2fs_grab_cache_page(mapping
, index
, true);
828 * before exiting, we should make sure ipage will be released
829 * if any error occur.
831 f2fs_put_page(ipage
, 1);
832 return ERR_PTR(-ENOMEM
);
835 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
836 err
= f2fs_reserve_block(&dn
, index
);
838 f2fs_put_page(page
, 1);
844 if (PageUptodate(page
))
847 if (dn
.data_blkaddr
== NEW_ADDR
) {
848 zero_user_segment(page
, 0, PAGE_SIZE
);
849 if (!PageUptodate(page
))
850 SetPageUptodate(page
);
852 f2fs_put_page(page
, 1);
854 /* if ipage exists, blkaddr should be NEW_ADDR */
855 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
856 page
= get_lock_data_page(inode
, index
, true);
861 if (new_i_size
&& i_size_read(inode
) <
862 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
863 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
867 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
869 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
870 struct f2fs_summary sum
;
876 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
879 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
880 dn
->node_page
, dn
->ofs_in_node
);
881 if (dn
->data_blkaddr
== NEW_ADDR
)
884 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
888 get_node_info(sbi
, dn
->nid
, &ni
);
889 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
891 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
892 &sum
, seg_type
, NULL
, false);
893 set_data_blkaddr(dn
);
896 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
898 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
899 f2fs_i_size_write(dn
->inode
,
900 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
904 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
906 struct inode
*inode
= file_inode(iocb
->ki_filp
);
907 struct f2fs_map_blocks map
;
910 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
912 /* convert inline data for Direct I/O*/
914 err
= f2fs_convert_inline_inode(inode
);
919 if (is_inode_flag_set(inode
, FI_NO_PREALLOC
))
922 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
923 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
924 if (map
.m_len
> map
.m_lblk
)
925 map
.m_len
-= map
.m_lblk
;
929 map
.m_next_pgofs
= NULL
;
930 map
.m_next_extent
= NULL
;
931 map
.m_seg_type
= NO_CHECK_TYPE
;
934 map
.m_seg_type
= rw_hint_to_seg_type(iocb
->ki_hint
);
935 flag
= f2fs_force_buffered_io(inode
, WRITE
) ?
936 F2FS_GET_BLOCK_PRE_AIO
:
937 F2FS_GET_BLOCK_PRE_DIO
;
940 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
941 err
= f2fs_convert_inline_inode(inode
);
945 if (f2fs_has_inline_data(inode
))
948 flag
= F2FS_GET_BLOCK_PRE_AIO
;
951 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
952 if (map
.m_len
> 0 && err
== -ENOSPC
) {
954 set_inode_flag(inode
, FI_NO_PREALLOC
);
960 static inline void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
962 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
964 down_read(&sbi
->node_change
);
966 up_read(&sbi
->node_change
);
976 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
977 * f2fs_map_blocks structure.
978 * If original data blocks are allocated, then give them to blockdev.
980 * a. preallocate requested block addresses
981 * b. do not use extent cache for better performance
982 * c. give the block addresses to blockdev
984 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
985 int create
, int flag
)
987 unsigned int maxblocks
= map
->m_len
;
988 struct dnode_of_data dn
;
989 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
990 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
991 pgoff_t pgofs
, end_offset
, end
;
992 int err
= 0, ofs
= 1;
993 unsigned int ofs_in_node
, last_ofs_in_node
;
995 struct extent_info ei
= {0,0,0};
997 unsigned int start_pgofs
;
1005 /* it only supports block size == page size */
1006 pgofs
= (pgoff_t
)map
->m_lblk
;
1007 end
= pgofs
+ maxblocks
;
1009 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
1010 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
1011 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
1012 map
->m_flags
= F2FS_MAP_MAPPED
;
1013 if (map
->m_next_extent
)
1014 *map
->m_next_extent
= pgofs
+ map
->m_len
;
1020 __do_map_lock(sbi
, flag
, true);
1022 /* When reading holes, we need its node page */
1023 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1024 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
1026 if (flag
== F2FS_GET_BLOCK_BMAP
)
1028 if (err
== -ENOENT
) {
1030 if (map
->m_next_pgofs
)
1031 *map
->m_next_pgofs
=
1032 get_next_page_offset(&dn
, pgofs
);
1033 if (map
->m_next_extent
)
1034 *map
->m_next_extent
=
1035 get_next_page_offset(&dn
, pgofs
);
1040 start_pgofs
= pgofs
;
1042 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
1043 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1046 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
1048 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
1050 if (unlikely(f2fs_cp_error(sbi
))) {
1054 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1055 if (blkaddr
== NULL_ADDR
) {
1057 last_ofs_in_node
= dn
.ofs_in_node
;
1060 err
= __allocate_data_block(&dn
,
1063 set_inode_flag(inode
, FI_APPEND_WRITE
);
1067 map
->m_flags
|= F2FS_MAP_NEW
;
1068 blkaddr
= dn
.data_blkaddr
;
1070 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1074 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1076 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1077 blkaddr
== NULL_ADDR
) {
1078 if (map
->m_next_pgofs
)
1079 *map
->m_next_pgofs
= pgofs
+ 1;
1082 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1083 /* for defragment case */
1084 if (map
->m_next_pgofs
)
1085 *map
->m_next_pgofs
= pgofs
+ 1;
1091 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1094 if (map
->m_len
== 0) {
1095 /* preallocated unwritten block should be mapped for fiemap. */
1096 if (blkaddr
== NEW_ADDR
)
1097 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1098 map
->m_flags
|= F2FS_MAP_MAPPED
;
1100 map
->m_pblk
= blkaddr
;
1102 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1103 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1104 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1105 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1116 /* preallocate blocks in batch for one dnode page */
1117 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1118 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1120 dn
.ofs_in_node
= ofs_in_node
;
1121 err
= reserve_new_blocks(&dn
, prealloc
);
1125 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1126 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1130 dn
.ofs_in_node
= end_offset
;
1135 else if (dn
.ofs_in_node
< end_offset
)
1138 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1139 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1140 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1142 f2fs_update_extent_cache_range(&dn
,
1143 start_pgofs
, map
->m_pblk
+ ofs
,
1148 f2fs_put_dnode(&dn
);
1151 __do_map_lock(sbi
, flag
, false);
1152 f2fs_balance_fs(sbi
, dn
.node_changed
);
1157 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1158 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1159 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1161 f2fs_update_extent_cache_range(&dn
,
1162 start_pgofs
, map
->m_pblk
+ ofs
,
1165 if (map
->m_next_extent
)
1166 *map
->m_next_extent
= pgofs
+ 1;
1168 f2fs_put_dnode(&dn
);
1171 __do_map_lock(sbi
, flag
, false);
1172 f2fs_balance_fs(sbi
, dn
.node_changed
);
1175 trace_f2fs_map_blocks(inode
, map
, err
);
1179 bool f2fs_overwrite_io(struct inode
*inode
, loff_t pos
, size_t len
)
1181 struct f2fs_map_blocks map
;
1185 if (pos
+ len
> i_size_read(inode
))
1188 map
.m_lblk
= F2FS_BYTES_TO_BLK(pos
);
1189 map
.m_next_pgofs
= NULL
;
1190 map
.m_next_extent
= NULL
;
1191 map
.m_seg_type
= NO_CHECK_TYPE
;
1192 last_lblk
= F2FS_BLK_ALIGN(pos
+ len
);
1194 while (map
.m_lblk
< last_lblk
) {
1195 map
.m_len
= last_lblk
- map
.m_lblk
;
1196 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1197 if (err
|| map
.m_len
== 0)
1199 map
.m_lblk
+= map
.m_len
;
1204 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1205 struct buffer_head
*bh
, int create
, int flag
,
1206 pgoff_t
*next_pgofs
, int seg_type
)
1208 struct f2fs_map_blocks map
;
1211 map
.m_lblk
= iblock
;
1212 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1213 map
.m_next_pgofs
= next_pgofs
;
1214 map
.m_next_extent
= NULL
;
1215 map
.m_seg_type
= seg_type
;
1217 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1219 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1220 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1221 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1226 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1227 struct buffer_head
*bh_result
, int create
, int flag
,
1228 pgoff_t
*next_pgofs
)
1230 return __get_data_block(inode
, iblock
, bh_result
, create
,
1235 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1236 struct buffer_head
*bh_result
, int create
)
1238 return __get_data_block(inode
, iblock
, bh_result
, create
,
1239 F2FS_GET_BLOCK_DEFAULT
, NULL
,
1240 rw_hint_to_seg_type(
1241 inode
->i_write_hint
));
1244 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1245 struct buffer_head
*bh_result
, int create
)
1247 /* Block number less than F2FS MAX BLOCKS */
1248 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1251 return __get_data_block(inode
, iblock
, bh_result
, create
,
1252 F2FS_GET_BLOCK_BMAP
, NULL
,
1256 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1258 return (offset
>> inode
->i_blkbits
);
1261 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1263 return (blk
<< inode
->i_blkbits
);
1266 static int f2fs_xattr_fiemap(struct inode
*inode
,
1267 struct fiemap_extent_info
*fieinfo
)
1269 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1271 struct node_info ni
;
1272 __u64 phys
= 0, len
;
1274 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1277 if (f2fs_has_inline_xattr(inode
)) {
1280 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1281 inode
->i_ino
, false);
1285 get_node_info(sbi
, inode
->i_ino
, &ni
);
1287 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1288 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1289 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1290 get_inline_xattr_addrs(inode
));
1293 len
= inline_xattr_size(inode
);
1295 f2fs_put_page(page
, 1);
1297 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1300 flags
|= FIEMAP_EXTENT_LAST
;
1302 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1303 if (err
|| err
== 1)
1308 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1312 get_node_info(sbi
, xnid
, &ni
);
1314 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1315 len
= inode
->i_sb
->s_blocksize
;
1317 f2fs_put_page(page
, 1);
1319 flags
= FIEMAP_EXTENT_LAST
;
1323 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1325 return (err
< 0 ? err
: 0);
1328 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1331 struct buffer_head map_bh
;
1332 sector_t start_blk
, last_blk
;
1334 u64 logical
= 0, phys
= 0, size
= 0;
1338 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1339 ret
= f2fs_precache_extents(inode
);
1344 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1350 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1351 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1355 if (f2fs_has_inline_data(inode
)) {
1356 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1361 if (logical_to_blk(inode
, len
) == 0)
1362 len
= blk_to_logical(inode
, 1);
1364 start_blk
= logical_to_blk(inode
, start
);
1365 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1368 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1369 map_bh
.b_size
= len
;
1371 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1372 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1377 if (!buffer_mapped(&map_bh
)) {
1378 start_blk
= next_pgofs
;
1380 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1381 F2FS_I_SB(inode
)->max_file_blocks
))
1384 flags
|= FIEMAP_EXTENT_LAST
;
1388 if (f2fs_encrypted_inode(inode
))
1389 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1391 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1395 if (start_blk
> last_blk
|| ret
)
1398 logical
= blk_to_logical(inode
, start_blk
);
1399 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1400 size
= map_bh
.b_size
;
1402 if (buffer_unwritten(&map_bh
))
1403 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1405 start_blk
+= logical_to_blk(inode
, size
);
1409 if (fatal_signal_pending(current
))
1417 inode_unlock(inode
);
1422 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1423 * Major change was from block_size == page_size in f2fs by default.
1425 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1426 struct list_head
*pages
, struct page
*page
,
1429 struct bio
*bio
= NULL
;
1430 sector_t last_block_in_bio
= 0;
1431 struct inode
*inode
= mapping
->host
;
1432 const unsigned blkbits
= inode
->i_blkbits
;
1433 const unsigned blocksize
= 1 << blkbits
;
1434 sector_t block_in_file
;
1435 sector_t last_block
;
1436 sector_t last_block_in_file
;
1438 struct f2fs_map_blocks map
;
1444 map
.m_next_pgofs
= NULL
;
1445 map
.m_next_extent
= NULL
;
1446 map
.m_seg_type
= NO_CHECK_TYPE
;
1448 for (; nr_pages
; nr_pages
--) {
1450 page
= list_last_entry(pages
, struct page
, lru
);
1452 prefetchw(&page
->flags
);
1453 list_del(&page
->lru
);
1454 if (add_to_page_cache_lru(page
, mapping
,
1456 readahead_gfp_mask(mapping
)))
1460 block_in_file
= (sector_t
)page
->index
;
1461 last_block
= block_in_file
+ nr_pages
;
1462 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1464 if (last_block
> last_block_in_file
)
1465 last_block
= last_block_in_file
;
1468 * Map blocks using the previous result first.
1470 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1471 block_in_file
> map
.m_lblk
&&
1472 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1476 * Then do more f2fs_map_blocks() calls until we are
1477 * done with this page.
1481 if (block_in_file
< last_block
) {
1482 map
.m_lblk
= block_in_file
;
1483 map
.m_len
= last_block
- block_in_file
;
1485 if (f2fs_map_blocks(inode
, &map
, 0,
1486 F2FS_GET_BLOCK_DEFAULT
))
1487 goto set_error_page
;
1490 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1491 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1492 SetPageMappedToDisk(page
);
1494 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1495 SetPageUptodate(page
);
1499 zero_user_segment(page
, 0, PAGE_SIZE
);
1500 if (!PageUptodate(page
))
1501 SetPageUptodate(page
);
1507 * This page will go to BIO. Do we need to send this
1510 if (bio
&& (last_block_in_bio
!= block_nr
- 1 ||
1511 !__same_bdev(F2FS_I_SB(inode
), block_nr
, bio
))) {
1513 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1517 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
);
1520 goto set_error_page
;
1524 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1525 goto submit_and_realloc
;
1527 last_block_in_bio
= block_nr
;
1531 zero_user_segment(page
, 0, PAGE_SIZE
);
1536 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1544 BUG_ON(pages
&& !list_empty(pages
));
1546 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1550 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1552 struct inode
*inode
= page
->mapping
->host
;
1555 trace_f2fs_readpage(page
, DATA
);
1557 /* If the file has inline data, try to read it directly */
1558 if (f2fs_has_inline_data(inode
))
1559 ret
= f2fs_read_inline_data(inode
, page
);
1561 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1565 static int f2fs_read_data_pages(struct file
*file
,
1566 struct address_space
*mapping
,
1567 struct list_head
*pages
, unsigned nr_pages
)
1569 struct inode
*inode
= mapping
->host
;
1570 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
1572 trace_f2fs_readpages(inode
, page
, nr_pages
);
1574 /* If the file has inline data, skip readpages */
1575 if (f2fs_has_inline_data(inode
))
1578 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1581 static int encrypt_one_page(struct f2fs_io_info
*fio
)
1583 struct inode
*inode
= fio
->page
->mapping
->host
;
1584 gfp_t gfp_flags
= GFP_NOFS
;
1586 if (!f2fs_encrypted_file(inode
))
1589 /* wait for GCed page writeback via META_MAPPING */
1590 f2fs_wait_on_block_writeback(fio
->sbi
, fio
->old_blkaddr
);
1593 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1594 PAGE_SIZE
, 0, fio
->page
->index
, gfp_flags
);
1595 if (!IS_ERR(fio
->encrypted_page
))
1598 /* flush pending IOs and wait for a while in the ENOMEM case */
1599 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
1600 f2fs_flush_merged_writes(fio
->sbi
);
1601 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1602 gfp_flags
|= __GFP_NOFAIL
;
1605 return PTR_ERR(fio
->encrypted_page
);
1608 static inline bool check_inplace_update_policy(struct inode
*inode
,
1609 struct f2fs_io_info
*fio
)
1611 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1612 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
1614 if (policy
& (0x1 << F2FS_IPU_FORCE
))
1616 if (policy
& (0x1 << F2FS_IPU_SSR
) && need_SSR(sbi
))
1618 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
1619 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1621 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && need_SSR(sbi
) &&
1622 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1626 * IPU for rewrite async pages
1628 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
1629 fio
&& fio
->op
== REQ_OP_WRITE
&&
1630 !(fio
->op_flags
& REQ_SYNC
) &&
1631 !f2fs_encrypted_inode(inode
))
1634 /* this is only set during fdatasync */
1635 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
1636 is_inode_flag_set(inode
, FI_NEED_IPU
))
1642 bool should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1644 if (f2fs_is_pinned_file(inode
))
1647 /* if this is cold file, we should overwrite to avoid fragmentation */
1648 if (file_is_cold(inode
))
1651 return check_inplace_update_policy(inode
, fio
);
1654 bool should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1656 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1658 if (test_opt(sbi
, LFS
))
1660 if (S_ISDIR(inode
->i_mode
))
1662 if (f2fs_is_atomic_file(inode
))
1665 if (is_cold_data(fio
->page
))
1667 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
1673 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
1675 struct inode
*inode
= fio
->page
->mapping
->host
;
1677 if (should_update_outplace(inode
, fio
))
1680 return should_update_inplace(inode
, fio
);
1683 static inline bool valid_ipu_blkaddr(struct f2fs_io_info
*fio
)
1685 if (fio
->old_blkaddr
== NEW_ADDR
)
1687 if (fio
->old_blkaddr
== NULL_ADDR
)
1692 int do_write_data_page(struct f2fs_io_info
*fio
)
1694 struct page
*page
= fio
->page
;
1695 struct inode
*inode
= page
->mapping
->host
;
1696 struct dnode_of_data dn
;
1697 struct extent_info ei
= {0,0,0};
1698 bool ipu_force
= false;
1701 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1702 if (need_inplace_update(fio
) &&
1703 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
1704 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
1706 if (valid_ipu_blkaddr(fio
)) {
1708 fio
->need_lock
= LOCK_DONE
;
1713 /* Deadlock due to between page->lock and f2fs_lock_op */
1714 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
1717 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1721 fio
->old_blkaddr
= dn
.data_blkaddr
;
1723 /* This page is already truncated */
1724 if (fio
->old_blkaddr
== NULL_ADDR
) {
1725 ClearPageUptodate(page
);
1730 * If current allocation needs SSR,
1731 * it had better in-place writes for updated data.
1733 if (ipu_force
|| (valid_ipu_blkaddr(fio
) && need_inplace_update(fio
))) {
1734 err
= encrypt_one_page(fio
);
1738 set_page_writeback(page
);
1739 ClearPageError(page
);
1740 f2fs_put_dnode(&dn
);
1741 if (fio
->need_lock
== LOCK_REQ
)
1742 f2fs_unlock_op(fio
->sbi
);
1743 err
= rewrite_data_page(fio
);
1744 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
1745 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1749 if (fio
->need_lock
== LOCK_RETRY
) {
1750 if (!f2fs_trylock_op(fio
->sbi
)) {
1754 fio
->need_lock
= LOCK_REQ
;
1757 err
= encrypt_one_page(fio
);
1761 set_page_writeback(page
);
1762 ClearPageError(page
);
1764 /* LFS mode write path */
1765 write_data_page(&dn
, fio
);
1766 trace_f2fs_do_write_data_page(page
, OPU
);
1767 set_inode_flag(inode
, FI_APPEND_WRITE
);
1768 if (page
->index
== 0)
1769 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1771 f2fs_put_dnode(&dn
);
1773 if (fio
->need_lock
== LOCK_REQ
)
1774 f2fs_unlock_op(fio
->sbi
);
1778 static int __write_data_page(struct page
*page
, bool *submitted
,
1779 struct writeback_control
*wbc
,
1780 enum iostat_type io_type
)
1782 struct inode
*inode
= page
->mapping
->host
;
1783 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1784 loff_t i_size
= i_size_read(inode
);
1785 const pgoff_t end_index
= ((unsigned long long) i_size
)
1787 loff_t psize
= (page
->index
+ 1) << PAGE_SHIFT
;
1788 unsigned offset
= 0;
1789 bool need_balance_fs
= false;
1791 struct f2fs_io_info fio
= {
1793 .ino
= inode
->i_ino
,
1796 .op_flags
= wbc_to_write_flags(wbc
),
1797 .old_blkaddr
= NULL_ADDR
,
1799 .encrypted_page
= NULL
,
1801 .need_lock
= LOCK_RETRY
,
1806 trace_f2fs_writepage(page
, DATA
);
1808 /* we should bypass data pages to proceed the kworkder jobs */
1809 if (unlikely(f2fs_cp_error(sbi
))) {
1810 mapping_set_error(page
->mapping
, -EIO
);
1814 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1817 if (page
->index
< end_index
)
1821 * If the offset is out-of-range of file size,
1822 * this page does not have to be written to disk.
1824 offset
= i_size
& (PAGE_SIZE
- 1);
1825 if ((page
->index
>= end_index
+ 1) || !offset
)
1828 zero_user_segment(page
, offset
, PAGE_SIZE
);
1830 if (f2fs_is_drop_cache(inode
))
1832 /* we should not write 0'th page having journal header */
1833 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1834 (!wbc
->for_reclaim
&&
1835 available_free_memory(sbi
, BASE_CHECK
))))
1838 /* Dentry blocks are controlled by checkpoint */
1839 if (S_ISDIR(inode
->i_mode
)) {
1840 fio
.need_lock
= LOCK_DONE
;
1841 err
= do_write_data_page(&fio
);
1845 if (!wbc
->for_reclaim
)
1846 need_balance_fs
= true;
1847 else if (has_not_enough_free_secs(sbi
, 0, 0))
1850 set_inode_flag(inode
, FI_HOT_DATA
);
1853 if (f2fs_has_inline_data(inode
)) {
1854 err
= f2fs_write_inline_data(inode
, page
);
1859 if (err
== -EAGAIN
) {
1860 err
= do_write_data_page(&fio
);
1861 if (err
== -EAGAIN
) {
1862 fio
.need_lock
= LOCK_REQ
;
1863 err
= do_write_data_page(&fio
);
1868 file_set_keep_isize(inode
);
1870 down_write(&F2FS_I(inode
)->i_sem
);
1871 if (F2FS_I(inode
)->last_disk_size
< psize
)
1872 F2FS_I(inode
)->last_disk_size
= psize
;
1873 up_write(&F2FS_I(inode
)->i_sem
);
1877 if (err
&& err
!= -ENOENT
)
1881 inode_dec_dirty_pages(inode
);
1883 ClearPageUptodate(page
);
1885 if (wbc
->for_reclaim
) {
1886 f2fs_submit_merged_write_cond(sbi
, inode
, 0, page
->index
, DATA
);
1887 clear_inode_flag(inode
, FI_HOT_DATA
);
1888 remove_dirty_inode(inode
);
1893 if (!S_ISDIR(inode
->i_mode
))
1894 f2fs_balance_fs(sbi
, need_balance_fs
);
1896 if (unlikely(f2fs_cp_error(sbi
))) {
1897 f2fs_submit_merged_write(sbi
, DATA
);
1902 *submitted
= fio
.submitted
;
1907 redirty_page_for_writepage(wbc
, page
);
1909 return AOP_WRITEPAGE_ACTIVATE
;
1914 static int f2fs_write_data_page(struct page
*page
,
1915 struct writeback_control
*wbc
)
1917 return __write_data_page(page
, NULL
, wbc
, FS_DATA_IO
);
1921 * This function was copied from write_cche_pages from mm/page-writeback.c.
1922 * The major change is making write step of cold data page separately from
1923 * warm/hot data page.
1925 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1926 struct writeback_control
*wbc
,
1927 enum iostat_type io_type
)
1931 struct pagevec pvec
;
1933 pgoff_t
uninitialized_var(writeback_index
);
1935 pgoff_t end
; /* Inclusive */
1937 pgoff_t last_idx
= ULONG_MAX
;
1939 int range_whole
= 0;
1942 pagevec_init(&pvec
, 0);
1944 if (get_dirty_pages(mapping
->host
) <=
1945 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
1946 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
1948 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
1950 if (wbc
->range_cyclic
) {
1951 writeback_index
= mapping
->writeback_index
; /* prev offset */
1952 index
= writeback_index
;
1959 index
= wbc
->range_start
>> PAGE_SHIFT
;
1960 end
= wbc
->range_end
>> PAGE_SHIFT
;
1961 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1963 cycled
= 1; /* ignore range_cyclic tests */
1965 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1966 tag
= PAGECACHE_TAG_TOWRITE
;
1968 tag
= PAGECACHE_TAG_DIRTY
;
1970 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1971 tag_pages_for_writeback(mapping
, index
, end
);
1973 while (!done
&& (index
<= end
)) {
1976 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1977 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1981 for (i
= 0; i
< nr_pages
; i
++) {
1982 struct page
*page
= pvec
.pages
[i
];
1983 bool submitted
= false;
1985 if (page
->index
> end
) {
1990 done_index
= page
->index
;
1994 if (unlikely(page
->mapping
!= mapping
)) {
2000 if (!PageDirty(page
)) {
2001 /* someone wrote it for us */
2002 goto continue_unlock
;
2005 if (PageWriteback(page
)) {
2006 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
2007 f2fs_wait_on_page_writeback(page
,
2010 goto continue_unlock
;
2013 BUG_ON(PageWriteback(page
));
2014 if (!clear_page_dirty_for_io(page
))
2015 goto continue_unlock
;
2017 ret
= __write_data_page(page
, &submitted
, wbc
, io_type
);
2018 if (unlikely(ret
)) {
2020 * keep nr_to_write, since vfs uses this to
2021 * get # of written pages.
2023 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
2027 } else if (ret
== -EAGAIN
) {
2029 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
2031 congestion_wait(BLK_RW_ASYNC
,
2037 done_index
= page
->index
+ 1;
2040 } else if (submitted
) {
2041 last_idx
= page
->index
;
2044 /* give a priority to WB_SYNC threads */
2045 if ((atomic_read(&F2FS_M_SB(mapping
)->wb_sync_req
) ||
2046 --wbc
->nr_to_write
<= 0) &&
2047 wbc
->sync_mode
== WB_SYNC_NONE
) {
2052 pagevec_release(&pvec
);
2056 if (!cycled
&& !done
) {
2059 end
= writeback_index
- 1;
2062 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2063 mapping
->writeback_index
= done_index
;
2065 if (last_idx
!= ULONG_MAX
)
2066 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
2072 int __f2fs_write_data_pages(struct address_space
*mapping
,
2073 struct writeback_control
*wbc
,
2074 enum iostat_type io_type
)
2076 struct inode
*inode
= mapping
->host
;
2077 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2078 struct blk_plug plug
;
2081 /* deal with chardevs and other special file */
2082 if (!mapping
->a_ops
->writepage
)
2085 /* skip writing if there is no dirty page in this inode */
2086 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
2089 /* during POR, we don't need to trigger writepage at all. */
2090 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
2093 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
2094 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
2095 available_free_memory(sbi
, DIRTY_DENTS
))
2098 /* skip writing during file defragment */
2099 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
2102 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2104 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2105 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2106 atomic_inc(&sbi
->wb_sync_req
);
2107 else if (atomic_read(&sbi
->wb_sync_req
))
2110 blk_start_plug(&plug
);
2111 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
2112 blk_finish_plug(&plug
);
2114 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2115 atomic_dec(&sbi
->wb_sync_req
);
2117 * if some pages were truncated, we cannot guarantee its mapping->host
2118 * to detect pending bios.
2121 remove_dirty_inode(inode
);
2125 wbc
->pages_skipped
+= get_dirty_pages(inode
);
2126 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2130 static int f2fs_write_data_pages(struct address_space
*mapping
,
2131 struct writeback_control
*wbc
)
2133 struct inode
*inode
= mapping
->host
;
2135 return __f2fs_write_data_pages(mapping
, wbc
,
2136 F2FS_I(inode
)->cp_task
== current
?
2137 FS_CP_DATA_IO
: FS_DATA_IO
);
2140 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
2142 struct inode
*inode
= mapping
->host
;
2143 loff_t i_size
= i_size_read(inode
);
2146 down_write(&F2FS_I(inode
)->i_mmap_sem
);
2147 truncate_pagecache(inode
, i_size
);
2148 truncate_blocks(inode
, i_size
, true);
2149 up_write(&F2FS_I(inode
)->i_mmap_sem
);
2153 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
2154 struct page
*page
, loff_t pos
, unsigned len
,
2155 block_t
*blk_addr
, bool *node_changed
)
2157 struct inode
*inode
= page
->mapping
->host
;
2158 pgoff_t index
= page
->index
;
2159 struct dnode_of_data dn
;
2161 bool locked
= false;
2162 struct extent_info ei
= {0,0,0};
2166 * we already allocated all the blocks, so we don't need to get
2167 * the block addresses when there is no need to fill the page.
2169 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
2170 !is_inode_flag_set(inode
, FI_NO_PREALLOC
))
2173 if (f2fs_has_inline_data(inode
) ||
2174 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
2175 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, true);
2179 /* check inline_data */
2180 ipage
= get_node_page(sbi
, inode
->i_ino
);
2181 if (IS_ERR(ipage
)) {
2182 err
= PTR_ERR(ipage
);
2186 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
2188 if (f2fs_has_inline_data(inode
)) {
2189 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
2190 read_inline_data(page
, ipage
);
2191 set_inode_flag(inode
, FI_DATA_EXIST
);
2193 set_inline_node(ipage
);
2195 err
= f2fs_convert_inline_page(&dn
, page
);
2198 if (dn
.data_blkaddr
== NULL_ADDR
)
2199 err
= f2fs_get_block(&dn
, index
);
2201 } else if (locked
) {
2202 err
= f2fs_get_block(&dn
, index
);
2204 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
2205 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
2208 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
2209 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
2210 f2fs_put_dnode(&dn
);
2211 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
2219 /* convert_inline_page can make node_changed */
2220 *blk_addr
= dn
.data_blkaddr
;
2221 *node_changed
= dn
.node_changed
;
2223 f2fs_put_dnode(&dn
);
2226 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, false);
2230 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
2231 loff_t pos
, unsigned len
, unsigned flags
,
2232 struct page
**pagep
, void **fsdata
)
2234 struct inode
*inode
= mapping
->host
;
2235 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2236 struct page
*page
= NULL
;
2237 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
2238 bool need_balance
= false, drop_atomic
= false;
2239 block_t blkaddr
= NULL_ADDR
;
2242 if (trace_android_fs_datawrite_start_enabled()) {
2243 char *path
, pathbuf
[MAX_TRACE_PATHBUF_LEN
];
2245 path
= android_fstrace_get_pathname(pathbuf
,
2246 MAX_TRACE_PATHBUF_LEN
,
2248 trace_android_fs_datawrite_start(inode
, pos
, len
,
2252 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
2254 if (f2fs_is_atomic_file(inode
) &&
2255 !available_free_memory(sbi
, INMEM_PAGES
)) {
2262 * We should check this at this moment to avoid deadlock on inode page
2263 * and #0 page. The locking rule for inline_data conversion should be:
2264 * lock_page(page #0) -> lock_page(inode_page)
2267 err
= f2fs_convert_inline_inode(inode
);
2273 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2274 * wait_for_stable_page. Will wait that below with our IO control.
2276 page
= f2fs_pagecache_get_page(mapping
, index
,
2277 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
2285 err
= prepare_write_begin(sbi
, page
, pos
, len
,
2286 &blkaddr
, &need_balance
);
2290 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
2292 f2fs_balance_fs(sbi
, true);
2294 if (page
->mapping
!= mapping
) {
2295 /* The page got truncated from under us */
2296 f2fs_put_page(page
, 1);
2301 f2fs_wait_on_page_writeback(page
, DATA
, false);
2303 /* wait for GCed page writeback via META_MAPPING */
2304 if (f2fs_post_read_required(inode
))
2305 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
2307 if (len
== PAGE_SIZE
|| PageUptodate(page
))
2310 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
)) {
2311 zero_user_segment(page
, len
, PAGE_SIZE
);
2315 if (blkaddr
== NEW_ADDR
) {
2316 zero_user_segment(page
, 0, PAGE_SIZE
);
2317 SetPageUptodate(page
);
2319 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
2324 if (unlikely(page
->mapping
!= mapping
)) {
2325 f2fs_put_page(page
, 1);
2328 if (unlikely(!PageUptodate(page
))) {
2336 f2fs_put_page(page
, 1);
2337 f2fs_write_failed(mapping
, pos
+ len
);
2339 drop_inmem_pages_all(sbi
);
2343 static int f2fs_write_end(struct file
*file
,
2344 struct address_space
*mapping
,
2345 loff_t pos
, unsigned len
, unsigned copied
,
2346 struct page
*page
, void *fsdata
)
2348 struct inode
*inode
= page
->mapping
->host
;
2350 trace_android_fs_datawrite_end(inode
, pos
, len
);
2351 trace_f2fs_write_end(inode
, pos
, len
, copied
);
2354 * This should be come from len == PAGE_SIZE, and we expect copied
2355 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2356 * let generic_perform_write() try to copy data again through copied=0.
2358 if (!PageUptodate(page
)) {
2359 if (unlikely(copied
!= len
))
2362 SetPageUptodate(page
);
2367 set_page_dirty(page
);
2369 if (pos
+ copied
> i_size_read(inode
))
2370 f2fs_i_size_write(inode
, pos
+ copied
);
2372 f2fs_put_page(page
, 1);
2373 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2377 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
2380 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
2382 if (offset
& blocksize_mask
)
2385 if (iov_iter_alignment(iter
) & blocksize_mask
)
2391 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2393 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
2394 struct inode
*inode
= mapping
->host
;
2395 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2396 size_t count
= iov_iter_count(iter
);
2397 loff_t offset
= iocb
->ki_pos
;
2398 int rw
= iov_iter_rw(iter
);
2400 enum rw_hint hint
= iocb
->ki_hint
;
2401 int whint_mode
= F2FS_OPTION(sbi
).whint_mode
;
2403 err
= check_direct_IO(inode
, iter
, offset
);
2407 if (f2fs_force_buffered_io(inode
, rw
))
2410 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
2412 if (trace_android_fs_dataread_start_enabled() &&
2414 char *path
, pathbuf
[MAX_TRACE_PATHBUF_LEN
];
2416 path
= android_fstrace_get_pathname(pathbuf
,
2417 MAX_TRACE_PATHBUF_LEN
,
2419 trace_android_fs_dataread_start(inode
, offset
,
2420 count
, current
->pid
, path
,
2423 if (trace_android_fs_datawrite_start_enabled() &&
2425 char *path
, pathbuf
[MAX_TRACE_PATHBUF_LEN
];
2427 path
= android_fstrace_get_pathname(pathbuf
,
2428 MAX_TRACE_PATHBUF_LEN
,
2430 trace_android_fs_datawrite_start(inode
, offset
, count
,
2434 if (rw
== WRITE
&& whint_mode
== WHINT_MODE_OFF
)
2435 iocb
->ki_hint
= WRITE_LIFE_NOT_SET
;
2437 if (!down_read_trylock(&F2FS_I(inode
)->dio_rwsem
[rw
])) {
2438 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
2439 iocb
->ki_hint
= hint
;
2443 down_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
2446 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
2447 up_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
2450 if (whint_mode
== WHINT_MODE_OFF
)
2451 iocb
->ki_hint
= hint
;
2453 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
2455 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2456 } else if (err
< 0) {
2457 f2fs_write_failed(mapping
, offset
+ count
);
2461 if (trace_android_fs_dataread_start_enabled() &&
2463 trace_android_fs_dataread_end(inode
, offset
, count
);
2464 if (trace_android_fs_datawrite_start_enabled() &&
2466 trace_android_fs_datawrite_end(inode
, offset
, count
);
2468 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
2473 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
2474 unsigned int length
)
2476 struct inode
*inode
= page
->mapping
->host
;
2477 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2479 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
2480 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
2483 if (PageDirty(page
)) {
2484 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
2485 dec_page_count(sbi
, F2FS_DIRTY_META
);
2486 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
2487 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
2489 inode_dec_dirty_pages(inode
);
2490 remove_dirty_inode(inode
);
2494 /* This is atomic written page, keep Private */
2495 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2496 return drop_inmem_page(inode
, page
);
2498 set_page_private(page
, 0);
2499 ClearPagePrivate(page
);
2502 int f2fs_release_page(struct page
*page
, gfp_t wait
)
2504 /* If this is dirty page, keep PagePrivate */
2505 if (PageDirty(page
))
2508 /* This is atomic written page, keep Private */
2509 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2512 set_page_private(page
, 0);
2513 ClearPagePrivate(page
);
2517 static int f2fs_set_data_page_dirty(struct page
*page
)
2519 struct address_space
*mapping
= page
->mapping
;
2520 struct inode
*inode
= mapping
->host
;
2522 trace_f2fs_set_page_dirty(page
, DATA
);
2524 if (!PageUptodate(page
))
2525 SetPageUptodate(page
);
2527 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
2528 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
2529 register_inmem_page(inode
, page
);
2533 * Previously, this page has been registered, we just
2539 if (!PageDirty(page
)) {
2540 __set_page_dirty_nobuffers(page
);
2541 update_dirty_page(inode
, page
);
2547 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
2549 struct inode
*inode
= mapping
->host
;
2551 if (f2fs_has_inline_data(inode
))
2554 /* make sure allocating whole blocks */
2555 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2556 filemap_write_and_wait(mapping
);
2558 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
2561 #ifdef CONFIG_MIGRATION
2562 #include <linux/migrate.h>
2564 int f2fs_migrate_page(struct address_space
*mapping
,
2565 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
2567 int rc
, extra_count
;
2568 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
2569 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
2571 BUG_ON(PageWriteback(page
));
2573 /* migrating an atomic written page is safe with the inmem_lock hold */
2574 if (atomic_written
) {
2575 if (mode
!= MIGRATE_SYNC
)
2577 if (!mutex_trylock(&fi
->inmem_lock
))
2582 * A reference is expected if PagePrivate set when move mapping,
2583 * however F2FS breaks this for maintaining dirty page counts when
2584 * truncating pages. So here adjusting the 'extra_count' make it work.
2586 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
2587 rc
= migrate_page_move_mapping(mapping
, newpage
,
2588 page
, NULL
, mode
, extra_count
);
2589 if (rc
!= MIGRATEPAGE_SUCCESS
) {
2591 mutex_unlock(&fi
->inmem_lock
);
2595 if (atomic_written
) {
2596 struct inmem_pages
*cur
;
2597 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
2598 if (cur
->page
== page
) {
2599 cur
->page
= newpage
;
2602 mutex_unlock(&fi
->inmem_lock
);
2607 if (PagePrivate(page
))
2608 SetPagePrivate(newpage
);
2609 set_page_private(newpage
, page_private(page
));
2611 if (mode
!= MIGRATE_SYNC_NO_COPY
)
2612 migrate_page_copy(newpage
, page
);
2614 migrate_page_states(newpage
, page
);
2616 return MIGRATEPAGE_SUCCESS
;
2620 const struct address_space_operations f2fs_dblock_aops
= {
2621 .readpage
= f2fs_read_data_page
,
2622 .readpages
= f2fs_read_data_pages
,
2623 .writepage
= f2fs_write_data_page
,
2624 .writepages
= f2fs_write_data_pages
,
2625 .write_begin
= f2fs_write_begin
,
2626 .write_end
= f2fs_write_end
,
2627 .set_page_dirty
= f2fs_set_data_page_dirty
,
2628 .invalidatepage
= f2fs_invalidate_page
,
2629 .releasepage
= f2fs_release_page
,
2630 .direct_IO
= f2fs_direct_IO
,
2632 #ifdef CONFIG_MIGRATION
2633 .migratepage
= f2fs_migrate_page
,
2637 int __init
f2fs_init_post_read_processing(void)
2639 bio_post_read_ctx_cache
= KMEM_CACHE(bio_post_read_ctx
, 0);
2640 if (!bio_post_read_ctx_cache
)
2642 bio_post_read_ctx_pool
=
2643 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS
,
2644 bio_post_read_ctx_cache
);
2645 if (!bio_post_read_ctx_pool
)
2646 goto fail_free_cache
;
2650 kmem_cache_destroy(bio_post_read_ctx_cache
);
2655 void __exit
f2fs_destroy_post_read_processing(void)
2657 mempool_destroy(bio_post_read_ctx_pool
);
2658 kmem_cache_destroy(bio_post_read_ctx_cache
);