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