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 | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 27 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 28 | * Lock ordering for the change of data block address: |
| 29 | * ->data_page |
| 30 | * ->node_page |
| 31 | * update block addresses in the node page |
| 32 | */ |
| 33 | static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr) |
| 34 | { |
| 35 | struct f2fs_node *rn; |
| 36 | __le32 *addr_array; |
| 37 | struct page *node_page = dn->node_page; |
| 38 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 39 | |
Jin Xu | a569469 | 2013-08-05 20:02:04 +0800 | [diff] [blame] | 40 | f2fs_wait_on_page_writeback(node_page, NODE, false); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 41 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 42 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 43 | |
| 44 | /* Get physical address of data block */ |
| 45 | addr_array = blkaddr_in_node(rn); |
| 46 | addr_array[ofs_in_node] = cpu_to_le32(new_addr); |
| 47 | set_page_dirty(node_page); |
| 48 | } |
| 49 | |
| 50 | int reserve_new_block(struct dnode_of_data *dn) |
| 51 | { |
| 52 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 53 | |
| 54 | if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)) |
| 55 | return -EPERM; |
| 56 | if (!inc_valid_block_count(sbi, dn->inode, 1)) |
| 57 | return -ENOSPC; |
| 58 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 59 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 60 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 61 | __set_data_blkaddr(dn, NEW_ADDR); |
| 62 | dn->data_blkaddr = NEW_ADDR; |
| 63 | sync_inode_page(dn); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int check_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 68 | struct buffer_head *bh_result) |
| 69 | { |
| 70 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 71 | pgoff_t start_fofs, end_fofs; |
| 72 | block_t start_blkaddr; |
| 73 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 74 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 75 | return 0; |
| 76 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 77 | read_lock(&fi->ext.ext_lock); |
| 78 | if (fi->ext.len == 0) { |
| 79 | read_unlock(&fi->ext.ext_lock); |
| 80 | return 0; |
| 81 | } |
| 82 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 83 | stat_inc_total_hit(inode->i_sb); |
| 84 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 85 | start_fofs = fi->ext.fofs; |
| 86 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 87 | start_blkaddr = fi->ext.blk_addr; |
| 88 | |
| 89 | if (pgofs >= start_fofs && pgofs <= end_fofs) { |
| 90 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 91 | size_t count; |
| 92 | |
| 93 | clear_buffer_new(bh_result); |
| 94 | map_bh(bh_result, inode->i_sb, |
| 95 | start_blkaddr + pgofs - start_fofs); |
| 96 | count = end_fofs - pgofs + 1; |
| 97 | if (count < (UINT_MAX >> blkbits)) |
| 98 | bh_result->b_size = (count << blkbits); |
| 99 | else |
| 100 | bh_result->b_size = UINT_MAX; |
| 101 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 102 | stat_inc_read_hit(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 103 | read_unlock(&fi->ext.ext_lock); |
| 104 | return 1; |
| 105 | } |
| 106 | read_unlock(&fi->ext.ext_lock); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn) |
| 111 | { |
| 112 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 113 | pgoff_t fofs, start_fofs, end_fofs; |
| 114 | block_t start_blkaddr, end_blkaddr; |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 115 | int need_update = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 116 | |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 117 | f2fs_bug_on(blk_addr == NEW_ADDR); |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 118 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 119 | dn->ofs_in_node; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 120 | |
| 121 | /* Update the page address in the parent node */ |
| 122 | __set_data_blkaddr(dn, blk_addr); |
| 123 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 124 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 125 | return; |
| 126 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 127 | write_lock(&fi->ext.ext_lock); |
| 128 | |
| 129 | start_fofs = fi->ext.fofs; |
| 130 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 131 | start_blkaddr = fi->ext.blk_addr; |
| 132 | end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1; |
| 133 | |
| 134 | /* Drop and initialize the matched extent */ |
| 135 | if (fi->ext.len == 1 && fofs == start_fofs) |
| 136 | fi->ext.len = 0; |
| 137 | |
| 138 | /* Initial extent */ |
| 139 | if (fi->ext.len == 0) { |
| 140 | if (blk_addr != NULL_ADDR) { |
| 141 | fi->ext.fofs = fofs; |
| 142 | fi->ext.blk_addr = blk_addr; |
| 143 | fi->ext.len = 1; |
| 144 | } |
| 145 | goto end_update; |
| 146 | } |
| 147 | |
Namjae Jeon | 6224da8 | 2013-04-06 14:44:32 +0900 | [diff] [blame] | 148 | /* Front merge */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 149 | if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) { |
| 150 | fi->ext.fofs--; |
| 151 | fi->ext.blk_addr--; |
| 152 | fi->ext.len++; |
| 153 | goto end_update; |
| 154 | } |
| 155 | |
| 156 | /* Back merge */ |
| 157 | if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) { |
| 158 | fi->ext.len++; |
| 159 | goto end_update; |
| 160 | } |
| 161 | |
| 162 | /* Split the existing extent */ |
| 163 | if (fi->ext.len > 1 && |
| 164 | fofs >= start_fofs && fofs <= end_fofs) { |
| 165 | if ((end_fofs - fofs) < (fi->ext.len >> 1)) { |
| 166 | fi->ext.len = fofs - start_fofs; |
| 167 | } else { |
| 168 | fi->ext.fofs = fofs + 1; |
| 169 | fi->ext.blk_addr = start_blkaddr + |
| 170 | fofs - start_fofs + 1; |
| 171 | fi->ext.len -= fofs - start_fofs + 1; |
| 172 | } |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 173 | } else { |
| 174 | need_update = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 175 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 176 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 177 | /* Finally, if the extent is very fragmented, let's drop the cache. */ |
| 178 | if (fi->ext.len < F2FS_MIN_EXTENT_LEN) { |
| 179 | fi->ext.len = 0; |
| 180 | set_inode_flag(fi, FI_NO_EXTENT); |
| 181 | need_update = true; |
| 182 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 183 | end_update: |
| 184 | write_unlock(&fi->ext.ext_lock); |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 185 | if (need_update) |
| 186 | sync_inode_page(dn); |
| 187 | return; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 190 | 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] | 191 | { |
| 192 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 193 | struct address_space *mapping = inode->i_mapping; |
| 194 | struct dnode_of_data dn; |
| 195 | struct page *page; |
| 196 | int err; |
| 197 | |
| 198 | page = find_get_page(mapping, index); |
| 199 | if (page && PageUptodate(page)) |
| 200 | return page; |
| 201 | f2fs_put_page(page, 0); |
| 202 | |
| 203 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 204 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 205 | if (err) |
| 206 | return ERR_PTR(err); |
| 207 | f2fs_put_dnode(&dn); |
| 208 | |
| 209 | if (dn.data_blkaddr == NULL_ADDR) |
| 210 | return ERR_PTR(-ENOENT); |
| 211 | |
| 212 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
| 213 | if (dn.data_blkaddr == NEW_ADDR) |
| 214 | return ERR_PTR(-EINVAL); |
| 215 | |
Jaegeuk Kim | 6f85b35 | 2013-05-20 16:15:22 +0900 | [diff] [blame] | 216 | page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 217 | if (!page) |
| 218 | return ERR_PTR(-ENOMEM); |
| 219 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 220 | if (PageUptodate(page)) { |
| 221 | unlock_page(page); |
| 222 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 223 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 224 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 225 | err = f2fs_readpage(sbi, page, dn.data_blkaddr, |
| 226 | sync ? READ_SYNC : READA); |
| 227 | if (sync) { |
| 228 | wait_on_page_locked(page); |
| 229 | if (!PageUptodate(page)) { |
| 230 | f2fs_put_page(page, 0); |
| 231 | return ERR_PTR(-EIO); |
| 232 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 233 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 234 | return page; |
| 235 | } |
| 236 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 237 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 238 | * If it tries to access a hole, return an error. |
| 239 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 240 | * whether this page exists or not. |
| 241 | */ |
| 242 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 243 | { |
| 244 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 245 | struct address_space *mapping = inode->i_mapping; |
| 246 | struct dnode_of_data dn; |
| 247 | struct page *page; |
| 248 | int err; |
| 249 | |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 250 | repeat: |
Jaegeuk Kim | 6f85b35 | 2013-05-20 16:15:22 +0900 | [diff] [blame] | 251 | page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 252 | if (!page) |
| 253 | return ERR_PTR(-ENOMEM); |
| 254 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 255 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 256 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 257 | if (err) { |
| 258 | f2fs_put_page(page, 1); |
| 259 | return ERR_PTR(err); |
| 260 | } |
| 261 | f2fs_put_dnode(&dn); |
| 262 | |
| 263 | if (dn.data_blkaddr == NULL_ADDR) { |
| 264 | f2fs_put_page(page, 1); |
| 265 | return ERR_PTR(-ENOENT); |
| 266 | } |
| 267 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 268 | if (PageUptodate(page)) |
| 269 | return page; |
| 270 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 271 | /* |
| 272 | * A new dentry page is allocated but not able to be written, since its |
| 273 | * new inode page couldn't be allocated due to -ENOSPC. |
| 274 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 275 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 276 | */ |
| 277 | if (dn.data_blkaddr == NEW_ADDR) { |
| 278 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 279 | SetPageUptodate(page); |
| 280 | return page; |
| 281 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 282 | |
| 283 | err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 284 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 285 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 286 | |
| 287 | lock_page(page); |
| 288 | if (!PageUptodate(page)) { |
| 289 | f2fs_put_page(page, 1); |
| 290 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 291 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 292 | if (page->mapping != mapping) { |
| 293 | f2fs_put_page(page, 1); |
| 294 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 295 | } |
| 296 | return page; |
| 297 | } |
| 298 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 299 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 300 | * Caller ensures that this data page is never allocated. |
| 301 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 302 | * |
| 303 | * Also, caller should grab and release a mutex by calling mutex_lock_op() and |
| 304 | * mutex_unlock_op(). |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 305 | * Note that, npage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 306 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 307 | struct page *get_new_data_page(struct inode *inode, |
| 308 | struct page *npage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 309 | { |
| 310 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 311 | struct address_space *mapping = inode->i_mapping; |
| 312 | struct page *page; |
| 313 | struct dnode_of_data dn; |
| 314 | int err; |
| 315 | |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 316 | set_new_dnode(&dn, inode, npage, npage, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 317 | err = get_dnode_of_data(&dn, index, ALLOC_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 318 | if (err) |
| 319 | return ERR_PTR(err); |
| 320 | |
| 321 | if (dn.data_blkaddr == NULL_ADDR) { |
| 322 | if (reserve_new_block(&dn)) { |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 323 | if (!npage) |
| 324 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 325 | return ERR_PTR(-ENOSPC); |
| 326 | } |
| 327 | } |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 328 | if (!npage) |
| 329 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 330 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 331 | page = grab_cache_page(mapping, index); |
| 332 | if (!page) |
| 333 | return ERR_PTR(-ENOMEM); |
| 334 | |
| 335 | if (PageUptodate(page)) |
| 336 | return page; |
| 337 | |
| 338 | if (dn.data_blkaddr == NEW_ADDR) { |
| 339 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 340 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 341 | } else { |
| 342 | err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 343 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 344 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 345 | lock_page(page); |
| 346 | if (!PageUptodate(page)) { |
| 347 | f2fs_put_page(page, 1); |
| 348 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 349 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 350 | if (page->mapping != mapping) { |
| 351 | f2fs_put_page(page, 1); |
| 352 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 353 | } |
| 354 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 355 | |
| 356 | if (new_i_size && |
| 357 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 358 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 359 | /* Only the directory inode sets new_i_size */ |
| 360 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 361 | mark_inode_dirty_sync(inode); |
| 362 | } |
| 363 | return page; |
| 364 | } |
| 365 | |
| 366 | static void read_end_io(struct bio *bio, int err) |
| 367 | { |
| 368 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 369 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 370 | |
| 371 | do { |
| 372 | struct page *page = bvec->bv_page; |
| 373 | |
| 374 | if (--bvec >= bio->bi_io_vec) |
| 375 | prefetchw(&bvec->bv_page->flags); |
| 376 | |
| 377 | if (uptodate) { |
| 378 | SetPageUptodate(page); |
| 379 | } else { |
| 380 | ClearPageUptodate(page); |
| 381 | SetPageError(page); |
| 382 | } |
| 383 | unlock_page(page); |
| 384 | } while (bvec >= bio->bi_io_vec); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 385 | bio_put(bio); |
| 386 | } |
| 387 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 388 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 389 | * Fill the locked page with data located in the block address. |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 390 | * Return unlocked page. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 391 | */ |
| 392 | int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page, |
| 393 | block_t blk_addr, int type) |
| 394 | { |
| 395 | struct block_device *bdev = sbi->sb->s_bdev; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 396 | struct bio *bio; |
| 397 | |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 398 | trace_f2fs_readpage(page, blk_addr, type); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 399 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 400 | /* Allocate a new bio */ |
Jaegeuk Kim | 3cd8a23 | 2012-12-10 09:26:05 +0900 | [diff] [blame] | 401 | bio = f2fs_bio_alloc(bdev, 1); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 402 | |
| 403 | /* Initialize the bio */ |
Jaegeuk Kim | 3cd8a23 | 2012-12-10 09:26:05 +0900 | [diff] [blame] | 404 | bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 405 | bio->bi_end_io = read_end_io; |
Jaegeuk Kim | 3cd8a23 | 2012-12-10 09:26:05 +0900 | [diff] [blame] | 406 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 407 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 408 | bio_put(bio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 409 | f2fs_put_page(page, 1); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 410 | return -EFAULT; |
| 411 | } |
| 412 | |
| 413 | submit_bio(type, bio); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Chao Yu | 924b720 | 2013-11-20 14:46:39 +0800 | [diff] [blame] | 417 | void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, int rw) |
| 418 | { |
| 419 | struct f2fs_bio_info *io = &sbi->read_io; |
| 420 | |
| 421 | if (!io->bio) |
| 422 | return; |
| 423 | |
Chao Yu | d4d288b | 2013-11-24 12:36:42 +0900 | [diff] [blame^] | 424 | trace_f2fs_submit_read_bio(sbi->sb, rw, META, io->bio); |
| 425 | |
Chao Yu | 924b720 | 2013-11-20 14:46:39 +0800 | [diff] [blame] | 426 | mutex_lock(&io->io_mutex); |
| 427 | if (io->bio) { |
| 428 | submit_bio(rw, io->bio); |
| 429 | io->bio = NULL; |
| 430 | } |
| 431 | mutex_unlock(&io->io_mutex); |
| 432 | } |
| 433 | |
| 434 | void submit_read_page(struct f2fs_sb_info *sbi, struct page *page, |
| 435 | block_t blk_addr, int rw) |
| 436 | { |
| 437 | struct block_device *bdev = sbi->sb->s_bdev; |
| 438 | struct f2fs_bio_info *io = &sbi->read_io; |
| 439 | int bio_blocks; |
| 440 | |
| 441 | verify_block_addr(sbi, blk_addr); |
| 442 | |
| 443 | mutex_lock(&io->io_mutex); |
| 444 | |
| 445 | if (io->bio && io->last_block_in_bio != blk_addr - 1) { |
| 446 | submit_bio(rw, io->bio); |
| 447 | io->bio = NULL; |
| 448 | } |
| 449 | alloc_new: |
| 450 | if (io->bio == NULL) { |
| 451 | bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); |
| 452 | io->bio = f2fs_bio_alloc(bdev, bio_blocks); |
| 453 | io->bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr); |
| 454 | io->bio->bi_end_io = read_end_io; |
| 455 | } |
| 456 | |
| 457 | if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) < |
| 458 | PAGE_CACHE_SIZE) { |
| 459 | submit_bio(rw, io->bio); |
| 460 | io->bio = NULL; |
| 461 | goto alloc_new; |
| 462 | } |
| 463 | |
| 464 | io->last_block_in_bio = blk_addr; |
| 465 | |
| 466 | mutex_unlock(&io->io_mutex); |
Chao Yu | 87b8872 | 2013-11-20 16:40:10 +0800 | [diff] [blame] | 467 | trace_f2fs_submit_read_page(page, rw, META, blk_addr); |
Chao Yu | 924b720 | 2013-11-20 14:46:39 +0800 | [diff] [blame] | 468 | } |
| 469 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 470 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 471 | * This function should be used by the data read flow only where it |
| 472 | * does not check the "create" flag that indicates block allocation. |
| 473 | * The reason for this special functionality is to exploit VFS readahead |
| 474 | * mechanism. |
| 475 | */ |
| 476 | static int get_data_block_ro(struct inode *inode, sector_t iblock, |
| 477 | struct buffer_head *bh_result, int create) |
| 478 | { |
| 479 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 480 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 481 | struct dnode_of_data dn; |
| 482 | pgoff_t pgofs; |
| 483 | int err; |
| 484 | |
| 485 | /* Get the page offset from the block offset(iblock) */ |
| 486 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 487 | |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 488 | if (check_extent_cache(inode, pgofs, bh_result)) { |
| 489 | trace_f2fs_get_data_block(inode, iblock, bh_result, 0); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 490 | return 0; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 491 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 492 | |
| 493 | /* When reading holes, we need its node page */ |
| 494 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 495 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 496 | if (err) { |
| 497 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 498 | return (err == -ENOENT) ? 0 : err; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 499 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 500 | |
| 501 | /* It does not support data allocation */ |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 502 | f2fs_bug_on(create); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 503 | |
| 504 | if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) { |
| 505 | int i; |
| 506 | unsigned int end_offset; |
| 507 | |
| 508 | end_offset = IS_INODE(dn.node_page) ? |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 509 | ADDRS_PER_INODE(F2FS_I(inode)) : |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 510 | ADDRS_PER_BLOCK; |
| 511 | |
| 512 | clear_buffer_new(bh_result); |
| 513 | |
| 514 | /* Give more consecutive addresses for the read ahead */ |
| 515 | for (i = 0; i < end_offset - dn.ofs_in_node; i++) |
| 516 | if (((datablock_addr(dn.node_page, |
| 517 | dn.ofs_in_node + i)) |
| 518 | != (dn.data_blkaddr + i)) || maxblocks == i) |
| 519 | break; |
| 520 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 521 | bh_result->b_size = (i << blkbits); |
| 522 | } |
| 523 | f2fs_put_dnode(&dn); |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 524 | trace_f2fs_get_data_block(inode, iblock, bh_result, 0); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 529 | { |
| 530 | return mpage_readpage(page, get_data_block_ro); |
| 531 | } |
| 532 | |
| 533 | static int f2fs_read_data_pages(struct file *file, |
| 534 | struct address_space *mapping, |
| 535 | struct list_head *pages, unsigned nr_pages) |
| 536 | { |
| 537 | return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro); |
| 538 | } |
| 539 | |
| 540 | int do_write_data_page(struct page *page) |
| 541 | { |
| 542 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 543 | block_t old_blk_addr, new_blk_addr; |
| 544 | struct dnode_of_data dn; |
| 545 | int err = 0; |
| 546 | |
| 547 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 548 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 549 | if (err) |
| 550 | return err; |
| 551 | |
| 552 | old_blk_addr = dn.data_blkaddr; |
| 553 | |
| 554 | /* This page is already truncated */ |
| 555 | if (old_blk_addr == NULL_ADDR) |
| 556 | goto out_writepage; |
| 557 | |
| 558 | set_page_writeback(page); |
| 559 | |
| 560 | /* |
| 561 | * If current allocation needs SSR, |
| 562 | * it had better in-place writes for updated data. |
| 563 | */ |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 564 | if (unlikely(old_blk_addr != NEW_ADDR && |
| 565 | !is_cold_data(page) && |
| 566 | need_inplace_update(inode))) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 567 | rewrite_data_page(F2FS_SB(inode->i_sb), page, |
| 568 | old_blk_addr); |
| 569 | } else { |
| 570 | write_data_page(inode, page, &dn, |
| 571 | old_blk_addr, &new_blk_addr); |
| 572 | update_extent_cache(new_blk_addr, &dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 573 | } |
| 574 | out_writepage: |
| 575 | f2fs_put_dnode(&dn); |
| 576 | return err; |
| 577 | } |
| 578 | |
| 579 | static int f2fs_write_data_page(struct page *page, |
| 580 | struct writeback_control *wbc) |
| 581 | { |
| 582 | struct inode *inode = page->mapping->host; |
| 583 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 584 | loff_t i_size = i_size_read(inode); |
| 585 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 586 | >> PAGE_CACHE_SHIFT; |
| 587 | unsigned offset; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 588 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 589 | int err = 0; |
| 590 | |
| 591 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 592 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * If the offset is out-of-range of file size, |
| 596 | * this page does not have to be written to disk. |
| 597 | */ |
| 598 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 599 | if ((page->index >= end_index + 1) || !offset) { |
| 600 | if (S_ISDIR(inode->i_mode)) { |
| 601 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 602 | inode_dec_dirty_dents(inode); |
| 603 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 604 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 608 | write: |
| 609 | if (sbi->por_doing) { |
| 610 | err = AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 611 | goto redirty_out; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 612 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 613 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 614 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 615 | if (S_ISDIR(inode->i_mode)) { |
| 616 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 617 | inode_dec_dirty_dents(inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 618 | err = do_write_data_page(page); |
| 619 | } else { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 620 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 621 | err = do_write_data_page(page); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 622 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 623 | need_balance_fs = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 624 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 625 | if (err == -ENOENT) |
| 626 | goto out; |
| 627 | else if (err) |
| 628 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 629 | |
| 630 | if (wbc->for_reclaim) |
| 631 | f2fs_submit_bio(sbi, DATA, true); |
| 632 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 633 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 634 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 635 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 636 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 637 | f2fs_balance_fs(sbi); |
| 638 | return 0; |
| 639 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 640 | redirty_out: |
| 641 | wbc->pages_skipped++; |
| 642 | set_page_dirty(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 643 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | #define MAX_DESIRED_PAGES_WP 4096 |
| 647 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 648 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 649 | void *data) |
| 650 | { |
| 651 | struct address_space *mapping = data; |
| 652 | int ret = mapping->a_ops->writepage(page, wbc); |
| 653 | mapping_set_error(mapping, ret); |
| 654 | return ret; |
| 655 | } |
| 656 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 657 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 658 | struct writeback_control *wbc) |
| 659 | { |
| 660 | struct inode *inode = mapping->host; |
| 661 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 662 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 663 | int ret; |
| 664 | long excess_nrtw = 0, desired_nrtw; |
| 665 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 666 | /* deal with chardevs and other special file */ |
| 667 | if (!mapping->a_ops->writepage) |
| 668 | return 0; |
| 669 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 670 | if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) { |
| 671 | desired_nrtw = MAX_DESIRED_PAGES_WP; |
| 672 | excess_nrtw = desired_nrtw - wbc->nr_to_write; |
| 673 | wbc->nr_to_write = desired_nrtw; |
| 674 | } |
| 675 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 676 | if (!S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 677 | mutex_lock(&sbi->writepages); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 678 | locked = true; |
| 679 | } |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 680 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 681 | if (locked) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 682 | mutex_unlock(&sbi->writepages); |
| 683 | f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL)); |
| 684 | |
| 685 | remove_dirty_dir_inode(inode); |
| 686 | |
| 687 | wbc->nr_to_write -= excess_nrtw; |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 692 | loff_t pos, unsigned len, unsigned flags, |
| 693 | struct page **pagep, void **fsdata) |
| 694 | { |
| 695 | struct inode *inode = mapping->host; |
| 696 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 697 | struct page *page; |
| 698 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 699 | struct dnode_of_data dn; |
| 700 | int err = 0; |
| 701 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 702 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 703 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 704 | page = grab_cache_page_write_begin(mapping, index, flags); |
| 705 | if (!page) |
| 706 | return -ENOMEM; |
| 707 | *pagep = page; |
| 708 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 709 | f2fs_lock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 710 | |
| 711 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 712 | err = get_dnode_of_data(&dn, index, ALLOC_NODE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 713 | if (err) |
| 714 | goto err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 715 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 716 | if (dn.data_blkaddr == NULL_ADDR) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 717 | err = reserve_new_block(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 718 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 719 | f2fs_put_dnode(&dn); |
| 720 | if (err) |
| 721 | goto err; |
| 722 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 723 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 724 | |
| 725 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 726 | return 0; |
| 727 | |
| 728 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 729 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 730 | unsigned end = start + len; |
| 731 | |
| 732 | /* Reading beyond i_size is simple: memset to zero */ |
| 733 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 734 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (dn.data_blkaddr == NEW_ADDR) { |
| 738 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 739 | } else { |
| 740 | err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 741 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 742 | return err; |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 743 | lock_page(page); |
| 744 | if (!PageUptodate(page)) { |
| 745 | f2fs_put_page(page, 1); |
| 746 | return -EIO; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 747 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 748 | if (page->mapping != mapping) { |
| 749 | f2fs_put_page(page, 1); |
| 750 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 751 | } |
| 752 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 753 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 754 | SetPageUptodate(page); |
| 755 | clear_cold_data(page); |
| 756 | return 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 757 | |
| 758 | err: |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 759 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 760 | f2fs_put_page(page, 1); |
| 761 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 762 | } |
| 763 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 764 | static int f2fs_write_end(struct file *file, |
| 765 | struct address_space *mapping, |
| 766 | loff_t pos, unsigned len, unsigned copied, |
| 767 | struct page *page, void *fsdata) |
| 768 | { |
| 769 | struct inode *inode = page->mapping->host; |
| 770 | |
| 771 | SetPageUptodate(page); |
| 772 | set_page_dirty(page); |
| 773 | |
| 774 | if (pos + copied > i_size_read(inode)) { |
| 775 | i_size_write(inode, pos + copied); |
| 776 | mark_inode_dirty(inode); |
| 777 | update_inode_page(inode); |
| 778 | } |
| 779 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 780 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 781 | return copied; |
| 782 | } |
| 783 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 784 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
| 785 | const struct iovec *iov, loff_t offset, unsigned long nr_segs) |
| 786 | { |
| 787 | struct file *file = iocb->ki_filp; |
| 788 | struct inode *inode = file->f_mapping->host; |
| 789 | |
| 790 | if (rw == WRITE) |
| 791 | return 0; |
| 792 | |
| 793 | /* Needs synchronization with the cleaner */ |
| 794 | return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, |
| 795 | get_data_block_ro); |
| 796 | } |
| 797 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 798 | static void f2fs_invalidate_data_page(struct page *page, unsigned int offset, |
| 799 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 800 | { |
| 801 | struct inode *inode = page->mapping->host; |
| 802 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 803 | if (S_ISDIR(inode->i_mode) && PageDirty(page)) { |
| 804 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 805 | inode_dec_dirty_dents(inode); |
| 806 | } |
| 807 | ClearPagePrivate(page); |
| 808 | } |
| 809 | |
| 810 | static int f2fs_release_data_page(struct page *page, gfp_t wait) |
| 811 | { |
| 812 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 813 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static int f2fs_set_data_page_dirty(struct page *page) |
| 817 | { |
| 818 | struct address_space *mapping = page->mapping; |
| 819 | struct inode *inode = mapping->host; |
| 820 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 821 | trace_f2fs_set_page_dirty(page, DATA); |
| 822 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 823 | SetPageUptodate(page); |
| 824 | if (!PageDirty(page)) { |
| 825 | __set_page_dirty_nobuffers(page); |
| 826 | set_dirty_dir_page(inode, page); |
| 827 | return 1; |
| 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 832 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 833 | { |
| 834 | return generic_block_bmap(mapping, block, get_data_block_ro); |
| 835 | } |
| 836 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 837 | const struct address_space_operations f2fs_dblock_aops = { |
| 838 | .readpage = f2fs_read_data_page, |
| 839 | .readpages = f2fs_read_data_pages, |
| 840 | .writepage = f2fs_write_data_page, |
| 841 | .writepages = f2fs_write_data_pages, |
| 842 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 843 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 844 | .set_page_dirty = f2fs_set_data_page_dirty, |
| 845 | .invalidatepage = f2fs_invalidate_data_page, |
| 846 | .releasepage = f2fs_release_data_page, |
| 847 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 848 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 849 | }; |