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 | */ |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 203 | static 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; |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -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 | |
| 260 | set_buffer_new(bh_result); |
| 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 | |
| 397 | static struct extent_node *__lookup_extent_tree(struct extent_tree *et, |
| 398 | unsigned int fofs) |
| 399 | { |
| 400 | struct rb_node *node = et->root.rb_node; |
| 401 | struct extent_node *en; |
| 402 | |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 403 | if (et->cached_en) { |
| 404 | struct extent_info *cei = &et->cached_en->ei; |
| 405 | |
| 406 | if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) |
| 407 | return et->cached_en; |
| 408 | } |
| 409 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 410 | while (node) { |
| 411 | en = rb_entry(node, struct extent_node, rb_node); |
| 412 | |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 413 | if (fofs < en->ei.fofs) { |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 414 | node = node->rb_left; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 415 | } else if (fofs >= en->ei.fofs + en->ei.len) { |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 416 | node = node->rb_right; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 417 | } else { |
| 418 | et->cached_en = en; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 419 | return en; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 420 | } |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 421 | } |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi, |
| 426 | struct extent_tree *et, struct extent_node *en) |
| 427 | { |
| 428 | struct extent_node *prev; |
| 429 | struct rb_node *node; |
| 430 | |
| 431 | node = rb_prev(&en->rb_node); |
| 432 | if (!node) |
| 433 | return NULL; |
| 434 | |
| 435 | prev = rb_entry(node, struct extent_node, rb_node); |
| 436 | if (__is_back_mergeable(&en->ei, &prev->ei)) { |
| 437 | en->ei.fofs = prev->ei.fofs; |
| 438 | en->ei.blk = prev->ei.blk; |
| 439 | en->ei.len += prev->ei.len; |
| 440 | __detach_extent_node(sbi, et, prev); |
| 441 | return prev; |
| 442 | } |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi, |
| 447 | struct extent_tree *et, struct extent_node *en) |
| 448 | { |
| 449 | struct extent_node *next; |
| 450 | struct rb_node *node; |
| 451 | |
| 452 | node = rb_next(&en->rb_node); |
| 453 | if (!node) |
| 454 | return NULL; |
| 455 | |
| 456 | next = rb_entry(node, struct extent_node, rb_node); |
| 457 | if (__is_front_mergeable(&en->ei, &next->ei)) { |
| 458 | en->ei.len += next->ei.len; |
| 459 | __detach_extent_node(sbi, et, next); |
| 460 | return next; |
| 461 | } |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi, |
| 466 | struct extent_tree *et, struct extent_info *ei, |
| 467 | struct extent_node **den) |
| 468 | { |
| 469 | struct rb_node **p = &et->root.rb_node; |
| 470 | struct rb_node *parent = NULL; |
| 471 | struct extent_node *en; |
| 472 | |
| 473 | while (*p) { |
| 474 | parent = *p; |
| 475 | en = rb_entry(parent, struct extent_node, rb_node); |
| 476 | |
| 477 | if (ei->fofs < en->ei.fofs) { |
| 478 | if (__is_front_mergeable(ei, &en->ei)) { |
| 479 | f2fs_bug_on(sbi, !den); |
| 480 | en->ei.fofs = ei->fofs; |
| 481 | en->ei.blk = ei->blk; |
| 482 | en->ei.len += ei->len; |
| 483 | *den = __try_back_merge(sbi, et, en); |
| 484 | return en; |
| 485 | } |
| 486 | p = &(*p)->rb_left; |
| 487 | } else if (ei->fofs >= en->ei.fofs + en->ei.len) { |
| 488 | if (__is_back_mergeable(ei, &en->ei)) { |
| 489 | f2fs_bug_on(sbi, !den); |
| 490 | en->ei.len += ei->len; |
| 491 | *den = __try_front_merge(sbi, et, en); |
| 492 | return en; |
| 493 | } |
| 494 | p = &(*p)->rb_right; |
| 495 | } else { |
| 496 | f2fs_bug_on(sbi, 1); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return __attach_extent_node(sbi, et, ei, parent, p); |
| 501 | } |
| 502 | |
| 503 | static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi, |
| 504 | struct extent_tree *et, bool free_all) |
| 505 | { |
| 506 | struct rb_node *node, *next; |
| 507 | struct extent_node *en; |
| 508 | unsigned int count = et->count; |
| 509 | |
| 510 | node = rb_first(&et->root); |
| 511 | while (node) { |
| 512 | next = rb_next(node); |
| 513 | en = rb_entry(node, struct extent_node, rb_node); |
| 514 | |
| 515 | if (free_all) { |
| 516 | spin_lock(&sbi->extent_lock); |
| 517 | if (!list_empty(&en->list)) |
| 518 | list_del_init(&en->list); |
| 519 | spin_unlock(&sbi->extent_lock); |
| 520 | } |
| 521 | |
| 522 | if (free_all || list_empty(&en->list)) { |
| 523 | __detach_extent_node(sbi, et, en); |
| 524 | kmem_cache_free(extent_node_slab, en); |
| 525 | } |
| 526 | node = next; |
| 527 | } |
| 528 | |
| 529 | return count - et->count; |
| 530 | } |
| 531 | |
| 532 | static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs, |
| 533 | struct extent_info *ei) |
| 534 | { |
| 535 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 536 | struct extent_tree *et; |
| 537 | struct extent_node *en; |
| 538 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 539 | trace_f2fs_lookup_extent_tree_start(inode, pgofs); |
| 540 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 541 | down_read(&sbi->extent_tree_lock); |
| 542 | et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino); |
| 543 | if (!et) { |
| 544 | up_read(&sbi->extent_tree_lock); |
| 545 | return false; |
| 546 | } |
| 547 | atomic_inc(&et->refcount); |
| 548 | up_read(&sbi->extent_tree_lock); |
| 549 | |
| 550 | read_lock(&et->lock); |
| 551 | en = __lookup_extent_tree(et, pgofs); |
| 552 | if (en) { |
| 553 | *ei = en->ei; |
| 554 | spin_lock(&sbi->extent_lock); |
| 555 | if (!list_empty(&en->list)) |
| 556 | list_move_tail(&en->list, &sbi->extent_list); |
| 557 | spin_unlock(&sbi->extent_lock); |
| 558 | stat_inc_read_hit(sbi->sb); |
| 559 | } |
| 560 | stat_inc_total_hit(sbi->sb); |
| 561 | read_unlock(&et->lock); |
| 562 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 563 | trace_f2fs_lookup_extent_tree_end(inode, pgofs, en); |
| 564 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 565 | atomic_dec(&et->refcount); |
| 566 | return en ? true : false; |
| 567 | } |
| 568 | |
| 569 | static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs, |
| 570 | block_t blkaddr) |
| 571 | { |
| 572 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 573 | nid_t ino = inode->i_ino; |
| 574 | struct extent_tree *et; |
| 575 | struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL; |
| 576 | struct extent_node *den = NULL; |
| 577 | struct extent_info ei, dei; |
| 578 | unsigned int endofs; |
| 579 | |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 580 | trace_f2fs_update_extent_tree(inode, fofs, blkaddr); |
| 581 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 582 | down_write(&sbi->extent_tree_lock); |
| 583 | et = radix_tree_lookup(&sbi->extent_tree_root, ino); |
| 584 | if (!et) { |
| 585 | et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS); |
| 586 | f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et); |
| 587 | memset(et, 0, sizeof(struct extent_tree)); |
| 588 | et->ino = ino; |
| 589 | et->root = RB_ROOT; |
Chao Yu | 62c8af6 | 2015-02-05 18:01:39 +0800 | [diff] [blame] | 590 | et->cached_en = NULL; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 591 | rwlock_init(&et->lock); |
| 592 | atomic_set(&et->refcount, 0); |
| 593 | et->count = 0; |
| 594 | sbi->total_ext_tree++; |
| 595 | } |
| 596 | atomic_inc(&et->refcount); |
| 597 | up_write(&sbi->extent_tree_lock); |
| 598 | |
| 599 | write_lock(&et->lock); |
| 600 | |
| 601 | /* 1. lookup and remove existing extent info in cache */ |
| 602 | en = __lookup_extent_tree(et, fofs); |
| 603 | if (!en) |
| 604 | goto update_extent; |
| 605 | |
| 606 | dei = en->ei; |
| 607 | __detach_extent_node(sbi, et, en); |
| 608 | |
| 609 | /* 2. if extent can be split more, split and insert the left part */ |
| 610 | if (dei.len > 1) { |
| 611 | /* insert left part of split extent into cache */ |
| 612 | if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) { |
| 613 | set_extent_info(&ei, dei.fofs, dei.blk, |
| 614 | fofs - dei.fofs); |
| 615 | en1 = __insert_extent_tree(sbi, et, &ei, NULL); |
| 616 | } |
| 617 | |
| 618 | /* insert right part of split extent into cache */ |
| 619 | endofs = dei.fofs + dei.len - 1; |
| 620 | if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) { |
| 621 | set_extent_info(&ei, fofs + 1, |
| 622 | fofs - dei.fofs + dei.blk, endofs - fofs); |
| 623 | en2 = __insert_extent_tree(sbi, et, &ei, NULL); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | update_extent: |
| 628 | /* 3. update extent in extent cache */ |
| 629 | if (blkaddr) { |
| 630 | set_extent_info(&ei, fofs, blkaddr, 1); |
| 631 | en3 = __insert_extent_tree(sbi, et, &ei, &den); |
| 632 | } |
| 633 | |
| 634 | /* 4. update in global extent list */ |
| 635 | spin_lock(&sbi->extent_lock); |
| 636 | if (en && !list_empty(&en->list)) |
| 637 | list_del(&en->list); |
| 638 | /* |
| 639 | * en1 and en2 split from en, they will become more and more smaller |
| 640 | * fragments after splitting several times. So if the length is smaller |
| 641 | * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree. |
| 642 | */ |
| 643 | if (en1) |
| 644 | list_add_tail(&en1->list, &sbi->extent_list); |
| 645 | if (en2) |
| 646 | list_add_tail(&en2->list, &sbi->extent_list); |
| 647 | if (en3) { |
| 648 | if (list_empty(&en3->list)) |
| 649 | list_add_tail(&en3->list, &sbi->extent_list); |
| 650 | else |
| 651 | list_move_tail(&en3->list, &sbi->extent_list); |
| 652 | } |
| 653 | if (den && !list_empty(&den->list)) |
| 654 | list_del(&den->list); |
| 655 | spin_unlock(&sbi->extent_lock); |
| 656 | |
| 657 | /* 5. release extent node */ |
| 658 | if (en) |
| 659 | kmem_cache_free(extent_node_slab, en); |
| 660 | if (den) |
| 661 | kmem_cache_free(extent_node_slab, den); |
| 662 | |
| 663 | write_unlock(&et->lock); |
| 664 | atomic_dec(&et->refcount); |
| 665 | } |
| 666 | |
| 667 | void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink) |
| 668 | { |
| 669 | struct extent_tree *treevec[EXT_TREE_VEC_SIZE]; |
| 670 | struct extent_node *en, *tmp; |
| 671 | unsigned long ino = F2FS_ROOT_INO(sbi); |
| 672 | struct radix_tree_iter iter; |
| 673 | void **slot; |
| 674 | unsigned int found; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 675 | unsigned int node_cnt = 0, tree_cnt = 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 676 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 677 | if (!test_opt(sbi, EXTENT_CACHE)) |
| 678 | return; |
| 679 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 680 | if (available_free_memory(sbi, EXTENT_CACHE)) |
| 681 | return; |
| 682 | |
| 683 | spin_lock(&sbi->extent_lock); |
| 684 | list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) { |
| 685 | if (!nr_shrink--) |
| 686 | break; |
| 687 | list_del_init(&en->list); |
| 688 | } |
| 689 | spin_unlock(&sbi->extent_lock); |
| 690 | |
| 691 | down_read(&sbi->extent_tree_lock); |
| 692 | while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root, |
| 693 | (void **)treevec, ino, EXT_TREE_VEC_SIZE))) { |
| 694 | unsigned i; |
| 695 | |
| 696 | ino = treevec[found - 1]->ino + 1; |
| 697 | for (i = 0; i < found; i++) { |
| 698 | struct extent_tree *et = treevec[i]; |
| 699 | |
| 700 | atomic_inc(&et->refcount); |
| 701 | write_lock(&et->lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 702 | node_cnt += __free_extent_tree(sbi, et, false); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 703 | write_unlock(&et->lock); |
| 704 | atomic_dec(&et->refcount); |
| 705 | } |
| 706 | } |
| 707 | up_read(&sbi->extent_tree_lock); |
| 708 | |
| 709 | down_write(&sbi->extent_tree_lock); |
| 710 | radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter, |
| 711 | F2FS_ROOT_INO(sbi)) { |
| 712 | struct extent_tree *et = (struct extent_tree *)*slot; |
| 713 | |
| 714 | if (!atomic_read(&et->refcount) && !et->count) { |
| 715 | radix_tree_delete(&sbi->extent_tree_root, et->ino); |
| 716 | kmem_cache_free(extent_tree_slab, et); |
| 717 | sbi->total_ext_tree--; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 718 | tree_cnt++; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | up_write(&sbi->extent_tree_lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 722 | |
| 723 | trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void f2fs_destroy_extent_tree(struct inode *inode) |
| 727 | { |
| 728 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 729 | struct extent_tree *et; |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 730 | unsigned int node_cnt = 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 731 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 732 | if (!test_opt(sbi, EXTENT_CACHE)) |
| 733 | return; |
| 734 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 735 | down_read(&sbi->extent_tree_lock); |
| 736 | et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino); |
| 737 | if (!et) { |
| 738 | up_read(&sbi->extent_tree_lock); |
| 739 | goto out; |
| 740 | } |
| 741 | atomic_inc(&et->refcount); |
| 742 | up_read(&sbi->extent_tree_lock); |
| 743 | |
| 744 | /* free all extent info belong to this extent tree */ |
| 745 | write_lock(&et->lock); |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 746 | node_cnt = __free_extent_tree(sbi, et, true); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 747 | write_unlock(&et->lock); |
| 748 | |
| 749 | atomic_dec(&et->refcount); |
| 750 | |
| 751 | /* try to find and delete extent tree entry in radix tree */ |
| 752 | down_write(&sbi->extent_tree_lock); |
| 753 | et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino); |
| 754 | if (!et) { |
| 755 | up_write(&sbi->extent_tree_lock); |
| 756 | goto out; |
| 757 | } |
| 758 | f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count); |
| 759 | radix_tree_delete(&sbi->extent_tree_root, inode->i_ino); |
| 760 | kmem_cache_free(extent_tree_slab, et); |
| 761 | sbi->total_ext_tree--; |
| 762 | up_write(&sbi->extent_tree_lock); |
| 763 | out: |
Chao Yu | 1ec4610 | 2015-02-05 17:59:59 +0800 | [diff] [blame] | 764 | trace_f2fs_destroy_extent_tree(inode, node_cnt); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 768 | static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 769 | struct extent_info *ei) |
| 770 | { |
Chao Yu | 91c5d9b | 2015-02-05 18:02:44 +0800 | [diff] [blame] | 771 | if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) |
| 772 | return false; |
| 773 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 774 | if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE)) |
| 775 | return f2fs_lookup_extent_tree(inode, pgofs, ei); |
| 776 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 777 | return lookup_extent_info(inode, pgofs, ei); |
| 778 | } |
| 779 | |
| 780 | void f2fs_update_extent_cache(struct dnode_of_data *dn) |
| 781 | { |
| 782 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 783 | pgoff_t fofs; |
| 784 | |
| 785 | f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR); |
| 786 | |
| 787 | /* Update the page address in the parent node */ |
| 788 | __set_data_blkaddr(dn); |
| 789 | |
Chao Yu | 91c5d9b | 2015-02-05 18:02:44 +0800 | [diff] [blame] | 790 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 791 | return; |
| 792 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 793 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 794 | dn->ofs_in_node; |
| 795 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 796 | if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE)) |
| 797 | return f2fs_update_extent_tree(dn->inode, fofs, |
| 798 | dn->data_blkaddr); |
| 799 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 800 | if (update_extent_info(dn->inode, fofs, dn->data_blkaddr)) |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 801 | sync_inode_page(dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 802 | } |
| 803 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 804 | 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] | 805 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 806 | struct address_space *mapping = inode->i_mapping; |
| 807 | struct dnode_of_data dn; |
| 808 | struct page *page; |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 809 | struct extent_info ei; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 810 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 811 | struct f2fs_io_info fio = { |
| 812 | .type = DATA, |
| 813 | .rw = sync ? READ_SYNC : READA, |
| 814 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 815 | |
| 816 | page = find_get_page(mapping, index); |
| 817 | if (page && PageUptodate(page)) |
| 818 | return page; |
| 819 | f2fs_put_page(page, 0); |
| 820 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 821 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 822 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 823 | goto got_it; |
| 824 | } |
| 825 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 826 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 827 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 828 | if (err) |
| 829 | return ERR_PTR(err); |
| 830 | f2fs_put_dnode(&dn); |
| 831 | |
| 832 | if (dn.data_blkaddr == NULL_ADDR) |
| 833 | return ERR_PTR(-ENOENT); |
| 834 | |
| 835 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 836 | if (unlikely(dn.data_blkaddr == NEW_ADDR)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 837 | return ERR_PTR(-EINVAL); |
| 838 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 839 | got_it: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 840 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 841 | if (!page) |
| 842 | return ERR_PTR(-ENOMEM); |
| 843 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 844 | if (PageUptodate(page)) { |
| 845 | unlock_page(page); |
| 846 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 847 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 848 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 849 | fio.blk_addr = dn.data_blkaddr; |
| 850 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Chao Yu | 1069bbf | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 851 | if (err) |
| 852 | return ERR_PTR(err); |
| 853 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 854 | if (sync) { |
| 855 | wait_on_page_locked(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 856 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 857 | f2fs_put_page(page, 0); |
| 858 | return ERR_PTR(-EIO); |
| 859 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 860 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 861 | return page; |
| 862 | } |
| 863 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 864 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 865 | * If it tries to access a hole, return an error. |
| 866 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 867 | * whether this page exists or not. |
| 868 | */ |
| 869 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 870 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 871 | struct address_space *mapping = inode->i_mapping; |
| 872 | struct dnode_of_data dn; |
| 873 | struct page *page; |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 874 | struct extent_info ei; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 875 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 876 | struct f2fs_io_info fio = { |
| 877 | .type = DATA, |
| 878 | .rw = READ_SYNC, |
| 879 | }; |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 880 | repeat: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 881 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 882 | if (!page) |
| 883 | return ERR_PTR(-ENOMEM); |
| 884 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 885 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 886 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 887 | goto got_it; |
| 888 | } |
| 889 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 890 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 891 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 892 | if (err) { |
| 893 | f2fs_put_page(page, 1); |
| 894 | return ERR_PTR(err); |
| 895 | } |
| 896 | f2fs_put_dnode(&dn); |
| 897 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 898 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 899 | f2fs_put_page(page, 1); |
| 900 | return ERR_PTR(-ENOENT); |
| 901 | } |
| 902 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 903 | got_it: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 904 | if (PageUptodate(page)) |
| 905 | return page; |
| 906 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 907 | /* |
| 908 | * A new dentry page is allocated but not able to be written, since its |
| 909 | * new inode page couldn't be allocated due to -ENOSPC. |
| 910 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 911 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 912 | */ |
| 913 | if (dn.data_blkaddr == NEW_ADDR) { |
| 914 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 915 | SetPageUptodate(page); |
| 916 | return page; |
| 917 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 918 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 919 | fio.blk_addr = dn.data_blkaddr; |
| 920 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 921 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 922 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 923 | |
| 924 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 925 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 926 | f2fs_put_page(page, 1); |
| 927 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 928 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 929 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 930 | f2fs_put_page(page, 1); |
| 931 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 932 | } |
| 933 | return page; |
| 934 | } |
| 935 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 936 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 937 | * Caller ensures that this data page is never allocated. |
| 938 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 939 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 940 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 941 | * f2fs_unlock_op(). |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 942 | * Note that, ipage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 943 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 944 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 945 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 946 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 947 | struct address_space *mapping = inode->i_mapping; |
| 948 | struct page *page; |
| 949 | struct dnode_of_data dn; |
| 950 | int err; |
| 951 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 952 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 953 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 954 | if (err) |
| 955 | return ERR_PTR(err); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 956 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 957 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 958 | if (!page) { |
| 959 | err = -ENOMEM; |
| 960 | goto put_err; |
| 961 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 962 | |
| 963 | if (PageUptodate(page)) |
| 964 | return page; |
| 965 | |
| 966 | if (dn.data_blkaddr == NEW_ADDR) { |
| 967 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 968 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 969 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 970 | struct f2fs_io_info fio = { |
| 971 | .type = DATA, |
| 972 | .rw = READ_SYNC, |
| 973 | .blk_addr = dn.data_blkaddr, |
| 974 | }; |
| 975 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 976 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 977 | goto put_err; |
| 978 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 979 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 980 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 981 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 982 | err = -EIO; |
| 983 | goto put_err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 984 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 985 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 986 | f2fs_put_page(page, 1); |
| 987 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 988 | } |
| 989 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 990 | |
| 991 | if (new_i_size && |
| 992 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 993 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 994 | /* Only the directory inode sets new_i_size */ |
| 995 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 996 | } |
| 997 | return page; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 998 | |
| 999 | put_err: |
| 1000 | f2fs_put_dnode(&dn); |
| 1001 | return ERR_PTR(err); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1002 | } |
| 1003 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1004 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 1005 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1006 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1007 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1008 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1009 | struct node_info ni; |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1010 | int seg = CURSEG_WARM_DATA; |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1011 | pgoff_t fofs; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1012 | |
| 1013 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 1014 | return -EPERM; |
| 1015 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 1016 | return -ENOSPC; |
| 1017 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1018 | get_node_info(sbi, dn->nid, &ni); |
| 1019 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 1020 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 1021 | if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page) |
| 1022 | seg = CURSEG_DIRECT_IO; |
| 1023 | |
| 1024 | allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1025 | |
| 1026 | /* direct IO doesn't use extent cache to maximize the performance */ |
Jaegeuk Kim | 41ef94b | 2014-12-30 23:12:11 -0800 | [diff] [blame] | 1027 | __set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1028 | |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 1029 | /* update i_size */ |
| 1030 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 1031 | dn->ofs_in_node; |
| 1032 | if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT)) |
| 1033 | i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT)); |
| 1034 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1038 | static void __allocate_data_blocks(struct inode *inode, loff_t offset, |
| 1039 | size_t count) |
| 1040 | { |
| 1041 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1042 | struct dnode_of_data dn; |
| 1043 | u64 start = F2FS_BYTES_TO_BLK(offset); |
| 1044 | u64 len = F2FS_BYTES_TO_BLK(count); |
| 1045 | bool allocated; |
| 1046 | u64 end_offset; |
| 1047 | |
| 1048 | while (len) { |
| 1049 | f2fs_balance_fs(sbi); |
| 1050 | f2fs_lock_op(sbi); |
| 1051 | |
| 1052 | /* When reading holes, we need its node page */ |
| 1053 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1054 | if (get_dnode_of_data(&dn, start, ALLOC_NODE)) |
| 1055 | goto out; |
| 1056 | |
| 1057 | allocated = false; |
| 1058 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 1059 | |
| 1060 | while (dn.ofs_in_node < end_offset && len) { |
| 1061 | if (dn.data_blkaddr == NULL_ADDR) { |
| 1062 | if (__allocate_data_block(&dn)) |
| 1063 | goto sync_out; |
| 1064 | allocated = true; |
| 1065 | } |
| 1066 | len--; |
| 1067 | start++; |
| 1068 | dn.ofs_in_node++; |
| 1069 | } |
| 1070 | |
| 1071 | if (allocated) |
| 1072 | sync_inode_page(&dn); |
| 1073 | |
| 1074 | f2fs_put_dnode(&dn); |
| 1075 | f2fs_unlock_op(sbi); |
| 1076 | } |
| 1077 | return; |
| 1078 | |
| 1079 | sync_out: |
| 1080 | if (allocated) |
| 1081 | sync_inode_page(&dn); |
| 1082 | f2fs_put_dnode(&dn); |
| 1083 | out: |
| 1084 | f2fs_unlock_op(sbi); |
| 1085 | return; |
| 1086 | } |
| 1087 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1088 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 1089 | * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh. |
| 1090 | * If original data blocks are allocated, then give them to blockdev. |
| 1091 | * Otherwise, |
| 1092 | * a. preallocate requested block addresses |
| 1093 | * b. do not use extent cache for better performance |
| 1094 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1095 | */ |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1096 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 1097 | struct buffer_head *bh_result, int create, bool fiemap) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1098 | { |
| 1099 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 1100 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 1101 | struct dnode_of_data dn; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1102 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 1103 | pgoff_t pgofs, end_offset; |
| 1104 | int err = 0, ofs = 1; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1105 | struct extent_info ei; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1106 | bool allocated = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1107 | |
| 1108 | /* Get the page offset from the block offset(iblock) */ |
| 1109 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 1110 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 1111 | if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1112 | f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1113 | goto out; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1114 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1115 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1116 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1117 | f2fs_lock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1118 | |
| 1119 | /* When reading holes, we need its node page */ |
| 1120 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1121 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1122 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1123 | if (err == -ENOENT) |
| 1124 | err = 0; |
| 1125 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 1126 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1127 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1128 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1129 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1130 | if (dn.data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 1131 | set_buffer_new(bh_result); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1132 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1133 | } else if (create) { |
| 1134 | err = __allocate_data_block(&dn); |
| 1135 | if (err) |
| 1136 | goto put_out; |
| 1137 | allocated = true; |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 1138 | set_buffer_new(bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1139 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 1140 | } else { |
| 1141 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1142 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1143 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 1144 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1145 | bh_result->b_size = (((size_t)1) << blkbits); |
| 1146 | dn.ofs_in_node++; |
| 1147 | pgofs++; |
| 1148 | |
| 1149 | get_next: |
| 1150 | if (dn.ofs_in_node >= end_offset) { |
| 1151 | if (allocated) |
| 1152 | sync_inode_page(&dn); |
| 1153 | allocated = false; |
| 1154 | f2fs_put_dnode(&dn); |
| 1155 | |
| 1156 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1157 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1158 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1159 | if (err == -ENOENT) |
| 1160 | err = 0; |
| 1161 | goto unlock_out; |
| 1162 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1163 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1164 | goto put_out; |
| 1165 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 1166 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (maxblocks > (bh_result->b_size >> blkbits)) { |
| 1170 | block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 1171 | if (blkaddr == NULL_ADDR && create) { |
| 1172 | err = __allocate_data_block(&dn); |
| 1173 | if (err) |
| 1174 | goto sync_out; |
| 1175 | allocated = true; |
| 1176 | blkaddr = dn.data_blkaddr; |
| 1177 | } |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1178 | /* Give more consecutive addresses for the readahead */ |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1179 | if (blkaddr == (bh_result->b_blocknr + ofs)) { |
| 1180 | ofs++; |
| 1181 | dn.ofs_in_node++; |
| 1182 | pgofs++; |
| 1183 | bh_result->b_size += (((size_t)1) << blkbits); |
| 1184 | goto get_next; |
| 1185 | } |
| 1186 | } |
| 1187 | sync_out: |
| 1188 | if (allocated) |
| 1189 | sync_inode_page(&dn); |
| 1190 | put_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1191 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1192 | unlock_out: |
| 1193 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1194 | f2fs_unlock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1195 | out: |
| 1196 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
| 1197 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1198 | } |
| 1199 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1200 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 1201 | struct buffer_head *bh_result, int create) |
| 1202 | { |
| 1203 | return __get_data_block(inode, iblock, bh_result, create, false); |
| 1204 | } |
| 1205 | |
| 1206 | static int get_data_block_fiemap(struct inode *inode, sector_t iblock, |
| 1207 | struct buffer_head *bh_result, int create) |
| 1208 | { |
| 1209 | return __get_data_block(inode, iblock, bh_result, create, true); |
| 1210 | } |
| 1211 | |
Jaegeuk Kim | 9ab70134 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1212 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1213 | u64 start, u64 len) |
| 1214 | { |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1215 | return generic_block_fiemap(inode, fieinfo, |
| 1216 | start, len, get_data_block_fiemap); |
Jaegeuk Kim | 9ab70134 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1217 | } |
| 1218 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1219 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 1220 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1221 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1222 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1223 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 1224 | trace_f2fs_readpage(page, DATA); |
| 1225 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1226 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1227 | if (f2fs_has_inline_data(inode)) |
| 1228 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1229 | if (ret == -EAGAIN) |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1230 | ret = mpage_readpage(page, get_data_block); |
| 1231 | |
| 1232 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static int f2fs_read_data_pages(struct file *file, |
| 1236 | struct address_space *mapping, |
| 1237 | struct list_head *pages, unsigned nr_pages) |
| 1238 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1239 | struct inode *inode = file->f_mapping->host; |
| 1240 | |
| 1241 | /* If the file has inline data, skip readpages */ |
| 1242 | if (f2fs_has_inline_data(inode)) |
| 1243 | return 0; |
| 1244 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1245 | return mpage_readpages(mapping, pages, nr_pages, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1246 | } |
| 1247 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1248 | 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] | 1249 | { |
| 1250 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1251 | struct dnode_of_data dn; |
| 1252 | int err = 0; |
| 1253 | |
| 1254 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 1255 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1256 | if (err) |
| 1257 | return err; |
| 1258 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1259 | fio->blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1260 | |
| 1261 | /* This page is already truncated */ |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame^] | 1262 | if (fio->blk_addr == NULL_ADDR) { |
| 1263 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1264 | goto out_writepage; |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame^] | 1265 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1266 | |
| 1267 | set_page_writeback(page); |
| 1268 | |
| 1269 | /* |
| 1270 | * If current allocation needs SSR, |
| 1271 | * it had better in-place writes for updated data. |
| 1272 | */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1273 | if (unlikely(fio->blk_addr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 1274 | !is_cold_data(page) && |
| 1275 | need_inplace_update(inode))) { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1276 | rewrite_data_page(page, fio); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1277 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1278 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1279 | write_data_page(page, &dn, fio); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 1280 | f2fs_update_extent_cache(&dn); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1281 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1282 | } |
| 1283 | out_writepage: |
| 1284 | f2fs_put_dnode(&dn); |
| 1285 | return err; |
| 1286 | } |
| 1287 | |
| 1288 | static int f2fs_write_data_page(struct page *page, |
| 1289 | struct writeback_control *wbc) |
| 1290 | { |
| 1291 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1292 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1293 | loff_t i_size = i_size_read(inode); |
| 1294 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 1295 | >> PAGE_CACHE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1296 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1297 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1298 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1299 | struct f2fs_io_info fio = { |
| 1300 | .type = DATA, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 1301 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1302 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1303 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1304 | trace_f2fs_writepage(page, DATA); |
| 1305 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1306 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1307 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1308 | |
| 1309 | /* |
| 1310 | * If the offset is out-of-range of file size, |
| 1311 | * this page does not have to be written to disk. |
| 1312 | */ |
| 1313 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1314 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1315 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1316 | |
| 1317 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1318 | write: |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1319 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1320 | goto redirty_out; |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1321 | if (f2fs_is_drop_cache(inode)) |
| 1322 | goto out; |
| 1323 | if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim && |
| 1324 | available_free_memory(sbi, BASE_CHECK)) |
| 1325 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1326 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1327 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1328 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1329 | if (unlikely(f2fs_cp_error(sbi))) |
| 1330 | goto redirty_out; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1331 | err = do_write_data_page(page, &fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1332 | goto done; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1333 | } |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1334 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1335 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1336 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1337 | SetPageError(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1338 | goto out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1341 | if (!wbc->for_reclaim) |
| 1342 | need_balance_fs = true; |
| 1343 | else if (has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1344 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1345 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1346 | err = -EAGAIN; |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1347 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1348 | if (f2fs_has_inline_data(inode)) |
| 1349 | err = f2fs_write_inline_data(inode, page); |
| 1350 | if (err == -EAGAIN) |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1351 | err = do_write_data_page(page, &fio); |
| 1352 | f2fs_unlock_op(sbi); |
| 1353 | done: |
| 1354 | if (err && err != -ENOENT) |
| 1355 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1356 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1357 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1358 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1359 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame^] | 1360 | if (err) |
| 1361 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1362 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1363 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1364 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 2aea39e | 2014-04-24 09:49:52 +0900 | [diff] [blame] | 1365 | if (wbc->for_reclaim) |
| 1366 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1367 | return 0; |
| 1368 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1369 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1370 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1371 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1372 | } |
| 1373 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1374 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 1375 | void *data) |
| 1376 | { |
| 1377 | struct address_space *mapping = data; |
| 1378 | int ret = mapping->a_ops->writepage(page, wbc); |
| 1379 | mapping_set_error(mapping, ret); |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 1383 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1384 | struct writeback_control *wbc) |
| 1385 | { |
| 1386 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1387 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 1388 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1389 | int ret; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1390 | long diff; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1391 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 1392 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 1393 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 1394 | /* deal with chardevs and other special file */ |
| 1395 | if (!mapping->a_ops->writepage) |
| 1396 | return 0; |
| 1397 | |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 1398 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1399 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1400 | available_free_memory(sbi, DIRTY_DENTS)) |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1401 | goto skip_write; |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 1402 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1403 | diff = nr_pages_to_write(sbi, DATA, wbc); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1404 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 1405 | if (!S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1406 | mutex_lock(&sbi->writepages); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 1407 | locked = true; |
| 1408 | } |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1409 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 1410 | if (locked) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1411 | mutex_unlock(&sbi->writepages); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1412 | |
| 1413 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1414 | |
| 1415 | remove_dirty_dir_inode(inode); |
| 1416 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1417 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1418 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1419 | |
| 1420 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1421 | wbc->pages_skipped += get_dirty_pages(inode); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1422 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1423 | } |
| 1424 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1425 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 1426 | { |
| 1427 | struct inode *inode = mapping->host; |
| 1428 | |
| 1429 | if (to > inode->i_size) { |
| 1430 | truncate_pagecache(inode, inode->i_size); |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 1431 | truncate_blocks(inode, inode->i_size, true); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1435 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 1436 | loff_t pos, unsigned len, unsigned flags, |
| 1437 | struct page **pagep, void **fsdata) |
| 1438 | { |
| 1439 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1440 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1441 | struct page *page, *ipage; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1442 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 1443 | struct dnode_of_data dn; |
| 1444 | int err = 0; |
| 1445 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 1446 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 1447 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1448 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1449 | |
| 1450 | /* |
| 1451 | * We should check this at this moment to avoid deadlock on inode page |
| 1452 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1453 | * lock_page(page #0) -> lock_page(inode_page) |
| 1454 | */ |
| 1455 | if (index != 0) { |
| 1456 | err = f2fs_convert_inline_inode(inode); |
| 1457 | if (err) |
| 1458 | goto fail; |
| 1459 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1460 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1461 | page = grab_cache_page_write_begin(mapping, index, flags); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1462 | if (!page) { |
| 1463 | err = -ENOMEM; |
| 1464 | goto fail; |
| 1465 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 1466 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1467 | *pagep = page; |
| 1468 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 1469 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* check inline_data */ |
| 1472 | ipage = get_node_page(sbi, inode->i_ino); |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1473 | if (IS_ERR(ipage)) { |
| 1474 | err = PTR_ERR(ipage); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1475 | goto unlock_fail; |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1476 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1477 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1478 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1479 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1480 | if (f2fs_has_inline_data(inode)) { |
| 1481 | if (pos + len <= MAX_INLINE_DATA) { |
| 1482 | read_inline_data(page, ipage); |
| 1483 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 1484 | sync_inode_page(&dn); |
| 1485 | goto put_next; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1486 | } |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1487 | err = f2fs_convert_inline_page(&dn, page); |
| 1488 | if (err) |
| 1489 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1490 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1491 | err = f2fs_reserve_block(&dn, index); |
| 1492 | if (err) |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1493 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1494 | put_next: |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1495 | f2fs_put_dnode(&dn); |
| 1496 | f2fs_unlock_op(sbi); |
| 1497 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1498 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 1499 | return 0; |
| 1500 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1501 | f2fs_wait_on_page_writeback(page, DATA); |
| 1502 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1503 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1504 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 1505 | unsigned end = start + len; |
| 1506 | |
| 1507 | /* Reading beyond i_size is simple: memset to zero */ |
| 1508 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1509 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1510 | } |
| 1511 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1512 | if (dn.data_blkaddr == NEW_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1513 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 1514 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1515 | struct f2fs_io_info fio = { |
| 1516 | .type = DATA, |
| 1517 | .rw = READ_SYNC, |
| 1518 | .blk_addr = dn.data_blkaddr, |
| 1519 | }; |
| 1520 | err = f2fs_submit_page_bio(sbi, page, &fio); |
Jan Kara | 9234f31 | 2014-10-22 15:21:47 +0200 | [diff] [blame] | 1521 | if (err) |
| 1522 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1523 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1524 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1525 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1526 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1527 | err = -EIO; |
| 1528 | goto fail; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1529 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1530 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1531 | f2fs_put_page(page, 1); |
| 1532 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1533 | } |
| 1534 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1535 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1536 | SetPageUptodate(page); |
| 1537 | clear_cold_data(page); |
| 1538 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1539 | |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1540 | put_fail: |
| 1541 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1542 | unlock_fail: |
| 1543 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1544 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1545 | fail: |
| 1546 | f2fs_write_failed(mapping, pos + len); |
| 1547 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1548 | } |
| 1549 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1550 | static int f2fs_write_end(struct file *file, |
| 1551 | struct address_space *mapping, |
| 1552 | loff_t pos, unsigned len, unsigned copied, |
| 1553 | struct page *page, void *fsdata) |
| 1554 | { |
| 1555 | struct inode *inode = page->mapping->host; |
| 1556 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 1557 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1558 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1559 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1560 | |
| 1561 | if (pos + copied > i_size_read(inode)) { |
| 1562 | i_size_write(inode, pos + copied); |
| 1563 | mark_inode_dirty(inode); |
| 1564 | update_inode_page(inode); |
| 1565 | } |
| 1566 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 1567 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1568 | return copied; |
| 1569 | } |
| 1570 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1571 | static int check_direct_IO(struct inode *inode, int rw, |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1572 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1573 | { |
| 1574 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1575 | |
| 1576 | if (rw == READ) |
| 1577 | return 0; |
| 1578 | |
| 1579 | if (offset & blocksize_mask) |
| 1580 | return -EINVAL; |
| 1581 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1582 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1583 | return -EINVAL; |
| 1584 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1585 | return 0; |
| 1586 | } |
| 1587 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1588 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 1589 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1590 | { |
| 1591 | struct file *file = iocb->ki_filp; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1592 | struct address_space *mapping = file->f_mapping; |
| 1593 | struct inode *inode = mapping->host; |
| 1594 | size_t count = iov_iter_count(iter); |
| 1595 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1596 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1597 | /* we don't need to use inline_data strictly */ |
| 1598 | if (f2fs_has_inline_data(inode)) { |
| 1599 | err = f2fs_convert_inline_inode(inode); |
| 1600 | if (err) |
| 1601 | return err; |
| 1602 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1603 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1604 | if (check_direct_IO(inode, rw, iter, offset)) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1605 | return 0; |
| 1606 | |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1607 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
| 1608 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1609 | if (rw & WRITE) |
| 1610 | __allocate_data_blocks(inode, offset, count); |
| 1611 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1612 | err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block); |
| 1613 | if (err < 0 && (rw & WRITE)) |
| 1614 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1615 | |
| 1616 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
| 1617 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1618 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1619 | } |
| 1620 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1621 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 1622 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1623 | { |
| 1624 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1625 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1626 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1627 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 1628 | (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1629 | return; |
| 1630 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1631 | if (PageDirty(page)) { |
| 1632 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 1633 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 1634 | else if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 1635 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1636 | else |
| 1637 | inode_dec_dirty_pages(inode); |
| 1638 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1639 | ClearPagePrivate(page); |
| 1640 | } |
| 1641 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1642 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1643 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 1644 | /* If this is dirty page, keep PagePrivate */ |
| 1645 | if (PageDirty(page)) |
| 1646 | return 0; |
| 1647 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1648 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 1649 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1653 | { |
| 1654 | struct address_space *mapping = page->mapping; |
| 1655 | struct inode *inode = mapping->host; |
| 1656 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1657 | trace_f2fs_set_page_dirty(page, DATA); |
| 1658 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1659 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1660 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1661 | if (f2fs_is_atomic_file(inode)) { |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1662 | register_inmem_page(inode, page); |
| 1663 | return 1; |
| 1664 | } |
| 1665 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 1666 | mark_inode_dirty(inode); |
| 1667 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1668 | if (!PageDirty(page)) { |
| 1669 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1670 | update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1671 | return 1; |
| 1672 | } |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1676 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1677 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 1678 | struct inode *inode = mapping->host; |
| 1679 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1680 | /* we don't need to use inline_data strictly */ |
| 1681 | if (f2fs_has_inline_data(inode)) { |
| 1682 | int err = f2fs_convert_inline_inode(inode); |
| 1683 | if (err) |
| 1684 | return err; |
| 1685 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1686 | return generic_block_bmap(mapping, block, get_data_block); |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1687 | } |
| 1688 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 1689 | void init_extent_cache_info(struct f2fs_sb_info *sbi) |
| 1690 | { |
| 1691 | INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO); |
| 1692 | init_rwsem(&sbi->extent_tree_lock); |
| 1693 | INIT_LIST_HEAD(&sbi->extent_list); |
| 1694 | spin_lock_init(&sbi->extent_lock); |
| 1695 | sbi->total_ext_tree = 0; |
| 1696 | atomic_set(&sbi->total_ext_node, 0); |
| 1697 | } |
| 1698 | |
| 1699 | int __init create_extent_cache(void) |
| 1700 | { |
| 1701 | extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree", |
| 1702 | sizeof(struct extent_tree)); |
| 1703 | if (!extent_tree_slab) |
| 1704 | return -ENOMEM; |
| 1705 | extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node", |
| 1706 | sizeof(struct extent_node)); |
| 1707 | if (!extent_node_slab) { |
| 1708 | kmem_cache_destroy(extent_tree_slab); |
| 1709 | return -ENOMEM; |
| 1710 | } |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
| 1714 | void destroy_extent_cache(void) |
| 1715 | { |
| 1716 | kmem_cache_destroy(extent_node_slab); |
| 1717 | kmem_cache_destroy(extent_tree_slab); |
| 1718 | } |
| 1719 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1720 | const struct address_space_operations f2fs_dblock_aops = { |
| 1721 | .readpage = f2fs_read_data_page, |
| 1722 | .readpages = f2fs_read_data_pages, |
| 1723 | .writepage = f2fs_write_data_page, |
| 1724 | .writepages = f2fs_write_data_pages, |
| 1725 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1726 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1727 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1728 | .invalidatepage = f2fs_invalidate_page, |
| 1729 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1730 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1731 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1732 | }; |