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" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 25 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 26 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 27 | static void f2fs_read_end_io(struct bio *bio, int err) |
| 28 | { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 29 | struct bio_vec *bvec; |
| 30 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 31 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 32 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 33 | struct page *page = bvec->bv_page; |
| 34 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 35 | if (!err) { |
| 36 | SetPageUptodate(page); |
| 37 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 38 | ClearPageUptodate(page); |
| 39 | SetPageError(page); |
| 40 | } |
| 41 | unlock_page(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 42 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 43 | bio_put(bio); |
| 44 | } |
| 45 | |
| 46 | static void f2fs_write_end_io(struct bio *bio, int err) |
| 47 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 48 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 49 | struct bio_vec *bvec; |
| 50 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 51 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 52 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 53 | struct page *page = bvec->bv_page; |
| 54 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 55 | if (unlikely(err)) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 56 | SetPageError(page); |
| 57 | set_bit(AS_EIO, &page->mapping->flags); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 58 | f2fs_stop_checkpoint(sbi); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 59 | } |
| 60 | end_page_writeback(page); |
| 61 | dec_page_count(sbi, F2FS_WRITEBACK); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 62 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 63 | |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 64 | if (sbi->wait_io) { |
| 65 | complete(sbi->wait_io); |
| 66 | sbi->wait_io = NULL; |
| 67 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 68 | |
| 69 | if (!get_pages(sbi, F2FS_WRITEBACK) && |
| 70 | !list_empty(&sbi->cp_wait.task_list)) |
| 71 | wake_up(&sbi->cp_wait); |
| 72 | |
| 73 | bio_put(bio); |
| 74 | } |
| 75 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 76 | /* |
| 77 | * Low-level block read/write IO operations. |
| 78 | */ |
| 79 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 80 | int npages, bool is_read) |
| 81 | { |
| 82 | struct bio *bio; |
| 83 | |
| 84 | /* No failure on bio allocation */ |
| 85 | bio = bio_alloc(GFP_NOIO, npages); |
| 86 | |
| 87 | bio->bi_bdev = sbi->sb->s_bdev; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 88 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 89 | 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] | 90 | bio->bi_private = sbi; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 91 | |
| 92 | return bio; |
| 93 | } |
| 94 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 95 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 96 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 97 | struct f2fs_io_info *fio = &io->fio; |
| 98 | int rw; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 99 | |
| 100 | if (!io->bio) |
| 101 | return; |
| 102 | |
Gu Zheng | 7e8f230 | 2013-12-20 18:17:49 +0800 | [diff] [blame] | 103 | rw = fio->rw; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 104 | |
| 105 | if (is_read_io(rw)) { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 106 | trace_f2fs_submit_read_bio(io->sbi->sb, rw, |
| 107 | fio->type, io->bio); |
Jaegeuk Kim | 76130cc | 2013-12-11 14:29:39 +0900 | [diff] [blame] | 108 | submit_bio(rw, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 109 | } else { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 110 | trace_f2fs_submit_write_bio(io->sbi->sb, rw, |
| 111 | fio->type, io->bio); |
| 112 | /* |
| 113 | * META_FLUSH is only from the checkpoint procedure, and we |
| 114 | * should wait this metadata bio for FS consistency. |
| 115 | */ |
| 116 | if (fio->type == META_FLUSH) { |
| 117 | DECLARE_COMPLETION_ONSTACK(wait); |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 118 | io->sbi->wait_io = &wait; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 119 | submit_bio(rw, io->bio); |
| 120 | wait_for_completion(&wait); |
| 121 | } else { |
| 122 | submit_bio(rw, io->bio); |
| 123 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 124 | } |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 125 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 126 | io->bio = NULL; |
| 127 | } |
| 128 | |
| 129 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 130 | enum page_type type, int rw) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 131 | { |
| 132 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 133 | struct f2fs_bio_info *io; |
| 134 | |
| 135 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 136 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 137 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 138 | |
| 139 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 140 | if (type >= META_FLUSH) { |
| 141 | io->fio.type = META_FLUSH; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 142 | if (test_opt(sbi, NOBARRIER)) |
| 143 | io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO; |
| 144 | else |
| 145 | io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 146 | } |
| 147 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 148 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Fill the locked page with data located in the block address. |
| 153 | * Return unlocked page. |
| 154 | */ |
| 155 | int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page, |
| 156 | block_t blk_addr, int rw) |
| 157 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 158 | struct bio *bio; |
| 159 | |
| 160 | trace_f2fs_submit_page_bio(page, blk_addr, rw); |
| 161 | |
| 162 | /* Allocate a new bio */ |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 163 | bio = __bio_alloc(sbi, blk_addr, 1, is_read_io(rw)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 164 | |
| 165 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 166 | bio_put(bio); |
| 167 | f2fs_put_page(page, 1); |
| 168 | return -EFAULT; |
| 169 | } |
| 170 | |
| 171 | submit_bio(rw, bio); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 176 | block_t blk_addr, struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 177 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 178 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 179 | struct f2fs_bio_info *io; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 180 | bool is_read = is_read_io(fio->rw); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 181 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 182 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 183 | |
| 184 | verify_block_addr(sbi, blk_addr); |
| 185 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 186 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 187 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 188 | if (!is_read) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 189 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 190 | |
Fan Li | 63a0b7cb | 2013-12-09 16:09:00 +0800 | [diff] [blame] | 191 | if (io->bio && (io->last_block_in_bio != blk_addr - 1 || |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 192 | io->fio.rw != fio->rw)) |
| 193 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 194 | alloc_new: |
| 195 | if (io->bio == NULL) { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 196 | int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); |
| 197 | |
| 198 | io->bio = __bio_alloc(sbi, blk_addr, bio_blocks, is_read); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 199 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) < |
| 203 | PAGE_CACHE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 204 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 205 | goto alloc_new; |
| 206 | } |
| 207 | |
| 208 | io->last_block_in_bio = blk_addr; |
| 209 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 210 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 211 | trace_f2fs_submit_page_mbio(page, fio->rw, fio->type, blk_addr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 215 | * Lock ordering for the change of data block address: |
| 216 | * ->data_page |
| 217 | * ->node_page |
| 218 | * update block addresses in the node page |
| 219 | */ |
| 220 | static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr) |
| 221 | { |
| 222 | struct f2fs_node *rn; |
| 223 | __le32 *addr_array; |
| 224 | struct page *node_page = dn->node_page; |
| 225 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 226 | |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 227 | f2fs_wait_on_page_writeback(node_page, NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 228 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 229 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 230 | |
| 231 | /* Get physical address of data block */ |
| 232 | addr_array = blkaddr_in_node(rn); |
| 233 | addr_array[ofs_in_node] = cpu_to_le32(new_addr); |
| 234 | set_page_dirty(node_page); |
| 235 | } |
| 236 | |
| 237 | int reserve_new_block(struct dnode_of_data *dn) |
| 238 | { |
| 239 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 240 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 241 | 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] | 242 | return -EPERM; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 243 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 244 | return -ENOSPC; |
| 245 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 246 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 247 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 248 | __set_data_blkaddr(dn, NEW_ADDR); |
| 249 | dn->data_blkaddr = NEW_ADDR; |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 250 | mark_inode_dirty(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 251 | sync_inode_page(dn); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 255 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 256 | { |
| 257 | bool need_put = dn->inode_page ? false : true; |
| 258 | int err; |
| 259 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 260 | /* if inode_page exists, index should be zero */ |
| 261 | f2fs_bug_on(!need_put && index); |
| 262 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 263 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 264 | if (err) |
| 265 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 266 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 267 | if (dn->data_blkaddr == NULL_ADDR) |
| 268 | err = reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 269 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 270 | f2fs_put_dnode(dn); |
| 271 | return err; |
| 272 | } |
| 273 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 274 | static int check_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 275 | struct buffer_head *bh_result) |
| 276 | { |
| 277 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 278 | pgoff_t start_fofs, end_fofs; |
| 279 | block_t start_blkaddr; |
| 280 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 281 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 282 | return 0; |
| 283 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 284 | read_lock(&fi->ext.ext_lock); |
| 285 | if (fi->ext.len == 0) { |
| 286 | read_unlock(&fi->ext.ext_lock); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 290 | stat_inc_total_hit(inode->i_sb); |
| 291 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 292 | start_fofs = fi->ext.fofs; |
| 293 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 294 | start_blkaddr = fi->ext.blk_addr; |
| 295 | |
| 296 | if (pgofs >= start_fofs && pgofs <= end_fofs) { |
| 297 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 298 | size_t count; |
| 299 | |
| 300 | clear_buffer_new(bh_result); |
| 301 | map_bh(bh_result, inode->i_sb, |
| 302 | start_blkaddr + pgofs - start_fofs); |
| 303 | count = end_fofs - pgofs + 1; |
| 304 | if (count < (UINT_MAX >> blkbits)) |
| 305 | bh_result->b_size = (count << blkbits); |
| 306 | else |
| 307 | bh_result->b_size = UINT_MAX; |
| 308 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 309 | stat_inc_read_hit(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 310 | read_unlock(&fi->ext.ext_lock); |
| 311 | return 1; |
| 312 | } |
| 313 | read_unlock(&fi->ext.ext_lock); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn) |
| 318 | { |
| 319 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 320 | pgoff_t fofs, start_fofs, end_fofs; |
| 321 | block_t start_blkaddr, end_blkaddr; |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 322 | int need_update = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 323 | |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 324 | f2fs_bug_on(blk_addr == NEW_ADDR); |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 325 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 326 | dn->ofs_in_node; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 327 | |
| 328 | /* Update the page address in the parent node */ |
| 329 | __set_data_blkaddr(dn, blk_addr); |
| 330 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 331 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 332 | return; |
| 333 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 334 | write_lock(&fi->ext.ext_lock); |
| 335 | |
| 336 | start_fofs = fi->ext.fofs; |
| 337 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 338 | start_blkaddr = fi->ext.blk_addr; |
| 339 | end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1; |
| 340 | |
| 341 | /* Drop and initialize the matched extent */ |
| 342 | if (fi->ext.len == 1 && fofs == start_fofs) |
| 343 | fi->ext.len = 0; |
| 344 | |
| 345 | /* Initial extent */ |
| 346 | if (fi->ext.len == 0) { |
| 347 | if (blk_addr != NULL_ADDR) { |
| 348 | fi->ext.fofs = fofs; |
| 349 | fi->ext.blk_addr = blk_addr; |
| 350 | fi->ext.len = 1; |
| 351 | } |
| 352 | goto end_update; |
| 353 | } |
| 354 | |
Namjae Jeon | 6224da8 | 2013-04-06 14:44:32 +0900 | [diff] [blame] | 355 | /* Front merge */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 356 | if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) { |
| 357 | fi->ext.fofs--; |
| 358 | fi->ext.blk_addr--; |
| 359 | fi->ext.len++; |
| 360 | goto end_update; |
| 361 | } |
| 362 | |
| 363 | /* Back merge */ |
| 364 | if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) { |
| 365 | fi->ext.len++; |
| 366 | goto end_update; |
| 367 | } |
| 368 | |
| 369 | /* Split the existing extent */ |
| 370 | if (fi->ext.len > 1 && |
| 371 | fofs >= start_fofs && fofs <= end_fofs) { |
| 372 | if ((end_fofs - fofs) < (fi->ext.len >> 1)) { |
| 373 | fi->ext.len = fofs - start_fofs; |
| 374 | } else { |
| 375 | fi->ext.fofs = fofs + 1; |
| 376 | fi->ext.blk_addr = start_blkaddr + |
| 377 | fofs - start_fofs + 1; |
| 378 | fi->ext.len -= fofs - start_fofs + 1; |
| 379 | } |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 380 | } else { |
| 381 | need_update = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 382 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 383 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 384 | /* Finally, if the extent is very fragmented, let's drop the cache. */ |
| 385 | if (fi->ext.len < F2FS_MIN_EXTENT_LEN) { |
| 386 | fi->ext.len = 0; |
| 387 | set_inode_flag(fi, FI_NO_EXTENT); |
| 388 | need_update = true; |
| 389 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 390 | end_update: |
| 391 | write_unlock(&fi->ext.ext_lock); |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 392 | if (need_update) |
| 393 | sync_inode_page(dn); |
| 394 | return; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 395 | } |
| 396 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 397 | 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] | 398 | { |
| 399 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 400 | struct address_space *mapping = inode->i_mapping; |
| 401 | struct dnode_of_data dn; |
| 402 | struct page *page; |
| 403 | int err; |
| 404 | |
| 405 | page = find_get_page(mapping, index); |
| 406 | if (page && PageUptodate(page)) |
| 407 | return page; |
| 408 | f2fs_put_page(page, 0); |
| 409 | |
| 410 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 411 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 412 | if (err) |
| 413 | return ERR_PTR(err); |
| 414 | f2fs_put_dnode(&dn); |
| 415 | |
| 416 | if (dn.data_blkaddr == NULL_ADDR) |
| 417 | return ERR_PTR(-ENOENT); |
| 418 | |
| 419 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 420 | if (unlikely(dn.data_blkaddr == NEW_ADDR)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 421 | return ERR_PTR(-EINVAL); |
| 422 | |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 423 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 424 | if (!page) |
| 425 | return ERR_PTR(-ENOMEM); |
| 426 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 427 | if (PageUptodate(page)) { |
| 428 | unlock_page(page); |
| 429 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 430 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 431 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 432 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 433 | sync ? READ_SYNC : READA); |
Chao Yu | 1069bbf | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 434 | if (err) |
| 435 | return ERR_PTR(err); |
| 436 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 437 | if (sync) { |
| 438 | wait_on_page_locked(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 439 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 440 | f2fs_put_page(page, 0); |
| 441 | return ERR_PTR(-EIO); |
| 442 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 443 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 444 | return page; |
| 445 | } |
| 446 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 447 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 448 | * If it tries to access a hole, return an error. |
| 449 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 450 | * whether this page exists or not. |
| 451 | */ |
| 452 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 453 | { |
| 454 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 455 | struct address_space *mapping = inode->i_mapping; |
| 456 | struct dnode_of_data dn; |
| 457 | struct page *page; |
| 458 | int err; |
| 459 | |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 460 | repeat: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 461 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 462 | if (!page) |
| 463 | return ERR_PTR(-ENOMEM); |
| 464 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 465 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 466 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 467 | if (err) { |
| 468 | f2fs_put_page(page, 1); |
| 469 | return ERR_PTR(err); |
| 470 | } |
| 471 | f2fs_put_dnode(&dn); |
| 472 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 473 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 474 | f2fs_put_page(page, 1); |
| 475 | return ERR_PTR(-ENOENT); |
| 476 | } |
| 477 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 478 | if (PageUptodate(page)) |
| 479 | return page; |
| 480 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 481 | /* |
| 482 | * A new dentry page is allocated but not able to be written, since its |
| 483 | * new inode page couldn't be allocated due to -ENOSPC. |
| 484 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 485 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 486 | */ |
| 487 | if (dn.data_blkaddr == NEW_ADDR) { |
| 488 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 489 | SetPageUptodate(page); |
| 490 | return page; |
| 491 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 492 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 493 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 494 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 495 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 496 | |
| 497 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 498 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 499 | f2fs_put_page(page, 1); |
| 500 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 501 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 502 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 503 | f2fs_put_page(page, 1); |
| 504 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 505 | } |
| 506 | return page; |
| 507 | } |
| 508 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 509 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 510 | * Caller ensures that this data page is never allocated. |
| 511 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 512 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 513 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 514 | * f2fs_unlock_op(). |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 515 | * Note that, ipage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 516 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 517 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 518 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 519 | { |
| 520 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 521 | struct address_space *mapping = inode->i_mapping; |
| 522 | struct page *page; |
| 523 | struct dnode_of_data dn; |
| 524 | int err; |
| 525 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 526 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 527 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 528 | if (err) |
| 529 | return ERR_PTR(err); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 530 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 531 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 532 | if (!page) { |
| 533 | err = -ENOMEM; |
| 534 | goto put_err; |
| 535 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 536 | |
| 537 | if (PageUptodate(page)) |
| 538 | return page; |
| 539 | |
| 540 | if (dn.data_blkaddr == NEW_ADDR) { |
| 541 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 542 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 543 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 544 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
| 545 | READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 546 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 547 | goto put_err; |
| 548 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 549 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 550 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 551 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 552 | err = -EIO; |
| 553 | goto put_err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 554 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 555 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 556 | f2fs_put_page(page, 1); |
| 557 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 558 | } |
| 559 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 560 | |
| 561 | if (new_i_size && |
| 562 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 563 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 564 | /* Only the directory inode sets new_i_size */ |
| 565 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 566 | } |
| 567 | return page; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 568 | |
| 569 | put_err: |
| 570 | f2fs_put_dnode(&dn); |
| 571 | return ERR_PTR(err); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 572 | } |
| 573 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 574 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 575 | { |
| 576 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 577 | struct f2fs_summary sum; |
| 578 | block_t new_blkaddr; |
| 579 | struct node_info ni; |
| 580 | int type; |
| 581 | |
| 582 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 583 | return -EPERM; |
| 584 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 585 | return -ENOSPC; |
| 586 | |
| 587 | __set_data_blkaddr(dn, NEW_ADDR); |
| 588 | dn->data_blkaddr = NEW_ADDR; |
| 589 | |
| 590 | get_node_info(sbi, dn->nid, &ni); |
| 591 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 592 | |
| 593 | type = CURSEG_WARM_DATA; |
| 594 | |
| 595 | allocate_data_block(sbi, NULL, NULL_ADDR, &new_blkaddr, &sum, type); |
| 596 | |
| 597 | /* direct IO doesn't use extent cache to maximize the performance */ |
| 598 | set_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT); |
| 599 | update_extent_cache(new_blkaddr, dn); |
| 600 | clear_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT); |
| 601 | |
| 602 | dn->data_blkaddr = new_blkaddr; |
| 603 | return 0; |
| 604 | } |
| 605 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 606 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 607 | * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh. |
| 608 | * If original data blocks are allocated, then give them to blockdev. |
| 609 | * Otherwise, |
| 610 | * a. preallocate requested block addresses |
| 611 | * b. do not use extent cache for better performance |
| 612 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 613 | */ |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 614 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 615 | struct buffer_head *bh_result, int create, bool fiemap) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 616 | { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 617 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 618 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 619 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 620 | struct dnode_of_data dn; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 621 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 622 | pgoff_t pgofs, end_offset; |
| 623 | int err = 0, ofs = 1; |
| 624 | bool allocated = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 625 | |
| 626 | /* Get the page offset from the block offset(iblock) */ |
| 627 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 628 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 629 | if (check_extent_cache(inode, pgofs, bh_result)) |
| 630 | goto out; |
| 631 | |
Huang Ying | 79e35dc | 2014-07-12 20:10:00 +0800 | [diff] [blame] | 632 | if (create) { |
| 633 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 634 | f2fs_lock_op(sbi); |
Huang Ying | 79e35dc | 2014-07-12 20:10:00 +0800 | [diff] [blame] | 635 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 636 | |
| 637 | /* When reading holes, we need its node page */ |
| 638 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 639 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 640 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 641 | if (err == -ENOENT) |
| 642 | err = 0; |
| 643 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 644 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 645 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 646 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 647 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 648 | if (dn.data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 649 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 650 | } else if (create) { |
| 651 | err = __allocate_data_block(&dn); |
| 652 | if (err) |
| 653 | goto put_out; |
| 654 | allocated = true; |
| 655 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 656 | } else { |
| 657 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 658 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 659 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 660 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 661 | bh_result->b_size = (((size_t)1) << blkbits); |
| 662 | dn.ofs_in_node++; |
| 663 | pgofs++; |
| 664 | |
| 665 | get_next: |
| 666 | if (dn.ofs_in_node >= end_offset) { |
| 667 | if (allocated) |
| 668 | sync_inode_page(&dn); |
| 669 | allocated = false; |
| 670 | f2fs_put_dnode(&dn); |
| 671 | |
| 672 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 673 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 674 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 675 | if (err == -ENOENT) |
| 676 | err = 0; |
| 677 | goto unlock_out; |
| 678 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 679 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 680 | goto put_out; |
| 681 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 682 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (maxblocks > (bh_result->b_size >> blkbits)) { |
| 686 | block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 687 | if (blkaddr == NULL_ADDR && create) { |
| 688 | err = __allocate_data_block(&dn); |
| 689 | if (err) |
| 690 | goto sync_out; |
| 691 | allocated = true; |
| 692 | blkaddr = dn.data_blkaddr; |
| 693 | } |
| 694 | /* Give more consecutive addresses for the read ahead */ |
| 695 | if (blkaddr == (bh_result->b_blocknr + ofs)) { |
| 696 | ofs++; |
| 697 | dn.ofs_in_node++; |
| 698 | pgofs++; |
| 699 | bh_result->b_size += (((size_t)1) << blkbits); |
| 700 | goto get_next; |
| 701 | } |
| 702 | } |
| 703 | sync_out: |
| 704 | if (allocated) |
| 705 | sync_inode_page(&dn); |
| 706 | put_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 707 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 708 | unlock_out: |
| 709 | if (create) |
| 710 | f2fs_unlock_op(sbi); |
| 711 | out: |
| 712 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
| 713 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 714 | } |
| 715 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 716 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 717 | struct buffer_head *bh_result, int create) |
| 718 | { |
| 719 | return __get_data_block(inode, iblock, bh_result, create, false); |
| 720 | } |
| 721 | |
| 722 | static int get_data_block_fiemap(struct inode *inode, sector_t iblock, |
| 723 | struct buffer_head *bh_result, int create) |
| 724 | { |
| 725 | return __get_data_block(inode, iblock, bh_result, create, true); |
| 726 | } |
| 727 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 728 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 729 | u64 start, u64 len) |
| 730 | { |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 731 | return generic_block_fiemap(inode, fieinfo, |
| 732 | start, len, get_data_block_fiemap); |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 733 | } |
| 734 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 735 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 736 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 737 | struct inode *inode = page->mapping->host; |
| 738 | int ret; |
| 739 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 740 | trace_f2fs_readpage(page, DATA); |
| 741 | |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 742 | /* If the file has inline data, try to read it directlly */ |
| 743 | if (f2fs_has_inline_data(inode)) |
| 744 | ret = f2fs_read_inline_data(inode, page); |
| 745 | else |
| 746 | ret = mpage_readpage(page, get_data_block); |
| 747 | |
| 748 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static int f2fs_read_data_pages(struct file *file, |
| 752 | struct address_space *mapping, |
| 753 | struct list_head *pages, unsigned nr_pages) |
| 754 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 755 | struct inode *inode = file->f_mapping->host; |
| 756 | |
| 757 | /* If the file has inline data, skip readpages */ |
| 758 | if (f2fs_has_inline_data(inode)) |
| 759 | return 0; |
| 760 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 761 | return mpage_readpages(mapping, pages, nr_pages, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 762 | } |
| 763 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 764 | 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] | 765 | { |
| 766 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 767 | block_t old_blkaddr, new_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 768 | struct dnode_of_data dn; |
| 769 | int err = 0; |
| 770 | |
| 771 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 772 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 773 | if (err) |
| 774 | return err; |
| 775 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 776 | old_blkaddr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 777 | |
| 778 | /* This page is already truncated */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 779 | if (old_blkaddr == NULL_ADDR) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 780 | goto out_writepage; |
| 781 | |
| 782 | set_page_writeback(page); |
| 783 | |
| 784 | /* |
| 785 | * If current allocation needs SSR, |
| 786 | * it had better in-place writes for updated data. |
| 787 | */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 788 | if (unlikely(old_blkaddr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 789 | !is_cold_data(page) && |
| 790 | need_inplace_update(inode))) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 791 | rewrite_data_page(page, old_blkaddr, fio); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 792 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 793 | } else { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 794 | write_data_page(page, &dn, &new_blkaddr, fio); |
| 795 | update_extent_cache(new_blkaddr, &dn); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 796 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 797 | } |
| 798 | out_writepage: |
| 799 | f2fs_put_dnode(&dn); |
| 800 | return err; |
| 801 | } |
| 802 | |
| 803 | static int f2fs_write_data_page(struct page *page, |
| 804 | struct writeback_control *wbc) |
| 805 | { |
| 806 | struct inode *inode = page->mapping->host; |
| 807 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 808 | loff_t i_size = i_size_read(inode); |
| 809 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 810 | >> PAGE_CACHE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 811 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 812 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 813 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 814 | struct f2fs_io_info fio = { |
| 815 | .type = DATA, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 816 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 817 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 818 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 819 | trace_f2fs_writepage(page, DATA); |
| 820 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 821 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 822 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | * If the offset is out-of-range of file size, |
| 826 | * this page does not have to be written to disk. |
| 827 | */ |
| 828 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 829 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 830 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 831 | |
| 832 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 833 | write: |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 834 | if (unlikely(sbi->por_doing)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 835 | goto redirty_out; |
| 836 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 837 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 838 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 839 | err = do_write_data_page(page, &fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 840 | goto done; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 841 | } |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 842 | |
| 843 | if (!wbc->for_reclaim) |
| 844 | need_balance_fs = true; |
| 845 | else if (has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 846 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 847 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 848 | f2fs_lock_op(sbi); |
| 849 | if (f2fs_has_inline_data(inode) || f2fs_may_inline(inode)) |
| 850 | err = f2fs_write_inline_data(inode, page, offset); |
| 851 | else |
| 852 | err = do_write_data_page(page, &fio); |
| 853 | f2fs_unlock_op(sbi); |
| 854 | done: |
| 855 | if (err && err != -ENOENT) |
| 856 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 857 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 858 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 859 | out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 860 | inode_dec_dirty_dents(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 861 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 862 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 863 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 2aea39e | 2014-04-24 09:49:52 +0900 | [diff] [blame] | 864 | if (wbc->for_reclaim) |
| 865 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 866 | return 0; |
| 867 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 868 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 869 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 870 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 871 | } |
| 872 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 873 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 874 | void *data) |
| 875 | { |
| 876 | struct address_space *mapping = data; |
| 877 | int ret = mapping->a_ops->writepage(page, wbc); |
| 878 | mapping_set_error(mapping, ret); |
| 879 | return ret; |
| 880 | } |
| 881 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 882 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 883 | struct writeback_control *wbc) |
| 884 | { |
| 885 | struct inode *inode = mapping->host; |
| 886 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 887 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 888 | int ret; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 889 | long diff; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 890 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 891 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 892 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 893 | /* deal with chardevs and other special file */ |
| 894 | if (!mapping->a_ops->writepage) |
| 895 | return 0; |
| 896 | |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 897 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 898 | get_dirty_dents(inode) < nr_pages_to_skip(sbi, DATA) && |
| 899 | available_free_memory(sbi, DIRTY_DENTS)) |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 900 | goto skip_write; |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 901 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 902 | diff = nr_pages_to_write(sbi, DATA, wbc); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 903 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 904 | if (!S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 905 | mutex_lock(&sbi->writepages); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 906 | locked = true; |
| 907 | } |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 908 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 909 | if (locked) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 910 | mutex_unlock(&sbi->writepages); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 911 | |
| 912 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 913 | |
| 914 | remove_dirty_dir_inode(inode); |
| 915 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 916 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 917 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 918 | |
| 919 | skip_write: |
| 920 | wbc->pages_skipped += get_dirty_dents(inode); |
| 921 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 922 | } |
| 923 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 924 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 925 | { |
| 926 | struct inode *inode = mapping->host; |
| 927 | |
| 928 | if (to > inode->i_size) { |
| 929 | truncate_pagecache(inode, inode->i_size); |
| 930 | truncate_blocks(inode, inode->i_size); |
| 931 | } |
| 932 | } |
| 933 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 934 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 935 | loff_t pos, unsigned len, unsigned flags, |
| 936 | struct page **pagep, void **fsdata) |
| 937 | { |
| 938 | struct inode *inode = mapping->host; |
| 939 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 940 | struct page *page; |
| 941 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 942 | struct dnode_of_data dn; |
| 943 | int err = 0; |
| 944 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 945 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 946 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 947 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 948 | repeat: |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 949 | err = f2fs_convert_inline_data(inode, pos + len); |
| 950 | if (err) |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 951 | goto fail; |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 952 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 953 | page = grab_cache_page_write_begin(mapping, index, flags); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 954 | if (!page) { |
| 955 | err = -ENOMEM; |
| 956 | goto fail; |
| 957 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 958 | |
| 959 | /* to avoid latency during memory pressure */ |
| 960 | unlock_page(page); |
| 961 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 962 | *pagep = page; |
| 963 | |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 964 | if (f2fs_has_inline_data(inode) && (pos + len) <= MAX_INLINE_DATA) |
| 965 | goto inline_data; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 966 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 967 | f2fs_lock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 968 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 969 | err = f2fs_reserve_block(&dn, index); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 970 | f2fs_unlock_op(sbi); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 971 | if (err) { |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 972 | f2fs_put_page(page, 0); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 973 | goto fail; |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 974 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 975 | inline_data: |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 976 | lock_page(page); |
| 977 | if (unlikely(page->mapping != mapping)) { |
| 978 | f2fs_put_page(page, 1); |
| 979 | goto repeat; |
| 980 | } |
| 981 | |
| 982 | f2fs_wait_on_page_writeback(page, DATA); |
| 983 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 984 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 985 | return 0; |
| 986 | |
| 987 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 988 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 989 | unsigned end = start + len; |
| 990 | |
| 991 | /* Reading beyond i_size is simple: memset to zero */ |
| 992 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 993 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | if (dn.data_blkaddr == NEW_ADDR) { |
| 997 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 998 | } else { |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 999 | if (f2fs_has_inline_data(inode)) { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1000 | err = f2fs_read_inline_data(inode, page); |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1001 | if (err) { |
| 1002 | page_cache_release(page); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1003 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1004 | } |
| 1005 | } else { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1006 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 1007 | READ_SYNC); |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1008 | if (err) |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1009 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1012 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1013 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1014 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1015 | err = -EIO; |
| 1016 | goto fail; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1017 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1018 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1019 | f2fs_put_page(page, 1); |
| 1020 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1021 | } |
| 1022 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1023 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1024 | SetPageUptodate(page); |
| 1025 | clear_cold_data(page); |
| 1026 | return 0; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1027 | fail: |
| 1028 | f2fs_write_failed(mapping, pos + len); |
| 1029 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1030 | } |
| 1031 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1032 | static int f2fs_write_end(struct file *file, |
| 1033 | struct address_space *mapping, |
| 1034 | loff_t pos, unsigned len, unsigned copied, |
| 1035 | struct page *page, void *fsdata) |
| 1036 | { |
| 1037 | struct inode *inode = page->mapping->host; |
| 1038 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 1039 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1040 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1041 | set_page_dirty(page); |
| 1042 | |
| 1043 | if (pos + copied > i_size_read(inode)) { |
| 1044 | i_size_write(inode, pos + copied); |
| 1045 | mark_inode_dirty(inode); |
| 1046 | update_inode_page(inode); |
| 1047 | } |
| 1048 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 1049 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1050 | return copied; |
| 1051 | } |
| 1052 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1053 | static int check_direct_IO(struct inode *inode, int rw, |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1054 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1055 | { |
| 1056 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1057 | |
| 1058 | if (rw == READ) |
| 1059 | return 0; |
| 1060 | |
| 1061 | if (offset & blocksize_mask) |
| 1062 | return -EINVAL; |
| 1063 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1064 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1065 | return -EINVAL; |
| 1066 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1070 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 1071 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1072 | { |
| 1073 | struct file *file = iocb->ki_filp; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1074 | struct address_space *mapping = file->f_mapping; |
| 1075 | struct inode *inode = mapping->host; |
| 1076 | size_t count = iov_iter_count(iter); |
| 1077 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1078 | |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1079 | /* Let buffer I/O handle the inline data case. */ |
| 1080 | if (f2fs_has_inline_data(inode)) |
| 1081 | return 0; |
| 1082 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1083 | if (check_direct_IO(inode, rw, iter, offset)) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1084 | return 0; |
| 1085 | |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 1086 | /* clear fsync mark to recover these blocks */ |
| 1087 | fsync_mark_clear(F2FS_SB(inode->i_sb), inode->i_ino); |
| 1088 | |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1089 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
| 1090 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1091 | err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block); |
| 1092 | if (err < 0 && (rw & WRITE)) |
| 1093 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1094 | |
| 1095 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
| 1096 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1097 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1098 | } |
| 1099 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 1100 | static void f2fs_invalidate_data_page(struct page *page, unsigned int offset, |
| 1101 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1102 | { |
| 1103 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 1fe54f9 | 2014-02-07 10:00:06 +0900 | [diff] [blame] | 1104 | if (PageDirty(page)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1105 | inode_dec_dirty_dents(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1106 | ClearPagePrivate(page); |
| 1107 | } |
| 1108 | |
| 1109 | static int f2fs_release_data_page(struct page *page, gfp_t wait) |
| 1110 | { |
| 1111 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 1112 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1116 | { |
| 1117 | struct address_space *mapping = page->mapping; |
| 1118 | struct inode *inode = mapping->host; |
| 1119 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1120 | trace_f2fs_set_page_dirty(page, DATA); |
| 1121 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1122 | SetPageUptodate(page); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 1123 | mark_inode_dirty(inode); |
| 1124 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1125 | if (!PageDirty(page)) { |
| 1126 | __set_page_dirty_nobuffers(page); |
| 1127 | set_dirty_dir_page(inode, page); |
| 1128 | return 1; |
| 1129 | } |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1133 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1134 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 1135 | struct inode *inode = mapping->host; |
| 1136 | |
| 1137 | if (f2fs_has_inline_data(inode)) |
| 1138 | return 0; |
| 1139 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1140 | return generic_block_bmap(mapping, block, get_data_block); |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1141 | } |
| 1142 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1143 | const struct address_space_operations f2fs_dblock_aops = { |
| 1144 | .readpage = f2fs_read_data_page, |
| 1145 | .readpages = f2fs_read_data_pages, |
| 1146 | .writepage = f2fs_write_data_page, |
| 1147 | .writepages = f2fs_write_data_pages, |
| 1148 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1149 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1150 | .set_page_dirty = f2fs_set_data_page_dirty, |
| 1151 | .invalidatepage = f2fs_invalidate_data_page, |
| 1152 | .releasepage = f2fs_release_data_page, |
| 1153 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1154 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1155 | }; |