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
);
85 SetPageUptodate(page
);
90 mempool_free(bio
->bi_private
, bio_post_read_ctx_pool
);
94 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
);
96 static void decrypt_work(struct work_struct
*work
)
98 struct bio_post_read_ctx
*ctx
=
99 container_of(work
, struct bio_post_read_ctx
, work
);
101 fscrypt_decrypt_bio(ctx
->bio
);
103 bio_post_read_processing(ctx
);
106 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
)
108 switch (++ctx
->cur_step
) {
110 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
)) {
111 INIT_WORK(&ctx
->work
, decrypt_work
);
112 fscrypt_enqueue_decrypt_work(&ctx
->work
);
118 __read_end_io(ctx
->bio
);
122 static bool f2fs_bio_post_read_required(struct bio
*bio
)
124 return bio
->bi_private
&& !bio
->bi_status
;
127 static void f2fs_read_end_io(struct bio
*bio
)
129 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio
)), FAULT_IO
)) {
130 f2fs_show_injection_info(FAULT_IO
);
131 bio
->bi_status
= BLK_STS_IOERR
;
134 if (f2fs_bio_post_read_required(bio
)) {
135 struct bio_post_read_ctx
*ctx
= bio
->bi_private
;
137 ctx
->cur_step
= STEP_INITIAL
;
138 bio_post_read_processing(ctx
);
145 static void f2fs_write_end_io(struct bio
*bio
)
147 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
148 struct bio_vec
*bvec
;
151 bio_for_each_segment_all(bvec
, bio
, i
) {
152 struct page
*page
= bvec
->bv_page
;
153 enum count_type type
= WB_DATA_TYPE(page
);
155 if (IS_DUMMY_WRITTEN_PAGE(page
)) {
156 set_page_private(page
, (unsigned long)NULL
);
157 ClearPagePrivate(page
);
159 mempool_free(page
, sbi
->write_io_dummy
);
161 if (unlikely(bio
->bi_status
))
162 f2fs_stop_checkpoint(sbi
, true);
166 fscrypt_pullback_bio_page(&page
, true);
168 if (unlikely(bio
->bi_status
)) {
169 mapping_set_error(page
->mapping
, -EIO
);
170 if (type
== F2FS_WB_CP_DATA
)
171 f2fs_stop_checkpoint(sbi
, true);
174 f2fs_bug_on(sbi
, page
->mapping
== NODE_MAPPING(sbi
) &&
175 page
->index
!= nid_of_node(page
));
177 dec_page_count(sbi
, type
);
178 if (f2fs_in_warm_node_list(sbi
, page
))
179 f2fs_del_fsync_node_entry(sbi
, page
);
180 clear_cold_data(page
);
181 end_page_writeback(page
);
183 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
184 wq_has_sleeper(&sbi
->cp_wait
))
185 wake_up(&sbi
->cp_wait
);
191 * Return true, if pre_bio's bdev is same as its target device.
193 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
194 block_t blk_addr
, struct bio
*bio
)
196 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
199 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
200 if (FDEV(i
).start_blk
<= blk_addr
&&
201 FDEV(i
).end_blk
>= blk_addr
) {
202 blk_addr
-= FDEV(i
).start_blk
;
208 bio_set_dev(bio
, bdev
);
209 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
214 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
218 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
219 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
224 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
225 block_t blk_addr
, struct bio
*bio
)
227 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
228 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
232 * Low-level block read/write IO operations.
234 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
235 struct writeback_control
*wbc
,
236 int npages
, bool is_read
,
237 enum page_type type
, enum temp_type temp
)
241 bio
= f2fs_bio_alloc(sbi
, npages
, true);
243 f2fs_target_device(sbi
, blk_addr
, bio
);
245 bio
->bi_end_io
= f2fs_read_end_io
;
246 bio
->bi_private
= NULL
;
248 bio
->bi_end_io
= f2fs_write_end_io
;
249 bio
->bi_private
= sbi
;
250 bio
->bi_write_hint
= f2fs_io_type_to_rw_hint(sbi
, type
, temp
);
253 wbc_init_bio(wbc
, bio
);
258 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
259 struct bio
*bio
, enum page_type type
)
261 if (!is_read_io(bio_op(bio
))) {
264 if (type
!= DATA
&& type
!= NODE
)
267 if (test_opt(sbi
, LFS
) && current
->plug
)
268 blk_finish_plug(current
->plug
);
270 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
271 start
%= F2FS_IO_SIZE(sbi
);
276 /* fill dummy pages */
277 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
279 mempool_alloc(sbi
->write_io_dummy
,
280 GFP_NOIO
| __GFP_ZERO
| __GFP_NOFAIL
);
281 f2fs_bug_on(sbi
, !page
);
283 SetPagePrivate(page
);
284 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
286 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
290 * In the NODE case, we lose next block address chain. So, we
291 * need to do checkpoint in f2fs_sync_file.
294 set_sbi_flag(sbi
, SBI_NEED_CP
);
297 if (is_read_io(bio_op(bio
)))
298 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
300 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
304 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
306 struct f2fs_io_info
*fio
= &io
->fio
;
311 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
313 if (is_read_io(fio
->op
))
314 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
316 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
318 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
322 static bool __has_merged_page(struct f2fs_bio_info
*io
,
323 struct inode
*inode
, nid_t ino
, pgoff_t idx
)
325 struct bio_vec
*bvec
;
335 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
337 if (bvec
->bv_page
->mapping
)
338 target
= bvec
->bv_page
;
340 target
= fscrypt_control_page(bvec
->bv_page
);
342 if (idx
!= target
->index
)
345 if (inode
&& inode
== target
->mapping
->host
)
347 if (ino
&& ino
== ino_of_node(target
))
354 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
355 nid_t ino
, pgoff_t idx
, enum page_type type
)
357 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
359 struct f2fs_bio_info
*io
;
362 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
363 io
= sbi
->write_io
[btype
] + temp
;
365 down_read(&io
->io_rwsem
);
366 ret
= __has_merged_page(io
, inode
, ino
, idx
);
367 up_read(&io
->io_rwsem
);
369 /* TODO: use HOT temp only for meta pages now. */
370 if (ret
|| btype
== META
)
376 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
377 enum page_type type
, enum temp_type temp
)
379 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
380 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
382 down_write(&io
->io_rwsem
);
384 /* change META to META_FLUSH in the checkpoint procedure */
385 if (type
>= META_FLUSH
) {
386 io
->fio
.type
= META_FLUSH
;
387 io
->fio
.op
= REQ_OP_WRITE
;
388 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
389 if (!test_opt(sbi
, NOBARRIER
))
390 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
392 __submit_merged_bio(io
);
393 up_write(&io
->io_rwsem
);
396 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
397 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
398 enum page_type type
, bool force
)
402 if (!force
&& !has_merged_page(sbi
, inode
, ino
, idx
, type
))
405 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
407 __f2fs_submit_merged_write(sbi
, type
, temp
);
409 /* TODO: use HOT temp only for meta pages now. */
415 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
417 __submit_merged_write_cond(sbi
, NULL
, 0, 0, type
, true);
420 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
421 struct inode
*inode
, nid_t ino
, pgoff_t idx
,
424 __submit_merged_write_cond(sbi
, inode
, ino
, idx
, type
, false);
427 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
429 f2fs_submit_merged_write(sbi
, DATA
);
430 f2fs_submit_merged_write(sbi
, NODE
);
431 f2fs_submit_merged_write(sbi
, META
);
435 * Fill the locked page with data located in the block address.
436 * A caller needs to unlock the page on failure.
438 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
441 struct page
*page
= fio
->encrypted_page
?
442 fio
->encrypted_page
: fio
->page
;
444 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
445 __is_meta_io(fio
) ? META_GENERIC
: DATA_GENERIC
))
448 trace_f2fs_submit_page_bio(page
, fio
);
449 f2fs_trace_ios(fio
, 0);
451 /* Allocate a new bio */
452 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
453 1, is_read_io(fio
->op
), fio
->type
, fio
->temp
);
455 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
459 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
461 __submit_bio(fio
->sbi
, bio
, fio
->type
);
463 if (!is_read_io(fio
->op
))
464 inc_page_count(fio
->sbi
, WB_DATA_TYPE(fio
->page
));
468 void f2fs_submit_page_write(struct f2fs_io_info
*fio
)
470 struct f2fs_sb_info
*sbi
= fio
->sbi
;
471 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
472 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
473 struct page
*bio_page
;
475 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
477 down_write(&io
->io_rwsem
);
480 spin_lock(&io
->io_lock
);
481 if (list_empty(&io
->io_list
)) {
482 spin_unlock(&io
->io_lock
);
485 fio
= list_first_entry(&io
->io_list
,
486 struct f2fs_io_info
, list
);
487 list_del(&fio
->list
);
488 spin_unlock(&io
->io_lock
);
491 if (__is_valid_data_blkaddr(fio
->old_blkaddr
))
492 verify_block_addr(fio
, fio
->old_blkaddr
);
493 verify_block_addr(fio
, fio
->new_blkaddr
);
495 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
497 /* set submitted = true as a return value */
498 fio
->submitted
= true;
500 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
502 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
503 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
) ||
504 !__same_bdev(sbi
, fio
->new_blkaddr
, io
->bio
)))
505 __submit_merged_bio(io
);
507 if (io
->bio
== NULL
) {
508 if ((fio
->type
== DATA
|| fio
->type
== NODE
) &&
509 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
510 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
514 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
, fio
->io_wbc
,
515 BIO_MAX_PAGES
, false,
516 fio
->type
, fio
->temp
);
520 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
521 __submit_merged_bio(io
);
526 wbc_account_io(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
528 io
->last_block_in_bio
= fio
->new_blkaddr
;
529 f2fs_trace_ios(fio
, 0);
531 trace_f2fs_submit_page_write(fio
->page
, fio
);
536 up_write(&io
->io_rwsem
);
539 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
540 unsigned nr_pages
, unsigned op_flag
)
542 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
544 struct bio_post_read_ctx
*ctx
;
545 unsigned int post_read_steps
= 0;
547 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
))
548 return ERR_PTR(-EFAULT
);
550 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
552 return ERR_PTR(-ENOMEM
);
553 f2fs_target_device(sbi
, blkaddr
, bio
);
554 bio
->bi_end_io
= f2fs_read_end_io
;
555 bio_set_op_attrs(bio
, REQ_OP_READ
, op_flag
);
557 if (f2fs_encrypted_file(inode
))
558 post_read_steps
|= 1 << STEP_DECRYPT
;
559 if (post_read_steps
) {
560 ctx
= mempool_alloc(bio_post_read_ctx_pool
, GFP_NOFS
);
563 return ERR_PTR(-ENOMEM
);
566 ctx
->enabled_steps
= post_read_steps
;
567 bio
->bi_private
= ctx
;
569 /* wait the page to be moved by cleaning */
570 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
576 /* This can handle encryption stuffs */
577 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
580 struct bio
*bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1, 0);
585 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
589 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
593 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
595 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
599 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
600 base
= get_extra_isize(dn
->inode
);
602 /* Get physical address of data block */
603 addr_array
= blkaddr_in_node(rn
);
604 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
608 * Lock ordering for the change of data block address:
611 * update block addresses in the node page
613 void f2fs_set_data_blkaddr(struct dnode_of_data
*dn
)
615 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
616 __set_data_blkaddr(dn
);
617 if (set_page_dirty(dn
->node_page
))
618 dn
->node_changed
= true;
621 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
623 dn
->data_blkaddr
= blkaddr
;
624 f2fs_set_data_blkaddr(dn
);
625 f2fs_update_extent_cache(dn
);
628 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
629 int f2fs_reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
631 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
637 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
639 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
642 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
643 dn
->ofs_in_node
, count
);
645 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
647 for (; count
> 0; dn
->ofs_in_node
++) {
648 block_t blkaddr
= datablock_addr(dn
->inode
,
649 dn
->node_page
, dn
->ofs_in_node
);
650 if (blkaddr
== NULL_ADDR
) {
651 dn
->data_blkaddr
= NEW_ADDR
;
652 __set_data_blkaddr(dn
);
657 if (set_page_dirty(dn
->node_page
))
658 dn
->node_changed
= true;
662 /* Should keep dn->ofs_in_node unchanged */
663 int f2fs_reserve_new_block(struct dnode_of_data
*dn
)
665 unsigned int ofs_in_node
= dn
->ofs_in_node
;
668 ret
= f2fs_reserve_new_blocks(dn
, 1);
669 dn
->ofs_in_node
= ofs_in_node
;
673 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
675 bool need_put
= dn
->inode_page
? false : true;
678 err
= f2fs_get_dnode_of_data(dn
, index
, ALLOC_NODE
);
682 if (dn
->data_blkaddr
== NULL_ADDR
)
683 err
= f2fs_reserve_new_block(dn
);
689 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
691 struct extent_info ei
= {0,0,0};
692 struct inode
*inode
= dn
->inode
;
694 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
695 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
699 return f2fs_reserve_block(dn
, index
);
702 struct page
*f2fs_get_read_data_page(struct inode
*inode
, pgoff_t index
,
703 int op_flags
, bool for_write
)
705 struct address_space
*mapping
= inode
->i_mapping
;
706 struct dnode_of_data dn
;
708 struct extent_info ei
= {0,0,0};
711 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
713 return ERR_PTR(-ENOMEM
);
715 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
716 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
720 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
721 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
726 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
731 if (PageUptodate(page
)) {
737 * A new dentry page is allocated but not able to be written, since its
738 * new inode page couldn't be allocated due to -ENOSPC.
739 * In such the case, its blkaddr can be remained as NEW_ADDR.
740 * see, f2fs_add_link -> f2fs_get_new_data_page ->
741 * f2fs_init_inode_metadata.
743 if (dn
.data_blkaddr
== NEW_ADDR
) {
744 zero_user_segment(page
, 0, PAGE_SIZE
);
745 if (!PageUptodate(page
))
746 SetPageUptodate(page
);
751 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
757 f2fs_put_page(page
, 1);
761 struct page
*f2fs_find_data_page(struct inode
*inode
, pgoff_t index
)
763 struct address_space
*mapping
= inode
->i_mapping
;
766 page
= find_get_page(mapping
, index
);
767 if (page
&& PageUptodate(page
))
769 f2fs_put_page(page
, 0);
771 page
= f2fs_get_read_data_page(inode
, index
, 0, false);
775 if (PageUptodate(page
))
778 wait_on_page_locked(page
);
779 if (unlikely(!PageUptodate(page
))) {
780 f2fs_put_page(page
, 0);
781 return ERR_PTR(-EIO
);
787 * If it tries to access a hole, return an error.
788 * Because, the callers, functions in dir.c and GC, should be able to know
789 * whether this page exists or not.
791 struct page
*f2fs_get_lock_data_page(struct inode
*inode
, pgoff_t index
,
794 struct address_space
*mapping
= inode
->i_mapping
;
797 page
= f2fs_get_read_data_page(inode
, index
, 0, for_write
);
801 /* wait for read completion */
803 if (unlikely(page
->mapping
!= mapping
)) {
804 f2fs_put_page(page
, 1);
807 if (unlikely(!PageUptodate(page
))) {
808 f2fs_put_page(page
, 1);
809 return ERR_PTR(-EIO
);
815 * Caller ensures that this data page is never allocated.
816 * A new zero-filled data page is allocated in the page cache.
818 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
820 * Note that, ipage is set only by make_empty_dir, and if any error occur,
821 * ipage should be released by this function.
823 struct page
*f2fs_get_new_data_page(struct inode
*inode
,
824 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
826 struct address_space
*mapping
= inode
->i_mapping
;
828 struct dnode_of_data dn
;
831 page
= f2fs_grab_cache_page(mapping
, index
, true);
834 * before exiting, we should make sure ipage will be released
835 * if any error occur.
837 f2fs_put_page(ipage
, 1);
838 return ERR_PTR(-ENOMEM
);
841 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
842 err
= f2fs_reserve_block(&dn
, index
);
844 f2fs_put_page(page
, 1);
850 if (PageUptodate(page
))
853 if (dn
.data_blkaddr
== NEW_ADDR
) {
854 zero_user_segment(page
, 0, PAGE_SIZE
);
855 if (!PageUptodate(page
))
856 SetPageUptodate(page
);
858 f2fs_put_page(page
, 1);
860 /* if ipage exists, blkaddr should be NEW_ADDR */
861 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
862 page
= f2fs_get_lock_data_page(inode
, index
, true);
867 if (new_i_size
&& i_size_read(inode
) <
868 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
869 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
873 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
875 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
876 struct f2fs_summary sum
;
882 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
885 err
= f2fs_get_node_info(sbi
, dn
->nid
, &ni
);
889 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
890 dn
->node_page
, dn
->ofs_in_node
);
891 if (dn
->data_blkaddr
== NEW_ADDR
)
894 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
898 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
900 f2fs_allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
901 &sum
, seg_type
, NULL
, false);
902 f2fs_set_data_blkaddr(dn
);
905 fofs
= f2fs_start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
907 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
908 f2fs_i_size_write(dn
->inode
,
909 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
913 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
915 struct inode
*inode
= file_inode(iocb
->ki_filp
);
916 struct f2fs_map_blocks map
;
919 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
921 /* convert inline data for Direct I/O*/
923 err
= f2fs_convert_inline_inode(inode
);
928 if (is_inode_flag_set(inode
, FI_NO_PREALLOC
))
931 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
932 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
933 if (map
.m_len
> map
.m_lblk
)
934 map
.m_len
-= map
.m_lblk
;
938 map
.m_next_pgofs
= NULL
;
939 map
.m_next_extent
= NULL
;
940 map
.m_seg_type
= NO_CHECK_TYPE
;
943 map
.m_seg_type
= f2fs_rw_hint_to_seg_type(iocb
->ki_hint
);
944 flag
= f2fs_force_buffered_io(inode
, WRITE
) ?
945 F2FS_GET_BLOCK_PRE_AIO
:
946 F2FS_GET_BLOCK_PRE_DIO
;
949 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
950 err
= f2fs_convert_inline_inode(inode
);
954 if (f2fs_has_inline_data(inode
))
957 flag
= F2FS_GET_BLOCK_PRE_AIO
;
960 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
961 if (map
.m_len
> 0 && err
== -ENOSPC
) {
963 set_inode_flag(inode
, FI_NO_PREALLOC
);
969 static inline void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
971 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
973 down_read(&sbi
->node_change
);
975 up_read(&sbi
->node_change
);
985 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
986 * f2fs_map_blocks structure.
987 * If original data blocks are allocated, then give them to blockdev.
989 * a. preallocate requested block addresses
990 * b. do not use extent cache for better performance
991 * c. give the block addresses to blockdev
993 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
994 int create
, int flag
)
996 unsigned int maxblocks
= map
->m_len
;
997 struct dnode_of_data dn
;
998 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
999 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
1000 pgoff_t pgofs
, end_offset
, end
;
1001 int err
= 0, ofs
= 1;
1002 unsigned int ofs_in_node
, last_ofs_in_node
;
1004 struct extent_info ei
= {0,0,0};
1006 unsigned int start_pgofs
;
1014 /* it only supports block size == page size */
1015 pgofs
= (pgoff_t
)map
->m_lblk
;
1016 end
= pgofs
+ maxblocks
;
1018 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
1019 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
1020 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
1021 map
->m_flags
= F2FS_MAP_MAPPED
;
1022 if (map
->m_next_extent
)
1023 *map
->m_next_extent
= pgofs
+ map
->m_len
;
1029 __do_map_lock(sbi
, flag
, true);
1031 /* When reading holes, we need its node page */
1032 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1033 err
= f2fs_get_dnode_of_data(&dn
, pgofs
, mode
);
1035 if (flag
== F2FS_GET_BLOCK_BMAP
)
1037 if (err
== -ENOENT
) {
1039 if (map
->m_next_pgofs
)
1040 *map
->m_next_pgofs
=
1041 f2fs_get_next_page_offset(&dn
, pgofs
);
1042 if (map
->m_next_extent
)
1043 *map
->m_next_extent
=
1044 f2fs_get_next_page_offset(&dn
, pgofs
);
1049 start_pgofs
= pgofs
;
1051 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
1052 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1055 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
1057 if (__is_valid_data_blkaddr(blkaddr
) &&
1058 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
)) {
1063 if (!is_valid_data_blkaddr(sbi
, blkaddr
)) {
1065 if (unlikely(f2fs_cp_error(sbi
))) {
1069 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1070 if (blkaddr
== NULL_ADDR
) {
1072 last_ofs_in_node
= dn
.ofs_in_node
;
1075 err
= __allocate_data_block(&dn
,
1078 set_inode_flag(inode
, FI_APPEND_WRITE
);
1082 map
->m_flags
|= F2FS_MAP_NEW
;
1083 blkaddr
= dn
.data_blkaddr
;
1085 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1089 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1091 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1092 blkaddr
== NULL_ADDR
) {
1093 if (map
->m_next_pgofs
)
1094 *map
->m_next_pgofs
= pgofs
+ 1;
1097 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1098 /* for defragment case */
1099 if (map
->m_next_pgofs
)
1100 *map
->m_next_pgofs
= pgofs
+ 1;
1106 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1109 if (map
->m_len
== 0) {
1110 /* preallocated unwritten block should be mapped for fiemap. */
1111 if (blkaddr
== NEW_ADDR
)
1112 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1113 map
->m_flags
|= F2FS_MAP_MAPPED
;
1115 map
->m_pblk
= blkaddr
;
1117 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1118 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1119 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1120 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1131 /* preallocate blocks in batch for one dnode page */
1132 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1133 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1135 dn
.ofs_in_node
= ofs_in_node
;
1136 err
= f2fs_reserve_new_blocks(&dn
, prealloc
);
1140 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1141 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1145 dn
.ofs_in_node
= end_offset
;
1150 else if (dn
.ofs_in_node
< end_offset
)
1153 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1154 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1155 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1157 f2fs_update_extent_cache_range(&dn
,
1158 start_pgofs
, map
->m_pblk
+ ofs
,
1163 f2fs_put_dnode(&dn
);
1166 __do_map_lock(sbi
, flag
, false);
1167 f2fs_balance_fs(sbi
, dn
.node_changed
);
1172 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1173 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1174 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1176 f2fs_update_extent_cache_range(&dn
,
1177 start_pgofs
, map
->m_pblk
+ ofs
,
1180 if (map
->m_next_extent
)
1181 *map
->m_next_extent
= pgofs
+ 1;
1183 f2fs_put_dnode(&dn
);
1186 __do_map_lock(sbi
, flag
, false);
1187 f2fs_balance_fs(sbi
, dn
.node_changed
);
1190 trace_f2fs_map_blocks(inode
, map
, err
);
1194 bool f2fs_overwrite_io(struct inode
*inode
, loff_t pos
, size_t len
)
1196 struct f2fs_map_blocks map
;
1200 if (pos
+ len
> i_size_read(inode
))
1203 map
.m_lblk
= F2FS_BYTES_TO_BLK(pos
);
1204 map
.m_next_pgofs
= NULL
;
1205 map
.m_next_extent
= NULL
;
1206 map
.m_seg_type
= NO_CHECK_TYPE
;
1207 last_lblk
= F2FS_BLK_ALIGN(pos
+ len
);
1209 while (map
.m_lblk
< last_lblk
) {
1210 map
.m_len
= last_lblk
- map
.m_lblk
;
1211 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1212 if (err
|| map
.m_len
== 0)
1214 map
.m_lblk
+= map
.m_len
;
1219 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1220 struct buffer_head
*bh
, int create
, int flag
,
1221 pgoff_t
*next_pgofs
, int seg_type
)
1223 struct f2fs_map_blocks map
;
1226 map
.m_lblk
= iblock
;
1227 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1228 map
.m_next_pgofs
= next_pgofs
;
1229 map
.m_next_extent
= NULL
;
1230 map
.m_seg_type
= seg_type
;
1232 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1234 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1235 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1236 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1241 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1242 struct buffer_head
*bh_result
, int create
, int flag
,
1243 pgoff_t
*next_pgofs
)
1245 return __get_data_block(inode
, iblock
, bh_result
, create
,
1250 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1251 struct buffer_head
*bh_result
, int create
)
1253 return __get_data_block(inode
, iblock
, bh_result
, create
,
1254 F2FS_GET_BLOCK_DEFAULT
, NULL
,
1255 f2fs_rw_hint_to_seg_type(
1256 inode
->i_write_hint
));
1259 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1260 struct buffer_head
*bh_result
, int create
)
1262 /* Block number less than F2FS MAX BLOCKS */
1263 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1266 return __get_data_block(inode
, iblock
, bh_result
, create
,
1267 F2FS_GET_BLOCK_BMAP
, NULL
,
1271 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1273 return (offset
>> inode
->i_blkbits
);
1276 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1278 return (blk
<< inode
->i_blkbits
);
1281 static int f2fs_xattr_fiemap(struct inode
*inode
,
1282 struct fiemap_extent_info
*fieinfo
)
1284 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1286 struct node_info ni
;
1287 __u64 phys
= 0, len
;
1289 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1292 if (f2fs_has_inline_xattr(inode
)) {
1295 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1296 inode
->i_ino
, false);
1300 err
= f2fs_get_node_info(sbi
, inode
->i_ino
, &ni
);
1302 f2fs_put_page(page
, 1);
1306 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1307 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1308 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1309 get_inline_xattr_addrs(inode
));
1312 len
= inline_xattr_size(inode
);
1314 f2fs_put_page(page
, 1);
1316 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1319 flags
|= FIEMAP_EXTENT_LAST
;
1321 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1322 if (err
|| err
== 1)
1327 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1331 err
= f2fs_get_node_info(sbi
, xnid
, &ni
);
1333 f2fs_put_page(page
, 1);
1337 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1338 len
= inode
->i_sb
->s_blocksize
;
1340 f2fs_put_page(page
, 1);
1342 flags
= FIEMAP_EXTENT_LAST
;
1346 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1348 return (err
< 0 ? err
: 0);
1351 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1354 struct buffer_head map_bh
;
1355 sector_t start_blk
, last_blk
;
1357 u64 logical
= 0, phys
= 0, size
= 0;
1361 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1362 ret
= f2fs_precache_extents(inode
);
1367 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1373 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1374 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1378 if (f2fs_has_inline_data(inode
)) {
1379 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1384 if (logical_to_blk(inode
, len
) == 0)
1385 len
= blk_to_logical(inode
, 1);
1387 start_blk
= logical_to_blk(inode
, start
);
1388 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1391 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1392 map_bh
.b_size
= len
;
1394 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1395 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1400 if (!buffer_mapped(&map_bh
)) {
1401 start_blk
= next_pgofs
;
1403 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1404 F2FS_I_SB(inode
)->max_file_blocks
))
1407 flags
|= FIEMAP_EXTENT_LAST
;
1411 if (f2fs_encrypted_inode(inode
))
1412 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1414 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1418 if (start_blk
> last_blk
|| ret
)
1421 logical
= blk_to_logical(inode
, start_blk
);
1422 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1423 size
= map_bh
.b_size
;
1425 if (buffer_unwritten(&map_bh
))
1426 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1428 start_blk
+= logical_to_blk(inode
, size
);
1432 if (fatal_signal_pending(current
))
1440 inode_unlock(inode
);
1445 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1446 * Major change was from block_size == page_size in f2fs by default.
1448 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1449 * this function ever deviates from doing just read-ahead, it should either
1450 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1453 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1454 struct list_head
*pages
, struct page
*page
,
1455 unsigned nr_pages
, bool is_readahead
)
1457 struct bio
*bio
= NULL
;
1458 sector_t last_block_in_bio
= 0;
1459 struct inode
*inode
= mapping
->host
;
1460 const unsigned blkbits
= inode
->i_blkbits
;
1461 const unsigned blocksize
= 1 << blkbits
;
1462 sector_t block_in_file
;
1463 sector_t last_block
;
1464 sector_t last_block_in_file
;
1466 struct f2fs_map_blocks map
;
1472 map
.m_next_pgofs
= NULL
;
1473 map
.m_next_extent
= NULL
;
1474 map
.m_seg_type
= NO_CHECK_TYPE
;
1476 for (; nr_pages
; nr_pages
--) {
1478 page
= list_last_entry(pages
, struct page
, lru
);
1480 prefetchw(&page
->flags
);
1481 list_del(&page
->lru
);
1482 if (add_to_page_cache_lru(page
, mapping
,
1484 readahead_gfp_mask(mapping
)))
1488 block_in_file
= (sector_t
)page
->index
;
1489 last_block
= block_in_file
+ nr_pages
;
1490 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1492 if (last_block
> last_block_in_file
)
1493 last_block
= last_block_in_file
;
1496 * Map blocks using the previous result first.
1498 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1499 block_in_file
> map
.m_lblk
&&
1500 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1504 * Then do more f2fs_map_blocks() calls until we are
1505 * done with this page.
1509 if (block_in_file
< last_block
) {
1510 map
.m_lblk
= block_in_file
;
1511 map
.m_len
= last_block
- block_in_file
;
1513 if (f2fs_map_blocks(inode
, &map
, 0,
1514 F2FS_GET_BLOCK_DEFAULT
))
1515 goto set_error_page
;
1518 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1519 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1520 SetPageMappedToDisk(page
);
1522 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1523 SetPageUptodate(page
);
1527 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), block_nr
,
1529 goto set_error_page
;
1531 zero_user_segment(page
, 0, PAGE_SIZE
);
1532 if (!PageUptodate(page
))
1533 SetPageUptodate(page
);
1539 * This page will go to BIO. Do we need to send this
1542 if (bio
&& (last_block_in_bio
!= block_nr
- 1 ||
1543 !__same_bdev(F2FS_I_SB(inode
), block_nr
, bio
))) {
1545 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1549 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
,
1550 is_readahead
? REQ_RAHEAD
: 0);
1553 goto set_error_page
;
1557 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1558 goto submit_and_realloc
;
1560 last_block_in_bio
= block_nr
;
1564 zero_user_segment(page
, 0, PAGE_SIZE
);
1569 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1577 BUG_ON(pages
&& !list_empty(pages
));
1579 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1583 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1585 struct inode
*inode
= page
->mapping
->host
;
1588 trace_f2fs_readpage(page
, DATA
);
1590 /* If the file has inline data, try to read it directly */
1591 if (f2fs_has_inline_data(inode
))
1592 ret
= f2fs_read_inline_data(inode
, page
);
1594 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1, false);
1598 static int f2fs_read_data_pages(struct file
*file
,
1599 struct address_space
*mapping
,
1600 struct list_head
*pages
, unsigned nr_pages
)
1602 struct inode
*inode
= mapping
->host
;
1603 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
1605 trace_f2fs_readpages(inode
, page
, nr_pages
);
1607 /* If the file has inline data, skip readpages */
1608 if (f2fs_has_inline_data(inode
))
1611 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
, true);
1614 static int encrypt_one_page(struct f2fs_io_info
*fio
)
1616 struct inode
*inode
= fio
->page
->mapping
->host
;
1617 gfp_t gfp_flags
= GFP_NOFS
;
1619 if (!f2fs_encrypted_file(inode
))
1622 /* wait for GCed page writeback via META_MAPPING */
1623 f2fs_wait_on_block_writeback(fio
->sbi
, fio
->old_blkaddr
);
1626 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1627 PAGE_SIZE
, 0, fio
->page
->index
, gfp_flags
);
1628 if (!IS_ERR(fio
->encrypted_page
))
1631 /* flush pending IOs and wait for a while in the ENOMEM case */
1632 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
1633 f2fs_flush_merged_writes(fio
->sbi
);
1634 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1635 gfp_flags
|= __GFP_NOFAIL
;
1638 return PTR_ERR(fio
->encrypted_page
);
1641 static inline bool check_inplace_update_policy(struct inode
*inode
,
1642 struct f2fs_io_info
*fio
)
1644 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1645 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
1647 if (policy
& (0x1 << F2FS_IPU_FORCE
))
1649 if (policy
& (0x1 << F2FS_IPU_SSR
) && f2fs_need_SSR(sbi
))
1651 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
1652 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1654 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && f2fs_need_SSR(sbi
) &&
1655 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
1659 * IPU for rewrite async pages
1661 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
1662 fio
&& fio
->op
== REQ_OP_WRITE
&&
1663 !(fio
->op_flags
& REQ_SYNC
) &&
1664 !f2fs_encrypted_inode(inode
))
1667 /* this is only set during fdatasync */
1668 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
1669 is_inode_flag_set(inode
, FI_NEED_IPU
))
1675 bool f2fs_should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1677 if (f2fs_is_pinned_file(inode
))
1680 /* if this is cold file, we should overwrite to avoid fragmentation */
1681 if (file_is_cold(inode
))
1684 return check_inplace_update_policy(inode
, fio
);
1687 bool f2fs_should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
1689 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1691 if (test_opt(sbi
, LFS
))
1693 if (S_ISDIR(inode
->i_mode
))
1695 if (f2fs_is_atomic_file(inode
))
1698 if (is_cold_data(fio
->page
))
1700 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
1706 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
1708 struct inode
*inode
= fio
->page
->mapping
->host
;
1710 if (f2fs_should_update_outplace(inode
, fio
))
1713 return f2fs_should_update_inplace(inode
, fio
);
1716 int f2fs_do_write_data_page(struct f2fs_io_info
*fio
)
1718 struct page
*page
= fio
->page
;
1719 struct inode
*inode
= page
->mapping
->host
;
1720 struct dnode_of_data dn
;
1721 struct extent_info ei
= {0,0,0};
1722 struct node_info ni
;
1723 bool ipu_force
= false;
1726 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1727 if (need_inplace_update(fio
) &&
1728 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
1729 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
1731 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
1736 fio
->need_lock
= LOCK_DONE
;
1740 /* Deadlock due to between page->lock and f2fs_lock_op */
1741 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
1744 err
= f2fs_get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1748 fio
->old_blkaddr
= dn
.data_blkaddr
;
1750 /* This page is already truncated */
1751 if (fio
->old_blkaddr
== NULL_ADDR
) {
1752 ClearPageUptodate(page
);
1756 if (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
1757 !f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
1763 * If current allocation needs SSR,
1764 * it had better in-place writes for updated data.
1766 if (ipu_force
|| (is_valid_data_blkaddr(fio
->sbi
, fio
->old_blkaddr
) &&
1767 need_inplace_update(fio
))) {
1768 err
= encrypt_one_page(fio
);
1772 set_page_writeback(page
);
1773 ClearPageError(page
);
1774 f2fs_put_dnode(&dn
);
1775 if (fio
->need_lock
== LOCK_REQ
)
1776 f2fs_unlock_op(fio
->sbi
);
1777 err
= f2fs_inplace_write_data(fio
);
1778 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
1779 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1783 if (fio
->need_lock
== LOCK_RETRY
) {
1784 if (!f2fs_trylock_op(fio
->sbi
)) {
1788 fio
->need_lock
= LOCK_REQ
;
1791 err
= f2fs_get_node_info(fio
->sbi
, dn
.nid
, &ni
);
1795 fio
->version
= ni
.version
;
1797 err
= encrypt_one_page(fio
);
1801 set_page_writeback(page
);
1802 ClearPageError(page
);
1804 /* LFS mode write path */
1805 f2fs_outplace_write_data(&dn
, fio
);
1806 trace_f2fs_do_write_data_page(page
, OPU
);
1807 set_inode_flag(inode
, FI_APPEND_WRITE
);
1808 if (page
->index
== 0)
1809 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1811 f2fs_put_dnode(&dn
);
1813 if (fio
->need_lock
== LOCK_REQ
)
1814 f2fs_unlock_op(fio
->sbi
);
1818 static int __write_data_page(struct page
*page
, bool *submitted
,
1819 struct writeback_control
*wbc
,
1820 enum iostat_type io_type
)
1822 struct inode
*inode
= page
->mapping
->host
;
1823 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1824 loff_t i_size
= i_size_read(inode
);
1825 const pgoff_t end_index
= ((unsigned long long) i_size
)
1827 loff_t psize
= (page
->index
+ 1) << PAGE_SHIFT
;
1828 unsigned offset
= 0;
1829 bool need_balance_fs
= false;
1831 struct f2fs_io_info fio
= {
1833 .ino
= inode
->i_ino
,
1836 .op_flags
= wbc_to_write_flags(wbc
),
1837 .old_blkaddr
= NULL_ADDR
,
1839 .encrypted_page
= NULL
,
1841 .need_lock
= LOCK_RETRY
,
1846 trace_f2fs_writepage(page
, DATA
);
1848 /* we should bypass data pages to proceed the kworkder jobs */
1849 if (unlikely(f2fs_cp_error(sbi
))) {
1850 mapping_set_error(page
->mapping
, -EIO
);
1852 * don't drop any dirty dentry pages for keeping lastest
1853 * directory structure.
1855 if (S_ISDIR(inode
->i_mode
))
1860 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1863 if (page
->index
< end_index
)
1867 * If the offset is out-of-range of file size,
1868 * this page does not have to be written to disk.
1870 offset
= i_size
& (PAGE_SIZE
- 1);
1871 if ((page
->index
>= end_index
+ 1) || !offset
)
1874 zero_user_segment(page
, offset
, PAGE_SIZE
);
1876 if (f2fs_is_drop_cache(inode
))
1878 /* we should not write 0'th page having journal header */
1879 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1880 (!wbc
->for_reclaim
&&
1881 f2fs_available_free_memory(sbi
, BASE_CHECK
))))
1884 /* Dentry blocks are controlled by checkpoint */
1885 if (S_ISDIR(inode
->i_mode
)) {
1886 fio
.need_lock
= LOCK_DONE
;
1887 err
= f2fs_do_write_data_page(&fio
);
1891 if (!wbc
->for_reclaim
)
1892 need_balance_fs
= true;
1893 else if (has_not_enough_free_secs(sbi
, 0, 0))
1896 set_inode_flag(inode
, FI_HOT_DATA
);
1899 if (f2fs_has_inline_data(inode
)) {
1900 err
= f2fs_write_inline_data(inode
, page
);
1905 if (err
== -EAGAIN
) {
1906 err
= f2fs_do_write_data_page(&fio
);
1907 if (err
== -EAGAIN
) {
1908 fio
.need_lock
= LOCK_REQ
;
1909 err
= f2fs_do_write_data_page(&fio
);
1914 file_set_keep_isize(inode
);
1916 down_write(&F2FS_I(inode
)->i_sem
);
1917 if (F2FS_I(inode
)->last_disk_size
< psize
)
1918 F2FS_I(inode
)->last_disk_size
= psize
;
1919 up_write(&F2FS_I(inode
)->i_sem
);
1923 if (err
&& err
!= -ENOENT
)
1927 inode_dec_dirty_pages(inode
);
1929 ClearPageUptodate(page
);
1931 if (wbc
->for_reclaim
) {
1932 f2fs_submit_merged_write_cond(sbi
, inode
, 0, page
->index
, DATA
);
1933 clear_inode_flag(inode
, FI_HOT_DATA
);
1934 f2fs_remove_dirty_inode(inode
);
1939 if (!S_ISDIR(inode
->i_mode
))
1940 f2fs_balance_fs(sbi
, need_balance_fs
);
1942 if (unlikely(f2fs_cp_error(sbi
))) {
1943 f2fs_submit_merged_write(sbi
, DATA
);
1948 *submitted
= fio
.submitted
;
1953 redirty_page_for_writepage(wbc
, page
);
1955 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1956 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1957 * file_write_and_wait_range() will see EIO error, which is critical
1958 * to return value of fsync() followed by atomic_write failure to user.
1960 if (!err
|| wbc
->for_reclaim
)
1961 return AOP_WRITEPAGE_ACTIVATE
;
1966 static int f2fs_write_data_page(struct page
*page
,
1967 struct writeback_control
*wbc
)
1969 return __write_data_page(page
, NULL
, wbc
, FS_DATA_IO
);
1973 * This function was copied from write_cche_pages from mm/page-writeback.c.
1974 * The major change is making write step of cold data page separately from
1975 * warm/hot data page.
1977 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1978 struct writeback_control
*wbc
,
1979 enum iostat_type io_type
)
1983 struct pagevec pvec
;
1984 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
1986 pgoff_t
uninitialized_var(writeback_index
);
1988 pgoff_t end
; /* Inclusive */
1990 pgoff_t last_idx
= ULONG_MAX
;
1992 int range_whole
= 0;
1995 pagevec_init(&pvec
);
1997 if (get_dirty_pages(mapping
->host
) <=
1998 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
1999 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
2001 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
2003 if (wbc
->range_cyclic
) {
2004 writeback_index
= mapping
->writeback_index
; /* prev offset */
2005 index
= writeback_index
;
2012 index
= wbc
->range_start
>> PAGE_SHIFT
;
2013 end
= wbc
->range_end
>> PAGE_SHIFT
;
2014 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2016 cycled
= 1; /* ignore range_cyclic tests */
2018 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2019 tag
= PAGECACHE_TAG_TOWRITE
;
2021 tag
= PAGECACHE_TAG_DIRTY
;
2023 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2024 tag_pages_for_writeback(mapping
, index
, end
);
2026 while (!done
&& (index
<= end
)) {
2029 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
2034 for (i
= 0; i
< nr_pages
; i
++) {
2035 struct page
*page
= pvec
.pages
[i
];
2036 bool submitted
= false;
2038 /* give a priority to WB_SYNC threads */
2039 if (atomic_read(&sbi
->wb_sync_req
[DATA
]) &&
2040 wbc
->sync_mode
== WB_SYNC_NONE
) {
2045 done_index
= page
->index
;
2049 if (unlikely(page
->mapping
!= mapping
)) {
2055 if (!PageDirty(page
)) {
2056 /* someone wrote it for us */
2057 goto continue_unlock
;
2060 if (PageWriteback(page
)) {
2061 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
2062 f2fs_wait_on_page_writeback(page
,
2065 goto continue_unlock
;
2068 BUG_ON(PageWriteback(page
));
2069 if (!clear_page_dirty_for_io(page
))
2070 goto continue_unlock
;
2072 ret
= __write_data_page(page
, &submitted
, wbc
, io_type
);
2073 if (unlikely(ret
)) {
2075 * keep nr_to_write, since vfs uses this to
2076 * get # of written pages.
2078 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
2082 } else if (ret
== -EAGAIN
) {
2084 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
2086 congestion_wait(BLK_RW_ASYNC
,
2092 done_index
= page
->index
+ 1;
2095 } else if (submitted
) {
2096 last_idx
= page
->index
;
2099 if (--wbc
->nr_to_write
<= 0 &&
2100 wbc
->sync_mode
== WB_SYNC_NONE
) {
2105 pagevec_release(&pvec
);
2109 if (!cycled
&& !done
) {
2112 end
= writeback_index
- 1;
2115 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2116 mapping
->writeback_index
= done_index
;
2118 if (last_idx
!= ULONG_MAX
)
2119 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
2125 static inline bool __should_serialize_io(struct inode
*inode
,
2126 struct writeback_control
*wbc
)
2128 if (!S_ISREG(inode
->i_mode
))
2130 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
2132 if (get_dirty_pages(inode
) >= SM_I(F2FS_I_SB(inode
))->min_seq_blocks
)
2137 static int __f2fs_write_data_pages(struct address_space
*mapping
,
2138 struct writeback_control
*wbc
,
2139 enum iostat_type io_type
)
2141 struct inode
*inode
= mapping
->host
;
2142 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2143 struct blk_plug plug
;
2145 bool locked
= false;
2147 /* deal with chardevs and other special file */
2148 if (!mapping
->a_ops
->writepage
)
2151 /* skip writing if there is no dirty page in this inode */
2152 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
2155 /* during POR, we don't need to trigger writepage at all. */
2156 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
2159 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
2160 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
2161 f2fs_available_free_memory(sbi
, DIRTY_DENTS
))
2164 /* skip writing during file defragment */
2165 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
2168 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2170 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2171 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2172 atomic_inc(&sbi
->wb_sync_req
[DATA
]);
2173 else if (atomic_read(&sbi
->wb_sync_req
[DATA
]))
2176 if (__should_serialize_io(inode
, wbc
)) {
2177 mutex_lock(&sbi
->writepages
);
2181 blk_start_plug(&plug
);
2182 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
2183 blk_finish_plug(&plug
);
2186 mutex_unlock(&sbi
->writepages
);
2188 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2189 atomic_dec(&sbi
->wb_sync_req
[DATA
]);
2191 * if some pages were truncated, we cannot guarantee its mapping->host
2192 * to detect pending bios.
2195 f2fs_remove_dirty_inode(inode
);
2199 wbc
->pages_skipped
+= get_dirty_pages(inode
);
2200 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
2204 static int f2fs_write_data_pages(struct address_space
*mapping
,
2205 struct writeback_control
*wbc
)
2207 struct inode
*inode
= mapping
->host
;
2209 return __f2fs_write_data_pages(mapping
, wbc
,
2210 F2FS_I(inode
)->cp_task
== current
?
2211 FS_CP_DATA_IO
: FS_DATA_IO
);
2214 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
2216 struct inode
*inode
= mapping
->host
;
2217 loff_t i_size
= i_size_read(inode
);
2220 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
2221 down_write(&F2FS_I(inode
)->i_mmap_sem
);
2223 truncate_pagecache(inode
, i_size
);
2224 f2fs_truncate_blocks(inode
, i_size
, true);
2226 up_write(&F2FS_I(inode
)->i_mmap_sem
);
2227 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
2231 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
2232 struct page
*page
, loff_t pos
, unsigned len
,
2233 block_t
*blk_addr
, bool *node_changed
)
2235 struct inode
*inode
= page
->mapping
->host
;
2236 pgoff_t index
= page
->index
;
2237 struct dnode_of_data dn
;
2239 bool locked
= false;
2240 struct extent_info ei
= {0,0,0};
2244 * we already allocated all the blocks, so we don't need to get
2245 * the block addresses when there is no need to fill the page.
2247 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
2248 !is_inode_flag_set(inode
, FI_NO_PREALLOC
))
2251 if (f2fs_has_inline_data(inode
) ||
2252 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
2253 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, true);
2257 /* check inline_data */
2258 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
2259 if (IS_ERR(ipage
)) {
2260 err
= PTR_ERR(ipage
);
2264 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
2266 if (f2fs_has_inline_data(inode
)) {
2267 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
2268 f2fs_do_read_inline_data(page
, ipage
);
2269 set_inode_flag(inode
, FI_DATA_EXIST
);
2271 set_inline_node(ipage
);
2273 err
= f2fs_convert_inline_page(&dn
, page
);
2276 if (dn
.data_blkaddr
== NULL_ADDR
)
2277 err
= f2fs_get_block(&dn
, index
);
2279 } else if (locked
) {
2280 err
= f2fs_get_block(&dn
, index
);
2282 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
2283 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
2286 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
2287 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
2288 f2fs_put_dnode(&dn
);
2289 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
2297 /* convert_inline_page can make node_changed */
2298 *blk_addr
= dn
.data_blkaddr
;
2299 *node_changed
= dn
.node_changed
;
2301 f2fs_put_dnode(&dn
);
2304 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, false);
2308 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
2309 loff_t pos
, unsigned len
, unsigned flags
,
2310 struct page
**pagep
, void **fsdata
)
2312 struct inode
*inode
= mapping
->host
;
2313 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2314 struct page
*page
= NULL
;
2315 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
2316 bool need_balance
= false, drop_atomic
= false;
2317 block_t blkaddr
= NULL_ADDR
;
2320 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
2322 if ((f2fs_is_atomic_file(inode
) &&
2323 !f2fs_available_free_memory(sbi
, INMEM_PAGES
)) ||
2324 is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
)) {
2331 * We should check this at this moment to avoid deadlock on inode page
2332 * and #0 page. The locking rule for inline_data conversion should be:
2333 * lock_page(page #0) -> lock_page(inode_page)
2336 err
= f2fs_convert_inline_inode(inode
);
2342 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2343 * wait_for_stable_page. Will wait that below with our IO control.
2345 page
= f2fs_pagecache_get_page(mapping
, index
,
2346 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
2354 err
= prepare_write_begin(sbi
, page
, pos
, len
,
2355 &blkaddr
, &need_balance
);
2359 if (need_balance
&& has_not_enough_free_secs(sbi
, 0, 0)) {
2361 f2fs_balance_fs(sbi
, true);
2363 if (page
->mapping
!= mapping
) {
2364 /* The page got truncated from under us */
2365 f2fs_put_page(page
, 1);
2370 f2fs_wait_on_page_writeback(page
, DATA
, false);
2372 /* wait for GCed page writeback via META_MAPPING */
2373 if (f2fs_post_read_required(inode
))
2374 f2fs_wait_on_block_writeback(sbi
, blkaddr
);
2376 if (len
== PAGE_SIZE
|| PageUptodate(page
))
2379 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
)) {
2380 zero_user_segment(page
, len
, PAGE_SIZE
);
2384 if (blkaddr
== NEW_ADDR
) {
2385 zero_user_segment(page
, 0, PAGE_SIZE
);
2386 SetPageUptodate(page
);
2388 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
2393 if (unlikely(page
->mapping
!= mapping
)) {
2394 f2fs_put_page(page
, 1);
2397 if (unlikely(!PageUptodate(page
))) {
2405 f2fs_put_page(page
, 1);
2406 f2fs_write_failed(mapping
, pos
+ len
);
2408 f2fs_drop_inmem_pages_all(sbi
, false);
2412 static int f2fs_write_end(struct file
*file
,
2413 struct address_space
*mapping
,
2414 loff_t pos
, unsigned len
, unsigned copied
,
2415 struct page
*page
, void *fsdata
)
2417 struct inode
*inode
= page
->mapping
->host
;
2419 trace_f2fs_write_end(inode
, pos
, len
, copied
);
2422 * This should be come from len == PAGE_SIZE, and we expect copied
2423 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2424 * let generic_perform_write() try to copy data again through copied=0.
2426 if (!PageUptodate(page
)) {
2427 if (unlikely(copied
!= len
))
2430 SetPageUptodate(page
);
2435 set_page_dirty(page
);
2437 if (pos
+ copied
> i_size_read(inode
))
2438 f2fs_i_size_write(inode
, pos
+ copied
);
2440 f2fs_put_page(page
, 1);
2441 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
2445 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
2448 unsigned i_blkbits
= READ_ONCE(inode
->i_blkbits
);
2449 unsigned blkbits
= i_blkbits
;
2450 unsigned blocksize_mask
= (1 << blkbits
) - 1;
2451 unsigned long align
= offset
| iov_iter_alignment(iter
);
2452 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
2454 if (align
& blocksize_mask
) {
2456 blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
2457 blocksize_mask
= (1 << blkbits
) - 1;
2458 if (align
& blocksize_mask
)
2465 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2467 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
2468 struct inode
*inode
= mapping
->host
;
2469 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2470 size_t count
= iov_iter_count(iter
);
2471 loff_t offset
= iocb
->ki_pos
;
2472 int rw
= iov_iter_rw(iter
);
2474 enum rw_hint hint
= iocb
->ki_hint
;
2475 int whint_mode
= F2FS_OPTION(sbi
).whint_mode
;
2477 err
= check_direct_IO(inode
, iter
, offset
);
2479 return err
< 0 ? err
: 0;
2481 if (f2fs_force_buffered_io(inode
, rw
))
2484 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
2486 if (rw
== WRITE
&& whint_mode
== WHINT_MODE_OFF
)
2487 iocb
->ki_hint
= WRITE_LIFE_NOT_SET
;
2489 if (!down_read_trylock(&F2FS_I(inode
)->i_gc_rwsem
[rw
])) {
2490 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
2491 iocb
->ki_hint
= hint
;
2495 down_read(&F2FS_I(inode
)->i_gc_rwsem
[rw
]);
2498 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
2499 up_read(&F2FS_I(inode
)->i_gc_rwsem
[rw
]);
2502 if (whint_mode
== WHINT_MODE_OFF
)
2503 iocb
->ki_hint
= hint
;
2505 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
2507 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2508 } else if (err
< 0) {
2509 f2fs_write_failed(mapping
, offset
+ count
);
2514 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
2519 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
2520 unsigned int length
)
2522 struct inode
*inode
= page
->mapping
->host
;
2523 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2525 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
2526 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
2529 if (PageDirty(page
)) {
2530 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
2531 dec_page_count(sbi
, F2FS_DIRTY_META
);
2532 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
2533 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
2535 inode_dec_dirty_pages(inode
);
2536 f2fs_remove_dirty_inode(inode
);
2540 /* This is atomic written page, keep Private */
2541 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2542 return f2fs_drop_inmem_page(inode
, page
);
2544 set_page_private(page
, 0);
2545 ClearPagePrivate(page
);
2548 int f2fs_release_page(struct page
*page
, gfp_t wait
)
2550 /* If this is dirty page, keep PagePrivate */
2551 if (PageDirty(page
))
2554 /* This is atomic written page, keep Private */
2555 if (IS_ATOMIC_WRITTEN_PAGE(page
))
2558 set_page_private(page
, 0);
2559 ClearPagePrivate(page
);
2563 static int f2fs_set_data_page_dirty(struct page
*page
)
2565 struct address_space
*mapping
= page
->mapping
;
2566 struct inode
*inode
= mapping
->host
;
2568 trace_f2fs_set_page_dirty(page
, DATA
);
2570 if (!PageUptodate(page
))
2571 SetPageUptodate(page
);
2573 /* don't remain PG_checked flag which was set during GC */
2574 if (is_cold_data(page
))
2575 clear_cold_data(page
);
2577 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
2578 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
2579 f2fs_register_inmem_page(inode
, page
);
2583 * Previously, this page has been registered, we just
2589 if (!PageDirty(page
)) {
2590 __set_page_dirty_nobuffers(page
);
2591 f2fs_update_dirty_page(inode
, page
);
2597 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
2599 struct inode
*inode
= mapping
->host
;
2601 if (f2fs_has_inline_data(inode
))
2604 /* make sure allocating whole blocks */
2605 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
2606 filemap_write_and_wait(mapping
);
2608 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
2611 #ifdef CONFIG_MIGRATION
2612 #include <linux/migrate.h>
2614 int f2fs_migrate_page(struct address_space
*mapping
,
2615 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
2617 int rc
, extra_count
;
2618 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
2619 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
2621 BUG_ON(PageWriteback(page
));
2623 /* migrating an atomic written page is safe with the inmem_lock hold */
2624 if (atomic_written
) {
2625 if (mode
!= MIGRATE_SYNC
)
2627 if (!mutex_trylock(&fi
->inmem_lock
))
2632 * A reference is expected if PagePrivate set when move mapping,
2633 * however F2FS breaks this for maintaining dirty page counts when
2634 * truncating pages. So here adjusting the 'extra_count' make it work.
2636 extra_count
= (atomic_written
? 1 : 0) - page_has_private(page
);
2637 rc
= migrate_page_move_mapping(mapping
, newpage
,
2638 page
, NULL
, mode
, extra_count
);
2639 if (rc
!= MIGRATEPAGE_SUCCESS
) {
2641 mutex_unlock(&fi
->inmem_lock
);
2645 if (atomic_written
) {
2646 struct inmem_pages
*cur
;
2647 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
2648 if (cur
->page
== page
) {
2649 cur
->page
= newpage
;
2652 mutex_unlock(&fi
->inmem_lock
);
2657 if (PagePrivate(page
))
2658 SetPagePrivate(newpage
);
2659 set_page_private(newpage
, page_private(page
));
2661 if (mode
!= MIGRATE_SYNC_NO_COPY
)
2662 migrate_page_copy(newpage
, page
);
2664 migrate_page_states(newpage
, page
);
2666 return MIGRATEPAGE_SUCCESS
;
2670 const struct address_space_operations f2fs_dblock_aops
= {
2671 .readpage
= f2fs_read_data_page
,
2672 .readpages
= f2fs_read_data_pages
,
2673 .writepage
= f2fs_write_data_page
,
2674 .writepages
= f2fs_write_data_pages
,
2675 .write_begin
= f2fs_write_begin
,
2676 .write_end
= f2fs_write_end
,
2677 .set_page_dirty
= f2fs_set_data_page_dirty
,
2678 .invalidatepage
= f2fs_invalidate_page
,
2679 .releasepage
= f2fs_release_page
,
2680 .direct_IO
= f2fs_direct_IO
,
2682 #ifdef CONFIG_MIGRATION
2683 .migratepage
= f2fs_migrate_page
,
2687 void f2fs_clear_radix_tree_dirty_tag(struct page
*page
)
2689 struct address_space
*mapping
= page_mapping(page
);
2690 unsigned long flags
;
2692 xa_lock_irqsave(&mapping
->i_pages
, flags
);
2693 radix_tree_tag_clear(&mapping
->i_pages
, page_index(page
),
2694 PAGECACHE_TAG_DIRTY
);
2695 xa_unlock_irqrestore(&mapping
->i_pages
, flags
);
2698 int __init
f2fs_init_post_read_processing(void)
2700 bio_post_read_ctx_cache
= KMEM_CACHE(bio_post_read_ctx
, 0);
2701 if (!bio_post_read_ctx_cache
)
2703 bio_post_read_ctx_pool
=
2704 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS
,
2705 bio_post_read_ctx_cache
);
2706 if (!bio_post_read_ctx_pool
)
2707 goto fail_free_cache
;
2711 kmem_cache_destroy(bio_post_read_ctx_cache
);
2716 void __exit
f2fs_destroy_post_read_processing(void)
2718 mempool_destroy(bio_post_read_ctx_pool
);
2719 kmem_cache_destroy(bio_post_read_ctx_cache
);