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>
27 * Lock ordering for the change of data block address:
30 * update block addresses in the node page
32 static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
36 struct page *node_page = dn->node_page;
37 unsigned int ofs_in_node = dn->ofs_in_node;
39 wait_on_page_writeback(node_page);
41 rn = (struct f2fs_node *)page_address(node_page);
43 /* Get physical address of data block */
44 addr_array = blkaddr_in_node(rn);
45 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
46 set_page_dirty(node_page);
49 int reserve_new_block(struct dnode_of_data *dn)
51 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
53 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
55 if (!inc_valid_block_count(sbi, dn->inode, 1))
58 __set_data_blkaddr(dn, NEW_ADDR);
59 dn->data_blkaddr = NEW_ADDR;
64 static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
65 struct buffer_head *bh_result)
67 struct f2fs_inode_info *fi = F2FS_I(inode);
68 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
69 pgoff_t start_fofs, end_fofs;
70 block_t start_blkaddr;
72 read_lock(&fi->ext.ext_lock);
73 if (fi->ext.len == 0) {
74 read_unlock(&fi->ext.ext_lock);
79 start_fofs = fi->ext.fofs;
80 end_fofs = fi->ext.fofs + fi->ext.len - 1;
81 start_blkaddr = fi->ext.blk_addr;
83 if (pgofs >= start_fofs && pgofs <= end_fofs) {
84 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
87 clear_buffer_new(bh_result);
88 map_bh(bh_result, inode->i_sb,
89 start_blkaddr + pgofs - start_fofs);
90 count = end_fofs - pgofs + 1;
91 if (count < (UINT_MAX >> blkbits))
92 bh_result->b_size = (count << blkbits);
94 bh_result->b_size = UINT_MAX;
97 read_unlock(&fi->ext.ext_lock);
100 read_unlock(&fi->ext.ext_lock);
104 void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
106 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
107 pgoff_t fofs, start_fofs, end_fofs;
108 block_t start_blkaddr, end_blkaddr;
110 BUG_ON(blk_addr == NEW_ADDR);
111 fofs = start_bidx_of_node(ofs_of_node(dn->node_page)) + dn->ofs_in_node;
113 /* Update the page address in the parent node */
114 __set_data_blkaddr(dn, blk_addr);
116 write_lock(&fi->ext.ext_lock);
118 start_fofs = fi->ext.fofs;
119 end_fofs = fi->ext.fofs + fi->ext.len - 1;
120 start_blkaddr = fi->ext.blk_addr;
121 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
123 /* Drop and initialize the matched extent */
124 if (fi->ext.len == 1 && fofs == start_fofs)
128 if (fi->ext.len == 0) {
129 if (blk_addr != NULL_ADDR) {
131 fi->ext.blk_addr = blk_addr;
138 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
146 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
151 /* Split the existing extent */
152 if (fi->ext.len > 1 &&
153 fofs >= start_fofs && fofs <= end_fofs) {
154 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
155 fi->ext.len = fofs - start_fofs;
157 fi->ext.fofs = fofs + 1;
158 fi->ext.blk_addr = start_blkaddr +
159 fofs - start_fofs + 1;
160 fi->ext.len -= fofs - start_fofs + 1;
164 write_unlock(&fi->ext.ext_lock);
168 write_unlock(&fi->ext.ext_lock);
173 struct page *find_data_page(struct inode *inode, pgoff_t index)
175 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
176 struct address_space *mapping = inode->i_mapping;
177 struct dnode_of_data dn;
181 page = find_get_page(mapping, index);
182 if (page && PageUptodate(page))
184 f2fs_put_page(page, 0);
186 set_new_dnode(&dn, inode, NULL, NULL, 0);
187 err = get_dnode_of_data(&dn, index, RDONLY_NODE);
192 if (dn.data_blkaddr == NULL_ADDR)
193 return ERR_PTR(-ENOENT);
195 /* By fallocate(), there is no cached page, but with NEW_ADDR */
196 if (dn.data_blkaddr == NEW_ADDR)
197 return ERR_PTR(-EINVAL);
199 page = grab_cache_page(mapping, index);
201 return ERR_PTR(-ENOMEM);
203 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
205 f2fs_put_page(page, 1);
213 * If it tries to access a hole, return an error.
214 * Because, the callers, functions in dir.c and GC, should be able to know
215 * whether this page exists or not.
217 struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
219 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
220 struct address_space *mapping = inode->i_mapping;
221 struct dnode_of_data dn;
225 set_new_dnode(&dn, inode, NULL, NULL, 0);
226 err = get_dnode_of_data(&dn, index, RDONLY_NODE);
231 if (dn.data_blkaddr == NULL_ADDR)
232 return ERR_PTR(-ENOENT);
234 page = grab_cache_page(mapping, index);
236 return ERR_PTR(-ENOMEM);
238 if (PageUptodate(page))
241 BUG_ON(dn.data_blkaddr == NEW_ADDR);
242 BUG_ON(dn.data_blkaddr == NULL_ADDR);
244 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
246 f2fs_put_page(page, 1);
253 * Caller ensures that this data page is never allocated.
254 * A new zero-filled data page is allocated in the page cache.
256 struct page *get_new_data_page(struct inode *inode, pgoff_t index,
259 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
260 struct address_space *mapping = inode->i_mapping;
262 struct dnode_of_data dn;
265 set_new_dnode(&dn, inode, NULL, NULL, 0);
266 err = get_dnode_of_data(&dn, index, 0);
270 if (dn.data_blkaddr == NULL_ADDR) {
271 if (reserve_new_block(&dn)) {
273 return ERR_PTR(-ENOSPC);
278 page = grab_cache_page(mapping, index);
280 return ERR_PTR(-ENOMEM);
282 if (PageUptodate(page))
285 if (dn.data_blkaddr == NEW_ADDR) {
286 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
288 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
290 f2fs_put_page(page, 1);
294 SetPageUptodate(page);
297 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
298 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
299 mark_inode_dirty_sync(inode);
304 static void read_end_io(struct bio *bio, int err)
306 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
307 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
310 struct page *page = bvec->bv_page;
312 if (--bvec >= bio->bi_io_vec)
313 prefetchw(&bvec->bv_page->flags);
316 SetPageUptodate(page);
318 ClearPageUptodate(page);
322 } while (bvec >= bio->bi_io_vec);
323 kfree(bio->bi_private);
328 * Fill the locked page with data located in the block address.
329 * Read operation is synchronous, and caller must unlock the page.
331 int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
332 block_t blk_addr, int type)
334 struct block_device *bdev = sbi->sb->s_bdev;
335 bool sync = (type == READ_SYNC);
338 /* This page can be already read by other threads */
339 if (PageUptodate(page)) {
345 down_read(&sbi->bio_sem);
347 /* Allocate a new bio */
348 bio = f2fs_bio_alloc(bdev, 1);
350 /* Initialize the bio */
351 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
352 bio->bi_end_io = read_end_io;
354 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
355 kfree(bio->bi_private);
357 up_read(&sbi->bio_sem);
361 submit_bio(type, bio);
362 up_read(&sbi->bio_sem);
364 /* wait for read completion if sync */
374 * This function should be used by the data read flow only where it
375 * does not check the "create" flag that indicates block allocation.
376 * The reason for this special functionality is to exploit VFS readahead
379 static int get_data_block_ro(struct inode *inode, sector_t iblock,
380 struct buffer_head *bh_result, int create)
382 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
383 unsigned maxblocks = bh_result->b_size >> blkbits;
384 struct dnode_of_data dn;
388 /* Get the page offset from the block offset(iblock) */
389 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
391 if (check_extent_cache(inode, pgofs, bh_result))
394 /* When reading holes, we need its node page */
395 set_new_dnode(&dn, inode, NULL, NULL, 0);
396 err = get_dnode_of_data(&dn, pgofs, RDONLY_NODE);
398 return (err == -ENOENT) ? 0 : err;
400 /* It does not support data allocation */
403 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
405 unsigned int end_offset;
407 end_offset = IS_INODE(dn.node_page) ?
411 clear_buffer_new(bh_result);
413 /* Give more consecutive addresses for the read ahead */
414 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
415 if (((datablock_addr(dn.node_page,
417 != (dn.data_blkaddr + i)) || maxblocks == i)
419 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
420 bh_result->b_size = (i << blkbits);
426 static int f2fs_read_data_page(struct file *file, struct page *page)
428 return mpage_readpage(page, get_data_block_ro);
431 static int f2fs_read_data_pages(struct file *file,
432 struct address_space *mapping,
433 struct list_head *pages, unsigned nr_pages)
435 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
438 int do_write_data_page(struct page *page)
440 struct inode *inode = page->mapping->host;
441 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
442 block_t old_blk_addr, new_blk_addr;
443 struct dnode_of_data dn;
446 set_new_dnode(&dn, inode, NULL, NULL, 0);
447 err = get_dnode_of_data(&dn, page->index, RDONLY_NODE);
451 old_blk_addr = dn.data_blkaddr;
453 /* This page is already truncated */
454 if (old_blk_addr == NULL_ADDR)
457 set_page_writeback(page);
460 * If current allocation needs SSR,
461 * it had better in-place writes for updated data.
463 if (old_blk_addr != NEW_ADDR && !is_cold_data(page) &&
464 need_inplace_update(inode)) {
465 rewrite_data_page(F2FS_SB(inode->i_sb), page,
468 write_data_page(inode, page, &dn,
469 old_blk_addr, &new_blk_addr);
470 update_extent_cache(new_blk_addr, &dn);
471 F2FS_I(inode)->data_version =
472 le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
479 static int f2fs_write_data_page(struct page *page,
480 struct writeback_control *wbc)
482 struct inode *inode = page->mapping->host;
483 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
484 loff_t i_size = i_size_read(inode);
485 const pgoff_t end_index = ((unsigned long long) i_size)
490 if (page->index < end_index)
494 * If the offset is out-of-range of file size,
495 * this page does not have to be written to disk.
497 offset = i_size & (PAGE_CACHE_SIZE - 1);
498 if ((page->index >= end_index + 1) || !offset) {
499 if (S_ISDIR(inode->i_mode)) {
500 dec_page_count(sbi, F2FS_DIRTY_DENTS);
501 inode_dec_dirty_dents(inode);
506 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
511 if (wbc->for_reclaim && !S_ISDIR(inode->i_mode) && !is_cold_data(page))
514 mutex_lock_op(sbi, DATA_WRITE);
515 if (S_ISDIR(inode->i_mode)) {
516 dec_page_count(sbi, F2FS_DIRTY_DENTS);
517 inode_dec_dirty_dents(inode);
519 err = do_write_data_page(page);
520 if (err && err != -ENOENT) {
521 wbc->pages_skipped++;
522 set_page_dirty(page);
524 mutex_unlock_op(sbi, DATA_WRITE);
526 if (wbc->for_reclaim)
527 f2fs_submit_bio(sbi, DATA, true);
532 clear_cold_data(page);
535 if (!wbc->for_reclaim && !S_ISDIR(inode->i_mode))
536 f2fs_balance_fs(sbi);
541 return (err == -ENOENT) ? 0 : err;
544 wbc->pages_skipped++;
545 set_page_dirty(page);
546 return AOP_WRITEPAGE_ACTIVATE;
549 #define MAX_DESIRED_PAGES_WP 4096
551 static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
554 struct address_space *mapping = data;
555 int ret = mapping->a_ops->writepage(page, wbc);
556 mapping_set_error(mapping, ret);
560 static int f2fs_write_data_pages(struct address_space *mapping,
561 struct writeback_control *wbc)
563 struct inode *inode = mapping->host;
564 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
566 long excess_nrtw = 0, desired_nrtw;
568 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
569 desired_nrtw = MAX_DESIRED_PAGES_WP;
570 excess_nrtw = desired_nrtw - wbc->nr_to_write;
571 wbc->nr_to_write = desired_nrtw;
574 if (!S_ISDIR(inode->i_mode))
575 mutex_lock(&sbi->writepages);
576 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
577 if (!S_ISDIR(inode->i_mode))
578 mutex_unlock(&sbi->writepages);
579 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
581 remove_dirty_dir_inode(inode);
583 wbc->nr_to_write -= excess_nrtw;
587 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
588 loff_t pos, unsigned len, unsigned flags,
589 struct page **pagep, void **fsdata)
591 struct inode *inode = mapping->host;
592 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
594 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
595 struct dnode_of_data dn;
598 /* for nobh_write_end */
601 f2fs_balance_fs(sbi);
603 page = grab_cache_page_write_begin(mapping, index, flags);
608 mutex_lock_op(sbi, DATA_NEW);
610 set_new_dnode(&dn, inode, NULL, NULL, 0);
611 err = get_dnode_of_data(&dn, index, 0);
613 mutex_unlock_op(sbi, DATA_NEW);
614 f2fs_put_page(page, 1);
618 if (dn.data_blkaddr == NULL_ADDR) {
619 err = reserve_new_block(&dn);
622 mutex_unlock_op(sbi, DATA_NEW);
623 f2fs_put_page(page, 1);
629 mutex_unlock_op(sbi, DATA_NEW);
631 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
634 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
635 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
636 unsigned end = start + len;
638 /* Reading beyond i_size is simple: memset to zero */
639 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
643 if (dn.data_blkaddr == NEW_ADDR) {
644 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
646 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
648 f2fs_put_page(page, 1);
652 SetPageUptodate(page);
653 clear_cold_data(page);
657 static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
658 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
660 struct file *file = iocb->ki_filp;
661 struct inode *inode = file->f_mapping->host;
666 /* Needs synchronization with the cleaner */
667 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
671 static void f2fs_invalidate_data_page(struct page *page, unsigned long offset)
673 struct inode *inode = page->mapping->host;
674 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
675 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
676 dec_page_count(sbi, F2FS_DIRTY_DENTS);
677 inode_dec_dirty_dents(inode);
679 ClearPagePrivate(page);
682 static int f2fs_release_data_page(struct page *page, gfp_t wait)
684 ClearPagePrivate(page);
688 static int f2fs_set_data_page_dirty(struct page *page)
690 struct address_space *mapping = page->mapping;
691 struct inode *inode = mapping->host;
693 SetPageUptodate(page);
694 if (!PageDirty(page)) {
695 __set_page_dirty_nobuffers(page);
696 set_dirty_dir_page(inode, page);
702 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
704 return generic_block_bmap(mapping, block, get_data_block_ro);
707 const struct address_space_operations f2fs_dblock_aops = {
708 .readpage = f2fs_read_data_page,
709 .readpages = f2fs_read_data_pages,
710 .writepage = f2fs_write_data_page,
711 .writepages = f2fs_write_data_pages,
712 .write_begin = f2fs_write_begin,
713 .write_end = nobh_write_end,
714 .set_page_dirty = f2fs_set_data_page_dirty,
715 .invalidatepage = f2fs_invalidate_data_page,
716 .releasepage = f2fs_release_data_page,
717 .direct_IO = f2fs_direct_IO,