Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2 | * fs/f2fs/data.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 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. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/mpage.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 15 | #include <linux/aio.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 16 | #include <linux/writeback.h> |
| 17 | #include <linux/backing-dev.h> |
| 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/bio.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 20 | #include <linux/prefetch.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 21 | |
| 22 | #include "f2fs.h" |
| 23 | #include "node.h" |
| 24 | #include "segment.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 25 | #include "trace.h" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 26 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 27 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 28 | static struct kmem_cache *extent_tree_slab; |
| 29 | static struct kmem_cache *extent_node_slab; |
| 30 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 31 | static void f2fs_read_end_io(struct bio *bio, int err) |
| 32 | { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 33 | struct bio_vec *bvec; |
| 34 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 35 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 36 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 37 | struct page *page = bvec->bv_page; |
| 38 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 39 | if (!err) { |
| 40 | SetPageUptodate(page); |
| 41 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 42 | ClearPageUptodate(page); |
| 43 | SetPageError(page); |
| 44 | } |
| 45 | unlock_page(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 46 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 47 | bio_put(bio); |
| 48 | } |
| 49 | |
| 50 | static void f2fs_write_end_io(struct bio *bio, int err) |
| 51 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 52 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 53 | struct bio_vec *bvec; |
| 54 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 55 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 56 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 57 | struct page *page = bvec->bv_page; |
| 58 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 59 | if (unlikely(err)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 60 | set_page_dirty(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 61 | set_bit(AS_EIO, &page->mapping->flags); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 62 | f2fs_stop_checkpoint(sbi); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 63 | } |
| 64 | end_page_writeback(page); |
| 65 | dec_page_count(sbi, F2FS_WRITEBACK); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 66 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 67 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 68 | if (!get_pages(sbi, F2FS_WRITEBACK) && |
| 69 | !list_empty(&sbi->cp_wait.task_list)) |
| 70 | wake_up(&sbi->cp_wait); |
| 71 | |
| 72 | bio_put(bio); |
| 73 | } |
| 74 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 75 | /* |
| 76 | * Low-level block read/write IO operations. |
| 77 | */ |
| 78 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 79 | int npages, bool is_read) |
| 80 | { |
| 81 | struct bio *bio; |
| 82 | |
| 83 | /* No failure on bio allocation */ |
| 84 | bio = bio_alloc(GFP_NOIO, npages); |
| 85 | |
| 86 | bio->bi_bdev = sbi->sb->s_bdev; |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 87 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 88 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 89 | bio->bi_private = sbi; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 90 | |
| 91 | return bio; |
| 92 | } |
| 93 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 94 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 95 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 96 | struct f2fs_io_info *fio = &io->fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 97 | |
| 98 | if (!io->bio) |
| 99 | return; |
| 100 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 101 | if (is_read_io(fio->rw)) |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 102 | trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 103 | else |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 104 | trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 105 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 106 | submit_bio(fio->rw, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 107 | io->bio = NULL; |
| 108 | } |
| 109 | |
| 110 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 111 | enum page_type type, int rw) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 112 | { |
| 113 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 114 | struct f2fs_bio_info *io; |
| 115 | |
| 116 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 117 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 118 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 119 | |
| 120 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 121 | if (type >= META_FLUSH) { |
| 122 | io->fio.type = META_FLUSH; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 123 | if (test_opt(sbi, NOBARRIER)) |
| 124 | io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO; |
| 125 | else |
| 126 | io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 127 | } |
| 128 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 129 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Fill the locked page with data located in the block address. |
| 134 | * Return unlocked page. |
| 135 | */ |
| 136 | int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 137 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 138 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 139 | struct bio *bio; |
| 140 | |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 141 | trace_f2fs_submit_page_bio(page, fio); |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 142 | f2fs_trace_ios(page, fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 143 | |
| 144 | /* Allocate a new bio */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 145 | bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 146 | |
| 147 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 148 | bio_put(bio); |
| 149 | f2fs_put_page(page, 1); |
| 150 | return -EFAULT; |
| 151 | } |
| 152 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 153 | submit_bio(fio->rw, bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 158 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 159 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 160 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 161 | struct f2fs_bio_info *io; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 162 | bool is_read = is_read_io(fio->rw); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 163 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 164 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 165 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 166 | verify_block_addr(sbi, fio->blk_addr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 167 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 168 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 169 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 170 | if (!is_read) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 171 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 172 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 173 | if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 || |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 174 | io->fio.rw != fio->rw)) |
| 175 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 176 | alloc_new: |
| 177 | if (io->bio == NULL) { |
Jaegeuk Kim | 90a893c | 2014-09-22 16:21:07 -0700 | [diff] [blame] | 178 | int bio_blocks = MAX_BIO_BLOCKS(sbi); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 179 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 180 | io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 181 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) < |
| 185 | PAGE_CACHE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 186 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 187 | goto alloc_new; |
| 188 | } |
| 189 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 190 | io->last_block_in_bio = fio->blk_addr; |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 191 | f2fs_trace_ios(page, fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 192 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 193 | up_write(&io->io_rwsem); |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 194 | trace_f2fs_submit_page_mbio(page, fio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 198 | * Lock ordering for the change of data block address: |
| 199 | * ->data_page |
| 200 | * ->node_page |
| 201 | * update block addresses in the node page |
| 202 | */ |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 203 | void set_data_blkaddr(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 204 | { |
| 205 | struct f2fs_node *rn; |
| 206 | __le32 *addr_array; |
| 207 | struct page *node_page = dn->node_page; |
| 208 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 209 | |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 210 | f2fs_wait_on_page_writeback(node_page, NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 211 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 212 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 213 | |
| 214 | /* Get physical address of data block */ |
| 215 | addr_array = blkaddr_in_node(rn); |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 216 | addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 217 | set_page_dirty(node_page); |
| 218 | } |
| 219 | |
| 220 | int reserve_new_block(struct dnode_of_data *dn) |
| 221 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 222 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 223 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 224 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 225 | return -EPERM; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 226 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 227 | return -ENOSPC; |
| 228 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 229 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 230 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 231 | dn->data_blkaddr = NEW_ADDR; |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 232 | set_data_blkaddr(dn); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 233 | mark_inode_dirty(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 234 | sync_inode_page(dn); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 238 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 239 | { |
| 240 | bool need_put = dn->inode_page ? false : true; |
| 241 | int err; |
| 242 | |
| 243 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 244 | if (err) |
| 245 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 246 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 247 | if (dn->data_blkaddr == NULL_ADDR) |
| 248 | err = reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 249 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 250 | f2fs_put_dnode(dn); |
| 251 | return err; |
| 252 | } |
| 253 | |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 254 | static void f2fs_map_bh(struct super_block *sb, pgoff_t pgofs, |
| 255 | struct extent_info *ei, struct buffer_head *bh_result) |
| 256 | { |
| 257 | unsigned int blkbits = sb->s_blocksize_bits; |
| 258 | size_t count; |
| 259 | |
Jaegeuk Kim | 3402e87 | 2015-03-11 23:27:25 -0400 | [diff] [blame] | 260 | clear_buffer_new(bh_result); |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 261 | map_bh(bh_result, sb, ei->blk + pgofs - ei->fofs); |
| 262 | count = ei->fofs + ei->len - pgofs; |
| 263 | if (count < (UINT_MAX >> blkbits)) |
| 264 | bh_result->b_size = (count << blkbits); |
| 265 | else |
| 266 | bh_result->b_size = UINT_MAX; |
| 267 | } |
| 268 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 269 | static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs, |
| 270 | struct extent_info *ei) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 271 | { |
| 272 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 273 | pgoff_t start_fofs, end_fofs; |
| 274 | block_t start_blkaddr; |
| 275 | |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 276 | read_lock(&fi->ext_lock); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 277 | if (fi->ext.len == 0) { |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 278 | read_unlock(&fi->ext_lock); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 279 | return false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 280 | } |
| 281 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 282 | stat_inc_total_hit(inode->i_sb); |
| 283 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 284 | start_fofs = fi->ext.fofs; |
| 285 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
Chao Yu | 4d0b0bd | 2015-02-05 17:47:25 +0800 | [diff] [blame] | 286 | start_blkaddr = fi->ext.blk; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 287 | |
| 288 | if (pgofs >= start_fofs && pgofs <= end_fofs) { |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 289 | *ei = fi->ext; |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 290 | stat_inc_read_hit(inode->i_sb); |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 291 | read_unlock(&fi->ext_lock); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 292 | return true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 293 | } |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 294 | read_unlock(&fi->ext_lock); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 295 | return false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 298 | static bool update_extent_info(struct inode *inode, pgoff_t fofs, |
| 299 | block_t blkaddr) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 300 | { |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 301 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 302 | pgoff_t start_fofs, end_fofs; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 303 | block_t start_blkaddr, end_blkaddr; |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 304 | int need_update = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 305 | |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 306 | write_lock(&fi->ext_lock); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 307 | |
| 308 | start_fofs = fi->ext.fofs; |
| 309 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
Chao Yu | 4d0b0bd | 2015-02-05 17:47:25 +0800 | [diff] [blame] | 310 | start_blkaddr = fi->ext.blk; |
| 311 | end_blkaddr = fi->ext.blk + fi->ext.len - 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 312 | |
| 313 | /* Drop and initialize the matched extent */ |
| 314 | if (fi->ext.len == 1 && fofs == start_fofs) |
| 315 | fi->ext.len = 0; |
| 316 | |
| 317 | /* Initial extent */ |
| 318 | if (fi->ext.len == 0) { |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 319 | if (blkaddr != NULL_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 320 | fi->ext.fofs = fofs; |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 321 | fi->ext.blk = blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 322 | fi->ext.len = 1; |
| 323 | } |
| 324 | goto end_update; |
| 325 | } |
| 326 | |
Namjae Jeon | 6224da8 | 2013-04-06 14:44:32 +0900 | [diff] [blame] | 327 | /* Front merge */ |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 328 | if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 329 | fi->ext.fofs--; |
Chao Yu | 4d0b0bd | 2015-02-05 17:47:25 +0800 | [diff] [blame] | 330 | fi->ext.blk--; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 331 | fi->ext.len++; |
| 332 | goto end_update; |
| 333 | } |
| 334 | |
| 335 | /* Back merge */ |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 336 | if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 337 | fi->ext.len++; |
| 338 | goto end_update; |
| 339 | } |
| 340 | |
| 341 | /* Split the existing extent */ |
| 342 | if (fi->ext.len > 1 && |
| 343 | fofs >= start_fofs && fofs <= end_fofs) { |
| 344 | if ((end_fofs - fofs) < (fi->ext.len >> 1)) { |
| 345 | fi->ext.len = fofs - start_fofs; |
| 346 | } else { |
| 347 | fi->ext.fofs = fofs + 1; |
Chao Yu | 4d0b0bd | 2015-02-05 17:47:25 +0800 | [diff] [blame] | 348 | fi->ext.blk = start_blkaddr + fofs - start_fofs + 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 349 | fi->ext.len -= fofs - start_fofs + 1; |
| 350 | } |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 351 | } else { |
| 352 | need_update = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 353 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 354 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 355 | /* Finally, if the extent is very fragmented, let's drop the cache. */ |
| 356 | if (fi->ext.len < F2FS_MIN_EXTENT_LEN) { |
| 357 | fi->ext.len = 0; |
| 358 | set_inode_flag(fi, FI_NO_EXTENT); |
| 359 | need_update = true; |
| 360 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 361 | end_update: |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 362 | write_unlock(&fi->ext_lock); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 363 | return need_update; |
| 364 | } |
| 365 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 366 | static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi, |
| 367 | struct extent_tree *et, struct extent_info *ei, |
| 368 | struct rb_node *parent, struct rb_node **p) |
| 369 | { |
| 370 | struct extent_node *en; |
| 371 | |
| 372 | en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC); |
| 373 | if (!en) |
| 374 | return NULL; |
| 375 | |
| 376 | en->ei = *ei; |
| 377 | INIT_LIST_HEAD(&en->list); |
| 378 | |
| 379 | rb_link_node(&en->rb_node, parent, p); |
| 380 | rb_insert_color(&en->rb_node, &et->root); |
| 381 | et->count++; |
| 382 | atomic_inc(&sbi->total_ext_node); |
| 383 | return en; |
| 384 | } |
| 385 | |
| 386 | static void __detach_extent_node(struct f2fs_sb_info *sbi, |
| 387 | struct extent_tree *et, struct extent_node *en) |
| 388 | { |
| 389 | rb_erase(&en->rb_node, &et->root); |
| 390 | et->count--; |
| 391 | atomic_dec(&sbi->total_ext_node); |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 392 | |
| 393 | if (et->cached_en == en) |
| 394 | et->cached_en = NULL; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Chao Yu | 93dfc52 | 2015-03-19 19:24:59 +0800 | [diff] [blame^] | 397 | static struct extent_tree *__find_extent_tree(struct f2fs_sb_info *sbi, |
| 398 | nid_t ino) |
| 399 | { |
| 400 | struct extent_tree *et; |
| 401 | |
| 402 | down_read(&sbi->extent_tree_lock); |
| 403 | et = radix_tree_lookup(&sbi->extent_tree_root, ino); |
| 404 | if (!et) { |
| 405 | up_read(&sbi->extent_tree_lock); |
| 406 | return NULL; |
| 407 | } |
| 408 | atomic_inc(&et->refcount); |
| 409 | up_read(&sbi->extent_tree_lock); |
| 410 | |
| 411 | return et; |
| 412 | } |
| 413 | |
| 414 | static struct extent_tree *__grab_extent_tree(struct inode *inode) |
| 415 | { |
| 416 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 417 | struct extent_tree *et; |
| 418 | nid_t ino = inode->i_ino; |
| 419 | |
| 420 | down_write(&sbi->extent_tree_lock); |
| 421 | et = radix_tree_lookup(&sbi->extent_tree_root, ino); |
| 422 | if (!et) { |
| 423 | et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS); |
| 424 | f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et); |
| 425 | memset(et, 0, sizeof(struct extent_tree)); |
| 426 | et->ino = ino; |
| 427 | et->root = RB_ROOT; |
| 428 | et->cached_en = NULL; |
| 429 | rwlock_init(&et->lock); |
| 430 | atomic_set(&et->refcount, 0); |
| 431 | et->count = 0; |
| 432 | sbi->total_ext_tree++; |
| 433 | } |
| 434 | atomic_inc(&et->refcount); |
| 435 | up_write(&sbi->extent_tree_lock); |
| 436 | |
| 437 | return et; |
| 438 | } |
| 439 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 440 | static struct extent_node *__lookup_extent_tree(struct extent_tree *et, |
| 441 | unsigned int fofs) |
| 442 | { |
| 443 | struct rb_node *node = et->root.rb_node; |
| 444 | struct extent_node *en; |
| 445 | |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 446 | if (et->cached_en) { |
| 447 | struct extent_info *cei = &et->cached_en->ei; |
| 448 | |
| 449 | if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) |
| 450 | return et->cached_en; |
| 451 | } |
| 452 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 453 | while (node) { |
| 454 | en = rb_entry(node, struct extent_node, rb_node); |
| 455 | |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 456 | if (fofs < en->ei.fofs) { |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 457 | node = node->rb_left; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 458 | } else if (fofs >= en->ei.fofs + en->ei.len) { |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 459 | node = node->rb_right; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 460 | } else { |
| 461 | et->cached_en = en; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 462 | return en; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 463 | } |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 464 | } |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi, |
| 469 | struct extent_tree *et, struct extent_node *en) |
| 470 | { |
| 471 | struct extent_node *prev; |
| 472 | struct rb_node *node; |
| 473 | |
| 474 | node = rb_prev(&en->rb_node); |
| 475 | if (!node) |
| 476 | return NULL; |
| 477 | |
| 478 | prev = rb_entry(node, struct extent_node, rb_node); |
| 479 | if (__is_back_mergeable(&en->ei, &prev->ei)) { |
| 480 | en->ei.fofs = prev->ei.fofs; |
| 481 | en->ei.blk = prev->ei.blk; |
| 482 | en->ei.len += prev->ei.len; |
| 483 | __detach_extent_node(sbi, et, prev); |
| 484 | return prev; |
| 485 | } |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi, |
| 490 | struct extent_tree *et, struct extent_node *en) |
| 491 | { |
| 492 | struct extent_node *next; |
| 493 | struct rb_node *node; |
| 494 | |
| 495 | node = rb_next(&en->rb_node); |
| 496 | if (!node) |
| 497 | return NULL; |
| 498 | |
| 499 | next = rb_entry(node, struct extent_node, rb_node); |
| 500 | if (__is_front_mergeable(&en->ei, &next->ei)) { |
| 501 | en->ei.len += next->ei.len; |
| 502 | __detach_extent_node(sbi, et, next); |
| 503 | return next; |
| 504 | } |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi, |
| 509 | struct extent_tree *et, struct extent_info *ei, |
| 510 | struct extent_node **den) |
| 511 | { |
| 512 | struct rb_node **p = &et->root.rb_node; |
| 513 | struct rb_node *parent = NULL; |
| 514 | struct extent_node *en; |
| 515 | |
| 516 | while (*p) { |
| 517 | parent = *p; |
| 518 | en = rb_entry(parent, struct extent_node, rb_node); |
| 519 | |
| 520 | if (ei->fofs < en->ei.fofs) { |
| 521 | if (__is_front_mergeable(ei, &en->ei)) { |
| 522 | f2fs_bug_on(sbi, !den); |
| 523 | en->ei.fofs = ei->fofs; |
| 524 | en->ei.blk = ei->blk; |
| 525 | en->ei.len += ei->len; |
| 526 | *den = __try_back_merge(sbi, et, en); |
| 527 | return en; |
| 528 | } |
| 529 | p = &(*p)->rb_left; |
| 530 | } else if (ei->fofs >= en->ei.fofs + en->ei.len) { |
| 531 | if (__is_back_mergeable(ei, &en->ei)) { |
| 532 | f2fs_bug_on(sbi, !den); |
| 533 | en->ei.len += ei->len; |
| 534 | *den = __try_front_merge(sbi, et, en); |
| 535 | return en; |
| 536 | } |
| 537 | p = &(*p)->rb_right; |
| 538 | } else { |
| 539 | f2fs_bug_on(sbi, 1); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return __attach_extent_node(sbi, et, ei, parent, p); |
| 544 | } |
| 545 | |
| 546 | static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi, |
| 547 | struct extent_tree *et, bool free_all) |
| 548 | { |
| 549 | struct rb_node *node, *next; |
| 550 | struct extent_node *en; |
| 551 | unsigned int count = et->count; |
| 552 | |
| 553 | node = rb_first(&et->root); |
| 554 | while (node) { |
| 555 | next = rb_next(node); |
| 556 | en = rb_entry(node, struct extent_node, rb_node); |
| 557 | |
| 558 | if (free_all) { |
| 559 | spin_lock(&sbi->extent_lock); |
| 560 | if (!list_empty(&en->list)) |
| 561 | list_del_init(&en->list); |
| 562 | spin_unlock(&sbi->extent_lock); |
| 563 | } |
| 564 | |
| 565 | if (free_all || list_empty(&en->list)) { |
| 566 | __detach_extent_node(sbi, et, en); |
| 567 | kmem_cache_free(extent_node_slab, en); |
| 568 | } |
| 569 | node = next; |
| 570 | } |
| 571 | |
| 572 | return count - et->count; |
| 573 | } |
| 574 | |
| 575 | static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs, |
| 576 | struct extent_info *ei) |
| 577 | { |
| 578 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 579 | struct extent_tree *et; |
| 580 | struct extent_node *en; |
| 581 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 582 | trace_f2fs_lookup_extent_tree_start(inode, pgofs); |
| 583 | |
Chao Yu | 93dfc52 | 2015-03-19 19:24:59 +0800 | [diff] [blame^] | 584 | et = __find_extent_tree(sbi, inode->i_ino); |
| 585 | if (!et) |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 586 | return false; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 587 | |
| 588 | read_lock(&et->lock); |
| 589 | en = __lookup_extent_tree(et, pgofs); |
| 590 | if (en) { |
| 591 | *ei = en->ei; |
| 592 | spin_lock(&sbi->extent_lock); |
| 593 | if (!list_empty(&en->list)) |
| 594 | list_move_tail(&en->list, &sbi->extent_list); |
| 595 | spin_unlock(&sbi->extent_lock); |
| 596 | stat_inc_read_hit(sbi->sb); |
| 597 | } |
| 598 | stat_inc_total_hit(sbi->sb); |
| 599 | read_unlock(&et->lock); |
| 600 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 601 | trace_f2fs_lookup_extent_tree_end(inode, pgofs, en); |
| 602 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 603 | atomic_dec(&et->refcount); |
| 604 | return en ? true : false; |
| 605 | } |
| 606 | |
| 607 | static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs, |
| 608 | block_t blkaddr) |
| 609 | { |
| 610 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 611 | struct extent_tree *et; |
| 612 | struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL; |
| 613 | struct extent_node *den = NULL; |
| 614 | struct extent_info ei, dei; |
| 615 | unsigned int endofs; |
| 616 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 617 | trace_f2fs_update_extent_tree(inode, fofs, blkaddr); |
| 618 | |
Chao Yu | 93dfc52 | 2015-03-19 19:24:59 +0800 | [diff] [blame^] | 619 | et = __grab_extent_tree(inode); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 620 | |
| 621 | write_lock(&et->lock); |
| 622 | |
| 623 | /* 1. lookup and remove existing extent info in cache */ |
| 624 | en = __lookup_extent_tree(et, fofs); |
| 625 | if (!en) |
| 626 | goto update_extent; |
| 627 | |
| 628 | dei = en->ei; |
| 629 | __detach_extent_node(sbi, et, en); |
| 630 | |
| 631 | /* 2. if extent can be split more, split and insert the left part */ |
| 632 | if (dei.len > 1) { |
| 633 | /* insert left part of split extent into cache */ |
| 634 | if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) { |
| 635 | set_extent_info(&ei, dei.fofs, dei.blk, |
| 636 | fofs - dei.fofs); |
| 637 | en1 = __insert_extent_tree(sbi, et, &ei, NULL); |
| 638 | } |
| 639 | |
| 640 | /* insert right part of split extent into cache */ |
| 641 | endofs = dei.fofs + dei.len - 1; |
| 642 | if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) { |
| 643 | set_extent_info(&ei, fofs + 1, |
| 644 | fofs - dei.fofs + dei.blk, endofs - fofs); |
| 645 | en2 = __insert_extent_tree(sbi, et, &ei, NULL); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | update_extent: |
| 650 | /* 3. update extent in extent cache */ |
| 651 | if (blkaddr) { |
| 652 | set_extent_info(&ei, fofs, blkaddr, 1); |
| 653 | en3 = __insert_extent_tree(sbi, et, &ei, &den); |
| 654 | } |
| 655 | |
| 656 | /* 4. update in global extent list */ |
| 657 | spin_lock(&sbi->extent_lock); |
| 658 | if (en && !list_empty(&en->list)) |
| 659 | list_del(&en->list); |
| 660 | /* |
| 661 | * en1 and en2 split from en, they will become more and more smaller |
| 662 | * fragments after splitting several times. So if the length is smaller |
| 663 | * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree. |
| 664 | */ |
| 665 | if (en1) |
| 666 | list_add_tail(&en1->list, &sbi->extent_list); |
| 667 | if (en2) |
| 668 | list_add_tail(&en2->list, &sbi->extent_list); |
| 669 | if (en3) { |
| 670 | if (list_empty(&en3->list)) |
| 671 | list_add_tail(&en3->list, &sbi->extent_list); |
| 672 | else |
| 673 | list_move_tail(&en3->list, &sbi->extent_list); |
| 674 | } |
| 675 | if (den && !list_empty(&den->list)) |
| 676 | list_del(&den->list); |
| 677 | spin_unlock(&sbi->extent_lock); |
| 678 | |
| 679 | /* 5. release extent node */ |
| 680 | if (en) |
| 681 | kmem_cache_free(extent_node_slab, en); |
| 682 | if (den) |
| 683 | kmem_cache_free(extent_node_slab, den); |
| 684 | |
| 685 | write_unlock(&et->lock); |
| 686 | atomic_dec(&et->refcount); |
| 687 | } |
| 688 | |
| 689 | void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink) |
| 690 | { |
| 691 | struct extent_tree *treevec[EXT_TREE_VEC_SIZE]; |
| 692 | struct extent_node *en, *tmp; |
| 693 | unsigned long ino = F2FS_ROOT_INO(sbi); |
| 694 | struct radix_tree_iter iter; |
| 695 | void **slot; |
| 696 | unsigned int found; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 697 | unsigned int node_cnt = 0, tree_cnt = 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 698 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 699 | if (!test_opt(sbi, EXTENT_CACHE)) |
| 700 | return; |
| 701 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 702 | if (available_free_memory(sbi, EXTENT_CACHE)) |
| 703 | return; |
| 704 | |
| 705 | spin_lock(&sbi->extent_lock); |
| 706 | list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) { |
| 707 | if (!nr_shrink--) |
| 708 | break; |
| 709 | list_del_init(&en->list); |
| 710 | } |
| 711 | spin_unlock(&sbi->extent_lock); |
| 712 | |
| 713 | down_read(&sbi->extent_tree_lock); |
| 714 | while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root, |
| 715 | (void **)treevec, ino, EXT_TREE_VEC_SIZE))) { |
| 716 | unsigned i; |
| 717 | |
| 718 | ino = treevec[found - 1]->ino + 1; |
| 719 | for (i = 0; i < found; i++) { |
| 720 | struct extent_tree *et = treevec[i]; |
| 721 | |
| 722 | atomic_inc(&et->refcount); |
| 723 | write_lock(&et->lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 724 | node_cnt += __free_extent_tree(sbi, et, false); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 725 | write_unlock(&et->lock); |
| 726 | atomic_dec(&et->refcount); |
| 727 | } |
| 728 | } |
| 729 | up_read(&sbi->extent_tree_lock); |
| 730 | |
| 731 | down_write(&sbi->extent_tree_lock); |
| 732 | radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter, |
| 733 | F2FS_ROOT_INO(sbi)) { |
| 734 | struct extent_tree *et = (struct extent_tree *)*slot; |
| 735 | |
| 736 | if (!atomic_read(&et->refcount) && !et->count) { |
| 737 | radix_tree_delete(&sbi->extent_tree_root, et->ino); |
| 738 | kmem_cache_free(extent_tree_slab, et); |
| 739 | sbi->total_ext_tree--; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 740 | tree_cnt++; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | up_write(&sbi->extent_tree_lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 744 | |
| 745 | trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void f2fs_destroy_extent_tree(struct inode *inode) |
| 749 | { |
| 750 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 751 | struct extent_tree *et; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 752 | unsigned int node_cnt = 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 753 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 754 | if (!test_opt(sbi, EXTENT_CACHE)) |
| 755 | return; |
| 756 | |
Chao Yu | 93dfc52 | 2015-03-19 19:24:59 +0800 | [diff] [blame^] | 757 | et = __find_extent_tree(sbi, inode->i_ino); |
| 758 | if (!et) |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 759 | goto out; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 760 | |
| 761 | /* free all extent info belong to this extent tree */ |
| 762 | write_lock(&et->lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 763 | node_cnt = __free_extent_tree(sbi, et, true); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 764 | write_unlock(&et->lock); |
| 765 | |
| 766 | atomic_dec(&et->refcount); |
| 767 | |
| 768 | /* try to find and delete extent tree entry in radix tree */ |
| 769 | down_write(&sbi->extent_tree_lock); |
| 770 | et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino); |
| 771 | if (!et) { |
| 772 | up_write(&sbi->extent_tree_lock); |
| 773 | goto out; |
| 774 | } |
| 775 | f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count); |
| 776 | radix_tree_delete(&sbi->extent_tree_root, inode->i_ino); |
| 777 | kmem_cache_free(extent_tree_slab, et); |
| 778 | sbi->total_ext_tree--; |
| 779 | up_write(&sbi->extent_tree_lock); |
| 780 | out: |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 781 | trace_f2fs_destroy_extent_tree(inode, node_cnt); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 782 | return; |
| 783 | } |
| 784 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 785 | static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 786 | struct extent_info *ei) |
| 787 | { |
Chao Yu | 91c5d9b | 2015-02-05 18:02:44 +0800 | [diff] [blame] | 788 | if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) |
| 789 | return false; |
| 790 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 791 | if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE)) |
| 792 | return f2fs_lookup_extent_tree(inode, pgofs, ei); |
| 793 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 794 | return lookup_extent_info(inode, pgofs, ei); |
| 795 | } |
| 796 | |
| 797 | void f2fs_update_extent_cache(struct dnode_of_data *dn) |
| 798 | { |
| 799 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 800 | pgoff_t fofs; |
| 801 | |
| 802 | f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR); |
| 803 | |
Chao Yu | 91c5d9b | 2015-02-05 18:02:44 +0800 | [diff] [blame] | 804 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 805 | return; |
| 806 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 807 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 808 | dn->ofs_in_node; |
| 809 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 810 | if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE)) |
| 811 | return f2fs_update_extent_tree(dn->inode, fofs, |
| 812 | dn->data_blkaddr); |
| 813 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 814 | if (update_extent_info(dn->inode, fofs, dn->data_blkaddr)) |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 815 | sync_inode_page(dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 816 | } |
| 817 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 818 | struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 819 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 820 | struct address_space *mapping = inode->i_mapping; |
| 821 | struct dnode_of_data dn; |
| 822 | struct page *page; |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 823 | struct extent_info ei; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 824 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 825 | struct f2fs_io_info fio = { |
| 826 | .type = DATA, |
| 827 | .rw = sync ? READ_SYNC : READA, |
| 828 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 829 | |
Jaegeuk Kim | b7f204c | 2015-02-25 19:54:48 -0800 | [diff] [blame] | 830 | /* |
| 831 | * If sync is false, it needs to check its block allocation. |
| 832 | * This is need and triggered by two flows: |
| 833 | * gc and truncate_partial_data_page. |
| 834 | */ |
| 835 | if (!sync) |
| 836 | goto search; |
| 837 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 838 | page = find_get_page(mapping, index); |
| 839 | if (page && PageUptodate(page)) |
| 840 | return page; |
| 841 | f2fs_put_page(page, 0); |
Jaegeuk Kim | b7f204c | 2015-02-25 19:54:48 -0800 | [diff] [blame] | 842 | search: |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 843 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 844 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 845 | goto got_it; |
| 846 | } |
| 847 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 848 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 849 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 850 | if (err) |
| 851 | return ERR_PTR(err); |
| 852 | f2fs_put_dnode(&dn); |
| 853 | |
| 854 | if (dn.data_blkaddr == NULL_ADDR) |
| 855 | return ERR_PTR(-ENOENT); |
| 856 | |
| 857 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 858 | if (unlikely(dn.data_blkaddr == NEW_ADDR)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 859 | return ERR_PTR(-EINVAL); |
| 860 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 861 | got_it: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 862 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 863 | if (!page) |
| 864 | return ERR_PTR(-ENOMEM); |
| 865 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 866 | if (PageUptodate(page)) { |
| 867 | unlock_page(page); |
| 868 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 869 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 870 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 871 | fio.blk_addr = dn.data_blkaddr; |
| 872 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Chao Yu | 1069bbf | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 873 | if (err) |
| 874 | return ERR_PTR(err); |
| 875 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 876 | if (sync) { |
| 877 | wait_on_page_locked(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 878 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 879 | f2fs_put_page(page, 0); |
| 880 | return ERR_PTR(-EIO); |
| 881 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 882 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 883 | return page; |
| 884 | } |
| 885 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 886 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 887 | * If it tries to access a hole, return an error. |
| 888 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 889 | * whether this page exists or not. |
| 890 | */ |
| 891 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 892 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 893 | struct address_space *mapping = inode->i_mapping; |
| 894 | struct dnode_of_data dn; |
| 895 | struct page *page; |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 896 | struct extent_info ei; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 897 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 898 | struct f2fs_io_info fio = { |
| 899 | .type = DATA, |
| 900 | .rw = READ_SYNC, |
| 901 | }; |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 902 | repeat: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 903 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 904 | if (!page) |
| 905 | return ERR_PTR(-ENOMEM); |
| 906 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 907 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 908 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 909 | goto got_it; |
| 910 | } |
| 911 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 912 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 913 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 914 | if (err) { |
| 915 | f2fs_put_page(page, 1); |
| 916 | return ERR_PTR(err); |
| 917 | } |
| 918 | f2fs_put_dnode(&dn); |
| 919 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 920 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 921 | f2fs_put_page(page, 1); |
| 922 | return ERR_PTR(-ENOENT); |
| 923 | } |
| 924 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 925 | got_it: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 926 | if (PageUptodate(page)) |
| 927 | return page; |
| 928 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 929 | /* |
| 930 | * A new dentry page is allocated but not able to be written, since its |
| 931 | * new inode page couldn't be allocated due to -ENOSPC. |
| 932 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 933 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 934 | */ |
| 935 | if (dn.data_blkaddr == NEW_ADDR) { |
| 936 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 937 | SetPageUptodate(page); |
| 938 | return page; |
| 939 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 940 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 941 | fio.blk_addr = dn.data_blkaddr; |
| 942 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 943 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 944 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 945 | |
| 946 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 947 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 948 | f2fs_put_page(page, 1); |
| 949 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 950 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 951 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 952 | f2fs_put_page(page, 1); |
| 953 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 954 | } |
| 955 | return page; |
| 956 | } |
| 957 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 958 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 959 | * Caller ensures that this data page is never allocated. |
| 960 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 961 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 962 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 963 | * f2fs_unlock_op(). |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 964 | * Note that, ipage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 965 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 966 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 967 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 968 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 969 | struct address_space *mapping = inode->i_mapping; |
| 970 | struct page *page; |
| 971 | struct dnode_of_data dn; |
| 972 | int err; |
| 973 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 974 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 975 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 976 | if (err) |
| 977 | return ERR_PTR(err); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 978 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 979 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 980 | if (!page) { |
| 981 | err = -ENOMEM; |
| 982 | goto put_err; |
| 983 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 984 | |
| 985 | if (PageUptodate(page)) |
| 986 | return page; |
| 987 | |
| 988 | if (dn.data_blkaddr == NEW_ADDR) { |
| 989 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 990 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 991 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 992 | struct f2fs_io_info fio = { |
| 993 | .type = DATA, |
| 994 | .rw = READ_SYNC, |
| 995 | .blk_addr = dn.data_blkaddr, |
| 996 | }; |
| 997 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 998 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 999 | goto put_err; |
| 1000 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1001 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1002 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1003 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 1004 | err = -EIO; |
| 1005 | goto put_err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1006 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1007 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1008 | f2fs_put_page(page, 1); |
| 1009 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1010 | } |
| 1011 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1012 | |
| 1013 | if (new_i_size && |
| 1014 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 1015 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 1016 | /* Only the directory inode sets new_i_size */ |
| 1017 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1018 | } |
| 1019 | return page; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 1020 | |
| 1021 | put_err: |
| 1022 | f2fs_put_dnode(&dn); |
| 1023 | return ERR_PTR(err); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1024 | } |
| 1025 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1026 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 1027 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1028 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1029 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1030 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1031 | struct node_info ni; |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1032 | int seg = CURSEG_WARM_DATA; |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1033 | pgoff_t fofs; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1034 | |
| 1035 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 1036 | return -EPERM; |
| 1037 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 1038 | return -ENOSPC; |
| 1039 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1040 | get_node_info(sbi, dn->nid, &ni); |
| 1041 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 1042 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1043 | if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page) |
| 1044 | seg = CURSEG_DIRECT_IO; |
| 1045 | |
| 1046 | allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1047 | |
| 1048 | /* direct IO doesn't use extent cache to maximize the performance */ |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 1049 | set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1050 | |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1051 | /* update i_size */ |
| 1052 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 1053 | dn->ofs_in_node; |
| 1054 | if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT)) |
| 1055 | i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT)); |
| 1056 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1060 | static void __allocate_data_blocks(struct inode *inode, loff_t offset, |
| 1061 | size_t count) |
| 1062 | { |
| 1063 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1064 | struct dnode_of_data dn; |
| 1065 | u64 start = F2FS_BYTES_TO_BLK(offset); |
| 1066 | u64 len = F2FS_BYTES_TO_BLK(count); |
| 1067 | bool allocated; |
| 1068 | u64 end_offset; |
| 1069 | |
| 1070 | while (len) { |
| 1071 | f2fs_balance_fs(sbi); |
| 1072 | f2fs_lock_op(sbi); |
| 1073 | |
| 1074 | /* When reading holes, we need its node page */ |
| 1075 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1076 | if (get_dnode_of_data(&dn, start, ALLOC_NODE)) |
| 1077 | goto out; |
| 1078 | |
| 1079 | allocated = false; |
| 1080 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 1081 | |
| 1082 | while (dn.ofs_in_node < end_offset && len) { |
Chao Yu | d6d4f1c | 2015-03-12 17:04:24 +0800 | [diff] [blame] | 1083 | block_t blkaddr; |
| 1084 | |
| 1085 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 1086 | if (blkaddr == NULL_ADDR) { |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1087 | if (__allocate_data_block(&dn)) |
| 1088 | goto sync_out; |
| 1089 | allocated = true; |
| 1090 | } |
| 1091 | len--; |
| 1092 | start++; |
| 1093 | dn.ofs_in_node++; |
| 1094 | } |
| 1095 | |
| 1096 | if (allocated) |
| 1097 | sync_inode_page(&dn); |
| 1098 | |
| 1099 | f2fs_put_dnode(&dn); |
| 1100 | f2fs_unlock_op(sbi); |
| 1101 | } |
| 1102 | return; |
| 1103 | |
| 1104 | sync_out: |
| 1105 | if (allocated) |
| 1106 | sync_inode_page(&dn); |
| 1107 | f2fs_put_dnode(&dn); |
| 1108 | out: |
| 1109 | f2fs_unlock_op(sbi); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1113 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 1114 | * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh. |
| 1115 | * If original data blocks are allocated, then give them to blockdev. |
| 1116 | * Otherwise, |
| 1117 | * a. preallocate requested block addresses |
| 1118 | * b. do not use extent cache for better performance |
| 1119 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1120 | */ |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1121 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 1122 | struct buffer_head *bh_result, int create, bool fiemap) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1123 | { |
| 1124 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 1125 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 1126 | struct dnode_of_data dn; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1127 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 1128 | pgoff_t pgofs, end_offset; |
| 1129 | int err = 0, ofs = 1; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1130 | struct extent_info ei; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1131 | bool allocated = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1132 | |
| 1133 | /* Get the page offset from the block offset(iblock) */ |
| 1134 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 1135 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 1136 | if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1137 | f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1138 | goto out; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1139 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1140 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1141 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1142 | f2fs_lock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1143 | |
| 1144 | /* When reading holes, we need its node page */ |
| 1145 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1146 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1147 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1148 | if (err == -ENOENT) |
| 1149 | err = 0; |
| 1150 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 1151 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1152 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1153 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1154 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1155 | if (dn.data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | 3402e87 | 2015-03-11 23:27:25 -0400 | [diff] [blame] | 1156 | clear_buffer_new(bh_result); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1157 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1158 | } else if (create) { |
| 1159 | err = __allocate_data_block(&dn); |
| 1160 | if (err) |
| 1161 | goto put_out; |
| 1162 | allocated = true; |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 1163 | set_buffer_new(bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1164 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 1165 | } else { |
| 1166 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1167 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1168 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 1169 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1170 | bh_result->b_size = (((size_t)1) << blkbits); |
| 1171 | dn.ofs_in_node++; |
| 1172 | pgofs++; |
| 1173 | |
| 1174 | get_next: |
| 1175 | if (dn.ofs_in_node >= end_offset) { |
| 1176 | if (allocated) |
| 1177 | sync_inode_page(&dn); |
| 1178 | allocated = false; |
| 1179 | f2fs_put_dnode(&dn); |
| 1180 | |
| 1181 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1182 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1183 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1184 | if (err == -ENOENT) |
| 1185 | err = 0; |
| 1186 | goto unlock_out; |
| 1187 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1188 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1189 | goto put_out; |
| 1190 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 1191 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (maxblocks > (bh_result->b_size >> blkbits)) { |
| 1195 | block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 1196 | if (blkaddr == NULL_ADDR && create) { |
| 1197 | err = __allocate_data_block(&dn); |
| 1198 | if (err) |
| 1199 | goto sync_out; |
| 1200 | allocated = true; |
Jaegeuk Kim | 3402e87 | 2015-03-11 23:27:25 -0400 | [diff] [blame] | 1201 | set_buffer_new(bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1202 | blkaddr = dn.data_blkaddr; |
| 1203 | } |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1204 | /* Give more consecutive addresses for the readahead */ |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1205 | if (blkaddr == (bh_result->b_blocknr + ofs)) { |
| 1206 | ofs++; |
| 1207 | dn.ofs_in_node++; |
| 1208 | pgofs++; |
| 1209 | bh_result->b_size += (((size_t)1) << blkbits); |
| 1210 | goto get_next; |
| 1211 | } |
| 1212 | } |
| 1213 | sync_out: |
| 1214 | if (allocated) |
| 1215 | sync_inode_page(&dn); |
| 1216 | put_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1217 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1218 | unlock_out: |
| 1219 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1220 | f2fs_unlock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1221 | out: |
| 1222 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
| 1223 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1224 | } |
| 1225 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1226 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 1227 | struct buffer_head *bh_result, int create) |
| 1228 | { |
| 1229 | return __get_data_block(inode, iblock, bh_result, create, false); |
| 1230 | } |
| 1231 | |
| 1232 | static int get_data_block_fiemap(struct inode *inode, sector_t iblock, |
| 1233 | struct buffer_head *bh_result, int create) |
| 1234 | { |
| 1235 | return __get_data_block(inode, iblock, bh_result, create, true); |
| 1236 | } |
| 1237 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1238 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1239 | u64 start, u64 len) |
| 1240 | { |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1241 | return generic_block_fiemap(inode, fieinfo, |
| 1242 | start, len, get_data_block_fiemap); |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1243 | } |
| 1244 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1245 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 1246 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1247 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1248 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1249 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 1250 | trace_f2fs_readpage(page, DATA); |
| 1251 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1252 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1253 | if (f2fs_has_inline_data(inode)) |
| 1254 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1255 | if (ret == -EAGAIN) |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1256 | ret = mpage_readpage(page, get_data_block); |
| 1257 | |
| 1258 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int f2fs_read_data_pages(struct file *file, |
| 1262 | struct address_space *mapping, |
| 1263 | struct list_head *pages, unsigned nr_pages) |
| 1264 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1265 | struct inode *inode = file->f_mapping->host; |
| 1266 | |
| 1267 | /* If the file has inline data, skip readpages */ |
| 1268 | if (f2fs_has_inline_data(inode)) |
| 1269 | return 0; |
| 1270 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1271 | return mpage_readpages(mapping, pages, nr_pages, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1272 | } |
| 1273 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1274 | int do_write_data_page(struct page *page, struct f2fs_io_info *fio) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1275 | { |
| 1276 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1277 | struct dnode_of_data dn; |
| 1278 | int err = 0; |
| 1279 | |
| 1280 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 1281 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1282 | if (err) |
| 1283 | return err; |
| 1284 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1285 | fio->blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1286 | |
| 1287 | /* This page is already truncated */ |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1288 | if (fio->blk_addr == NULL_ADDR) { |
| 1289 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1290 | goto out_writepage; |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1291 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1292 | |
| 1293 | set_page_writeback(page); |
| 1294 | |
| 1295 | /* |
| 1296 | * If current allocation needs SSR, |
| 1297 | * it had better in-place writes for updated data. |
| 1298 | */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1299 | if (unlikely(fio->blk_addr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 1300 | !is_cold_data(page) && |
| 1301 | need_inplace_update(inode))) { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1302 | rewrite_data_page(page, fio); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1303 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 1304 | trace_f2fs_do_write_data_page(page, IPU); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1305 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1306 | write_data_page(page, &dn, fio); |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 1307 | set_data_blkaddr(&dn); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 1308 | f2fs_update_extent_cache(&dn); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 1309 | trace_f2fs_do_write_data_page(page, OPU); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1310 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 1311 | if (page->index == 0) |
| 1312 | set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1313 | } |
| 1314 | out_writepage: |
| 1315 | f2fs_put_dnode(&dn); |
| 1316 | return err; |
| 1317 | } |
| 1318 | |
| 1319 | static int f2fs_write_data_page(struct page *page, |
| 1320 | struct writeback_control *wbc) |
| 1321 | { |
| 1322 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1323 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1324 | loff_t i_size = i_size_read(inode); |
| 1325 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 1326 | >> PAGE_CACHE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1327 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1328 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1329 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1330 | struct f2fs_io_info fio = { |
| 1331 | .type = DATA, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 1332 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1333 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1334 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1335 | trace_f2fs_writepage(page, DATA); |
| 1336 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1337 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1338 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * If the offset is out-of-range of file size, |
| 1342 | * this page does not have to be written to disk. |
| 1343 | */ |
| 1344 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1345 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1346 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1347 | |
| 1348 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1349 | write: |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1350 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1351 | goto redirty_out; |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1352 | if (f2fs_is_drop_cache(inode)) |
| 1353 | goto out; |
| 1354 | if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim && |
| 1355 | available_free_memory(sbi, BASE_CHECK)) |
| 1356 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1357 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1358 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1359 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1360 | if (unlikely(f2fs_cp_error(sbi))) |
| 1361 | goto redirty_out; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1362 | err = do_write_data_page(page, &fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1363 | goto done; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1364 | } |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1365 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1366 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1367 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1368 | SetPageError(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1369 | goto out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1372 | if (!wbc->for_reclaim) |
| 1373 | need_balance_fs = true; |
| 1374 | else if (has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1375 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1376 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1377 | err = -EAGAIN; |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1378 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1379 | if (f2fs_has_inline_data(inode)) |
| 1380 | err = f2fs_write_inline_data(inode, page); |
| 1381 | if (err == -EAGAIN) |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1382 | err = do_write_data_page(page, &fio); |
| 1383 | f2fs_unlock_op(sbi); |
| 1384 | done: |
| 1385 | if (err && err != -ENOENT) |
| 1386 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1387 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1388 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1389 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1390 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1391 | if (err) |
| 1392 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1393 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1394 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1395 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 2aea39e | 2014-04-24 09:49:52 +0900 | [diff] [blame] | 1396 | if (wbc->for_reclaim) |
| 1397 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1398 | return 0; |
| 1399 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1400 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1401 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1402 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1403 | } |
| 1404 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1405 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 1406 | void *data) |
| 1407 | { |
| 1408 | struct address_space *mapping = data; |
| 1409 | int ret = mapping->a_ops->writepage(page, wbc); |
| 1410 | mapping_set_error(mapping, ret); |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 1414 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1415 | struct writeback_control *wbc) |
| 1416 | { |
| 1417 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1418 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1419 | int ret; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1420 | long diff; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1421 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 1422 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 1423 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 1424 | /* deal with chardevs and other special file */ |
| 1425 | if (!mapping->a_ops->writepage) |
| 1426 | return 0; |
| 1427 | |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 1428 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1429 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1430 | available_free_memory(sbi, DIRTY_DENTS)) |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1431 | goto skip_write; |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 1432 | |
Jaegeuk Kim | d5669f7 | 2015-02-27 13:37:39 -0800 | [diff] [blame] | 1433 | /* during POR, we don't need to trigger writepage at all. */ |
| 1434 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1435 | goto skip_write; |
| 1436 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1437 | diff = nr_pages_to_write(sbi, DATA, wbc); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1438 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1439 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1440 | |
| 1441 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1442 | |
| 1443 | remove_dirty_dir_inode(inode); |
| 1444 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1445 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1446 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1447 | |
| 1448 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1449 | wbc->pages_skipped += get_dirty_pages(inode); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1450 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1451 | } |
| 1452 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1453 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 1454 | { |
| 1455 | struct inode *inode = mapping->host; |
| 1456 | |
| 1457 | if (to > inode->i_size) { |
| 1458 | truncate_pagecache(inode, inode->i_size); |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 1459 | truncate_blocks(inode, inode->i_size, true); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1463 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 1464 | loff_t pos, unsigned len, unsigned flags, |
| 1465 | struct page **pagep, void **fsdata) |
| 1466 | { |
| 1467 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1468 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1469 | struct page *page, *ipage; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1470 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 1471 | struct dnode_of_data dn; |
| 1472 | int err = 0; |
| 1473 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 1474 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 1475 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1476 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1477 | |
| 1478 | /* |
| 1479 | * We should check this at this moment to avoid deadlock on inode page |
| 1480 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1481 | * lock_page(page #0) -> lock_page(inode_page) |
| 1482 | */ |
| 1483 | if (index != 0) { |
| 1484 | err = f2fs_convert_inline_inode(inode); |
| 1485 | if (err) |
| 1486 | goto fail; |
| 1487 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1488 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1489 | page = grab_cache_page_write_begin(mapping, index, flags); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1490 | if (!page) { |
| 1491 | err = -ENOMEM; |
| 1492 | goto fail; |
| 1493 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 1494 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1495 | *pagep = page; |
| 1496 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 1497 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1498 | |
| 1499 | /* check inline_data */ |
| 1500 | ipage = get_node_page(sbi, inode->i_ino); |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1501 | if (IS_ERR(ipage)) { |
| 1502 | err = PTR_ERR(ipage); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1503 | goto unlock_fail; |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1504 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1505 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1506 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1507 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1508 | if (f2fs_has_inline_data(inode)) { |
| 1509 | if (pos + len <= MAX_INLINE_DATA) { |
| 1510 | read_inline_data(page, ipage); |
| 1511 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 1512 | sync_inode_page(&dn); |
| 1513 | goto put_next; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1514 | } |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1515 | err = f2fs_convert_inline_page(&dn, page); |
| 1516 | if (err) |
| 1517 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1518 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1519 | err = f2fs_reserve_block(&dn, index); |
| 1520 | if (err) |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1521 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1522 | put_next: |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1523 | f2fs_put_dnode(&dn); |
| 1524 | f2fs_unlock_op(sbi); |
| 1525 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1526 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 1527 | return 0; |
| 1528 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1529 | f2fs_wait_on_page_writeback(page, DATA); |
| 1530 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1531 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1532 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 1533 | unsigned end = start + len; |
| 1534 | |
| 1535 | /* Reading beyond i_size is simple: memset to zero */ |
| 1536 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1537 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1538 | } |
| 1539 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1540 | if (dn.data_blkaddr == NEW_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1541 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 1542 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1543 | struct f2fs_io_info fio = { |
| 1544 | .type = DATA, |
| 1545 | .rw = READ_SYNC, |
| 1546 | .blk_addr = dn.data_blkaddr, |
| 1547 | }; |
| 1548 | err = f2fs_submit_page_bio(sbi, page, &fio); |
Jan Kara | 9234f31 | 2014-10-22 15:21:47 +0200 | [diff] [blame] | 1549 | if (err) |
| 1550 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1551 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1552 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1553 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1554 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1555 | err = -EIO; |
| 1556 | goto fail; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1557 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1558 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1559 | f2fs_put_page(page, 1); |
| 1560 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1561 | } |
| 1562 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1563 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1564 | SetPageUptodate(page); |
| 1565 | clear_cold_data(page); |
| 1566 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1567 | |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1568 | put_fail: |
| 1569 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1570 | unlock_fail: |
| 1571 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1572 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1573 | fail: |
| 1574 | f2fs_write_failed(mapping, pos + len); |
| 1575 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1578 | static int f2fs_write_end(struct file *file, |
| 1579 | struct address_space *mapping, |
| 1580 | loff_t pos, unsigned len, unsigned copied, |
| 1581 | struct page *page, void *fsdata) |
| 1582 | { |
| 1583 | struct inode *inode = page->mapping->host; |
| 1584 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 1585 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1586 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1587 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1588 | |
| 1589 | if (pos + copied > i_size_read(inode)) { |
| 1590 | i_size_write(inode, pos + copied); |
| 1591 | mark_inode_dirty(inode); |
| 1592 | update_inode_page(inode); |
| 1593 | } |
| 1594 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 1595 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1596 | return copied; |
| 1597 | } |
| 1598 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1599 | static int check_direct_IO(struct inode *inode, int rw, |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1600 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1601 | { |
| 1602 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1603 | |
| 1604 | if (rw == READ) |
| 1605 | return 0; |
| 1606 | |
| 1607 | if (offset & blocksize_mask) |
| 1608 | return -EINVAL; |
| 1609 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1610 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1611 | return -EINVAL; |
| 1612 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1613 | return 0; |
| 1614 | } |
| 1615 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1616 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 1617 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1618 | { |
| 1619 | struct file *file = iocb->ki_filp; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1620 | struct address_space *mapping = file->f_mapping; |
| 1621 | struct inode *inode = mapping->host; |
| 1622 | size_t count = iov_iter_count(iter); |
| 1623 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1624 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1625 | /* we don't need to use inline_data strictly */ |
| 1626 | if (f2fs_has_inline_data(inode)) { |
| 1627 | err = f2fs_convert_inline_inode(inode); |
| 1628 | if (err) |
| 1629 | return err; |
| 1630 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1631 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1632 | if (check_direct_IO(inode, rw, iter, offset)) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1633 | return 0; |
| 1634 | |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1635 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
| 1636 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1637 | if (rw & WRITE) |
| 1638 | __allocate_data_blocks(inode, offset, count); |
| 1639 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1640 | err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block); |
| 1641 | if (err < 0 && (rw & WRITE)) |
| 1642 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1643 | |
| 1644 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
| 1645 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1646 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1647 | } |
| 1648 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1649 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 1650 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1651 | { |
| 1652 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1653 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1654 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1655 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 1656 | (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1657 | return; |
| 1658 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1659 | if (PageDirty(page)) { |
| 1660 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 1661 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 1662 | else if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 1663 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1664 | else |
| 1665 | inode_dec_dirty_pages(inode); |
| 1666 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1667 | ClearPagePrivate(page); |
| 1668 | } |
| 1669 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1670 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1671 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 1672 | /* If this is dirty page, keep PagePrivate */ |
| 1673 | if (PageDirty(page)) |
| 1674 | return 0; |
| 1675 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1676 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 1677 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1681 | { |
| 1682 | struct address_space *mapping = page->mapping; |
| 1683 | struct inode *inode = mapping->host; |
| 1684 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1685 | trace_f2fs_set_page_dirty(page, DATA); |
| 1686 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1687 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1688 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1689 | if (f2fs_is_atomic_file(inode)) { |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1690 | register_inmem_page(inode, page); |
| 1691 | return 1; |
| 1692 | } |
| 1693 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 1694 | mark_inode_dirty(inode); |
| 1695 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1696 | if (!PageDirty(page)) { |
| 1697 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1698 | update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1699 | return 1; |
| 1700 | } |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1704 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1705 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 1706 | struct inode *inode = mapping->host; |
| 1707 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1708 | /* we don't need to use inline_data strictly */ |
| 1709 | if (f2fs_has_inline_data(inode)) { |
| 1710 | int err = f2fs_convert_inline_inode(inode); |
| 1711 | if (err) |
| 1712 | return err; |
| 1713 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1714 | return generic_block_bmap(mapping, block, get_data_block); |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1715 | } |
| 1716 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 1717 | void init_extent_cache_info(struct f2fs_sb_info *sbi) |
| 1718 | { |
| 1719 | INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO); |
| 1720 | init_rwsem(&sbi->extent_tree_lock); |
| 1721 | INIT_LIST_HEAD(&sbi->extent_list); |
| 1722 | spin_lock_init(&sbi->extent_lock); |
| 1723 | sbi->total_ext_tree = 0; |
| 1724 | atomic_set(&sbi->total_ext_node, 0); |
| 1725 | } |
| 1726 | |
| 1727 | int __init create_extent_cache(void) |
| 1728 | { |
| 1729 | extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree", |
| 1730 | sizeof(struct extent_tree)); |
| 1731 | if (!extent_tree_slab) |
| 1732 | return -ENOMEM; |
| 1733 | extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node", |
| 1734 | sizeof(struct extent_node)); |
| 1735 | if (!extent_node_slab) { |
| 1736 | kmem_cache_destroy(extent_tree_slab); |
| 1737 | return -ENOMEM; |
| 1738 | } |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | void destroy_extent_cache(void) |
| 1743 | { |
| 1744 | kmem_cache_destroy(extent_node_slab); |
| 1745 | kmem_cache_destroy(extent_tree_slab); |
| 1746 | } |
| 1747 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1748 | const struct address_space_operations f2fs_dblock_aops = { |
| 1749 | .readpage = f2fs_read_data_page, |
| 1750 | .readpages = f2fs_read_data_pages, |
| 1751 | .writepage = f2fs_write_data_page, |
| 1752 | .writepages = f2fs_write_data_pages, |
| 1753 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1754 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1755 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1756 | .invalidatepage = f2fs_invalidate_page, |
| 1757 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1758 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1759 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1760 | }; |