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>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache
*bio_post_read_ctx_cache
;
34 static mempool_t
*bio_post_read_ctx_pool
;
36 static bool __is_cp_guaranteed(struct page
*page
)
38 struct address_space
*mapping
= page
->mapping
;
40 struct f2fs_sb_info
*sbi
;
45 inode
= mapping
->host
;
46 sbi
= F2FS_I_SB(inode
);
48 if (inode
->i_ino
== F2FS_META_INO(sbi
) ||
49 inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
50 S_ISDIR(inode
->i_mode
) ||
51 (S_ISREG(inode
->i_mode
) &&
52 is_inode_flag_set(inode
, FI_ATOMIC_FILE
)) ||
58 /* postprocessing steps for read bios */
59 enum bio_post_read_step
{
64 struct bio_post_read_ctx
{
66 struct work_struct work
;
67 unsigned int cur_step
;
68 unsigned int enabled_steps
;
71 static void __read_end_io(struct bio
*bio
)
77 bio_for_each_segment_all(bv
, bio
, i
) {
80 /* PG_error was set if any post_read step failed */
81 if (bio
->bi_status
|| PageError(page
)) {
82 ClearPageUptodate(page
);
83 /* will re-read again later */
86 SetPageUptodate(page
);
91 mempool_free(bio
->bi_private
, bio_post_read_ctx_pool
);
95 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
);
97 static void decrypt_work(struct work_struct
*work
)
99 struct bio_post_read_ctx
*ctx
=
100 container_of(work
, struct bio_post_read_ctx
, work
);
102 fscrypt_decrypt_bio(ctx
->bio
);
104 bio_post_read_processing(ctx
);
107 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
)
109 switch (++ctx
->cur_step
) {
111 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
)) {
112 INIT_WORK(&ctx
->work
, decrypt_work
);
113 fscrypt_enqueue_decrypt_work(&ctx
->work
);
119 __read_end_io(ctx
->bio
);
123 static bool f2fs_bio_post_read_required(struct bio
*bio
)
125 return bio
->bi_private
&& !bio
->bi_status
;
128 static void f2fs_read_end_io(struct bio
*bio
)
130 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio
)), FAULT_IO
)) {
131 f2fs_show_injection_info(FAULT_IO
);
132 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 if (f2fs_in_warm_node_list(sbi
, page
))
180 f2fs_del_fsync_node_entry(sbi
, page
);
181 clear_cold_data(page
);
182 end_page_writeback(page
);
184 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
185 wq_has_sleeper(&sbi
->cp_wait
))
186 wake_up(&sbi
->cp_wait
);
192 * Return true, if pre_bio's bdev is same as its target device.
194 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
195 block_t blk_addr
, struct bio
*bio
)
197 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
200 if (f2fs_is_multi_device(sbi
)) {
201 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
202 if (FDEV(i
).start_blk
<= blk_addr
&&
203 FDEV(i
).end_blk
>= blk_addr
) {
204 blk_addr
-= FDEV(i
).start_blk
;
211 bio_set_dev(bio
, bdev
);
212 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
217 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
221 if (!f2fs_is_multi_device(sbi
))
224 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
225 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
230 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
231 block_t blk_addr
, struct bio
*bio
)
233 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
234 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
238 * Low-level block read/write IO operations.
240 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
241 struct writeback_control
*wbc
,
242 int npages
, bool is_read
,
243 enum page_type type
, enum temp_type temp
)
247 bio
= f2fs_bio_alloc(sbi
, npages
, true);
249 f2fs_target_device(sbi
, blk_addr
, bio
);
251 bio
->bi_end_io
= f2fs_read_end_io
;
252 bio
->bi_private
= NULL
;
254 bio
->bi_end_io
= f2fs_write_end_io
;
255 bio
->bi_private
= sbi
;
256 bio
->bi_write_hint
= f2fs_io_type_to_rw_hint(sbi
, type
, temp
);
259 wbc_init_bio(wbc
, bio
);
264 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
265 struct bio
*bio
, enum page_type type
)
267 if (!is_read_io(bio_op(bio
))) {
270 if (type
!= DATA
&& type
!= NODE
)
273 if (test_opt(sbi
, LFS
) && current
->plug
)
274 blk_finish_plug(current
->plug
);
276 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
277 start
%= F2FS_IO_SIZE(sbi
);
282 /* fill dummy pages */
283 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
285 mempool_alloc(sbi
->write_io_dummy
,
286 GFP_NOIO
| __GFP_ZERO
| __GFP_NOFAIL
);
287 f2fs_bug_on(sbi
, !page
);
289 SetPagePrivate(page
);
290 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
292 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
296 * In the NODE case, we lose next block address chain. So, we
297 * need to do checkpoint in f2fs_sync_file.
300 set_sbi_flag(sbi
, SBI_NEED_CP
);
303 if (is_read_io(bio_op(bio
)))
304 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
306 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
310 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
312 struct f2fs_io_info
*fio
= &io
->fio
;
317 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
319 if (is_read_io(fio
->op
))
320 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
322 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
324 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
328 static bool __has_merged_page(struct f2fs_bio_info
*io
,
329 struct inode
*inode
, nid_t ino
, pgoff_t idx
)
331 struct bio_vec
*bvec
;
341 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
343 if (bvec
->bv_page
->mapping
)
344 target
= bvec
->bv_page
;
346 target
= fscrypt_control_page(bvec
->bv_page
);
348 if (idx
!= target
->index
)
351 if (inode
&& inode
== target
->mapping
->host
)
353 if (ino
&& ino
== ino_of_node(target
))
360 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
361 nid_t ino
, pgoff_t idx
, enum page_type type
)
363 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
365 struct f2fs_bio_info
*io
;
368 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
369 io
= sbi
->write_io
[btype
] + temp
;
371 down_read(&io
->io_rwsem
);
372 ret
= __has_merged_page(io
, inode
, ino
, idx
);
373 up_read(&io
->io_rwsem
);
375 /* TODO: use HOT temp only for meta pages now. */
376 if (ret
|| btype
== META
)
382 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
383 enum page_type type
, enum temp_type temp
)
385 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
386 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
388 down_write(&io
->io_rwsem
);
390 /* change META to META_FLUSH in the checkpoint procedure */
391 if (type
>= META_FLUSH
) {
392 io
->fio
.type
= META_FLUSH
;
393 io
->fio
.op
= REQ_OP_WRITE
;
394 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
395 if (!test_opt(sbi
, NOBARRIER
))
396 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
398 __submit_merged_bio(io
);
399 up_write(&io
->io_rwsem
);
402 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
403 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
404 enum page_type type
, bool force
)
408 if (!force
&& !has_merged_page(sbi
, inode
, ino
, idx
, type
))
411 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
413 __f2fs_submit_merged_write(sbi
, type
, temp
);
415 /* TODO: use HOT temp only for meta pages now. */
421 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
423 __submit_merged_write_cond(sbi
, NULL
, 0, 0, type
, true);
426 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
427 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
430 __submit_merged_write_cond(sbi
, inode
, ino
, idx
, type
, false);
433 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
435 f2fs_submit_merged_write(sbi
, DATA
);
436 f2fs_submit_merged_write(sbi
, NODE
);
437 f2fs_submit_merged_write(sbi
, META
);
441 * Fill the locked page with data located in the block address.
442 * A caller needs to unlock the page on failure.
444 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
447 struct page
*page
= fio
->encrypted_page
?
448 fio
->encrypted_page
: fio
->page
;
450 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
451 __is_meta_io(fio
) ? META_GENERIC
: DATA_GENERIC
))
452 return -EFSCORRUPTED
;
454 trace_f2fs_submit_page_bio(page
, fio
);
455 f2fs_trace_ios(fio
, 0);
457 /* Allocate a new bio */
458 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
459 1, is_read_io(fio
->op
), fio
->type
, fio
->temp
);
461 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
466 if (fio
->io_wbc
&& !is_read_io(fio
->op
))
467 wbc_account_io(fio
->io_wbc
, page
, PAGE_SIZE
);
469 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
471 if (!is_read_io(fio
->op
))
472 inc_page_count(fio
->sbi
, WB_DATA_TYPE(fio
->page
));
474 __submit_bio(fio
->sbi
, bio
, fio
->type
);
478 void f2fs_submit_page_write(struct f2fs_io_info
*fio
)
480 struct f2fs_sb_info
*sbi
= fio
->sbi
;
481 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
482 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
483 struct page
*bio_page
;
485 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
487 down_write(&io
->io_rwsem
);
490 spin_lock(&io
->io_lock
);
491 if (list_empty(&io
->io_list
)) {
492 spin_unlock(&io
->io_lock
);
495 fio
= list_first_entry(&io
->io_list
,
496 struct f2fs_io_info
, list
);
497 list_del(&fio
->list
);
498 spin_unlock(&io
->io_lock
);
501 if (__is_valid_data_blkaddr(fio
->old_blkaddr
))
502 verify_block_addr(fio
, fio
->old_blkaddr
);
503 verify_block_addr(fio
, fio
->new_blkaddr
);
505 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
507 /* set submitted = true as a return value */
508 fio
->submitted
= true;
510 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
512 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
513 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
) ||
514 !__same_bdev(sbi
, fio
->new_blkaddr
, io
->bio
)))
515 __submit_merged_bio(io
);
517 if (io
->bio
== NULL
) {
518 if ((fio
->type
== DATA
|| fio
->type
== NODE
) &&
519 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
520 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
524 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
525 BIO_MAX_PAGES
, false,
526 fio
->type
, fio
->temp
);
530 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
531 __submit_merged_bio(io
);
536 wbc_account_io(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
538 io
->last_block_in_bio
= fio
->new_blkaddr
;
539 f2fs_trace_ios(fio
, 0);
541 trace_f2fs_submit_page_write(fio
->page
, fio
);
546 if (is_sbi_flag_set(sbi
, SBI_IS_SHUTDOWN
))
547 __submit_merged_bio(io
);
548 up_write(&io
->io_rwsem
);
551 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
552 unsigned nr_pages
, unsigned op_flag
)
554 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
556 struct bio_post_read_ctx
*ctx
;
557 unsigned int post_read_steps
= 0;
559 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
))
560 return ERR_PTR(-EFAULT
);
562 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
564 return ERR_PTR(-ENOMEM
);
565 f2fs_target_device(sbi
, blkaddr
, bio
);
566 bio
->bi_end_io
= f2fs_read_end_io
;
567 bio_set_op_attrs(bio
, REQ_OP_READ
, op_flag
);
569 if (f2fs_encrypted_file(inode
))
570 post_read_steps
|= 1 << STEP_DECRYPT
;
571 if (post_read_steps
) {
572 ctx
= mempool_alloc(bio_post_read_ctx_pool
, GFP_NOFS
);
575 return ERR_PTR(-ENOMEM
);
578 ctx
->enabled_steps
= post_read_steps
;
579 bio
->bi_private
= ctx
;
585 /* This can handle encryption stuffs */
586 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
589 struct bio
*bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1, 0);
594 /* wait for GCed page writeback via META_MAPPING */
595 f2fs_wait_on_block_writeback(inode
, blkaddr
);
597 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
601 ClearPageError(page
);
602 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
606 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
608 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
612 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
613 base
= get_extra_isize(dn
->inode
);
615 /* Get physical address of data block */
616 addr_array
= blkaddr_in_node(rn
);
617 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
621 * Lock ordering for the change of data block address:
624 * update block addresses in the node page
626 void f2fs_set_data_blkaddr(struct dnode_of_data
*dn
)
628 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
629 __set_data_blkaddr(dn
);
630 if (set_page_dirty(dn
->node_page
))
631 dn
->node_changed
= true;
634 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
636 dn
->data_blkaddr
= blkaddr
;
637 f2fs_set_data_blkaddr(dn
);
638 f2fs_update_extent_cache(dn
);
641 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
642 int f2fs_reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
644 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
650 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
652 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
655 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
656 dn
->ofs_in_node
, count
);
658 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
660 for (; count
> 0; dn
->ofs_in_node
++) {
661 block_t blkaddr
= datablock_addr(dn
->inode
,
662 dn
->node_page
, dn
->ofs_in_node
);
663 if (blkaddr
== NULL_ADDR
) {
664 dn
->data_blkaddr
= NEW_ADDR
;
665 __set_data_blkaddr(dn
);
670 if (set_page_dirty(dn
->node_page
))
671 dn
->node_changed
= true;
675 /* Should keep dn->ofs_in_node unchanged */
676 int f2fs_reserve_new_block(struct dnode_of_data
*dn
)
678 unsigned int ofs_in_node
= dn
->ofs_in_node
;
681 ret
= f2fs_reserve_new_blocks(dn
, 1);
682 dn
->ofs_in_node
= ofs_in_node
;
686 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
688 bool need_put
= dn
->inode_page
? false : true;
691 err
= f2fs_get_dnode_of_data(dn
, index
, ALLOC_NODE
);
695 if (dn
->data_blkaddr
== NULL_ADDR
)
696 err
= f2fs_reserve_new_block(dn
);
702 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
704 struct extent_info ei
= {0,0,0};
705 struct inode
*inode
= dn
->inode
;
707 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
708 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
712 return f2fs_reserve_block(dn
, index
);
715 struct page
*f2fs_get_read_data_page(struct inode
*inode
, pgoff_t index
,
716 int op_flags
, bool for_write
)
718 struct address_space
*mapping
= inode
->i_mapping
;
719 struct dnode_of_data dn
;
721 struct extent_info ei
= {0,0,0};
724 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
726 return ERR_PTR(-ENOMEM
);
728 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
729 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
733 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
734 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
739 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
744 if (PageUptodate(page
)) {
750 * A new dentry page is allocated but not able to be written, since its
751 * new inode page couldn't be allocated due to -ENOSPC.
752 * In such the case, its blkaddr can be remained as NEW_ADDR.
753 * see, f2fs_add_link -> f2fs_get_new_data_page ->
754 * f2fs_init_inode_metadata.
756 if (dn
.data_blkaddr
== NEW_ADDR
) {
757 zero_user_segment(page
, 0, PAGE_SIZE
);
758 if (!PageUptodate(page
))
759 SetPageUptodate(page
);
764 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
770 f2fs_put_page(page
, 1);
774 struct page
*f2fs_find_data_page(struct inode
*inode
, pgoff_t index
)
776 struct address_space
*mapping
= inode
->i_mapping
;
779 page
= find_get_page(mapping
, index
);
780 if (page
&& PageUptodate(page
))
782 f2fs_put_page(page
, 0);
784 page
= f2fs_get_read_data_page(inode
, index
, 0, false);
788 if (PageUptodate(page
))
791 wait_on_page_locked(page
);
792 if (unlikely(!PageUptodate(page
))) {
793 f2fs_put_page(page
, 0);
794 return ERR_PTR(-EIO
);
800 * If it tries to access a hole, return an error.
801 * Because, the callers, functions in dir.c and GC, should be able to know
802 * whether this page exists or not.
804 struct page
*f2fs_get_lock_data_page(struct inode
*inode
, pgoff_t index
,
807 struct address_space
*mapping
= inode
->i_mapping
;
810 page
= f2fs_get_read_data_page(inode
, index
, 0, for_write
);
814 /* wait for read completion */
816 if (unlikely(page
->mapping
!= mapping
)) {
817 f2fs_put_page(page
, 1);
820 if (unlikely(!PageUptodate(page
))) {
821 f2fs_put_page(page
, 1);
822 return ERR_PTR(-EIO
);
828 * Caller ensures that this data page is never allocated.
829 * A new zero-filled data page is allocated in the page cache.
831 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
833 * Note that, ipage is set only by make_empty_dir, and if any error occur,
834 * ipage should be released by this function.
836 struct page
*f2fs_get_new_data_page(struct inode
*inode
,
837 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
839 struct address_space
*mapping
= inode
->i_mapping
;
841 struct dnode_of_data dn
;
844 page
= f2fs_grab_cache_page(mapping
, index
, true);
847 * before exiting, we should make sure ipage will be released
848 * if any error occur.
850 f2fs_put_page(ipage
, 1);
851 return ERR_PTR(-ENOMEM
);
854 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
855 err
= f2fs_reserve_block(&dn
, index
);
857 f2fs_put_page(page
, 1);
863 if (PageUptodate(page
))
866 if (dn
.data_blkaddr
== NEW_ADDR
) {
867 zero_user_segment(page
, 0, PAGE_SIZE
);
868 if (!PageUptodate(page
))
869 SetPageUptodate(page
);
871 f2fs_put_page(page
, 1);
873 /* if ipage exists, blkaddr should be NEW_ADDR */
874 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
875 page
= f2fs_get_lock_data_page(inode
, index
, true);
880 if (new_i_size
&& i_size_read(inode
) <
881 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
882 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
886 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
888 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
889 struct f2fs_summary sum
;
895 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
898 err
= f2fs_get_node_info(sbi
, dn
->nid
, &ni
);
902 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
903 dn
->node_page
, dn
->ofs_in_node
);
904 if (dn
->data_blkaddr
== NEW_ADDR
)
907 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
911 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
912 old_blkaddr
= dn
->data_blkaddr
;
913 f2fs_allocate_data_block(sbi
, NULL
, old_blkaddr
, &dn
->data_blkaddr
,
914 &sum
, seg_type
, NULL
, false);
915 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
916 invalidate_mapping_pages(META_MAPPING(sbi
),
917 old_blkaddr
, old_blkaddr
);
918 f2fs_set_data_blkaddr(dn
);
921 * i_size will be updated by direct_IO. Otherwise, we'll get stale
922 * data from unwritten block via dio_read.
927 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
929 struct inode
*inode
= file_inode(iocb
->ki_filp
);
930 struct f2fs_map_blocks map
;
933 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
935 /* convert inline data for Direct I/O*/
937 err
= f2fs_convert_inline_inode(inode
);
942 if (is_inode_flag_set(inode
, FI_NO_PREALLOC
))
945 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
946 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
947 if (map
.m_len
> map
.m_lblk
)
948 map
.m_len
-= map
.m_lblk
;
952 map
.m_next_pgofs
= NULL
;
953 map
.m_next_extent
= NULL
;
954 map
.m_seg_type
= NO_CHECK_TYPE
;
957 map
.m_seg_type
= f2fs_rw_hint_to_seg_type(iocb
->ki_hint
);
958 flag
= f2fs_force_buffered_io(inode
, WRITE
) ?
959 F2FS_GET_BLOCK_PRE_AIO
:
960 F2FS_GET_BLOCK_PRE_DIO
;
963 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
964 err
= f2fs_convert_inline_inode(inode
);
968 if (f2fs_has_inline_data(inode
))
971 flag
= F2FS_GET_BLOCK_PRE_AIO
;
974 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
975 if (map
.m_len
> 0 && err
== -ENOSPC
) {
977 set_inode_flag(inode
, FI_NO_PREALLOC
);
983 static inline void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
985 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
987 down_read(&sbi
->node_change
);
989 up_read(&sbi
->node_change
);
999 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1000 * f2fs_map_blocks structure.
1001 * If original data blocks are allocated, then give them to blockdev.
1003 * a. preallocate requested block addresses
1004 * b. do not use extent cache for better performance
1005 * c. give the block addresses to blockdev
1007 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
1008 int create
, int flag
)
1010 unsigned int maxblocks
= map
->m_len
;
1011 struct dnode_of_data dn
;
1012 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1013 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
1014 pgoff_t pgofs
, end_offset
, end
;
1015 int err
= 0, ofs
= 1;
1016 unsigned int ofs_in_node
, last_ofs_in_node
;
1018 struct extent_info ei
= {0,0,0};
1020 unsigned int start_pgofs
;
1028 /* it only supports block size == page size */
1029 pgofs
= (pgoff_t
)map
->m_lblk
;
1030 end
= pgofs
+ maxblocks
;
1032 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
1033 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
1034 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
1035 map
->m_flags
= F2FS_MAP_MAPPED
;
1036 if (map
->m_next_extent
)
1037 *map
->m_next_extent
= pgofs
+ map
->m_len
;
1043 __do_map_lock(sbi
, flag
, true);
1045 /* When reading holes, we need its node page */
1046 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1047 err
= f2fs_get_dnode_of_data(&dn
, pgofs
, mode
);
1049 if (flag
== F2FS_GET_BLOCK_BMAP
)
1051 if (err
== -ENOENT
) {
1053 if (map
->m_next_pgofs
)
1054 *map
->m_next_pgofs
=
1055 f2fs_get_next_page_offset(&dn
, pgofs
);
1056 if (map
->m_next_extent
)
1057 *map
->m_next_extent
=
1058 f2fs_get_next_page_offset(&dn
, pgofs
);
1063 start_pgofs
= pgofs
;
1065 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
1066 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1069 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
1071 if (__is_valid_data_blkaddr(blkaddr
) &&
1072 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
)) {
1073 err
= -EFSCORRUPTED
;
1077 if (!is_valid_data_blkaddr(sbi
, blkaddr
)) {
1079 if (unlikely(f2fs_cp_error(sbi
))) {
1083 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1084 if (blkaddr
== NULL_ADDR
) {
1086 last_ofs_in_node
= dn
.ofs_in_node
;
1089 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_DIO
&&
1090 flag
!= F2FS_GET_BLOCK_DIO
);
1091 err
= __allocate_data_block(&dn
,
1094 set_inode_flag(inode
, FI_APPEND_WRITE
);
1098 map
->m_flags
|= F2FS_MAP_NEW
;
1099 blkaddr
= dn
.data_blkaddr
;
1101 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1105 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1107 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1108 blkaddr
== NULL_ADDR
) {
1109 if (map
->m_next_pgofs
)
1110 *map
->m_next_pgofs
= pgofs
+ 1;
1113 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1114 /* for defragment case */
1115 if (map
->m_next_pgofs
)
1116 *map
->m_next_pgofs
= pgofs
+ 1;
1122 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1125 if (map
->m_len
== 0) {
1126 /* preallocated unwritten block should be mapped for fiemap. */
1127 if (blkaddr
== NEW_ADDR
)
1128 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1129 map
->m_flags
|= F2FS_MAP_MAPPED
;
1131 map
->m_pblk
= blkaddr
;
1133 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1134 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1135 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1136 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1147 /* preallocate blocks in batch for one dnode page */
1148 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1149 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1151 dn
.ofs_in_node
= ofs_in_node
;
1152 err
= f2fs_reserve_new_blocks(&dn
, prealloc
);
1156 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1157 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1161 dn
.ofs_in_node
= end_offset
;
1166 else if (dn
.ofs_in_node
< end_offset
)
1169 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1170 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1171 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1173 f2fs_update_extent_cache_range(&dn
,
1174 start_pgofs
, map
->m_pblk
+ ofs
,
1179 f2fs_put_dnode(&dn
);
1182 __do_map_lock(sbi
, flag
, false);
1183 f2fs_balance_fs(sbi
, dn
.node_changed
);
1188 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1189 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1190 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1192 f2fs_update_extent_cache_range(&dn
,
1193 start_pgofs
, map
->m_pblk
+ ofs
,
1196 if (map
->m_next_extent
)
1197 *map
->m_next_extent
= pgofs
+ 1;
1199 f2fs_put_dnode(&dn
);
1202 __do_map_lock(sbi
, flag
, false);
1203 f2fs_balance_fs(sbi
, dn
.node_changed
);
1206 trace_f2fs_map_blocks(inode
, map
, err
);
1210 bool f2fs_overwrite_io(struct inode
*inode
, loff_t pos
, size_t len
)
1212 struct f2fs_map_blocks map
;
1216 if (pos
+ len
> i_size_read(inode
))
1219 map
.m_lblk
= F2FS_BYTES_TO_BLK(pos
);
1220 map
.m_next_pgofs
= NULL
;
1221 map
.m_next_extent
= NULL
;
1222 map
.m_seg_type
= NO_CHECK_TYPE
;
1223 last_lblk
= F2FS_BLK_ALIGN(pos
+ len
);
1225 while (map
.m_lblk
< last_lblk
) {
1226 map
.m_len
= last_lblk
- map
.m_lblk
;
1227 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1228 if (err
|| map
.m_len
== 0)
1230 map
.m_lblk
+= map
.m_len
;
1235 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1236 struct buffer_head
*bh
, int create
, int flag
,
1237 pgoff_t
*next_pgofs
, int seg_type
)
1239 struct f2fs_map_blocks map
;
1242 map
.m_lblk
= iblock
;
1243 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1244 map
.m_next_pgofs
= next_pgofs
;
1245 map
.m_next_extent
= NULL
;
1246 map
.m_seg_type
= seg_type
;
1248 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1250 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1251 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1252 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1257 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1258 struct buffer_head
*bh_result
, int create
, int flag
,
1259 pgoff_t
*next_pgofs
)
1261 return __get_data_block(inode
, iblock
, bh_result
, create
,
1266 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1267 struct buffer_head
*bh_result
, int create
)
1269 return __get_data_block(inode
, iblock
, bh_result
, create
,
1270 F2FS_GET_BLOCK_DIO
, NULL
,
1271 f2fs_rw_hint_to_seg_type(
1272 inode
->i_write_hint
));
1275 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1276 struct buffer_head
*bh_result
, int create
)
1278 /* Block number less than F2FS MAX BLOCKS */
1279 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1282 return __get_data_block(inode
, iblock
, bh_result
, create
,
1283 F2FS_GET_BLOCK_BMAP
, NULL
,
1287 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1289 return (offset
>> inode
->i_blkbits
);
1292 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1294 return (blk
<< inode
->i_blkbits
);
1297 static int f2fs_xattr_fiemap(struct inode
*inode
,
1298 struct fiemap_extent_info
*fieinfo
)
1300 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1302 struct node_info ni
;
1303 __u64 phys
= 0, len
;
1305 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1308 if (f2fs_has_inline_xattr(inode
)) {
1311 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1312 inode
->i_ino
, false);
1316 err
= f2fs_get_node_info(sbi
, inode
->i_ino
, &ni
);
1318 f2fs_put_page(page
, 1);
1322 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1323 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1324 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1325 get_inline_xattr_addrs(inode
));
1328 len
= inline_xattr_size(inode
);
1330 f2fs_put_page(page
, 1);
1332 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1335 flags
|= FIEMAP_EXTENT_LAST
;
1337 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1338 if (err
|| err
== 1)
1343 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1347 err
= f2fs_get_node_info(sbi
, xnid
, &ni
);
1349 f2fs_put_page(page
, 1);
1353 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1354 len
= inode
->i_sb
->s_blocksize
;
1356 f2fs_put_page(page
, 1);
1358 flags
= FIEMAP_EXTENT_LAST
;
1362 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1364 return (err
< 0 ? err
: 0);
1367 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1370 struct buffer_head map_bh
;
1371 sector_t start_blk
, last_blk
;
1373 u64 logical
= 0, phys
= 0, size
= 0;
1377 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1378 ret
= f2fs_precache_extents(inode
);
1383 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1389 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1390 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1394 if (f2fs_has_inline_data(inode
)) {
1395 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1400 if (logical_to_blk(inode
, len
) == 0)
1401 len
= blk_to_logical(inode
, 1);
1403 start_blk
= logical_to_blk(inode
, start
);
1404 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1407 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1408 map_bh
.b_size
= len
;
1410 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1411 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1416 if (!buffer_mapped(&map_bh
)) {
1417 start_blk
= next_pgofs
;
1419 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1420 F2FS_I_SB(inode
)->max_file_blocks
))
1423 flags
|= FIEMAP_EXTENT_LAST
;
1427 if (f2fs_encrypted_inode(inode
))
1428 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1430 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1434 if (start_blk
> last_blk
|| ret
)
1437 logical
= blk_to_logical(inode
, start_blk
);
1438 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1439 size
= map_bh
.b_size
;
1441 if (buffer_unwritten(&map_bh
))
1442 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1444 start_blk
+= logical_to_blk(inode
, size
);
1448 if (fatal_signal_pending(current
))
1456 inode_unlock(inode
);
1461 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1462 * Major change was from block_size == page_size in f2fs by default.
1464 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1465 * this function ever deviates from doing just read-ahead, it should either
1466 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1469 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1470 struct list_head
*pages
, struct page
*page
,
1471 unsigned nr_pages
, bool is_readahead
)
1473 struct bio
*bio
= NULL
;
1474 sector_t last_block_in_bio
= 0;
1475 struct inode
*inode
= mapping
->host
;
1476 const unsigned blkbits
= inode
->i_blkbits
;
1477 const unsigned blocksize
= 1 << blkbits
;
1478 sector_t block_in_file
;
1479 sector_t last_block
;
1480 sector_t last_block_in_file
;
1482 struct f2fs_map_blocks map
;
1488 map
.m_next_pgofs
= NULL
;
1489 map
.m_next_extent
= NULL
;
1490 map
.m_seg_type
= NO_CHECK_TYPE
;
1492 for (; nr_pages
; nr_pages
--) {
1494 page
= list_last_entry(pages
, struct page
, lru
);
1496 prefetchw(&page
->flags
);
1497 list_del(&page
->lru
);
1498 if (add_to_page_cache_lru(page
, mapping
,
1500 readahead_gfp_mask(mapping
)))
1504 block_in_file
= (sector_t
)page
->index
;
1505 last_block
= block_in_file
+ nr_pages
;
1506 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1508 if (last_block
> last_block_in_file
)
1509 last_block
= last_block_in_file
;
1512 * Map blocks using the previous result first.
1514 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1515 block_in_file
> map
.m_lblk
&&
1516 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1520 * Then do more f2fs_map_blocks() calls until we are
1521 * done with this page.
1525 if (block_in_file
< last_block
) {
1526 map
.m_lblk
= block_in_file
;
1527 map
.m_len
= last_block
- block_in_file
;
1529 if (f2fs_map_blocks(inode
, &map
, 0,
1530 F2FS_GET_BLOCK_DEFAULT
))
1531 goto set_error_page
;
1534 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1535 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1536 SetPageMappedToDisk(page
);
1538 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1539 SetPageUptodate(page
);
1543 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), block_nr
,
1545 goto set_error_page
;
1547 zero_user_segment(page
, 0, PAGE_SIZE
);
1548 if (!PageUptodate(page
))
1549 SetPageUptodate(page
);
1555 * This page will go to BIO. Do we need to send this
1558 if (bio
&& (last_block_in_bio
!= block_nr
- 1 ||
1559 !__same_bdev(F2FS_I_SB(inode
), block_nr
, bio
))) {
1561 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1565 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
,
1566 is_readahead
? REQ_RAHEAD
: 0);
1569 goto set_error_page
;
1574 * If the page is under writeback, we need to wait for
1575 * its completion to see the correct decrypted data.
1577 f2fs_wait_on_block_writeback(inode
, block_nr
);
1579 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1580 goto submit_and_realloc
;
1582 ClearPageError(page
);
1583 last_block_in_bio
= block_nr
;
1587 zero_user_segment(page
, 0, PAGE_SIZE
);
1592 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1600 BUG_ON(pages
&& !list_empty(pages
));
1602 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1606 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1608 struct inode
*inode
= page
->mapping
->host
;
1611 trace_f2fs_readpage(page
, DATA
);
1613 /* If the file has inline data, try to read it directly */
1614 if (f2fs_has_inline_data(inode
))
1615 ret
= f2fs_read_inline_data(inode
, page
);
1617 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1, false);
1621 static int f2fs_read_data_pages(struct file
*file
,
1622 struct address_space
*mapping
,
1623 struct list_head
*pages
, unsigned nr_pages
)
1625 struct inode
*inode
= mapping
->host
;
1626 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
1628 trace_f2fs_readpages(inode
, page
, nr_pages
);
1630 /* If the file has inline data, skip readpages */
1631 if (f2fs_has_inline_data(inode
))
1634 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
, true);
1637 static int encrypt_one_page(struct f2fs_io_info
*fio
)
1639 struct inode
*inode
= fio
->page
->mapping
->host
;
1641 gfp_t gfp_flags
= GFP_NOFS
;
1643 if (!f2fs_encrypted_file(inode
))
1646 /* wait for GCed page writeback via META_MAPPING */
1647 f2fs_wait_on_block_writeback(inode
, fio
->old_blkaddr
);
1650 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1651 PAGE_SIZE
, 0, fio
->page
->index
, gfp_flags
);
1652 if (IS_ERR(fio
->encrypted_page
)) {
1653 /* flush pending IOs and wait for a while in the ENOMEM case */
1654 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
1655 f2fs_flush_merged_writes(fio
->sbi
);
1656 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1657 gfp_flags
|= __GFP_NOFAIL
;
1660 return PTR_ERR(fio
->encrypted_page
);
1663 mpage
= find_lock_page(META_MAPPING(fio
->sbi
), fio
->old_blkaddr
);
1665 if (PageUptodate(mpage
))
1666 memcpy(page_address(mpage
),
1667 page_address(fio
->encrypted_page
), PAGE_SIZE
);
1668 f2fs_put_page(mpage
, 1);
1673 static inline bool check_inplace_update_policy(struct inode
*inode
,
1674 struct f2fs_io_info
*fio
)
1676 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1677 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
1679 if (policy
& (0x1 << F2FS_IPU_FORCE
))
1681 if (policy
& (0x1 << F2FS_IPU_SSR
) && f2fs_need_SSR(sbi
))
1683 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
1684 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1686 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && f2fs_need_SSR(sbi
) &&
1687 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1691 * IPU for rewrite async pages
1693 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
1694 fio
&& fio
->op
== REQ_OP_WRITE
&&
1695 !(fio
->op_flags
& REQ_SYNC
) &&
1696 !f2fs_encrypted_inode(inode
))
1699 /* this is only set during fdatasync */
1700 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
1701 is_inode_flag_set(inode
, FI_NEED_IPU
))
1707 bool f2fs_should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1709 if (f2fs_is_pinned_file(inode
))
1712 /* if this is cold file, we should overwrite to avoid fragmentation */
1713 if (file_is_cold(inode
))
1716 return check_inplace_update_policy(inode
, fio
);
1719 bool f2fs_should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1721 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1723 if (test_opt(sbi
, LFS
))
1725 if (S_ISDIR(inode
->i_mode
))
1727 if (f2fs_is_atomic_file(inode
))
1730 if (is_cold_data(fio
->page
))
1732 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
1738 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
1740 struct inode
*inode
= fio
->page
->mapping
->host
;
1742 if (f2fs_should_update_outplace(inode
, fio
))
1745 return f2fs_should_update_inplace(inode
, fio
);
1748 int f2fs_do_write_data_page(struct f2fs_io_info
*fio
)
1750 struct page
*page
= fio
->page
;
1751 struct inode
*inode
= page
->mapping
->host
;
1752 struct dnode_of_data dn
;
1753 struct extent_info ei
= {0,0,0};
1754 struct node_info ni
;
1755 bool ipu_force
= false;
1758 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1759 if (need_inplace_update(fio
) &&
1760 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
1761 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
1763 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
1765 return -EFSCORRUPTED
;
1768 fio
->need_lock
= LOCK_DONE
;
1772 /* Deadlock due to between page->lock and f2fs_lock_op */
1773 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
1776 err
= f2fs_get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1780 fio
->old_blkaddr
= dn
.data_blkaddr
;
1782 /* This page is already truncated */
1783 if (fio
->old_blkaddr
== NULL_ADDR
) {
1784 ClearPageUptodate(page
);
1785 clear_cold_data(page
);
1789 if (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
1790 !f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
1792 err
= -EFSCORRUPTED
;
1796 * If current allocation needs SSR,
1797 * it had better in-place writes for updated data.
1799 if (ipu_force
|| (is_valid_data_blkaddr(fio
->sbi
, fio
->old_blkaddr
) &&
1800 need_inplace_update(fio
))) {
1801 err
= encrypt_one_page(fio
);
1805 set_page_writeback(page
);
1806 ClearPageError(page
);
1807 f2fs_put_dnode(&dn
);
1808 if (fio
->need_lock
== LOCK_REQ
)
1809 f2fs_unlock_op(fio
->sbi
);
1810 err
= f2fs_inplace_write_data(fio
);
1811 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
1812 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1816 if (fio
->need_lock
== LOCK_RETRY
) {
1817 if (!f2fs_trylock_op(fio
->sbi
)) {
1821 fio
->need_lock
= LOCK_REQ
;
1824 err
= f2fs_get_node_info(fio
->sbi
, dn
.nid
, &ni
);
1828 fio
->version
= ni
.version
;
1830 err
= encrypt_one_page(fio
);
1834 set_page_writeback(page
);
1835 ClearPageError(page
);
1837 /* LFS mode write path */
1838 f2fs_outplace_write_data(&dn
, fio
);
1839 trace_f2fs_do_write_data_page(page
, OPU
);
1840 set_inode_flag(inode
, FI_APPEND_WRITE
);
1841 if (page
->index
== 0)
1842 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1844 f2fs_put_dnode(&dn
);
1846 if (fio
->need_lock
== LOCK_REQ
)
1847 f2fs_unlock_op(fio
->sbi
);
1851 static int __write_data_page(struct page
*page
, bool *submitted
,
1852 struct writeback_control
*wbc
,
1853 enum iostat_type io_type
)
1855 struct inode
*inode
= page
->mapping
->host
;
1856 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1857 loff_t i_size
= i_size_read(inode
);
1858 const pgoff_t end_index
= ((unsigned long long) i_size
)
1860 loff_t psize
= (loff_t
)(page
->index
+ 1) << PAGE_SHIFT
;
1861 unsigned offset
= 0;
1862 bool need_balance_fs
= false;
1864 struct f2fs_io_info fio
= {
1866 .ino
= inode
->i_ino
,
1869 .op_flags
= wbc_to_write_flags(wbc
),
1870 .old_blkaddr
= NULL_ADDR
,
1872 .encrypted_page
= NULL
,
1874 .need_lock
= LOCK_RETRY
,
1879 trace_f2fs_writepage(page
, DATA
);
1881 /* we should bypass data pages to proceed the kworkder jobs */
1882 if (unlikely(f2fs_cp_error(sbi
))) {
1883 mapping_set_error(page
->mapping
, -EIO
);
1885 * don't drop any dirty dentry pages for keeping lastest
1886 * directory structure.
1888 if (S_ISDIR(inode
->i_mode
))
1893 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1896 if (page
->index
< end_index
)
1900 * If the offset is out-of-range of file size,
1901 * this page does not have to be written to disk.
1903 offset
= i_size
& (PAGE_SIZE
- 1);
1904 if ((page
->index
>= end_index
+ 1) || !offset
)
1907 zero_user_segment(page
, offset
, PAGE_SIZE
);
1909 if (f2fs_is_drop_cache(inode
))
1911 /* we should not write 0'th page having journal header */
1912 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1913 (!wbc
->for_reclaim
&&
1914 f2fs_available_free_memory(sbi
, BASE_CHECK
))))
1917 /* Dentry blocks are controlled by checkpoint */
1918 if (S_ISDIR(inode
->i_mode
)) {
1919 fio
.need_lock
= LOCK_DONE
;
1920 err
= f2fs_do_write_data_page(&fio
);
1924 if (!wbc
->for_reclaim
)
1925 need_balance_fs
= true;
1926 else if (has_not_enough_free_secs(sbi
, 0, 0))
1929 set_inode_flag(inode
, FI_HOT_DATA
);
1932 if (f2fs_has_inline_data(inode
)) {
1933 err
= f2fs_write_inline_data(inode
, page
);
1938 if (err
== -EAGAIN
) {
1939 err
= f2fs_do_write_data_page(&fio
);
1940 if (err
== -EAGAIN
) {
1941 fio
.need_lock
= LOCK_REQ
;
1942 err
= f2fs_do_write_data_page(&fio
);
1947 file_set_keep_isize(inode
);
1949 down_write(&F2FS_I(inode
)->i_sem
);
1950 if (F2FS_I(inode
)->last_disk_size
< psize
)
1951 F2FS_I(inode
)->last_disk_size
= psize
;
1952 up_write(&F2FS_I(inode
)->i_sem
);
1956 if (err
&& err
!= -ENOENT
)
1960 inode_dec_dirty_pages(inode
);
1962 ClearPageUptodate(page
);
1963 clear_cold_data(page
);
1966 if (wbc
->for_reclaim
) {
1967 f2fs_submit_merged_write_cond(sbi
, inode
, 0, page
->index
, DATA
);
1968 clear_inode_flag(inode
, FI_HOT_DATA
);
1969 f2fs_remove_dirty_inode(inode
);
1974 if (!S_ISDIR(inode
->i_mode
))
1975 f2fs_balance_fs(sbi
, need_balance_fs
);
1977 if (unlikely(f2fs_cp_error(sbi
))) {
1978 f2fs_submit_merged_write(sbi
, DATA
);
1983 *submitted
= fio
.submitted
;
1988 redirty_page_for_writepage(wbc
, page
);
1990 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1991 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1992 * file_write_and_wait_range() will see EIO error, which is critical
1993 * to return value of fsync() followed by atomic_write failure to user.
1995 if (!err
|| wbc
->for_reclaim
)
1996 return AOP_WRITEPAGE_ACTIVATE
;
2001 static int f2fs_write_data_page(struct page
*page
,
2002 struct writeback_control
*wbc
)
2004 return __write_data_page(page
, NULL
, wbc
, FS_DATA_IO
);
2008 * This function was copied from write_cche_pages from mm/page-writeback.c.
2009 * The major change is making write step of cold data page separately from
2010 * warm/hot data page.
2012 static int f2fs_write_cache_pages(struct address_space
*mapping
,
2013 struct writeback_control
*wbc
,
2014 enum iostat_type io_type
)
2018 struct pagevec pvec
;
2019 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
2021 pgoff_t
uninitialized_var(writeback_index
);
2023 pgoff_t end
; /* Inclusive */
2025 pgoff_t last_idx
= ULONG_MAX
;
2027 int range_whole
= 0;
2030 pagevec_init(&pvec
);
2032 if (get_dirty_pages(mapping
->host
) <=
2033 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
2034 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
2036 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
2038 if (wbc
->range_cyclic
) {
2039 writeback_index
= mapping
->writeback_index
; /* prev offset */
2040 index
= writeback_index
;
2047 index
= wbc
->range_start
>> PAGE_SHIFT
;
2048 end
= wbc
->range_end
>> PAGE_SHIFT
;
2049 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2051 cycled
= 1; /* ignore range_cyclic tests */
2053 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2054 tag
= PAGECACHE_TAG_TOWRITE
;
2056 tag
= PAGECACHE_TAG_DIRTY
;
2058 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2059 tag_pages_for_writeback(mapping
, index
, end
);
2061 while (!done
&& (index
<= end
)) {
2064 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
2069 for (i
= 0; i
< nr_pages
; i
++) {
2070 struct page
*page
= pvec
.pages
[i
];
2071 bool submitted
= false;
2073 /* give a priority to WB_SYNC threads */
2074 if (atomic_read(&sbi
->wb_sync_req
[DATA
]) &&
2075 wbc
->sync_mode
== WB_SYNC_NONE
) {
2080 done_index
= page
->index
;
2084 if (unlikely(page
->mapping
!= mapping
)) {
2090 if (!PageDirty(page
)) {
2091 /* someone wrote it for us */
2092 goto continue_unlock
;
2095 if (PageWriteback(page
)) {
2096 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
2097 f2fs_wait_on_page_writeback(page
,
2100 goto continue_unlock
;
2103 BUG_ON(PageWriteback(page
));
2104 if (!clear_page_dirty_for_io(page
))
2105 goto continue_unlock
;
2107 ret
= __write_data_page(page
, &submitted
, wbc
, io_type
);
2108 if (unlikely(ret
)) {
2110 * keep nr_to_write, since vfs uses this to
2111 * get # of written pages.
2113 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
2117 } else if (ret
== -EAGAIN
) {
2119 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
2121 congestion_wait(BLK_RW_ASYNC
,
2127 done_index
= page
->index
+ 1;
2130 } else if (submitted
) {
2131 last_idx
= page
->index
;
2134 if (--wbc
->nr_to_write
<= 0 &&
2135 wbc
->sync_mode
== WB_SYNC_NONE
) {
2140 pagevec_release(&pvec
);
2144 if (!cycled
&& !done
) {
2147 end
= writeback_index
- 1;
2150 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2151 mapping
->writeback_index
= done_index
;
2153 if (last_idx
!= ULONG_MAX
)
2154 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
2160 static inline bool __should_serialize_io(struct inode
*inode
,
2161 struct writeback_control
*wbc
)
2163 if (!S_ISREG(inode
->i_mode
))
2165 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
2167 if (get_dirty_pages(inode
) >= SM_I(F2FS_I_SB(inode
))->min_seq_blocks
)
2172 static int __f2fs_write_data_pages(struct address_space
*mapping
,
2173 struct writeback_control
*wbc
,
2174 enum iostat_type io_type
)
2176 struct inode
*inode
= mapping
->host
;
2177 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2178 struct blk_plug plug
;
2180 bool locked
= false;
2182 /* deal with chardevs and other special file */
2183 if (!mapping
->a_ops
->writepage
)
2186 /* skip writing if there is no dirty page in this inode */
2187 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
2190 /* during POR, we don't need to trigger writepage at all. */
2191 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
2194 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
2195 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
2196 f2fs_available_free_memory(sbi
, DIRTY_DENTS
))
2199 /* skip writing during file defragment */
2200 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
2203 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2205 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2206 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2207 atomic_inc(&sbi
->wb_sync_req
[DATA
]);
2208 else if (atomic_read(&sbi
->wb_sync_req
[DATA
]))
2211 if (__should_serialize_io(inode
, wbc
)) {
2212 mutex_lock(&sbi
->writepages
);
2216 blk_start_plug(&plug
);
2217 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
2218 blk_finish_plug(&plug
);
2221 mutex_unlock(&sbi
->writepages
);
2223 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2224 atomic_dec(&sbi
->wb_sync_req
[DATA
]);
2226 * if some pages were truncated, we cannot guarantee its mapping->host
2227 * to detect pending bios.
2230 f2fs_remove_dirty_inode(inode
);
2234 wbc
->pages_skipped
+= get_dirty_pages(inode
);
2235 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2239 static int f2fs_write_data_pages(struct address_space
*mapping
,
2240 struct writeback_control
*wbc
)
2242 struct inode
*inode
= mapping
->host
;
2244 return __f2fs_write_data_pages(mapping
, wbc
,
2245 F2FS_I(inode
)->cp_task
== current
?
2246 FS_CP_DATA_IO
: FS_DATA_IO
);
2249 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
2251 struct inode
*inode
= mapping
->host
;
2252 loff_t i_size
= i_size_read(inode
);
2255 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
2256 down_write(&F2FS_I(inode
)->i_mmap_sem
);
2258 truncate_pagecache(inode
, i_size
);
2259 f2fs_truncate_blocks(inode
, i_size
, true);
2261 up_write(&F2FS_I(inode
)->i_mmap_sem
);
2262 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
2266 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
2267 struct page
*page
, loff_t pos
, unsigned len
,
2268 block_t
*blk_addr
, bool *node_changed
)
2270 struct inode
*inode
= page
->mapping
->host
;
2271 pgoff_t index
= page
->index
;
2272 struct dnode_of_data dn
;
2274 bool locked
= false;
2275 struct extent_info ei
= {0,0,0};
2280 * we already allocated all the blocks, so we don't need to get
2281 * the block addresses when there is no need to fill the page.
2283 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
2284 !is_inode_flag_set(inode
, FI_NO_PREALLOC
))
2287 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2288 if (f2fs_has_inline_data(inode
) && pos
+ len
> MAX_INLINE_DATA(inode
))
2289 flag
= F2FS_GET_BLOCK_DEFAULT
;
2291 flag
= F2FS_GET_BLOCK_PRE_AIO
;
2293 if (f2fs_has_inline_data(inode
) ||
2294 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
2295 __do_map_lock(sbi
, flag
, true);
2299 /* check inline_data */
2300 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
2301 if (IS_ERR(ipage
)) {
2302 err
= PTR_ERR(ipage
);
2306 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
2308 if (f2fs_has_inline_data(inode
)) {
2309 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
2310 f2fs_do_read_inline_data(page
, ipage
);
2311 set_inode_flag(inode
, FI_DATA_EXIST
);
2313 set_inline_node(ipage
);
2315 err
= f2fs_convert_inline_page(&dn
, page
);
2318 if (dn
.data_blkaddr
== NULL_ADDR
)
2319 err
= f2fs_get_block(&dn
, index
);
2321 } else if (locked
) {
2322 err
= f2fs_get_block(&dn
, index
);
2324 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
2325 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
2328 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
2329 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
2330 f2fs_put_dnode(&dn
);
2331 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
2333 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_AIO
);
2340 /* convert_inline_page can make node_changed */
2341 *blk_addr
= dn
.data_blkaddr
;
2342 *node_changed
= dn
.node_changed
;
2344 f2fs_put_dnode(&dn
);
2347 __do_map_lock(sbi
, flag
, false);
2351 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
2352 loff_t pos
, unsigned len
, unsigned flags
,
2353 struct page
**pagep
, void **fsdata
)
2355 struct inode
*inode
= mapping
->host
;
2356 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2357 struct page
*page
= NULL
;
2358 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
2359 bool need_balance
= false, drop_atomic
= false;
2360 block_t blkaddr
= NULL_ADDR
;
2363 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
2365 if ((f2fs_is_atomic_file(inode
) &&
2366 !f2fs_available_free_memory(sbi
, INMEM_PAGES
)) ||
2367 is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
)) {
2374 * We should check this at this moment to avoid deadlock on inode page
2375 * and #0 page. The locking rule for inline_data conversion should be:
2376 * lock_page(page #0) -> lock_page(inode_page)
2379 err
= f2fs_convert_inline_inode(inode
);
2385 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2386 * wait_for_stable_page. Will wait that below with our IO control.
2388 page
= f2fs_pagecache_get_page(mapping
, index
,
2389 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
2397 err
= prepare_write_begin(sbi
, page
, pos
, len
,
2398 &blkaddr
, &need_balance
);
2402 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
2404 f2fs_balance_fs(sbi
, true);
2406 if (page
->mapping
!= mapping
) {
2407 /* The page got truncated from under us */
2408 f2fs_put_page(page
, 1);
2413 f2fs_wait_on_page_writeback(page
, DATA
, false);
2415 if (len
== PAGE_SIZE
|| PageUptodate(page
))
2418 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
)) {
2419 zero_user_segment(page
, len
, PAGE_SIZE
);
2423 if (blkaddr
== NEW_ADDR
) {
2424 zero_user_segment(page
, 0, PAGE_SIZE
);
2425 SetPageUptodate(page
);
2427 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
2432 if (unlikely(page
->mapping
!= mapping
)) {
2433 f2fs_put_page(page
, 1);
2436 if (unlikely(!PageUptodate(page
))) {
2444 f2fs_put_page(page
, 1);
2445 f2fs_write_failed(mapping
, pos
+ len
);
2447 f2fs_drop_inmem_pages_all(sbi
, false);
2451 static int f2fs_write_end(struct file
*file
,
2452 struct address_space
*mapping
,
2453 loff_t pos
, unsigned len
, unsigned copied
,
2454 struct page
*page
, void *fsdata
)
2456 struct inode
*inode
= page
->mapping
->host
;
2458 trace_f2fs_write_end(inode
, pos
, len
, copied
);
2461 * This should be come from len == PAGE_SIZE, and we expect copied
2462 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2463 * let generic_perform_write() try to copy data again through copied=0.
2465 if (!PageUptodate(page
)) {
2466 if (unlikely(copied
!= len
))
2469 SetPageUptodate(page
);
2474 set_page_dirty(page
);
2476 if (pos
+ copied
> i_size_read(inode
))
2477 f2fs_i_size_write(inode
, pos
+ copied
);
2479 f2fs_put_page(page
, 1);
2480 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2484 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
2487 unsigned i_blkbits
= READ_ONCE(inode
->i_blkbits
);
2488 unsigned blkbits
= i_blkbits
;
2489 unsigned blocksize_mask
= (1 << blkbits
) - 1;
2490 unsigned long align
= offset
| iov_iter_alignment(iter
);
2491 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
2493 if (align
& blocksize_mask
) {
2495 blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
2496 blocksize_mask
= (1 << blkbits
) - 1;
2497 if (align
& blocksize_mask
)
2504 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2506 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
2507 struct inode
*inode
= mapping
->host
;
2508 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2509 size_t count
= iov_iter_count(iter
);
2510 loff_t offset
= iocb
->ki_pos
;
2511 int rw
= iov_iter_rw(iter
);
2513 enum rw_hint hint
= iocb
->ki_hint
;
2514 int whint_mode
= F2FS_OPTION(sbi
).whint_mode
;
2516 err
= check_direct_IO(inode
, iter
, offset
);
2518 return err
< 0 ? err
: 0;
2520 if (f2fs_force_buffered_io(inode
, rw
))
2523 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
2525 if (rw
== WRITE
&& whint_mode
== WHINT_MODE_OFF
)
2526 iocb
->ki_hint
= WRITE_LIFE_NOT_SET
;
2528 if (!down_read_trylock(&F2FS_I(inode
)->i_gc_rwsem
[rw
])) {
2529 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
2530 iocb
->ki_hint
= hint
;
2534 down_read(&F2FS_I(inode
)->i_gc_rwsem
[rw
]);
2537 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
2538 up_read(&F2FS_I(inode
)->i_gc_rwsem
[rw
]);
2541 if (whint_mode
== WHINT_MODE_OFF
)
2542 iocb
->ki_hint
= hint
;
2544 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
2546 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2547 } else if (err
< 0) {
2548 f2fs_write_failed(mapping
, offset
+ count
);
2553 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
2558 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
2559 unsigned int length
)
2561 struct inode
*inode
= page
->mapping
->host
;
2562 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2564 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
2565 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
2568 if (PageDirty(page
)) {
2569 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
2570 dec_page_count(sbi
, F2FS_DIRTY_META
);
2571 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
2572 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
2574 inode_dec_dirty_pages(inode
);
2575 f2fs_remove_dirty_inode(inode
);
2579 clear_cold_data(page
);
2581 /* This is atomic written page, keep Private */
2582 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2583 return f2fs_drop_inmem_page(inode
, page
);
2585 set_page_private(page
, 0);
2586 ClearPagePrivate(page
);
2589 int f2fs_release_page(struct page
*page
, gfp_t wait
)
2591 /* If this is dirty page, keep PagePrivate */
2592 if (PageDirty(page
))
2595 /* This is atomic written page, keep Private */
2596 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2599 clear_cold_data(page
);
2600 set_page_private(page
, 0);
2601 ClearPagePrivate(page
);
2605 static int f2fs_set_data_page_dirty(struct page
*page
)
2607 struct address_space
*mapping
= page
->mapping
;
2608 struct inode
*inode
= mapping
->host
;
2610 trace_f2fs_set_page_dirty(page
, DATA
);
2612 if (!PageUptodate(page
))
2613 SetPageUptodate(page
);
2615 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
2616 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
2617 f2fs_register_inmem_page(inode
, page
);
2621 * Previously, this page has been registered, we just
2627 if (!PageDirty(page
)) {
2628 __set_page_dirty_nobuffers(page
);
2629 f2fs_update_dirty_page(inode
, page
);
2635 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
2637 struct inode
*inode
= mapping
->host
;
2639 if (f2fs_has_inline_data(inode
))
2642 /* make sure allocating whole blocks */
2643 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2644 filemap_write_and_wait(mapping
);
2646 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
2649 #ifdef CONFIG_MIGRATION
2650 #include <linux/migrate.h>
2652 int f2fs_migrate_page(struct address_space
*mapping
,
2653 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
2655 int rc
, extra_count
;
2656 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
2657 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
2659 BUG_ON(PageWriteback(page
));
2661 /* migrating an atomic written page is safe with the inmem_lock hold */
2662 if (atomic_written
) {
2663 if (mode
!= MIGRATE_SYNC
)
2665 if (!mutex_trylock(&fi
->inmem_lock
))
2670 * A reference is expected if PagePrivate set when move mapping,
2671 * however F2FS breaks this for maintaining dirty page counts when
2672 * truncating pages. So here adjusting the 'extra_count' make it work.
2674 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
2675 rc
= migrate_page_move_mapping(mapping
, newpage
,
2676 page
, NULL
, mode
, extra_count
);
2677 if (rc
!= MIGRATEPAGE_SUCCESS
) {
2679 mutex_unlock(&fi
->inmem_lock
);
2683 if (atomic_written
) {
2684 struct inmem_pages
*cur
;
2685 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
2686 if (cur
->page
== page
) {
2687 cur
->page
= newpage
;
2690 mutex_unlock(&fi
->inmem_lock
);
2695 if (PagePrivate(page
))
2696 SetPagePrivate(newpage
);
2697 set_page_private(newpage
, page_private(page
));
2699 if (mode
!= MIGRATE_SYNC_NO_COPY
)
2700 migrate_page_copy(newpage
, page
);
2702 migrate_page_states(newpage
, page
);
2704 return MIGRATEPAGE_SUCCESS
;
2708 const struct address_space_operations f2fs_dblock_aops
= {
2709 .readpage
= f2fs_read_data_page
,
2710 .readpages
= f2fs_read_data_pages
,
2711 .writepage
= f2fs_write_data_page
,
2712 .writepages
= f2fs_write_data_pages
,
2713 .write_begin
= f2fs_write_begin
,
2714 .write_end
= f2fs_write_end
,
2715 .set_page_dirty
= f2fs_set_data_page_dirty
,
2716 .invalidatepage
= f2fs_invalidate_page
,
2717 .releasepage
= f2fs_release_page
,
2718 .direct_IO
= f2fs_direct_IO
,
2720 #ifdef CONFIG_MIGRATION
2721 .migratepage
= f2fs_migrate_page
,
2725 void f2fs_clear_radix_tree_dirty_tag(struct page
*page
)
2727 struct address_space
*mapping
= page_mapping(page
);
2728 unsigned long flags
;
2730 xa_lock_irqsave(&mapping
->i_pages
, flags
);
2731 radix_tree_tag_clear(&mapping
->i_pages
, page_index(page
),
2732 PAGECACHE_TAG_DIRTY
);
2733 xa_unlock_irqrestore(&mapping
->i_pages
, flags
);
2736 int __init
f2fs_init_post_read_processing(void)
2738 bio_post_read_ctx_cache
= KMEM_CACHE(bio_post_read_ctx
, 0);
2739 if (!bio_post_read_ctx_cache
)
2741 bio_post_read_ctx_pool
=
2742 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS
,
2743 bio_post_read_ctx_cache
);
2744 if (!bio_post_read_ctx_pool
)
2745 goto fail_free_cache
;
2749 kmem_cache_destroy(bio_post_read_ctx_cache
);
2754 void __exit
f2fs_destroy_post_read_processing(void)
2756 mempool_destroy(bio_post_read_ctx_pool
);
2757 kmem_cache_destroy(bio_post_read_ctx_cache
);