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