4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/pagevec.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
21 #include <linux/uio.h>
23 #include <linux/memcontrol.h>
24 #include <linux/cleancache.h>
30 #include <trace/events/f2fs.h>
32 static void f2fs_read_end_io(struct bio
*bio
)
37 if (f2fs_bio_encrypted(bio
)) {
39 fscrypt_release_ctx(bio
->bi_private
);
41 fscrypt_decrypt_bio_pages(bio
->bi_private
, bio
);
46 bio_for_each_segment_all(bvec
, bio
, i
) {
47 struct page
*page
= bvec
->bv_page
;
50 if (!PageUptodate(page
))
51 SetPageUptodate(page
);
53 ClearPageUptodate(page
);
61 static void f2fs_write_end_io(struct bio
*bio
)
63 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
67 bio_for_each_segment_all(bvec
, bio
, i
) {
68 struct page
*page
= bvec
->bv_page
;
70 fscrypt_pullback_bio_page(&page
, true);
72 if (unlikely(bio
->bi_error
)) {
73 set_bit(AS_EIO
, &page
->mapping
->flags
);
74 f2fs_stop_checkpoint(sbi
, true);
76 end_page_writeback(page
);
78 if (atomic_dec_and_test(&sbi
->nr_wb_bios
) &&
79 wq_has_sleeper(&sbi
->cp_wait
))
80 wake_up(&sbi
->cp_wait
);
86 * Low-level block read/write IO operations.
88 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
89 int npages
, bool is_read
)
93 bio
= f2fs_bio_alloc(npages
);
95 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
96 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
97 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
98 bio
->bi_private
= is_read
? NULL
: sbi
;
103 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
104 struct bio
*bio
, enum page_type type
)
106 if (!is_read_io(bio_op(bio
))) {
107 atomic_inc(&sbi
->nr_wb_bios
);
108 if (f2fs_sb_mounted_hmsmr(sbi
->sb
) &&
109 current
->plug
&& (type
== DATA
|| type
== NODE
))
110 blk_finish_plug(current
->plug
);
115 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
117 struct f2fs_io_info
*fio
= &io
->fio
;
122 if (is_read_io(fio
->op
))
123 trace_f2fs_submit_read_bio(io
->sbi
->sb
, fio
, io
->bio
);
125 trace_f2fs_submit_write_bio(io
->sbi
->sb
, fio
, io
->bio
);
127 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
129 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
133 static bool __has_merged_page(struct f2fs_bio_info
*io
, struct inode
*inode
,
134 struct page
*page
, nid_t ino
)
136 struct bio_vec
*bvec
;
143 if (!inode
&& !page
&& !ino
)
146 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
148 if (bvec
->bv_page
->mapping
)
149 target
= bvec
->bv_page
;
151 target
= fscrypt_control_page(bvec
->bv_page
);
153 if (inode
&& inode
== target
->mapping
->host
)
155 if (page
&& page
== target
)
157 if (ino
&& ino
== ino_of_node(target
))
164 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
165 struct page
*page
, nid_t ino
,
168 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
169 struct f2fs_bio_info
*io
= &sbi
->write_io
[btype
];
172 down_read(&io
->io_rwsem
);
173 ret
= __has_merged_page(io
, inode
, page
, ino
);
174 up_read(&io
->io_rwsem
);
178 static void __f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
,
179 struct inode
*inode
, struct page
*page
,
180 nid_t ino
, enum page_type type
, int rw
)
182 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
183 struct f2fs_bio_info
*io
;
185 io
= is_read_io(rw
) ? &sbi
->read_io
: &sbi
->write_io
[btype
];
187 down_write(&io
->io_rwsem
);
189 if (!__has_merged_page(io
, inode
, page
, ino
))
192 /* change META to META_FLUSH in the checkpoint procedure */
193 if (type
>= META_FLUSH
) {
194 io
->fio
.type
= META_FLUSH
;
195 io
->fio
.op
= REQ_OP_WRITE
;
196 if (test_opt(sbi
, NOBARRIER
))
197 io
->fio
.op_flags
= WRITE_FLUSH
| REQ_META
| REQ_PRIO
;
199 io
->fio
.op_flags
= WRITE_FLUSH_FUA
| REQ_META
|
202 __submit_merged_bio(io
);
204 up_write(&io
->io_rwsem
);
207 void f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
, enum page_type type
,
210 __f2fs_submit_merged_bio(sbi
, NULL
, NULL
, 0, type
, rw
);
213 void f2fs_submit_merged_bio_cond(struct f2fs_sb_info
*sbi
,
214 struct inode
*inode
, struct page
*page
,
215 nid_t ino
, enum page_type type
, int rw
)
217 if (has_merged_page(sbi
, inode
, page
, ino
, type
))
218 __f2fs_submit_merged_bio(sbi
, inode
, page
, ino
, type
, rw
);
221 void f2fs_flush_merged_bios(struct f2fs_sb_info
*sbi
)
223 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
224 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
225 f2fs_submit_merged_bio(sbi
, META
, WRITE
);
229 * Fill the locked page with data located in the block address.
230 * Return unlocked page.
232 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
235 struct page
*page
= fio
->encrypted_page
?
236 fio
->encrypted_page
: fio
->page
;
238 trace_f2fs_submit_page_bio(page
, fio
);
239 f2fs_trace_ios(fio
, 0);
241 /* Allocate a new bio */
242 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, 1, is_read_io(fio
->op
));
244 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
248 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
250 __submit_bio(fio
->sbi
, bio
, fio
->type
);
254 void f2fs_submit_page_mbio(struct f2fs_io_info
*fio
)
256 struct f2fs_sb_info
*sbi
= fio
->sbi
;
257 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
258 struct f2fs_bio_info
*io
;
259 bool is_read
= is_read_io(fio
->op
);
260 struct page
*bio_page
;
262 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
264 if (fio
->old_blkaddr
!= NEW_ADDR
)
265 verify_block_addr(sbi
, fio
->old_blkaddr
);
266 verify_block_addr(sbi
, fio
->new_blkaddr
);
268 down_write(&io
->io_rwsem
);
270 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
271 (io
->fio
.op
!= fio
->op
|| io
->fio
.op_flags
!= fio
->op_flags
)))
272 __submit_merged_bio(io
);
274 if (io
->bio
== NULL
) {
275 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
277 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
,
278 bio_blocks
, is_read
);
282 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
284 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) <
286 __submit_merged_bio(io
);
290 io
->last_block_in_bio
= fio
->new_blkaddr
;
291 f2fs_trace_ios(fio
, 0);
293 up_write(&io
->io_rwsem
);
294 trace_f2fs_submit_page_mbio(fio
->page
, fio
);
297 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
299 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
302 /* Get physical address of data block */
303 addr_array
= blkaddr_in_node(rn
);
304 addr_array
[dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
308 * Lock ordering for the change of data block address:
311 * update block addresses in the node page
313 void set_data_blkaddr(struct dnode_of_data
*dn
)
315 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
316 __set_data_blkaddr(dn
);
317 if (set_page_dirty(dn
->node_page
))
318 dn
->node_changed
= true;
321 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
323 dn
->data_blkaddr
= blkaddr
;
324 set_data_blkaddr(dn
);
325 f2fs_update_extent_cache(dn
);
328 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
329 int reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
331 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
336 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
338 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
341 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
342 dn
->ofs_in_node
, count
);
344 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true);
346 for (; count
> 0; dn
->ofs_in_node
++) {
348 datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
349 if (blkaddr
== NULL_ADDR
) {
350 dn
->data_blkaddr
= NEW_ADDR
;
351 __set_data_blkaddr(dn
);
356 if (set_page_dirty(dn
->node_page
))
357 dn
->node_changed
= true;
361 /* Should keep dn->ofs_in_node unchanged */
362 int reserve_new_block(struct dnode_of_data
*dn
)
364 unsigned int ofs_in_node
= dn
->ofs_in_node
;
367 ret
= reserve_new_blocks(dn
, 1);
368 dn
->ofs_in_node
= ofs_in_node
;
372 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
374 bool need_put
= dn
->inode_page
? false : true;
377 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
381 if (dn
->data_blkaddr
== NULL_ADDR
)
382 err
= reserve_new_block(dn
);
388 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
390 struct extent_info ei
;
391 struct inode
*inode
= dn
->inode
;
393 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
394 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
398 return f2fs_reserve_block(dn
, index
);
401 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
402 int op_flags
, bool for_write
)
404 struct address_space
*mapping
= inode
->i_mapping
;
405 struct dnode_of_data dn
;
407 struct extent_info ei
;
409 struct f2fs_io_info fio
= {
410 .sbi
= F2FS_I_SB(inode
),
413 .op_flags
= op_flags
,
414 .encrypted_page
= NULL
,
417 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
418 return read_mapping_page(mapping
, index
, NULL
);
420 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
422 return ERR_PTR(-ENOMEM
);
424 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
425 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
429 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
430 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
435 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
440 if (PageUptodate(page
)) {
446 * A new dentry page is allocated but not able to be written, since its
447 * new inode page couldn't be allocated due to -ENOSPC.
448 * In such the case, its blkaddr can be remained as NEW_ADDR.
449 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
451 if (dn
.data_blkaddr
== NEW_ADDR
) {
452 zero_user_segment(page
, 0, PAGE_SIZE
);
453 if (!PageUptodate(page
))
454 SetPageUptodate(page
);
459 fio
.new_blkaddr
= fio
.old_blkaddr
= dn
.data_blkaddr
;
461 err
= f2fs_submit_page_bio(&fio
);
467 f2fs_put_page(page
, 1);
471 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
473 struct address_space
*mapping
= inode
->i_mapping
;
476 page
= find_get_page(mapping
, index
);
477 if (page
&& PageUptodate(page
))
479 f2fs_put_page(page
, 0);
481 page
= get_read_data_page(inode
, index
, READ_SYNC
, false);
485 if (PageUptodate(page
))
488 wait_on_page_locked(page
);
489 if (unlikely(!PageUptodate(page
))) {
490 f2fs_put_page(page
, 0);
491 return ERR_PTR(-EIO
);
497 * If it tries to access a hole, return an error.
498 * Because, the callers, functions in dir.c and GC, should be able to know
499 * whether this page exists or not.
501 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
504 struct address_space
*mapping
= inode
->i_mapping
;
507 page
= get_read_data_page(inode
, index
, READ_SYNC
, for_write
);
511 /* wait for read completion */
513 if (unlikely(page
->mapping
!= mapping
)) {
514 f2fs_put_page(page
, 1);
517 if (unlikely(!PageUptodate(page
))) {
518 f2fs_put_page(page
, 1);
519 return ERR_PTR(-EIO
);
525 * Caller ensures that this data page is never allocated.
526 * A new zero-filled data page is allocated in the page cache.
528 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
530 * Note that, ipage is set only by make_empty_dir, and if any error occur,
531 * ipage should be released by this function.
533 struct page
*get_new_data_page(struct inode
*inode
,
534 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
536 struct address_space
*mapping
= inode
->i_mapping
;
538 struct dnode_of_data dn
;
541 page
= f2fs_grab_cache_page(mapping
, index
, true);
544 * before exiting, we should make sure ipage will be released
545 * if any error occur.
547 f2fs_put_page(ipage
, 1);
548 return ERR_PTR(-ENOMEM
);
551 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
552 err
= f2fs_reserve_block(&dn
, index
);
554 f2fs_put_page(page
, 1);
560 if (PageUptodate(page
))
563 if (dn
.data_blkaddr
== NEW_ADDR
) {
564 zero_user_segment(page
, 0, PAGE_SIZE
);
565 if (!PageUptodate(page
))
566 SetPageUptodate(page
);
568 f2fs_put_page(page
, 1);
570 /* if ipage exists, blkaddr should be NEW_ADDR */
571 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
572 page
= get_lock_data_page(inode
, index
, true);
577 if (new_i_size
&& i_size_read(inode
) <
578 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
579 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
583 static int __allocate_data_block(struct dnode_of_data
*dn
)
585 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
586 struct f2fs_summary sum
;
588 int seg
= CURSEG_WARM_DATA
;
592 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
595 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
596 if (dn
->data_blkaddr
== NEW_ADDR
)
599 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, &count
)))
603 get_node_info(sbi
, dn
->nid
, &ni
);
604 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
606 if (dn
->ofs_in_node
== 0 && dn
->inode_page
== dn
->node_page
)
607 seg
= CURSEG_DIRECT_IO
;
609 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
611 set_data_blkaddr(dn
);
614 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
616 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
617 f2fs_i_size_write(dn
->inode
,
618 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
622 ssize_t
f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
624 struct inode
*inode
= file_inode(iocb
->ki_filp
);
625 struct f2fs_map_blocks map
;
628 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
629 map
.m_len
= F2FS_BYTES_TO_BLK(iov_iter_count(from
));
630 map
.m_next_pgofs
= NULL
;
632 if (f2fs_encrypted_inode(inode
))
635 if (iocb
->ki_flags
& IOCB_DIRECT
) {
636 ret
= f2fs_convert_inline_inode(inode
);
639 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_DIO
);
641 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA
) {
642 ret
= f2fs_convert_inline_inode(inode
);
646 if (!f2fs_has_inline_data(inode
))
647 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
652 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
653 * f2fs_map_blocks structure.
654 * If original data blocks are allocated, then give them to blockdev.
656 * a. preallocate requested block addresses
657 * b. do not use extent cache for better performance
658 * c. give the block addresses to blockdev
660 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
661 int create
, int flag
)
663 unsigned int maxblocks
= map
->m_len
;
664 struct dnode_of_data dn
;
665 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
666 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE
;
667 pgoff_t pgofs
, end_offset
, end
;
668 int err
= 0, ofs
= 1;
669 unsigned int ofs_in_node
, last_ofs_in_node
;
671 struct extent_info ei
;
672 bool allocated
= false;
678 /* it only supports block size == page size */
679 pgofs
= (pgoff_t
)map
->m_lblk
;
680 end
= pgofs
+ maxblocks
;
682 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
683 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
684 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
685 map
->m_flags
= F2FS_MAP_MAPPED
;
693 /* When reading holes, we need its node page */
694 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
695 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
697 if (flag
== F2FS_GET_BLOCK_BMAP
)
699 if (err
== -ENOENT
) {
701 if (map
->m_next_pgofs
)
703 get_next_page_offset(&dn
, pgofs
);
709 ofs_in_node
= dn
.ofs_in_node
;
710 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
713 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
715 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
717 if (unlikely(f2fs_cp_error(sbi
))) {
721 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
722 if (blkaddr
== NULL_ADDR
) {
724 last_ofs_in_node
= dn
.ofs_in_node
;
727 err
= __allocate_data_block(&dn
);
729 set_inode_flag(inode
, FI_APPEND_WRITE
);
735 map
->m_flags
= F2FS_MAP_NEW
;
736 blkaddr
= dn
.data_blkaddr
;
738 if (flag
== F2FS_GET_BLOCK_BMAP
) {
742 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
743 blkaddr
== NULL_ADDR
) {
744 if (map
->m_next_pgofs
)
745 *map
->m_next_pgofs
= pgofs
+ 1;
747 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
753 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
756 if (map
->m_len
== 0) {
757 /* preallocated unwritten block should be mapped for fiemap. */
758 if (blkaddr
== NEW_ADDR
)
759 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
760 map
->m_flags
|= F2FS_MAP_MAPPED
;
762 map
->m_pblk
= blkaddr
;
764 } else if ((map
->m_pblk
!= NEW_ADDR
&&
765 blkaddr
== (map
->m_pblk
+ ofs
)) ||
766 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
767 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
778 /* preallocate blocks in batch for one dnode page */
779 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
780 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
782 dn
.ofs_in_node
= ofs_in_node
;
783 err
= reserve_new_blocks(&dn
, prealloc
);
787 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
788 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
792 dn
.ofs_in_node
= end_offset
;
797 else if (dn
.ofs_in_node
< end_offset
)
804 f2fs_balance_fs(sbi
, allocated
);
814 f2fs_balance_fs(sbi
, allocated
);
817 trace_f2fs_map_blocks(inode
, map
, err
);
821 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
822 struct buffer_head
*bh
, int create
, int flag
,
825 struct f2fs_map_blocks map
;
829 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
830 map
.m_next_pgofs
= next_pgofs
;
832 ret
= f2fs_map_blocks(inode
, &map
, create
, flag
);
834 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
835 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
836 bh
->b_size
= map
.m_len
<< inode
->i_blkbits
;
841 static int get_data_block(struct inode
*inode
, sector_t iblock
,
842 struct buffer_head
*bh_result
, int create
, int flag
,
845 return __get_data_block(inode
, iblock
, bh_result
, create
,
849 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
850 struct buffer_head
*bh_result
, int create
)
852 return __get_data_block(inode
, iblock
, bh_result
, create
,
853 F2FS_GET_BLOCK_DIO
, NULL
);
856 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
857 struct buffer_head
*bh_result
, int create
)
859 /* Block number less than F2FS MAX BLOCKS */
860 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
863 return __get_data_block(inode
, iblock
, bh_result
, create
,
864 F2FS_GET_BLOCK_BMAP
, NULL
);
867 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
869 return (offset
>> inode
->i_blkbits
);
872 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
874 return (blk
<< inode
->i_blkbits
);
877 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
880 struct buffer_head map_bh
;
881 sector_t start_blk
, last_blk
;
884 u64 logical
= 0, phys
= 0, size
= 0;
888 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
892 if (f2fs_has_inline_data(inode
)) {
893 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
900 isize
= i_size_read(inode
);
904 if (start
+ len
> isize
)
907 if (logical_to_blk(inode
, len
) == 0)
908 len
= blk_to_logical(inode
, 1);
910 start_blk
= logical_to_blk(inode
, start
);
911 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
914 memset(&map_bh
, 0, sizeof(struct buffer_head
));
917 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
918 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
923 if (!buffer_mapped(&map_bh
)) {
924 start_blk
= next_pgofs
;
925 /* Go through holes util pass the EOF */
926 if (blk_to_logical(inode
, start_blk
) < isize
)
928 /* Found a hole beyond isize means no more extents.
929 * Note that the premise is that filesystems don't
930 * punch holes beyond isize and keep size unchanged.
932 flags
|= FIEMAP_EXTENT_LAST
;
936 if (f2fs_encrypted_inode(inode
))
937 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
939 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
943 if (start_blk
> last_blk
|| ret
)
946 logical
= blk_to_logical(inode
, start_blk
);
947 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
948 size
= map_bh
.b_size
;
950 if (buffer_unwritten(&map_bh
))
951 flags
= FIEMAP_EXTENT_UNWRITTEN
;
953 start_blk
+= logical_to_blk(inode
, size
);
957 if (fatal_signal_pending(current
))
969 struct bio
*f2fs_grab_bio(struct inode
*inode
, block_t blkaddr
,
972 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
973 struct fscrypt_ctx
*ctx
= NULL
;
974 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
977 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
978 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
980 return ERR_CAST(ctx
);
982 /* wait the page to be moved by cleaning */
983 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
986 bio
= bio_alloc(GFP_KERNEL
, min_t(int, nr_pages
, BIO_MAX_PAGES
));
989 fscrypt_release_ctx(ctx
);
990 return ERR_PTR(-ENOMEM
);
993 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blkaddr
);
994 bio
->bi_end_io
= f2fs_read_end_io
;
995 bio
->bi_private
= ctx
;
1001 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1002 * Major change was from block_size == page_size in f2fs by default.
1004 static int f2fs_mpage_readpages(struct address_space
*mapping
,
1005 struct list_head
*pages
, struct page
*page
,
1008 struct bio
*bio
= NULL
;
1010 sector_t last_block_in_bio
= 0;
1011 struct inode
*inode
= mapping
->host
;
1012 const unsigned blkbits
= inode
->i_blkbits
;
1013 const unsigned blocksize
= 1 << blkbits
;
1014 sector_t block_in_file
;
1015 sector_t last_block
;
1016 sector_t last_block_in_file
;
1018 struct f2fs_map_blocks map
;
1024 map
.m_next_pgofs
= NULL
;
1026 for (page_idx
= 0; nr_pages
; page_idx
++, nr_pages
--) {
1028 prefetchw(&page
->flags
);
1030 page
= list_entry(pages
->prev
, struct page
, lru
);
1031 list_del(&page
->lru
);
1032 if (add_to_page_cache_lru(page
, mapping
,
1034 readahead_gfp_mask(mapping
)))
1038 block_in_file
= (sector_t
)page
->index
;
1039 last_block
= block_in_file
+ nr_pages
;
1040 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
1042 if (last_block
> last_block_in_file
)
1043 last_block
= last_block_in_file
;
1046 * Map blocks using the previous result first.
1048 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
1049 block_in_file
> map
.m_lblk
&&
1050 block_in_file
< (map
.m_lblk
+ map
.m_len
))
1054 * Then do more f2fs_map_blocks() calls until we are
1055 * done with this page.
1059 if (block_in_file
< last_block
) {
1060 map
.m_lblk
= block_in_file
;
1061 map
.m_len
= last_block
- block_in_file
;
1063 if (f2fs_map_blocks(inode
, &map
, 0,
1064 F2FS_GET_BLOCK_READ
))
1065 goto set_error_page
;
1068 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
1069 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
1070 SetPageMappedToDisk(page
);
1072 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
1073 SetPageUptodate(page
);
1077 zero_user_segment(page
, 0, PAGE_SIZE
);
1078 if (!PageUptodate(page
))
1079 SetPageUptodate(page
);
1085 * This page will go to BIO. Do we need to send this
1088 if (bio
&& (last_block_in_bio
!= block_nr
- 1)) {
1090 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1094 bio
= f2fs_grab_bio(inode
, block_nr
, nr_pages
);
1097 goto set_error_page
;
1099 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
1102 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1103 goto submit_and_realloc
;
1105 last_block_in_bio
= block_nr
;
1109 zero_user_segment(page
, 0, PAGE_SIZE
);
1114 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1122 BUG_ON(pages
&& !list_empty(pages
));
1124 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1128 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1130 struct inode
*inode
= page
->mapping
->host
;
1133 trace_f2fs_readpage(page
, DATA
);
1135 /* If the file has inline data, try to read it directly */
1136 if (f2fs_has_inline_data(inode
))
1137 ret
= f2fs_read_inline_data(inode
, page
);
1139 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1143 static int f2fs_read_data_pages(struct file
*file
,
1144 struct address_space
*mapping
,
1145 struct list_head
*pages
, unsigned nr_pages
)
1147 struct inode
*inode
= file
->f_mapping
->host
;
1148 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
1150 trace_f2fs_readpages(inode
, page
, nr_pages
);
1152 /* If the file has inline data, skip readpages */
1153 if (f2fs_has_inline_data(inode
))
1156 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1159 int do_write_data_page(struct f2fs_io_info
*fio
)
1161 struct page
*page
= fio
->page
;
1162 struct inode
*inode
= page
->mapping
->host
;
1163 struct dnode_of_data dn
;
1166 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1167 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1171 fio
->old_blkaddr
= dn
.data_blkaddr
;
1173 /* This page is already truncated */
1174 if (fio
->old_blkaddr
== NULL_ADDR
) {
1175 ClearPageUptodate(page
);
1179 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1180 gfp_t gfp_flags
= GFP_NOFS
;
1182 /* wait for GCed encrypted page writeback */
1183 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode
),
1186 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1188 if (IS_ERR(fio
->encrypted_page
)) {
1189 err
= PTR_ERR(fio
->encrypted_page
);
1190 if (err
== -ENOMEM
) {
1191 /* flush pending ios and wait for a while */
1192 f2fs_flush_merged_bios(F2FS_I_SB(inode
));
1193 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1194 gfp_flags
|= __GFP_NOFAIL
;
1202 set_page_writeback(page
);
1205 * If current allocation needs SSR,
1206 * it had better in-place writes for updated data.
1208 if (unlikely(fio
->old_blkaddr
!= NEW_ADDR
&&
1209 !is_cold_data(page
) &&
1210 !IS_ATOMIC_WRITTEN_PAGE(page
) &&
1211 need_inplace_update(inode
))) {
1212 rewrite_data_page(fio
);
1213 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1214 trace_f2fs_do_write_data_page(page
, IPU
);
1216 write_data_page(&dn
, fio
);
1217 trace_f2fs_do_write_data_page(page
, OPU
);
1218 set_inode_flag(inode
, FI_APPEND_WRITE
);
1219 if (page
->index
== 0)
1220 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
1223 f2fs_put_dnode(&dn
);
1227 static int f2fs_write_data_page(struct page
*page
,
1228 struct writeback_control
*wbc
)
1230 struct inode
*inode
= page
->mapping
->host
;
1231 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1232 loff_t i_size
= i_size_read(inode
);
1233 const pgoff_t end_index
= ((unsigned long long) i_size
)
1235 loff_t psize
= (page
->index
+ 1) << PAGE_SHIFT
;
1236 unsigned offset
= 0;
1237 bool need_balance_fs
= false;
1239 struct f2fs_io_info fio
= {
1243 .op_flags
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: 0,
1245 .encrypted_page
= NULL
,
1248 trace_f2fs_writepage(page
, DATA
);
1250 if (page
->index
< end_index
)
1254 * If the offset is out-of-range of file size,
1255 * this page does not have to be written to disk.
1257 offset
= i_size
& (PAGE_SIZE
- 1);
1258 if ((page
->index
>= end_index
+ 1) || !offset
)
1261 zero_user_segment(page
, offset
, PAGE_SIZE
);
1263 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1265 if (f2fs_is_drop_cache(inode
))
1267 /* we should not write 0'th page having journal header */
1268 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
1269 (!wbc
->for_reclaim
&&
1270 available_free_memory(sbi
, BASE_CHECK
))))
1273 /* we should bypass data pages to proceed the kworkder jobs */
1274 if (unlikely(f2fs_cp_error(sbi
))) {
1275 mapping_set_error(page
->mapping
, -EIO
);
1279 /* Dentry blocks are controlled by checkpoint */
1280 if (S_ISDIR(inode
->i_mode
)) {
1281 err
= do_write_data_page(&fio
);
1285 if (!wbc
->for_reclaim
)
1286 need_balance_fs
= true;
1287 else if (has_not_enough_free_secs(sbi
, 0))
1292 if (f2fs_has_inline_data(inode
))
1293 err
= f2fs_write_inline_data(inode
, page
);
1295 err
= do_write_data_page(&fio
);
1296 if (F2FS_I(inode
)->last_disk_size
< psize
)
1297 F2FS_I(inode
)->last_disk_size
= psize
;
1298 f2fs_unlock_op(sbi
);
1300 if (err
&& err
!= -ENOENT
)
1303 clear_cold_data(page
);
1305 inode_dec_dirty_pages(inode
);
1307 ClearPageUptodate(page
);
1309 if (wbc
->for_reclaim
) {
1310 f2fs_submit_merged_bio_cond(sbi
, NULL
, page
, 0, DATA
, WRITE
);
1311 remove_dirty_inode(inode
);
1315 f2fs_balance_fs(sbi
, need_balance_fs
);
1317 if (unlikely(f2fs_cp_error(sbi
)))
1318 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1323 redirty_page_for_writepage(wbc
, page
);
1329 * This function was copied from write_cche_pages from mm/page-writeback.c.
1330 * The major change is making write step of cold data page separately from
1331 * warm/hot data page.
1333 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1334 struct writeback_control
*wbc
)
1338 struct pagevec pvec
;
1340 pgoff_t
uninitialized_var(writeback_index
);
1342 pgoff_t end
; /* Inclusive */
1345 int range_whole
= 0;
1348 pagevec_init(&pvec
, 0);
1350 if (wbc
->range_cyclic
) {
1351 writeback_index
= mapping
->writeback_index
; /* prev offset */
1352 index
= writeback_index
;
1359 index
= wbc
->range_start
>> PAGE_SHIFT
;
1360 end
= wbc
->range_end
>> PAGE_SHIFT
;
1361 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1363 cycled
= 1; /* ignore range_cyclic tests */
1365 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1366 tag
= PAGECACHE_TAG_TOWRITE
;
1368 tag
= PAGECACHE_TAG_DIRTY
;
1370 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1371 tag_pages_for_writeback(mapping
, index
, end
);
1373 while (!done
&& (index
<= end
)) {
1376 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1377 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1381 for (i
= 0; i
< nr_pages
; i
++) {
1382 struct page
*page
= pvec
.pages
[i
];
1384 if (page
->index
> end
) {
1389 done_index
= page
->index
;
1393 if (unlikely(page
->mapping
!= mapping
)) {
1399 if (!PageDirty(page
)) {
1400 /* someone wrote it for us */
1401 goto continue_unlock
;
1404 if (PageWriteback(page
)) {
1405 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1406 f2fs_wait_on_page_writeback(page
,
1409 goto continue_unlock
;
1412 BUG_ON(PageWriteback(page
));
1413 if (!clear_page_dirty_for_io(page
))
1414 goto continue_unlock
;
1416 ret
= mapping
->a_ops
->writepage(page
, wbc
);
1417 if (unlikely(ret
)) {
1418 done_index
= page
->index
+ 1;
1423 if (--wbc
->nr_to_write
<= 0 &&
1424 wbc
->sync_mode
== WB_SYNC_NONE
) {
1429 pagevec_release(&pvec
);
1433 if (!cycled
&& !done
) {
1436 end
= writeback_index
- 1;
1439 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1440 mapping
->writeback_index
= done_index
;
1445 static int f2fs_write_data_pages(struct address_space
*mapping
,
1446 struct writeback_control
*wbc
)
1448 struct inode
*inode
= mapping
->host
;
1449 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1450 struct blk_plug plug
;
1453 /* deal with chardevs and other special file */
1454 if (!mapping
->a_ops
->writepage
)
1457 /* skip writing if there is no dirty page in this inode */
1458 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1461 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
1462 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
1463 available_free_memory(sbi
, DIRTY_DENTS
))
1466 /* skip writing during file defragment */
1467 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
1470 /* during POR, we don't need to trigger writepage at all. */
1471 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1474 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1476 blk_start_plug(&plug
);
1477 ret
= f2fs_write_cache_pages(mapping
, wbc
);
1478 blk_finish_plug(&plug
);
1480 * if some pages were truncated, we cannot guarantee its mapping->host
1481 * to detect pending bios.
1483 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1485 remove_dirty_inode(inode
);
1489 wbc
->pages_skipped
+= get_dirty_pages(inode
);
1490 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1494 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
1496 struct inode
*inode
= mapping
->host
;
1497 loff_t i_size
= i_size_read(inode
);
1500 truncate_pagecache(inode
, i_size
);
1501 truncate_blocks(inode
, i_size
, true);
1505 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
1506 struct page
*page
, loff_t pos
, unsigned len
,
1507 block_t
*blk_addr
, bool *node_changed
)
1509 struct inode
*inode
= page
->mapping
->host
;
1510 pgoff_t index
= page
->index
;
1511 struct dnode_of_data dn
;
1513 bool locked
= false;
1514 struct extent_info ei
;
1518 * we already allocated all the blocks, so we don't need to get
1519 * the block addresses when there is no need to fill the page.
1521 if (!f2fs_has_inline_data(inode
) && !f2fs_encrypted_inode(inode
) &&
1525 if (f2fs_has_inline_data(inode
) ||
1526 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1531 /* check inline_data */
1532 ipage
= get_node_page(sbi
, inode
->i_ino
);
1533 if (IS_ERR(ipage
)) {
1534 err
= PTR_ERR(ipage
);
1538 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
1540 if (f2fs_has_inline_data(inode
)) {
1541 if (pos
+ len
<= MAX_INLINE_DATA
) {
1542 read_inline_data(page
, ipage
);
1543 set_inode_flag(inode
, FI_DATA_EXIST
);
1545 set_inline_node(ipage
);
1547 err
= f2fs_convert_inline_page(&dn
, page
);
1550 if (dn
.data_blkaddr
== NULL_ADDR
)
1551 err
= f2fs_get_block(&dn
, index
);
1553 } else if (locked
) {
1554 err
= f2fs_get_block(&dn
, index
);
1556 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1557 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1560 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1561 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
1562 f2fs_put_dnode(&dn
);
1570 /* convert_inline_page can make node_changed */
1571 *blk_addr
= dn
.data_blkaddr
;
1572 *node_changed
= dn
.node_changed
;
1574 f2fs_put_dnode(&dn
);
1577 f2fs_unlock_op(sbi
);
1581 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
1582 loff_t pos
, unsigned len
, unsigned flags
,
1583 struct page
**pagep
, void **fsdata
)
1585 struct inode
*inode
= mapping
->host
;
1586 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1587 struct page
*page
= NULL
;
1588 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
1589 bool need_balance
= false;
1590 block_t blkaddr
= NULL_ADDR
;
1593 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
1596 * We should check this at this moment to avoid deadlock on inode page
1597 * and #0 page. The locking rule for inline_data conversion should be:
1598 * lock_page(page #0) -> lock_page(inode_page)
1601 err
= f2fs_convert_inline_inode(inode
);
1606 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1614 err
= prepare_write_begin(sbi
, page
, pos
, len
,
1615 &blkaddr
, &need_balance
);
1619 if (need_balance
&& has_not_enough_free_secs(sbi
, 0)) {
1621 f2fs_balance_fs(sbi
, true);
1623 if (page
->mapping
!= mapping
) {
1624 /* The page got truncated from under us */
1625 f2fs_put_page(page
, 1);
1630 f2fs_wait_on_page_writeback(page
, DATA
, false);
1632 /* wait for GCed encrypted page writeback */
1633 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1634 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
1636 if (len
== PAGE_SIZE
)
1638 if (PageUptodate(page
))
1641 if ((pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1642 unsigned start
= pos
& (PAGE_SIZE
- 1);
1643 unsigned end
= start
+ len
;
1645 /* Reading beyond i_size is simple: memset to zero */
1646 zero_user_segments(page
, 0, start
, end
, PAGE_SIZE
);
1650 if (blkaddr
== NEW_ADDR
) {
1651 zero_user_segment(page
, 0, PAGE_SIZE
);
1655 bio
= f2fs_grab_bio(inode
, blkaddr
, 1);
1660 bio_set_op_attrs(bio
, REQ_OP_READ
, READ_SYNC
);
1661 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
1667 __submit_bio(sbi
, bio
, DATA
);
1670 if (unlikely(page
->mapping
!= mapping
)) {
1671 f2fs_put_page(page
, 1);
1674 if (unlikely(!PageUptodate(page
))) {
1680 if (!PageUptodate(page
))
1681 SetPageUptodate(page
);
1683 clear_cold_data(page
);
1687 f2fs_put_page(page
, 1);
1688 f2fs_write_failed(mapping
, pos
+ len
);
1692 static int f2fs_write_end(struct file
*file
,
1693 struct address_space
*mapping
,
1694 loff_t pos
, unsigned len
, unsigned copied
,
1695 struct page
*page
, void *fsdata
)
1697 struct inode
*inode
= page
->mapping
->host
;
1699 trace_f2fs_write_end(inode
, pos
, len
, copied
);
1701 set_page_dirty(page
);
1702 f2fs_put_page(page
, 1);
1704 if (pos
+ copied
> i_size_read(inode
))
1705 f2fs_i_size_write(inode
, pos
+ copied
);
1707 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1711 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
1714 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1716 if (offset
& blocksize_mask
)
1719 if (iov_iter_alignment(iter
) & blocksize_mask
)
1725 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
1727 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1728 struct inode
*inode
= mapping
->host
;
1729 size_t count
= iov_iter_count(iter
);
1730 loff_t offset
= iocb
->ki_pos
;
1731 int rw
= iov_iter_rw(iter
);
1734 err
= check_direct_IO(inode
, iter
, offset
);
1738 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1740 if (test_opt(F2FS_I_SB(inode
), LFS
))
1743 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
1745 down_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1746 err
= blockdev_direct_IO(iocb
, inode
, iter
, get_data_block_dio
);
1747 up_read(&F2FS_I(inode
)->dio_rwsem
[rw
]);
1751 set_inode_flag(inode
, FI_UPDATE_WRITE
);
1753 f2fs_write_failed(mapping
, offset
+ count
);
1756 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
1761 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
1762 unsigned int length
)
1764 struct inode
*inode
= page
->mapping
->host
;
1765 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1767 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
1768 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
1771 if (PageDirty(page
)) {
1772 if (inode
->i_ino
== F2FS_META_INO(sbi
))
1773 dec_page_count(sbi
, F2FS_DIRTY_META
);
1774 else if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
1775 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1777 inode_dec_dirty_pages(inode
);
1780 /* This is atomic written page, keep Private */
1781 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1784 set_page_private(page
, 0);
1785 ClearPagePrivate(page
);
1788 int f2fs_release_page(struct page
*page
, gfp_t wait
)
1790 /* If this is dirty page, keep PagePrivate */
1791 if (PageDirty(page
))
1794 /* This is atomic written page, keep Private */
1795 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1798 set_page_private(page
, 0);
1799 ClearPagePrivate(page
);
1804 * This was copied from __set_page_dirty_buffers which gives higher performance
1805 * in very high speed storages. (e.g., pmem)
1807 void f2fs_set_page_dirty_nobuffers(struct page
*page
)
1809 struct address_space
*mapping
= page
->mapping
;
1810 unsigned long flags
;
1812 if (unlikely(!mapping
))
1815 spin_lock(&mapping
->private_lock
);
1816 lock_page_memcg(page
);
1818 spin_unlock(&mapping
->private_lock
);
1820 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
1821 WARN_ON_ONCE(!PageUptodate(page
));
1822 account_page_dirtied(page
, mapping
);
1823 radix_tree_tag_set(&mapping
->page_tree
,
1824 page_index(page
), PAGECACHE_TAG_DIRTY
);
1825 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
1826 unlock_page_memcg(page
);
1828 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1832 static int f2fs_set_data_page_dirty(struct page
*page
)
1834 struct address_space
*mapping
= page
->mapping
;
1835 struct inode
*inode
= mapping
->host
;
1837 trace_f2fs_set_page_dirty(page
, DATA
);
1839 if (!PageUptodate(page
))
1840 SetPageUptodate(page
);
1842 if (f2fs_is_atomic_file(inode
)) {
1843 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
1844 register_inmem_page(inode
, page
);
1848 * Previously, this page has been registered, we just
1854 if (!PageDirty(page
)) {
1855 f2fs_set_page_dirty_nobuffers(page
);
1856 update_dirty_page(inode
, page
);
1862 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1864 struct inode
*inode
= mapping
->host
;
1866 if (f2fs_has_inline_data(inode
))
1869 /* make sure allocating whole blocks */
1870 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
1871 filemap_write_and_wait(mapping
);
1873 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
1876 const struct address_space_operations f2fs_dblock_aops
= {
1877 .readpage
= f2fs_read_data_page
,
1878 .readpages
= f2fs_read_data_pages
,
1879 .writepage
= f2fs_write_data_page
,
1880 .writepages
= f2fs_write_data_pages
,
1881 .write_begin
= f2fs_write_begin
,
1882 .write_end
= f2fs_write_end
,
1883 .set_page_dirty
= f2fs_set_data_page_dirty
,
1884 .invalidatepage
= f2fs_invalidate_page
,
1885 .releasepage
= f2fs_release_page
,
1886 .direct_IO
= f2fs_direct_IO
,