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