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