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>
28 #include <trace/events/f2fs.h>
30 static void f2fs_read_end_io(struct bio
*bio
)
35 if (f2fs_bio_encrypted(bio
)) {
37 fscrypt_release_ctx(bio
->bi_private
);
39 fscrypt_decrypt_bio_pages(bio
->bi_private
, bio
);
44 bio_for_each_segment_all(bvec
, bio
, i
) {
45 struct page
*page
= bvec
->bv_page
;
48 SetPageUptodate(page
);
50 ClearPageUptodate(page
);
58 static void f2fs_write_end_io(struct bio
*bio
)
60 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
64 bio_for_each_segment_all(bvec
, bio
, i
) {
65 struct page
*page
= bvec
->bv_page
;
67 fscrypt_pullback_bio_page(&page
, true);
69 if (unlikely(bio
->bi_error
)) {
70 set_bit(AS_EIO
, &page
->mapping
->flags
);
71 f2fs_stop_checkpoint(sbi
);
73 end_page_writeback(page
);
74 dec_page_count(sbi
, F2FS_WRITEBACK
);
77 if (!get_pages(sbi
, F2FS_WRITEBACK
) && wq_has_sleeper(&sbi
->cp_wait
))
78 wake_up(&sbi
->cp_wait
);
84 * Low-level block read/write IO operations.
86 static struct bio
*__bio_alloc(struct f2fs_sb_info
*sbi
, block_t blk_addr
,
87 int npages
, bool is_read
)
91 bio
= f2fs_bio_alloc(npages
);
93 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
94 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
95 bio
->bi_end_io
= is_read
? f2fs_read_end_io
: f2fs_write_end_io
;
96 bio
->bi_private
= is_read
? NULL
: sbi
;
101 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
103 struct f2fs_io_info
*fio
= &io
->fio
;
108 if (is_read_io(fio
->rw
))
109 trace_f2fs_submit_read_bio(io
->sbi
->sb
, fio
, io
->bio
);
111 trace_f2fs_submit_write_bio(io
->sbi
->sb
, fio
, io
->bio
);
113 submit_bio(fio
->rw
, io
->bio
);
117 static bool __has_merged_page(struct f2fs_bio_info
*io
, struct inode
*inode
,
118 struct page
*page
, nid_t ino
)
120 struct bio_vec
*bvec
;
127 if (!inode
&& !page
&& !ino
)
130 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
132 if (bvec
->bv_page
->mapping
)
133 target
= bvec
->bv_page
;
135 target
= fscrypt_control_page(bvec
->bv_page
);
137 if (inode
&& inode
== target
->mapping
->host
)
139 if (page
&& page
== target
)
141 if (ino
&& ino
== ino_of_node(target
))
148 static bool has_merged_page(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
149 struct page
*page
, nid_t ino
,
152 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
153 struct f2fs_bio_info
*io
= &sbi
->write_io
[btype
];
156 down_read(&io
->io_rwsem
);
157 ret
= __has_merged_page(io
, inode
, page
, ino
);
158 up_read(&io
->io_rwsem
);
162 static void __f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
,
163 struct inode
*inode
, struct page
*page
,
164 nid_t ino
, enum page_type type
, int rw
)
166 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
167 struct f2fs_bio_info
*io
;
169 io
= is_read_io(rw
) ? &sbi
->read_io
: &sbi
->write_io
[btype
];
171 down_write(&io
->io_rwsem
);
173 if (!__has_merged_page(io
, inode
, page
, ino
))
176 /* change META to META_FLUSH in the checkpoint procedure */
177 if (type
>= META_FLUSH
) {
178 io
->fio
.type
= META_FLUSH
;
179 if (test_opt(sbi
, NOBARRIER
))
180 io
->fio
.rw
= WRITE_FLUSH
| REQ_META
| REQ_PRIO
;
182 io
->fio
.rw
= WRITE_FLUSH_FUA
| REQ_META
| REQ_PRIO
;
184 __submit_merged_bio(io
);
186 up_write(&io
->io_rwsem
);
189 void f2fs_submit_merged_bio(struct f2fs_sb_info
*sbi
, enum page_type type
,
192 __f2fs_submit_merged_bio(sbi
, NULL
, NULL
, 0, type
, rw
);
195 void f2fs_submit_merged_bio_cond(struct f2fs_sb_info
*sbi
,
196 struct inode
*inode
, struct page
*page
,
197 nid_t ino
, enum page_type type
, int rw
)
199 if (has_merged_page(sbi
, inode
, page
, ino
, type
))
200 __f2fs_submit_merged_bio(sbi
, inode
, page
, ino
, type
, rw
);
203 void f2fs_flush_merged_bios(struct f2fs_sb_info
*sbi
)
205 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
206 f2fs_submit_merged_bio(sbi
, NODE
, WRITE
);
207 f2fs_submit_merged_bio(sbi
, META
, WRITE
);
211 * Fill the locked page with data located in the block address.
212 * Return unlocked page.
214 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
217 struct page
*page
= fio
->encrypted_page
?
218 fio
->encrypted_page
: fio
->page
;
220 trace_f2fs_submit_page_bio(page
, fio
);
221 f2fs_trace_ios(fio
, 0);
223 /* Allocate a new bio */
224 bio
= __bio_alloc(fio
->sbi
, fio
->new_blkaddr
, 1, is_read_io(fio
->rw
));
226 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
231 submit_bio(fio
->rw
, bio
);
235 void f2fs_submit_page_mbio(struct f2fs_io_info
*fio
)
237 struct f2fs_sb_info
*sbi
= fio
->sbi
;
238 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
239 struct f2fs_bio_info
*io
;
240 bool is_read
= is_read_io(fio
->rw
);
241 struct page
*bio_page
;
243 io
= is_read
? &sbi
->read_io
: &sbi
->write_io
[btype
];
245 if (fio
->old_blkaddr
!= NEW_ADDR
)
246 verify_block_addr(sbi
, fio
->old_blkaddr
);
247 verify_block_addr(sbi
, fio
->new_blkaddr
);
249 down_write(&io
->io_rwsem
);
252 inc_page_count(sbi
, F2FS_WRITEBACK
);
254 if (io
->bio
&& (io
->last_block_in_bio
!= fio
->new_blkaddr
- 1 ||
255 io
->fio
.rw
!= fio
->rw
))
256 __submit_merged_bio(io
);
258 if (io
->bio
== NULL
) {
259 int bio_blocks
= MAX_BIO_BLOCKS(sbi
);
261 io
->bio
= __bio_alloc(sbi
, fio
->new_blkaddr
,
262 bio_blocks
, is_read
);
266 bio_page
= fio
->encrypted_page
? fio
->encrypted_page
: fio
->page
;
268 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) <
270 __submit_merged_bio(io
);
274 io
->last_block_in_bio
= fio
->new_blkaddr
;
275 f2fs_trace_ios(fio
, 0);
277 up_write(&io
->io_rwsem
);
278 trace_f2fs_submit_page_mbio(fio
->page
, fio
);
282 * Lock ordering for the change of data block address:
285 * update block addresses in the node page
287 void set_data_blkaddr(struct dnode_of_data
*dn
)
289 struct f2fs_node
*rn
;
291 struct page
*node_page
= dn
->node_page
;
292 unsigned int ofs_in_node
= dn
->ofs_in_node
;
294 f2fs_wait_on_page_writeback(node_page
, NODE
, true);
296 rn
= F2FS_NODE(node_page
);
298 /* Get physical address of data block */
299 addr_array
= blkaddr_in_node(rn
);
300 addr_array
[ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
301 if (set_page_dirty(node_page
))
302 dn
->node_changed
= true;
305 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
307 dn
->data_blkaddr
= blkaddr
;
308 set_data_blkaddr(dn
);
309 f2fs_update_extent_cache(dn
);
312 int reserve_new_block(struct dnode_of_data
*dn
)
314 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
316 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
318 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
321 trace_f2fs_reserve_new_block(dn
->inode
, dn
->nid
, dn
->ofs_in_node
);
323 dn
->data_blkaddr
= NEW_ADDR
;
324 set_data_blkaddr(dn
);
325 mark_inode_dirty(dn
->inode
);
330 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
332 bool need_put
= dn
->inode_page
? false : true;
335 err
= get_dnode_of_data(dn
, index
, ALLOC_NODE
);
339 if (dn
->data_blkaddr
== NULL_ADDR
)
340 err
= reserve_new_block(dn
);
346 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
348 struct extent_info ei
;
349 struct inode
*inode
= dn
->inode
;
351 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
352 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
356 return f2fs_reserve_block(dn
, index
);
359 struct page
*get_read_data_page(struct inode
*inode
, pgoff_t index
,
360 int rw
, bool for_write
)
362 struct address_space
*mapping
= inode
->i_mapping
;
363 struct dnode_of_data dn
;
365 struct extent_info ei
;
367 struct f2fs_io_info fio
= {
368 .sbi
= F2FS_I_SB(inode
),
371 .encrypted_page
= NULL
,
374 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
375 return read_mapping_page(mapping
, index
, NULL
);
377 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
379 return ERR_PTR(-ENOMEM
);
381 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
382 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
386 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
387 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
392 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
397 if (PageUptodate(page
)) {
403 * A new dentry page is allocated but not able to be written, since its
404 * new inode page couldn't be allocated due to -ENOSPC.
405 * In such the case, its blkaddr can be remained as NEW_ADDR.
406 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
408 if (dn
.data_blkaddr
== NEW_ADDR
) {
409 zero_user_segment(page
, 0, PAGE_SIZE
);
410 SetPageUptodate(page
);
415 fio
.new_blkaddr
= fio
.old_blkaddr
= dn
.data_blkaddr
;
417 err
= f2fs_submit_page_bio(&fio
);
423 f2fs_put_page(page
, 1);
427 struct page
*find_data_page(struct inode
*inode
, pgoff_t index
)
429 struct address_space
*mapping
= inode
->i_mapping
;
432 page
= find_get_page(mapping
, index
);
433 if (page
&& PageUptodate(page
))
435 f2fs_put_page(page
, 0);
437 page
= get_read_data_page(inode
, index
, READ_SYNC
, false);
441 if (PageUptodate(page
))
444 wait_on_page_locked(page
);
445 if (unlikely(!PageUptodate(page
))) {
446 f2fs_put_page(page
, 0);
447 return ERR_PTR(-EIO
);
453 * If it tries to access a hole, return an error.
454 * Because, the callers, functions in dir.c and GC, should be able to know
455 * whether this page exists or not.
457 struct page
*get_lock_data_page(struct inode
*inode
, pgoff_t index
,
460 struct address_space
*mapping
= inode
->i_mapping
;
463 page
= get_read_data_page(inode
, index
, READ_SYNC
, for_write
);
467 /* wait for read completion */
469 if (unlikely(!PageUptodate(page
))) {
470 f2fs_put_page(page
, 1);
471 return ERR_PTR(-EIO
);
473 if (unlikely(page
->mapping
!= mapping
)) {
474 f2fs_put_page(page
, 1);
481 * Caller ensures that this data page is never allocated.
482 * A new zero-filled data page is allocated in the page cache.
484 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
486 * Note that, ipage is set only by make_empty_dir, and if any error occur,
487 * ipage should be released by this function.
489 struct page
*get_new_data_page(struct inode
*inode
,
490 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
492 struct address_space
*mapping
= inode
->i_mapping
;
494 struct dnode_of_data dn
;
497 page
= f2fs_grab_cache_page(mapping
, index
, true);
500 * before exiting, we should make sure ipage will be released
501 * if any error occur.
503 f2fs_put_page(ipage
, 1);
504 return ERR_PTR(-ENOMEM
);
507 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
508 err
= f2fs_reserve_block(&dn
, index
);
510 f2fs_put_page(page
, 1);
516 if (PageUptodate(page
))
519 if (dn
.data_blkaddr
== NEW_ADDR
) {
520 zero_user_segment(page
, 0, PAGE_SIZE
);
521 SetPageUptodate(page
);
523 f2fs_put_page(page
, 1);
525 /* if ipage exists, blkaddr should be NEW_ADDR */
526 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
527 page
= get_lock_data_page(inode
, index
, true);
532 if (new_i_size
&& i_size_read(inode
) <
533 ((loff_t
)(index
+ 1) << PAGE_SHIFT
)) {
534 i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
535 /* Only the directory inode sets new_i_size */
536 set_inode_flag(F2FS_I(inode
), FI_UPDATE_DIR
);
541 static int __allocate_data_block(struct dnode_of_data
*dn
)
543 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
544 struct f2fs_summary sum
;
546 int seg
= CURSEG_WARM_DATA
;
549 if (unlikely(is_inode_flag_set(F2FS_I(dn
->inode
), FI_NO_ALLOC
)))
552 dn
->data_blkaddr
= datablock_addr(dn
->node_page
, dn
->ofs_in_node
);
553 if (dn
->data_blkaddr
== NEW_ADDR
)
556 if (unlikely(!inc_valid_block_count(sbi
, dn
->inode
, 1)))
560 get_node_info(sbi
, dn
->nid
, &ni
);
561 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
563 if (dn
->ofs_in_node
== 0 && dn
->inode_page
== dn
->node_page
)
564 seg
= CURSEG_DIRECT_IO
;
566 allocate_data_block(sbi
, NULL
, dn
->data_blkaddr
, &dn
->data_blkaddr
,
568 set_data_blkaddr(dn
);
571 fofs
= start_bidx_of_node(ofs_of_node(dn
->node_page
), dn
->inode
) +
573 if (i_size_read(dn
->inode
) < ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
))
574 i_size_write(dn
->inode
,
575 ((loff_t
)(fofs
+ 1) << PAGE_SHIFT
));
579 ssize_t
f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
581 struct inode
*inode
= file_inode(iocb
->ki_filp
);
582 struct f2fs_map_blocks map
;
585 map
.m_lblk
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
);
586 map
.m_len
= F2FS_BLK_ALIGN(iov_iter_count(from
));
587 map
.m_next_pgofs
= NULL
;
589 if (f2fs_encrypted_inode(inode
))
592 if (iocb
->ki_flags
& IOCB_DIRECT
) {
593 ret
= f2fs_convert_inline_inode(inode
);
596 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_DIO
);
598 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA
) {
599 ret
= f2fs_convert_inline_inode(inode
);
603 if (!f2fs_has_inline_data(inode
))
604 return f2fs_map_blocks(inode
, &map
, 1, F2FS_GET_BLOCK_PRE_AIO
);
609 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
610 * f2fs_map_blocks structure.
611 * If original data blocks are allocated, then give them to blockdev.
613 * a. preallocate requested block addresses
614 * b. do not use extent cache for better performance
615 * c. give the block addresses to blockdev
617 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
618 int create
, int flag
)
620 unsigned int maxblocks
= map
->m_len
;
621 struct dnode_of_data dn
;
622 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
623 int mode
= create
? ALLOC_NODE
: LOOKUP_NODE_RA
;
624 pgoff_t pgofs
, end_offset
;
625 int err
= 0, ofs
= 1;
626 struct extent_info ei
;
627 bool allocated
= false;
633 /* it only supports block size == page size */
634 pgofs
= (pgoff_t
)map
->m_lblk
;
636 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
637 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
638 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
639 map
->m_flags
= F2FS_MAP_MAPPED
;
647 /* When reading holes, we need its node page */
648 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
649 err
= get_dnode_of_data(&dn
, pgofs
, mode
);
651 if (err
== -ENOENT
) {
653 if (map
->m_next_pgofs
)
655 get_next_page_offset(&dn
, pgofs
);
660 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
663 blkaddr
= datablock_addr(dn
.node_page
, dn
.ofs_in_node
);
665 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
) {
667 if (unlikely(f2fs_cp_error(sbi
))) {
671 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
672 if (blkaddr
== NULL_ADDR
)
673 err
= reserve_new_block(&dn
);
675 err
= __allocate_data_block(&dn
);
680 map
->m_flags
= F2FS_MAP_NEW
;
681 blkaddr
= dn
.data_blkaddr
;
683 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
684 blkaddr
== NULL_ADDR
) {
685 if (map
->m_next_pgofs
)
686 *map
->m_next_pgofs
= pgofs
+ 1;
688 if (flag
!= F2FS_GET_BLOCK_FIEMAP
||
689 blkaddr
!= NEW_ADDR
) {
690 if (flag
== F2FS_GET_BLOCK_BMAP
)
697 if (map
->m_len
== 0) {
698 /* preallocated unwritten block should be mapped for fiemap. */
699 if (blkaddr
== NEW_ADDR
)
700 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
701 map
->m_flags
|= F2FS_MAP_MAPPED
;
703 map
->m_pblk
= blkaddr
;
705 } else if ((map
->m_pblk
!= NEW_ADDR
&&
706 blkaddr
== (map
->m_pblk
+ ofs
)) ||
707 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
708 flag
== F2FS_GET_BLOCK_PRE_DIO
||
709 flag
== F2FS_GET_BLOCK_PRE_AIO
) {
719 if (map
->m_len
< maxblocks
) {
720 if (dn
.ofs_in_node
< end_offset
)
724 sync_inode_page(&dn
);
729 f2fs_balance_fs(sbi
, allocated
);
737 sync_inode_page(&dn
);
742 f2fs_balance_fs(sbi
, allocated
);
745 trace_f2fs_map_blocks(inode
, map
, err
);
749 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
750 struct buffer_head
*bh
, int create
, int flag
,
753 struct f2fs_map_blocks map
;
757 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
758 map
.m_next_pgofs
= next_pgofs
;
760 ret
= f2fs_map_blocks(inode
, &map
, create
, flag
);
762 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
763 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
764 bh
->b_size
= map
.m_len
<< inode
->i_blkbits
;
769 static int get_data_block(struct inode
*inode
, sector_t iblock
,
770 struct buffer_head
*bh_result
, int create
, int flag
,
773 return __get_data_block(inode
, iblock
, bh_result
, create
,
777 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
778 struct buffer_head
*bh_result
, int create
)
780 return __get_data_block(inode
, iblock
, bh_result
, create
,
781 F2FS_GET_BLOCK_DIO
, NULL
);
784 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
785 struct buffer_head
*bh_result
, int create
)
787 /* Block number less than F2FS MAX BLOCKS */
788 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
791 return __get_data_block(inode
, iblock
, bh_result
, create
,
792 F2FS_GET_BLOCK_BMAP
, NULL
);
795 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
797 return (offset
>> inode
->i_blkbits
);
800 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
802 return (blk
<< inode
->i_blkbits
);
805 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
808 struct buffer_head map_bh
;
809 sector_t start_blk
, last_blk
;
812 u64 logical
= 0, phys
= 0, size
= 0;
816 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
820 if (f2fs_has_inline_data(inode
)) {
821 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
828 isize
= i_size_read(inode
);
832 if (start
+ len
> isize
)
835 if (logical_to_blk(inode
, len
) == 0)
836 len
= blk_to_logical(inode
, 1);
838 start_blk
= logical_to_blk(inode
, start
);
839 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
842 memset(&map_bh
, 0, sizeof(struct buffer_head
));
845 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
846 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
851 if (!buffer_mapped(&map_bh
)) {
852 start_blk
= next_pgofs
;
853 /* Go through holes util pass the EOF */
854 if (blk_to_logical(inode
, start_blk
) < isize
)
856 /* Found a hole beyond isize means no more extents.
857 * Note that the premise is that filesystems don't
858 * punch holes beyond isize and keep size unchanged.
860 flags
|= FIEMAP_EXTENT_LAST
;
864 if (f2fs_encrypted_inode(inode
))
865 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
867 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
871 if (start_blk
> last_blk
|| ret
)
874 logical
= blk_to_logical(inode
, start_blk
);
875 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
876 size
= map_bh
.b_size
;
878 if (buffer_unwritten(&map_bh
))
879 flags
= FIEMAP_EXTENT_UNWRITTEN
;
881 start_blk
+= logical_to_blk(inode
, size
);
885 if (fatal_signal_pending(current
))
898 * This function was originally taken from fs/mpage.c, and customized for f2fs.
899 * Major change was from block_size == page_size in f2fs by default.
901 static int f2fs_mpage_readpages(struct address_space
*mapping
,
902 struct list_head
*pages
, struct page
*page
,
905 struct bio
*bio
= NULL
;
907 sector_t last_block_in_bio
= 0;
908 struct inode
*inode
= mapping
->host
;
909 const unsigned blkbits
= inode
->i_blkbits
;
910 const unsigned blocksize
= 1 << blkbits
;
911 sector_t block_in_file
;
913 sector_t last_block_in_file
;
915 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
916 struct f2fs_map_blocks map
;
922 map
.m_next_pgofs
= NULL
;
924 for (page_idx
= 0; nr_pages
; page_idx
++, nr_pages
--) {
926 prefetchw(&page
->flags
);
928 page
= list_entry(pages
->prev
, struct page
, lru
);
929 list_del(&page
->lru
);
930 if (add_to_page_cache_lru(page
, mapping
,
931 page
->index
, GFP_KERNEL
))
935 block_in_file
= (sector_t
)page
->index
;
936 last_block
= block_in_file
+ nr_pages
;
937 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >>
939 if (last_block
> last_block_in_file
)
940 last_block
= last_block_in_file
;
943 * Map blocks using the previous result first.
945 if ((map
.m_flags
& F2FS_MAP_MAPPED
) &&
946 block_in_file
> map
.m_lblk
&&
947 block_in_file
< (map
.m_lblk
+ map
.m_len
))
951 * Then do more f2fs_map_blocks() calls until we are
952 * done with this page.
956 if (block_in_file
< last_block
) {
957 map
.m_lblk
= block_in_file
;
958 map
.m_len
= last_block
- block_in_file
;
960 if (f2fs_map_blocks(inode
, &map
, 0,
961 F2FS_GET_BLOCK_READ
))
965 if ((map
.m_flags
& F2FS_MAP_MAPPED
)) {
966 block_nr
= map
.m_pblk
+ block_in_file
- map
.m_lblk
;
967 SetPageMappedToDisk(page
);
969 if (!PageUptodate(page
) && !cleancache_get_page(page
)) {
970 SetPageUptodate(page
);
974 zero_user_segment(page
, 0, PAGE_SIZE
);
975 SetPageUptodate(page
);
981 * This page will go to BIO. Do we need to send this
984 if (bio
&& (last_block_in_bio
!= block_nr
- 1)) {
986 submit_bio(READ
, bio
);
990 struct fscrypt_ctx
*ctx
= NULL
;
992 if (f2fs_encrypted_inode(inode
) &&
993 S_ISREG(inode
->i_mode
)) {
995 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
999 /* wait the page to be moved by cleaning */
1000 f2fs_wait_on_encrypted_page_writeback(
1001 F2FS_I_SB(inode
), block_nr
);
1004 bio
= bio_alloc(GFP_KERNEL
,
1005 min_t(int, nr_pages
, BIO_MAX_PAGES
));
1008 fscrypt_release_ctx(ctx
);
1009 goto set_error_page
;
1011 bio
->bi_bdev
= bdev
;
1012 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(block_nr
);
1013 bio
->bi_end_io
= f2fs_read_end_io
;
1014 bio
->bi_private
= ctx
;
1017 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1018 goto submit_and_realloc
;
1020 last_block_in_bio
= block_nr
;
1024 zero_user_segment(page
, 0, PAGE_SIZE
);
1029 submit_bio(READ
, bio
);
1037 BUG_ON(pages
&& !list_empty(pages
));
1039 submit_bio(READ
, bio
);
1043 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
1045 struct inode
*inode
= page
->mapping
->host
;
1048 trace_f2fs_readpage(page
, DATA
);
1050 /* If the file has inline data, try to read it directly */
1051 if (f2fs_has_inline_data(inode
))
1052 ret
= f2fs_read_inline_data(inode
, page
);
1054 ret
= f2fs_mpage_readpages(page
->mapping
, NULL
, page
, 1);
1058 static int f2fs_read_data_pages(struct file
*file
,
1059 struct address_space
*mapping
,
1060 struct list_head
*pages
, unsigned nr_pages
)
1062 struct inode
*inode
= file
->f_mapping
->host
;
1063 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
1065 trace_f2fs_readpages(inode
, page
, nr_pages
);
1067 /* If the file has inline data, skip readpages */
1068 if (f2fs_has_inline_data(inode
))
1071 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
);
1074 int do_write_data_page(struct f2fs_io_info
*fio
)
1076 struct page
*page
= fio
->page
;
1077 struct inode
*inode
= page
->mapping
->host
;
1078 struct dnode_of_data dn
;
1081 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1082 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
1086 fio
->old_blkaddr
= dn
.data_blkaddr
;
1088 /* This page is already truncated */
1089 if (fio
->old_blkaddr
== NULL_ADDR
) {
1090 ClearPageUptodate(page
);
1094 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1095 gfp_t gfp_flags
= GFP_NOFS
;
1097 /* wait for GCed encrypted page writeback */
1098 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode
),
1101 fio
->encrypted_page
= fscrypt_encrypt_page(inode
, fio
->page
,
1103 if (IS_ERR(fio
->encrypted_page
)) {
1104 err
= PTR_ERR(fio
->encrypted_page
);
1105 if (err
== -ENOMEM
) {
1106 /* flush pending ios and wait for a while */
1107 f2fs_flush_merged_bios(F2FS_I_SB(inode
));
1108 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
1109 gfp_flags
|= __GFP_NOFAIL
;
1117 set_page_writeback(page
);
1120 * If current allocation needs SSR,
1121 * it had better in-place writes for updated data.
1123 if (unlikely(fio
->old_blkaddr
!= NEW_ADDR
&&
1124 !is_cold_data(page
) &&
1125 !IS_ATOMIC_WRITTEN_PAGE(page
) &&
1126 need_inplace_update(inode
))) {
1127 rewrite_data_page(fio
);
1128 set_inode_flag(F2FS_I(inode
), FI_UPDATE_WRITE
);
1129 trace_f2fs_do_write_data_page(page
, IPU
);
1131 write_data_page(&dn
, fio
);
1132 trace_f2fs_do_write_data_page(page
, OPU
);
1133 set_inode_flag(F2FS_I(inode
), FI_APPEND_WRITE
);
1134 if (page
->index
== 0)
1135 set_inode_flag(F2FS_I(inode
), FI_FIRST_BLOCK_WRITTEN
);
1138 f2fs_put_dnode(&dn
);
1142 static int f2fs_write_data_page(struct page
*page
,
1143 struct writeback_control
*wbc
)
1145 struct inode
*inode
= page
->mapping
->host
;
1146 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1147 loff_t i_size
= i_size_read(inode
);
1148 const pgoff_t end_index
= ((unsigned long long) i_size
)
1150 unsigned offset
= 0;
1151 bool need_balance_fs
= false;
1153 struct f2fs_io_info fio
= {
1156 .rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
,
1158 .encrypted_page
= NULL
,
1161 trace_f2fs_writepage(page
, DATA
);
1163 if (page
->index
< end_index
)
1167 * If the offset is out-of-range of file size,
1168 * this page does not have to be written to disk.
1170 offset
= i_size
& (PAGE_SIZE
- 1);
1171 if ((page
->index
>= end_index
+ 1) || !offset
)
1174 zero_user_segment(page
, offset
, PAGE_SIZE
);
1176 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1178 if (f2fs_is_drop_cache(inode
))
1180 if (f2fs_is_volatile_file(inode
) && !wbc
->for_reclaim
&&
1181 available_free_memory(sbi
, BASE_CHECK
))
1184 /* Dentry blocks are controlled by checkpoint */
1185 if (S_ISDIR(inode
->i_mode
)) {
1186 if (unlikely(f2fs_cp_error(sbi
)))
1188 err
= do_write_data_page(&fio
);
1192 /* we should bypass data pages to proceed the kworkder jobs */
1193 if (unlikely(f2fs_cp_error(sbi
))) {
1198 if (!wbc
->for_reclaim
)
1199 need_balance_fs
= true;
1200 else if (has_not_enough_free_secs(sbi
, 0))
1205 if (f2fs_has_inline_data(inode
))
1206 err
= f2fs_write_inline_data(inode
, page
);
1208 err
= do_write_data_page(&fio
);
1209 f2fs_unlock_op(sbi
);
1211 if (err
&& err
!= -ENOENT
)
1214 clear_cold_data(page
);
1216 inode_dec_dirty_pages(inode
);
1218 ClearPageUptodate(page
);
1220 if (wbc
->for_reclaim
) {
1221 f2fs_submit_merged_bio_cond(sbi
, NULL
, page
, 0, DATA
, WRITE
);
1222 remove_dirty_inode(inode
);
1226 f2fs_balance_fs(sbi
, need_balance_fs
);
1228 if (unlikely(f2fs_cp_error(sbi
)))
1229 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
1234 redirty_page_for_writepage(wbc
, page
);
1235 return AOP_WRITEPAGE_ACTIVATE
;
1238 static int __f2fs_writepage(struct page
*page
, struct writeback_control
*wbc
,
1241 struct address_space
*mapping
= data
;
1242 int ret
= mapping
->a_ops
->writepage(page
, wbc
);
1243 mapping_set_error(mapping
, ret
);
1248 * This function was copied from write_cche_pages from mm/page-writeback.c.
1249 * The major change is making write step of cold data page separately from
1250 * warm/hot data page.
1252 static int f2fs_write_cache_pages(struct address_space
*mapping
,
1253 struct writeback_control
*wbc
, writepage_t writepage
,
1258 struct pagevec pvec
;
1260 pgoff_t
uninitialized_var(writeback_index
);
1262 pgoff_t end
; /* Inclusive */
1265 int range_whole
= 0;
1269 pagevec_init(&pvec
, 0);
1271 if (wbc
->range_cyclic
) {
1272 writeback_index
= mapping
->writeback_index
; /* prev offset */
1273 index
= writeback_index
;
1280 index
= wbc
->range_start
>> PAGE_SHIFT
;
1281 end
= wbc
->range_end
>> PAGE_SHIFT
;
1282 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
1284 cycled
= 1; /* ignore range_cyclic tests */
1286 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1287 tag
= PAGECACHE_TAG_TOWRITE
;
1289 tag
= PAGECACHE_TAG_DIRTY
;
1291 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
1292 tag_pages_for_writeback(mapping
, index
, end
);
1294 while (!done
&& (index
<= end
)) {
1297 nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
, tag
,
1298 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
- 1) + 1);
1302 for (i
= 0; i
< nr_pages
; i
++) {
1303 struct page
*page
= pvec
.pages
[i
];
1305 if (page
->index
> end
) {
1310 done_index
= page
->index
;
1314 if (unlikely(page
->mapping
!= mapping
)) {
1320 if (!PageDirty(page
)) {
1321 /* someone wrote it for us */
1322 goto continue_unlock
;
1325 if (step
== is_cold_data(page
))
1326 goto continue_unlock
;
1328 if (PageWriteback(page
)) {
1329 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1330 f2fs_wait_on_page_writeback(page
,
1333 goto continue_unlock
;
1336 BUG_ON(PageWriteback(page
));
1337 if (!clear_page_dirty_for_io(page
))
1338 goto continue_unlock
;
1340 ret
= (*writepage
)(page
, wbc
, data
);
1341 if (unlikely(ret
)) {
1342 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
1346 done_index
= page
->index
+ 1;
1352 if (--wbc
->nr_to_write
<= 0 &&
1353 wbc
->sync_mode
== WB_SYNC_NONE
) {
1358 pagevec_release(&pvec
);
1367 if (!cycled
&& !done
) {
1370 end
= writeback_index
- 1;
1373 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1374 mapping
->writeback_index
= done_index
;
1379 static int f2fs_write_data_pages(struct address_space
*mapping
,
1380 struct writeback_control
*wbc
)
1382 struct inode
*inode
= mapping
->host
;
1383 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1384 bool locked
= false;
1388 /* deal with chardevs and other special file */
1389 if (!mapping
->a_ops
->writepage
)
1392 /* skip writing if there is no dirty page in this inode */
1393 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
1396 if (S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_NONE
&&
1397 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
1398 available_free_memory(sbi
, DIRTY_DENTS
))
1401 /* skip writing during file defragment */
1402 if (is_inode_flag_set(F2FS_I(inode
), FI_DO_DEFRAG
))
1405 /* during POR, we don't need to trigger writepage at all. */
1406 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
1409 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1411 diff
= nr_pages_to_write(sbi
, DATA
, wbc
);
1413 if (!S_ISDIR(inode
->i_mode
) && wbc
->sync_mode
== WB_SYNC_ALL
) {
1414 mutex_lock(&sbi
->writepages
);
1417 ret
= f2fs_write_cache_pages(mapping
, wbc
, __f2fs_writepage
, mapping
);
1418 f2fs_submit_merged_bio_cond(sbi
, inode
, NULL
, 0, DATA
, WRITE
);
1420 mutex_unlock(&sbi
->writepages
);
1422 remove_dirty_inode(inode
);
1424 wbc
->nr_to_write
= max((long)0, wbc
->nr_to_write
- diff
);
1428 wbc
->pages_skipped
+= get_dirty_pages(inode
);
1429 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
1433 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
1435 struct inode
*inode
= mapping
->host
;
1436 loff_t i_size
= i_size_read(inode
);
1439 truncate_pagecache(inode
, i_size
);
1440 truncate_blocks(inode
, i_size
, true);
1444 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
1445 struct page
*page
, loff_t pos
, unsigned len
,
1446 block_t
*blk_addr
, bool *node_changed
)
1448 struct inode
*inode
= page
->mapping
->host
;
1449 pgoff_t index
= page
->index
;
1450 struct dnode_of_data dn
;
1452 bool locked
= false;
1453 struct extent_info ei
;
1457 * we already allocated all the blocks, so we don't need to get
1458 * the block addresses when there is no need to fill the page.
1460 if (!f2fs_has_inline_data(inode
) && !f2fs_encrypted_inode(inode
) &&
1464 if (f2fs_has_inline_data(inode
) ||
1465 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1470 /* check inline_data */
1471 ipage
= get_node_page(sbi
, inode
->i_ino
);
1472 if (IS_ERR(ipage
)) {
1473 err
= PTR_ERR(ipage
);
1477 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
1479 if (f2fs_has_inline_data(inode
)) {
1480 if (pos
+ len
<= MAX_INLINE_DATA
) {
1481 read_inline_data(page
, ipage
);
1482 set_inode_flag(F2FS_I(inode
), FI_DATA_EXIST
);
1483 set_inline_node(ipage
);
1485 err
= f2fs_convert_inline_page(&dn
, page
);
1488 if (dn
.data_blkaddr
== NULL_ADDR
)
1489 err
= f2fs_get_block(&dn
, index
);
1491 } else if (locked
) {
1492 err
= f2fs_get_block(&dn
, index
);
1494 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1495 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1498 err
= get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1499 if (err
|| (!err
&& dn
.data_blkaddr
== NULL_ADDR
)) {
1500 f2fs_put_dnode(&dn
);
1508 /* convert_inline_page can make node_changed */
1509 *blk_addr
= dn
.data_blkaddr
;
1510 *node_changed
= dn
.node_changed
;
1512 f2fs_put_dnode(&dn
);
1515 f2fs_unlock_op(sbi
);
1519 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
1520 loff_t pos
, unsigned len
, unsigned flags
,
1521 struct page
**pagep
, void **fsdata
)
1523 struct inode
*inode
= mapping
->host
;
1524 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1525 struct page
*page
= NULL
;
1526 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
1527 bool need_balance
= false;
1528 block_t blkaddr
= NULL_ADDR
;
1531 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
1534 * We should check this at this moment to avoid deadlock on inode page
1535 * and #0 page. The locking rule for inline_data conversion should be:
1536 * lock_page(page #0) -> lock_page(inode_page)
1539 err
= f2fs_convert_inline_inode(inode
);
1544 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1552 err
= prepare_write_begin(sbi
, page
, pos
, len
,
1553 &blkaddr
, &need_balance
);
1557 if (need_balance
&& has_not_enough_free_secs(sbi
, 0)) {
1559 f2fs_balance_fs(sbi
, true);
1561 if (page
->mapping
!= mapping
) {
1562 /* The page got truncated from under us */
1563 f2fs_put_page(page
, 1);
1568 f2fs_wait_on_page_writeback(page
, DATA
, false);
1570 /* wait for GCed encrypted page writeback */
1571 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1572 f2fs_wait_on_encrypted_page_writeback(sbi
, blkaddr
);
1574 if (len
== PAGE_SIZE
)
1576 if (PageUptodate(page
))
1579 if ((pos
& PAGE_MASK
) >= i_size_read(inode
)) {
1580 unsigned start
= pos
& (PAGE_SIZE
- 1);
1581 unsigned end
= start
+ len
;
1583 /* Reading beyond i_size is simple: memset to zero */
1584 zero_user_segments(page
, 0, start
, end
, PAGE_SIZE
);
1588 if (blkaddr
== NEW_ADDR
) {
1589 zero_user_segment(page
, 0, PAGE_SIZE
);
1591 struct f2fs_io_info fio
= {
1595 .old_blkaddr
= blkaddr
,
1596 .new_blkaddr
= blkaddr
,
1598 .encrypted_page
= NULL
,
1600 err
= f2fs_submit_page_bio(&fio
);
1605 if (unlikely(!PageUptodate(page
))) {
1609 if (unlikely(page
->mapping
!= mapping
)) {
1610 f2fs_put_page(page
, 1);
1614 /* avoid symlink page */
1615 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
)) {
1616 err
= fscrypt_decrypt_page(page
);
1622 SetPageUptodate(page
);
1624 clear_cold_data(page
);
1628 f2fs_put_page(page
, 1);
1629 f2fs_write_failed(mapping
, pos
+ len
);
1633 static int f2fs_write_end(struct file
*file
,
1634 struct address_space
*mapping
,
1635 loff_t pos
, unsigned len
, unsigned copied
,
1636 struct page
*page
, void *fsdata
)
1638 struct inode
*inode
= page
->mapping
->host
;
1640 trace_f2fs_write_end(inode
, pos
, len
, copied
);
1642 set_page_dirty(page
);
1644 if (pos
+ copied
> i_size_read(inode
)) {
1645 i_size_write(inode
, pos
+ copied
);
1646 mark_inode_dirty(inode
);
1649 f2fs_put_page(page
, 1);
1650 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
1654 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
1657 unsigned blocksize_mask
= inode
->i_sb
->s_blocksize
- 1;
1659 if (offset
& blocksize_mask
)
1662 if (iov_iter_alignment(iter
) & blocksize_mask
)
1668 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
1671 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1672 struct inode
*inode
= mapping
->host
;
1673 size_t count
= iov_iter_count(iter
);
1676 err
= check_direct_IO(inode
, iter
, offset
);
1680 if (f2fs_encrypted_inode(inode
) && S_ISREG(inode
->i_mode
))
1683 trace_f2fs_direct_IO_enter(inode
, offset
, count
, iov_iter_rw(iter
));
1685 err
= blockdev_direct_IO(iocb
, inode
, iter
, offset
, get_data_block_dio
);
1686 if (err
< 0 && iov_iter_rw(iter
) == WRITE
)
1687 f2fs_write_failed(mapping
, offset
+ count
);
1689 trace_f2fs_direct_IO_exit(inode
, offset
, count
, iov_iter_rw(iter
), err
);
1694 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
1695 unsigned int length
)
1697 struct inode
*inode
= page
->mapping
->host
;
1698 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1700 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
1701 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
1704 if (PageDirty(page
)) {
1705 if (inode
->i_ino
== F2FS_META_INO(sbi
))
1706 dec_page_count(sbi
, F2FS_DIRTY_META
);
1707 else if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
1708 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
1710 inode_dec_dirty_pages(inode
);
1713 /* This is atomic written page, keep Private */
1714 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1717 ClearPagePrivate(page
);
1720 int f2fs_release_page(struct page
*page
, gfp_t wait
)
1722 /* If this is dirty page, keep PagePrivate */
1723 if (PageDirty(page
))
1726 /* This is atomic written page, keep Private */
1727 if (IS_ATOMIC_WRITTEN_PAGE(page
))
1730 ClearPagePrivate(page
);
1734 static int f2fs_set_data_page_dirty(struct page
*page
)
1736 struct address_space
*mapping
= page
->mapping
;
1737 struct inode
*inode
= mapping
->host
;
1739 trace_f2fs_set_page_dirty(page
, DATA
);
1741 SetPageUptodate(page
);
1743 if (f2fs_is_atomic_file(inode
)) {
1744 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
1745 register_inmem_page(inode
, page
);
1749 * Previously, this page has been registered, we just
1755 if (!PageDirty(page
)) {
1756 __set_page_dirty_nobuffers(page
);
1757 update_dirty_page(inode
, page
);
1763 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
1765 struct inode
*inode
= mapping
->host
;
1767 if (f2fs_has_inline_data(inode
))
1770 /* make sure allocating whole blocks */
1771 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
1772 filemap_write_and_wait(mapping
);
1774 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
1777 const struct address_space_operations f2fs_dblock_aops
= {
1778 .readpage
= f2fs_read_data_page
,
1779 .readpages
= f2fs_read_data_pages
,
1780 .writepage
= f2fs_write_data_page
,
1781 .writepages
= f2fs_write_data_pages
,
1782 .write_begin
= f2fs_write_begin
,
1783 .write_end
= f2fs_write_end
,
1784 .set_page_dirty
= f2fs_set_data_page_dirty
,
1785 .invalidatepage
= f2fs_invalidate_page
,
1786 .releasepage
= f2fs_release_page
,
1787 .direct_IO
= f2fs_direct_IO
,