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/aio.h>
16 #include <linux/writeback.h>
17 #include <linux/backing-dev.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
25 #include <trace/events/f2fs.h>
28 * Lock ordering for the change of data block address:
31 * update block addresses in the node page
33 static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
37 struct page *node_page = dn->node_page;
38 unsigned int ofs_in_node = dn->ofs_in_node;
40 f2fs_wait_on_page_writeback(node_page, NODE, false);
42 rn = F2FS_NODE(node_page);
44 /* Get physical address of data block */
45 addr_array = blkaddr_in_node(rn);
46 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
47 set_page_dirty(node_page);
50 int reserve_new_block(struct dnode_of_data *dn)
52 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
54 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
56 if (!inc_valid_block_count(sbi, dn->inode, 1))
59 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
61 __set_data_blkaddr(dn, NEW_ADDR);
62 dn->data_blkaddr = NEW_ADDR;
67 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
69 bool need_put = dn->inode_page ? false : true;
72 err = get_dnode_of_data(dn, index, ALLOC_NODE);
75 if (dn->data_blkaddr == NULL_ADDR)
76 err = reserve_new_block(dn);
83 static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
84 struct buffer_head *bh_result)
86 struct f2fs_inode_info *fi = F2FS_I(inode);
87 pgoff_t start_fofs, end_fofs;
88 block_t start_blkaddr;
90 if (is_inode_flag_set(fi, FI_NO_EXTENT))
93 read_lock(&fi->ext.ext_lock);
94 if (fi->ext.len == 0) {
95 read_unlock(&fi->ext.ext_lock);
99 stat_inc_total_hit(inode->i_sb);
101 start_fofs = fi->ext.fofs;
102 end_fofs = fi->ext.fofs + fi->ext.len - 1;
103 start_blkaddr = fi->ext.blk_addr;
105 if (pgofs >= start_fofs && pgofs <= end_fofs) {
106 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
109 clear_buffer_new(bh_result);
110 map_bh(bh_result, inode->i_sb,
111 start_blkaddr + pgofs - start_fofs);
112 count = end_fofs - pgofs + 1;
113 if (count < (UINT_MAX >> blkbits))
114 bh_result->b_size = (count << blkbits);
116 bh_result->b_size = UINT_MAX;
118 stat_inc_read_hit(inode->i_sb);
119 read_unlock(&fi->ext.ext_lock);
122 read_unlock(&fi->ext.ext_lock);
126 void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
128 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
129 pgoff_t fofs, start_fofs, end_fofs;
130 block_t start_blkaddr, end_blkaddr;
131 int need_update = true;
133 f2fs_bug_on(blk_addr == NEW_ADDR);
134 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
137 /* Update the page address in the parent node */
138 __set_data_blkaddr(dn, blk_addr);
140 if (is_inode_flag_set(fi, FI_NO_EXTENT))
143 write_lock(&fi->ext.ext_lock);
145 start_fofs = fi->ext.fofs;
146 end_fofs = fi->ext.fofs + fi->ext.len - 1;
147 start_blkaddr = fi->ext.blk_addr;
148 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
150 /* Drop and initialize the matched extent */
151 if (fi->ext.len == 1 && fofs == start_fofs)
155 if (fi->ext.len == 0) {
156 if (blk_addr != NULL_ADDR) {
158 fi->ext.blk_addr = blk_addr;
165 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
173 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
178 /* Split the existing extent */
179 if (fi->ext.len > 1 &&
180 fofs >= start_fofs && fofs <= end_fofs) {
181 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
182 fi->ext.len = fofs - start_fofs;
184 fi->ext.fofs = fofs + 1;
185 fi->ext.blk_addr = start_blkaddr +
186 fofs - start_fofs + 1;
187 fi->ext.len -= fofs - start_fofs + 1;
193 /* Finally, if the extent is very fragmented, let's drop the cache. */
194 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
196 set_inode_flag(fi, FI_NO_EXTENT);
200 write_unlock(&fi->ext.ext_lock);
206 struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
208 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
209 struct address_space *mapping = inode->i_mapping;
210 struct dnode_of_data dn;
214 page = find_get_page(mapping, index);
215 if (page && PageUptodate(page))
217 f2fs_put_page(page, 0);
219 set_new_dnode(&dn, inode, NULL, NULL, 0);
220 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
225 if (dn.data_blkaddr == NULL_ADDR)
226 return ERR_PTR(-ENOENT);
228 /* By fallocate(), there is no cached page, but with NEW_ADDR */
229 if (dn.data_blkaddr == NEW_ADDR)
230 return ERR_PTR(-EINVAL);
232 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
234 return ERR_PTR(-ENOMEM);
236 if (PageUptodate(page)) {
241 err = f2fs_readpage(sbi, page, dn.data_blkaddr,
242 sync ? READ_SYNC : READA);
244 wait_on_page_locked(page);
245 if (!PageUptodate(page)) {
246 f2fs_put_page(page, 0);
247 return ERR_PTR(-EIO);
254 * If it tries to access a hole, return an error.
255 * Because, the callers, functions in dir.c and GC, should be able to know
256 * whether this page exists or not.
258 struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
260 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
261 struct address_space *mapping = inode->i_mapping;
262 struct dnode_of_data dn;
267 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
269 return ERR_PTR(-ENOMEM);
271 set_new_dnode(&dn, inode, NULL, NULL, 0);
272 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
274 f2fs_put_page(page, 1);
279 if (dn.data_blkaddr == NULL_ADDR) {
280 f2fs_put_page(page, 1);
281 return ERR_PTR(-ENOENT);
284 if (PageUptodate(page))
288 * A new dentry page is allocated but not able to be written, since its
289 * new inode page couldn't be allocated due to -ENOSPC.
290 * In such the case, its blkaddr can be remained as NEW_ADDR.
291 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
293 if (dn.data_blkaddr == NEW_ADDR) {
294 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
295 SetPageUptodate(page);
299 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
304 if (!PageUptodate(page)) {
305 f2fs_put_page(page, 1);
306 return ERR_PTR(-EIO);
308 if (page->mapping != mapping) {
309 f2fs_put_page(page, 1);
316 * Caller ensures that this data page is never allocated.
317 * A new zero-filled data page is allocated in the page cache.
319 * Also, caller should grab and release a mutex by calling mutex_lock_op() and
321 * Note that, npage is set only by make_empty_dir.
323 struct page *get_new_data_page(struct inode *inode,
324 struct page *npage, pgoff_t index, bool new_i_size)
326 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
327 struct address_space *mapping = inode->i_mapping;
329 struct dnode_of_data dn;
332 set_new_dnode(&dn, inode, npage, npage, 0);
333 err = f2fs_reserve_block(&dn, index);
338 page = grab_cache_page(mapping, index);
340 return ERR_PTR(-ENOMEM);
342 if (PageUptodate(page))
345 if (dn.data_blkaddr == NEW_ADDR) {
346 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
347 SetPageUptodate(page);
349 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
353 if (!PageUptodate(page)) {
354 f2fs_put_page(page, 1);
355 return ERR_PTR(-EIO);
357 if (page->mapping != mapping) {
358 f2fs_put_page(page, 1);
364 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
365 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
366 /* Only the directory inode sets new_i_size */
367 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
368 mark_inode_dirty_sync(inode);
373 static void read_end_io(struct bio *bio, int err)
375 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
376 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
379 struct page *page = bvec->bv_page;
381 if (--bvec >= bio->bi_io_vec)
382 prefetchw(&bvec->bv_page->flags);
385 SetPageUptodate(page);
387 ClearPageUptodate(page);
391 } while (bvec >= bio->bi_io_vec);
396 * Fill the locked page with data located in the block address.
397 * Return unlocked page.
399 int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
400 block_t blk_addr, int type)
402 struct block_device *bdev = sbi->sb->s_bdev;
405 trace_f2fs_readpage(page, blk_addr, type);
407 /* Allocate a new bio */
408 bio = f2fs_bio_alloc(bdev, 1);
410 /* Initialize the bio */
411 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
412 bio->bi_end_io = read_end_io;
414 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
416 f2fs_put_page(page, 1);
420 submit_bio(type, bio);
424 void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, int rw)
426 struct f2fs_bio_info *io = &sbi->read_io;
431 trace_f2fs_submit_read_bio(sbi->sb, rw, META, io->bio);
433 mutex_lock(&io->io_mutex);
435 submit_bio(rw, io->bio);
438 mutex_unlock(&io->io_mutex);
441 void submit_read_page(struct f2fs_sb_info *sbi, struct page *page,
442 block_t blk_addr, int rw)
444 struct block_device *bdev = sbi->sb->s_bdev;
445 struct f2fs_bio_info *io = &sbi->read_io;
448 verify_block_addr(sbi, blk_addr);
450 mutex_lock(&io->io_mutex);
452 if (io->bio && io->last_block_in_bio != blk_addr - 1) {
453 submit_bio(rw, io->bio);
457 if (io->bio == NULL) {
458 bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
459 io->bio = f2fs_bio_alloc(bdev, bio_blocks);
460 io->bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
461 io->bio->bi_end_io = read_end_io;
464 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
466 submit_bio(rw, io->bio);
471 io->last_block_in_bio = blk_addr;
473 mutex_unlock(&io->io_mutex);
474 trace_f2fs_submit_read_page(page, rw, META, blk_addr);
478 * This function should be used by the data read flow only where it
479 * does not check the "create" flag that indicates block allocation.
480 * The reason for this special functionality is to exploit VFS readahead
483 static int get_data_block_ro(struct inode *inode, sector_t iblock,
484 struct buffer_head *bh_result, int create)
486 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
487 unsigned maxblocks = bh_result->b_size >> blkbits;
488 struct dnode_of_data dn;
492 /* Get the page offset from the block offset(iblock) */
493 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
495 if (check_extent_cache(inode, pgofs, bh_result)) {
496 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
500 /* When reading holes, we need its node page */
501 set_new_dnode(&dn, inode, NULL, NULL, 0);
502 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
504 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
505 return (err == -ENOENT) ? 0 : err;
508 /* It does not support data allocation */
511 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
513 unsigned int end_offset;
515 end_offset = IS_INODE(dn.node_page) ?
516 ADDRS_PER_INODE(F2FS_I(inode)) :
519 clear_buffer_new(bh_result);
521 /* Give more consecutive addresses for the read ahead */
522 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
523 if (((datablock_addr(dn.node_page,
525 != (dn.data_blkaddr + i)) || maxblocks == i)
527 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
528 bh_result->b_size = (i << blkbits);
531 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
535 static int f2fs_read_data_page(struct file *file, struct page *page)
537 return mpage_readpage(page, get_data_block_ro);
540 static int f2fs_read_data_pages(struct file *file,
541 struct address_space *mapping,
542 struct list_head *pages, unsigned nr_pages)
544 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
547 int do_write_data_page(struct page *page)
549 struct inode *inode = page->mapping->host;
550 block_t old_blk_addr, new_blk_addr;
551 struct dnode_of_data dn;
554 set_new_dnode(&dn, inode, NULL, NULL, 0);
555 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
559 old_blk_addr = dn.data_blkaddr;
561 /* This page is already truncated */
562 if (old_blk_addr == NULL_ADDR)
565 set_page_writeback(page);
568 * If current allocation needs SSR,
569 * it had better in-place writes for updated data.
571 if (unlikely(old_blk_addr != NEW_ADDR &&
572 !is_cold_data(page) &&
573 need_inplace_update(inode))) {
574 rewrite_data_page(F2FS_SB(inode->i_sb), page,
577 write_data_page(inode, page, &dn,
578 old_blk_addr, &new_blk_addr);
579 update_extent_cache(new_blk_addr, &dn);
586 static int f2fs_write_data_page(struct page *page,
587 struct writeback_control *wbc)
589 struct inode *inode = page->mapping->host;
590 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
591 loff_t i_size = i_size_read(inode);
592 const pgoff_t end_index = ((unsigned long long) i_size)
595 bool need_balance_fs = false;
598 if (page->index < end_index)
602 * If the offset is out-of-range of file size,
603 * this page does not have to be written to disk.
605 offset = i_size & (PAGE_CACHE_SIZE - 1);
606 if ((page->index >= end_index + 1) || !offset) {
607 if (S_ISDIR(inode->i_mode)) {
608 dec_page_count(sbi, F2FS_DIRTY_DENTS);
609 inode_dec_dirty_dents(inode);
614 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
616 if (sbi->por_doing) {
617 err = AOP_WRITEPAGE_ACTIVATE;
621 /* Dentry blocks are controlled by checkpoint */
622 if (S_ISDIR(inode->i_mode)) {
623 dec_page_count(sbi, F2FS_DIRTY_DENTS);
624 inode_dec_dirty_dents(inode);
625 err = do_write_data_page(page);
628 err = do_write_data_page(page);
630 need_balance_fs = true;
637 if (wbc->for_reclaim)
638 f2fs_submit_bio(sbi, DATA, true);
640 clear_cold_data(page);
644 f2fs_balance_fs(sbi);
648 wbc->pages_skipped++;
649 set_page_dirty(page);
653 #define MAX_DESIRED_PAGES_WP 4096
655 static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
658 struct address_space *mapping = data;
659 int ret = mapping->a_ops->writepage(page, wbc);
660 mapping_set_error(mapping, ret);
664 static int f2fs_write_data_pages(struct address_space *mapping,
665 struct writeback_control *wbc)
667 struct inode *inode = mapping->host;
668 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
671 long excess_nrtw = 0, desired_nrtw;
673 /* deal with chardevs and other special file */
674 if (!mapping->a_ops->writepage)
677 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
678 desired_nrtw = MAX_DESIRED_PAGES_WP;
679 excess_nrtw = desired_nrtw - wbc->nr_to_write;
680 wbc->nr_to_write = desired_nrtw;
683 if (!S_ISDIR(inode->i_mode)) {
684 mutex_lock(&sbi->writepages);
687 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
689 mutex_unlock(&sbi->writepages);
690 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
692 remove_dirty_dir_inode(inode);
694 wbc->nr_to_write -= excess_nrtw;
698 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
699 loff_t pos, unsigned len, unsigned flags,
700 struct page **pagep, void **fsdata)
702 struct inode *inode = mapping->host;
703 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
705 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
706 struct dnode_of_data dn;
709 f2fs_balance_fs(sbi);
711 page = grab_cache_page_write_begin(mapping, index, flags);
717 set_new_dnode(&dn, inode, NULL, NULL, 0);
718 err = f2fs_reserve_block(&dn, index);
722 f2fs_put_page(page, 1);
726 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
729 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
730 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
731 unsigned end = start + len;
733 /* Reading beyond i_size is simple: memset to zero */
734 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
738 if (dn.data_blkaddr == NEW_ADDR) {
739 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
741 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
745 if (!PageUptodate(page)) {
746 f2fs_put_page(page, 1);
749 if (page->mapping != mapping) {
750 f2fs_put_page(page, 1);
755 SetPageUptodate(page);
756 clear_cold_data(page);
760 static int f2fs_write_end(struct file *file,
761 struct address_space *mapping,
762 loff_t pos, unsigned len, unsigned copied,
763 struct page *page, void *fsdata)
765 struct inode *inode = page->mapping->host;
767 SetPageUptodate(page);
768 set_page_dirty(page);
770 if (pos + copied > i_size_read(inode)) {
771 i_size_write(inode, pos + copied);
772 mark_inode_dirty(inode);
773 update_inode_page(inode);
776 f2fs_put_page(page, 1);
780 static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
781 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
783 struct file *file = iocb->ki_filp;
784 struct inode *inode = file->f_mapping->host;
789 /* Needs synchronization with the cleaner */
790 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
794 static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
797 struct inode *inode = page->mapping->host;
798 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
799 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
800 dec_page_count(sbi, F2FS_DIRTY_DENTS);
801 inode_dec_dirty_dents(inode);
803 ClearPagePrivate(page);
806 static int f2fs_release_data_page(struct page *page, gfp_t wait)
808 ClearPagePrivate(page);
812 static int f2fs_set_data_page_dirty(struct page *page)
814 struct address_space *mapping = page->mapping;
815 struct inode *inode = mapping->host;
817 trace_f2fs_set_page_dirty(page, DATA);
819 SetPageUptodate(page);
820 if (!PageDirty(page)) {
821 __set_page_dirty_nobuffers(page);
822 set_dirty_dir_page(inode, page);
828 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
830 return generic_block_bmap(mapping, block, get_data_block_ro);
833 const struct address_space_operations f2fs_dblock_aops = {
834 .readpage = f2fs_read_data_page,
835 .readpages = f2fs_read_data_pages,
836 .writepage = f2fs_write_data_page,
837 .writepages = f2fs_write_data_pages,
838 .write_begin = f2fs_write_begin,
839 .write_end = f2fs_write_end,
840 .set_page_dirty = f2fs_set_data_page_dirty,
841 .invalidatepage = f2fs_invalidate_data_page,
842 .releasepage = f2fs_release_data_page,
843 .direct_IO = f2fs_direct_IO,