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