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