| 1 | /* |
| 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> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/backing-dev.h> |
| 17 | #include <linux/pagevec.h> |
| 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/bio.h> |
| 20 | #include <linux/prefetch.h> |
| 21 | #include <linux/uio.h> |
| 22 | #include <linux/cleancache.h> |
| 23 | |
| 24 | #include "f2fs.h" |
| 25 | #include "node.h" |
| 26 | #include "segment.h" |
| 27 | #include "trace.h" |
| 28 | #include <trace/events/f2fs.h> |
| 29 | |
| 30 | static void f2fs_read_end_io(struct bio *bio) |
| 31 | { |
| 32 | struct bio_vec *bvec; |
| 33 | int i; |
| 34 | |
| 35 | if (f2fs_bio_encrypted(bio)) { |
| 36 | if (bio->bi_error) { |
| 37 | f2fs_release_crypto_ctx(bio->bi_private); |
| 38 | } else { |
| 39 | f2fs_end_io_crypto_work(bio->bi_private, bio); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bio_for_each_segment_all(bvec, bio, i) { |
| 45 | struct page *page = bvec->bv_page; |
| 46 | |
| 47 | if (!bio->bi_error) { |
| 48 | SetPageUptodate(page); |
| 49 | } else { |
| 50 | ClearPageUptodate(page); |
| 51 | SetPageError(page); |
| 52 | } |
| 53 | unlock_page(page); |
| 54 | } |
| 55 | bio_put(bio); |
| 56 | } |
| 57 | |
| 58 | static void f2fs_write_end_io(struct bio *bio) |
| 59 | { |
| 60 | struct f2fs_sb_info *sbi = bio->bi_private; |
| 61 | struct bio_vec *bvec; |
| 62 | int i; |
| 63 | |
| 64 | bio_for_each_segment_all(bvec, bio, i) { |
| 65 | struct page *page = bvec->bv_page; |
| 66 | |
| 67 | f2fs_restore_and_release_control_page(&page); |
| 68 | |
| 69 | if (unlikely(bio->bi_error)) { |
| 70 | set_page_dirty(page); |
| 71 | set_bit(AS_EIO, &page->mapping->flags); |
| 72 | f2fs_stop_checkpoint(sbi); |
| 73 | } |
| 74 | end_page_writeback(page); |
| 75 | dec_page_count(sbi, F2FS_WRITEBACK); |
| 76 | } |
| 77 | |
| 78 | if (!get_pages(sbi, F2FS_WRITEBACK) && |
| 79 | !list_empty(&sbi->cp_wait.task_list)) |
| 80 | wake_up(&sbi->cp_wait); |
| 81 | |
| 82 | bio_put(bio); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Low-level block read/write IO operations. |
| 87 | */ |
| 88 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 89 | int npages, bool is_read) |
| 90 | { |
| 91 | struct bio *bio; |
| 92 | |
| 93 | bio = f2fs_bio_alloc(npages); |
| 94 | |
| 95 | bio->bi_bdev = sbi->sb->s_bdev; |
| 96 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
| 97 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
| 98 | bio->bi_private = is_read ? NULL : sbi; |
| 99 | |
| 100 | return bio; |
| 101 | } |
| 102 | |
| 103 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
| 104 | { |
| 105 | struct f2fs_io_info *fio = &io->fio; |
| 106 | |
| 107 | if (!io->bio) |
| 108 | return; |
| 109 | |
| 110 | if (is_read_io(fio->rw)) |
| 111 | trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio); |
| 112 | else |
| 113 | trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio); |
| 114 | |
| 115 | submit_bio(fio->rw, io->bio); |
| 116 | io->bio = NULL; |
| 117 | } |
| 118 | |
| 119 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
| 120 | enum page_type type, int rw) |
| 121 | { |
| 122 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 123 | struct f2fs_bio_info *io; |
| 124 | |
| 125 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 126 | |
| 127 | down_write(&io->io_rwsem); |
| 128 | |
| 129 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 130 | if (type >= META_FLUSH) { |
| 131 | io->fio.type = META_FLUSH; |
| 132 | if (test_opt(sbi, NOBARRIER)) |
| 133 | io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO; |
| 134 | else |
| 135 | io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO; |
| 136 | } |
| 137 | __submit_merged_bio(io); |
| 138 | up_write(&io->io_rwsem); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Fill the locked page with data located in the block address. |
| 143 | * Return unlocked page. |
| 144 | */ |
| 145 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
| 146 | { |
| 147 | struct bio *bio; |
| 148 | struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 149 | |
| 150 | trace_f2fs_submit_page_bio(page, fio); |
| 151 | f2fs_trace_ios(fio, 0); |
| 152 | |
| 153 | /* Allocate a new bio */ |
| 154 | bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw)); |
| 155 | |
| 156 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 157 | bio_put(bio); |
| 158 | return -EFAULT; |
| 159 | } |
| 160 | |
| 161 | submit_bio(fio->rw, bio); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | void f2fs_submit_page_mbio(struct f2fs_io_info *fio) |
| 166 | { |
| 167 | struct f2fs_sb_info *sbi = fio->sbi; |
| 168 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
| 169 | struct f2fs_bio_info *io; |
| 170 | bool is_read = is_read_io(fio->rw); |
| 171 | struct page *bio_page; |
| 172 | |
| 173 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
| 174 | |
| 175 | verify_block_addr(sbi, fio->blk_addr); |
| 176 | |
| 177 | down_write(&io->io_rwsem); |
| 178 | |
| 179 | if (!is_read) |
| 180 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 181 | |
| 182 | if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 || |
| 183 | io->fio.rw != fio->rw)) |
| 184 | __submit_merged_bio(io); |
| 185 | alloc_new: |
| 186 | if (io->bio == NULL) { |
| 187 | int bio_blocks = MAX_BIO_BLOCKS(sbi); |
| 188 | |
| 189 | io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read); |
| 190 | io->fio = *fio; |
| 191 | } |
| 192 | |
| 193 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 194 | |
| 195 | if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) < |
| 196 | PAGE_CACHE_SIZE) { |
| 197 | __submit_merged_bio(io); |
| 198 | goto alloc_new; |
| 199 | } |
| 200 | |
| 201 | io->last_block_in_bio = fio->blk_addr; |
| 202 | f2fs_trace_ios(fio, 0); |
| 203 | |
| 204 | up_write(&io->io_rwsem); |
| 205 | trace_f2fs_submit_page_mbio(fio->page, fio); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Lock ordering for the change of data block address: |
| 210 | * ->data_page |
| 211 | * ->node_page |
| 212 | * update block addresses in the node page |
| 213 | */ |
| 214 | void set_data_blkaddr(struct dnode_of_data *dn) |
| 215 | { |
| 216 | struct f2fs_node *rn; |
| 217 | __le32 *addr_array; |
| 218 | struct page *node_page = dn->node_page; |
| 219 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 220 | |
| 221 | f2fs_wait_on_page_writeback(node_page, NODE); |
| 222 | |
| 223 | rn = F2FS_NODE(node_page); |
| 224 | |
| 225 | /* Get physical address of data block */ |
| 226 | addr_array = blkaddr_in_node(rn); |
| 227 | addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
| 228 | if (set_page_dirty(node_page)) |
| 229 | dn->node_changed = true; |
| 230 | } |
| 231 | |
| 232 | int reserve_new_block(struct dnode_of_data *dn) |
| 233 | { |
| 234 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 235 | |
| 236 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 237 | return -EPERM; |
| 238 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 239 | return -ENOSPC; |
| 240 | |
| 241 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 242 | |
| 243 | dn->data_blkaddr = NEW_ADDR; |
| 244 | set_data_blkaddr(dn); |
| 245 | mark_inode_dirty(dn->inode); |
| 246 | sync_inode_page(dn); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 251 | { |
| 252 | bool need_put = dn->inode_page ? false : true; |
| 253 | int err; |
| 254 | |
| 255 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 256 | if (err) |
| 257 | return err; |
| 258 | |
| 259 | if (dn->data_blkaddr == NULL_ADDR) |
| 260 | err = reserve_new_block(dn); |
| 261 | if (err || need_put) |
| 262 | f2fs_put_dnode(dn); |
| 263 | return err; |
| 264 | } |
| 265 | |
| 266 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
| 267 | { |
| 268 | struct extent_info ei; |
| 269 | struct inode *inode = dn->inode; |
| 270 | |
| 271 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 272 | dn->data_blkaddr = ei.blk + index - ei.fofs; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | return f2fs_reserve_block(dn, index); |
| 277 | } |
| 278 | |
| 279 | struct page *get_read_data_page(struct inode *inode, pgoff_t index, |
| 280 | int rw, bool for_write) |
| 281 | { |
| 282 | struct address_space *mapping = inode->i_mapping; |
| 283 | struct dnode_of_data dn; |
| 284 | struct page *page; |
| 285 | struct extent_info ei; |
| 286 | int err; |
| 287 | struct f2fs_io_info fio = { |
| 288 | .sbi = F2FS_I_SB(inode), |
| 289 | .type = DATA, |
| 290 | .rw = rw, |
| 291 | .encrypted_page = NULL, |
| 292 | }; |
| 293 | |
| 294 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
| 295 | return read_mapping_page(mapping, index, NULL); |
| 296 | |
| 297 | page = f2fs_grab_cache_page(mapping, index, for_write); |
| 298 | if (!page) |
| 299 | return ERR_PTR(-ENOMEM); |
| 300 | |
| 301 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 302 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 303 | goto got_it; |
| 304 | } |
| 305 | |
| 306 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 307 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 308 | if (err) |
| 309 | goto put_err; |
| 310 | f2fs_put_dnode(&dn); |
| 311 | |
| 312 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
| 313 | err = -ENOENT; |
| 314 | goto put_err; |
| 315 | } |
| 316 | got_it: |
| 317 | if (PageUptodate(page)) { |
| 318 | unlock_page(page); |
| 319 | return page; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * A new dentry page is allocated but not able to be written, since its |
| 324 | * new inode page couldn't be allocated due to -ENOSPC. |
| 325 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 326 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 327 | */ |
| 328 | if (dn.data_blkaddr == NEW_ADDR) { |
| 329 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 330 | SetPageUptodate(page); |
| 331 | unlock_page(page); |
| 332 | return page; |
| 333 | } |
| 334 | |
| 335 | fio.blk_addr = dn.data_blkaddr; |
| 336 | fio.page = page; |
| 337 | err = f2fs_submit_page_bio(&fio); |
| 338 | if (err) |
| 339 | goto put_err; |
| 340 | return page; |
| 341 | |
| 342 | put_err: |
| 343 | f2fs_put_page(page, 1); |
| 344 | return ERR_PTR(err); |
| 345 | } |
| 346 | |
| 347 | struct page *find_data_page(struct inode *inode, pgoff_t index) |
| 348 | { |
| 349 | struct address_space *mapping = inode->i_mapping; |
| 350 | struct page *page; |
| 351 | |
| 352 | page = find_get_page(mapping, index); |
| 353 | if (page && PageUptodate(page)) |
| 354 | return page; |
| 355 | f2fs_put_page(page, 0); |
| 356 | |
| 357 | page = get_read_data_page(inode, index, READ_SYNC, false); |
| 358 | if (IS_ERR(page)) |
| 359 | return page; |
| 360 | |
| 361 | if (PageUptodate(page)) |
| 362 | return page; |
| 363 | |
| 364 | wait_on_page_locked(page); |
| 365 | if (unlikely(!PageUptodate(page))) { |
| 366 | f2fs_put_page(page, 0); |
| 367 | return ERR_PTR(-EIO); |
| 368 | } |
| 369 | return page; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * If it tries to access a hole, return an error. |
| 374 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 375 | * whether this page exists or not. |
| 376 | */ |
| 377 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index, |
| 378 | bool for_write) |
| 379 | { |
| 380 | struct address_space *mapping = inode->i_mapping; |
| 381 | struct page *page; |
| 382 | repeat: |
| 383 | page = get_read_data_page(inode, index, READ_SYNC, for_write); |
| 384 | if (IS_ERR(page)) |
| 385 | return page; |
| 386 | |
| 387 | /* wait for read completion */ |
| 388 | lock_page(page); |
| 389 | if (unlikely(!PageUptodate(page))) { |
| 390 | f2fs_put_page(page, 1); |
| 391 | return ERR_PTR(-EIO); |
| 392 | } |
| 393 | if (unlikely(page->mapping != mapping)) { |
| 394 | f2fs_put_page(page, 1); |
| 395 | goto repeat; |
| 396 | } |
| 397 | return page; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Caller ensures that this data page is never allocated. |
| 402 | * A new zero-filled data page is allocated in the page cache. |
| 403 | * |
| 404 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 405 | * f2fs_unlock_op(). |
| 406 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
| 407 | * ipage should be released by this function. |
| 408 | */ |
| 409 | struct page *get_new_data_page(struct inode *inode, |
| 410 | struct page *ipage, pgoff_t index, bool new_i_size) |
| 411 | { |
| 412 | struct address_space *mapping = inode->i_mapping; |
| 413 | struct page *page; |
| 414 | struct dnode_of_data dn; |
| 415 | int err; |
| 416 | |
| 417 | page = f2fs_grab_cache_page(mapping, index, true); |
| 418 | if (!page) { |
| 419 | /* |
| 420 | * before exiting, we should make sure ipage will be released |
| 421 | * if any error occur. |
| 422 | */ |
| 423 | f2fs_put_page(ipage, 1); |
| 424 | return ERR_PTR(-ENOMEM); |
| 425 | } |
| 426 | |
| 427 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
| 428 | err = f2fs_reserve_block(&dn, index); |
| 429 | if (err) { |
| 430 | f2fs_put_page(page, 1); |
| 431 | return ERR_PTR(err); |
| 432 | } |
| 433 | if (!ipage) |
| 434 | f2fs_put_dnode(&dn); |
| 435 | |
| 436 | if (PageUptodate(page)) |
| 437 | goto got_it; |
| 438 | |
| 439 | if (dn.data_blkaddr == NEW_ADDR) { |
| 440 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 441 | SetPageUptodate(page); |
| 442 | } else { |
| 443 | f2fs_put_page(page, 1); |
| 444 | |
| 445 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
| 446 | f2fs_bug_on(F2FS_I_SB(inode), ipage); |
| 447 | page = get_lock_data_page(inode, index, true); |
| 448 | if (IS_ERR(page)) |
| 449 | return page; |
| 450 | } |
| 451 | got_it: |
| 452 | if (new_i_size && i_size_read(inode) < |
| 453 | ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) { |
| 454 | i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)); |
| 455 | /* Only the directory inode sets new_i_size */ |
| 456 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
| 457 | } |
| 458 | return page; |
| 459 | } |
| 460 | |
| 461 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 462 | { |
| 463 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 464 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 465 | struct f2fs_summary sum; |
| 466 | struct node_info ni; |
| 467 | int seg = CURSEG_WARM_DATA; |
| 468 | pgoff_t fofs; |
| 469 | |
| 470 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 471 | return -EPERM; |
| 472 | |
| 473 | dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); |
| 474 | if (dn->data_blkaddr == NEW_ADDR) |
| 475 | goto alloc; |
| 476 | |
| 477 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 478 | return -ENOSPC; |
| 479 | |
| 480 | alloc: |
| 481 | get_node_info(sbi, dn->nid, &ni); |
| 482 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 483 | |
| 484 | if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page) |
| 485 | seg = CURSEG_DIRECT_IO; |
| 486 | |
| 487 | allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr, |
| 488 | &sum, seg); |
| 489 | set_data_blkaddr(dn); |
| 490 | |
| 491 | /* update i_size */ |
| 492 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 493 | dn->ofs_in_node; |
| 494 | if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT)) |
| 495 | i_size_write(dn->inode, |
| 496 | ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT)); |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int __allocate_data_blocks(struct inode *inode, loff_t offset, |
| 501 | size_t count) |
| 502 | { |
| 503 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 504 | struct dnode_of_data dn; |
| 505 | u64 start = F2FS_BYTES_TO_BLK(offset); |
| 506 | u64 len = F2FS_BYTES_TO_BLK(count); |
| 507 | bool allocated; |
| 508 | u64 end_offset; |
| 509 | int err = 0; |
| 510 | |
| 511 | while (len) { |
| 512 | f2fs_lock_op(sbi); |
| 513 | |
| 514 | /* When reading holes, we need its node page */ |
| 515 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 516 | err = get_dnode_of_data(&dn, start, ALLOC_NODE); |
| 517 | if (err) |
| 518 | goto out; |
| 519 | |
| 520 | allocated = false; |
| 521 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 522 | |
| 523 | while (dn.ofs_in_node < end_offset && len) { |
| 524 | block_t blkaddr; |
| 525 | |
| 526 | if (unlikely(f2fs_cp_error(sbi))) { |
| 527 | err = -EIO; |
| 528 | goto sync_out; |
| 529 | } |
| 530 | |
| 531 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 532 | if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) { |
| 533 | err = __allocate_data_block(&dn); |
| 534 | if (err) |
| 535 | goto sync_out; |
| 536 | allocated = true; |
| 537 | } |
| 538 | len--; |
| 539 | start++; |
| 540 | dn.ofs_in_node++; |
| 541 | } |
| 542 | |
| 543 | if (allocated) |
| 544 | sync_inode_page(&dn); |
| 545 | |
| 546 | f2fs_put_dnode(&dn); |
| 547 | f2fs_unlock_op(sbi); |
| 548 | |
| 549 | if (dn.node_changed) |
| 550 | f2fs_balance_fs(sbi); |
| 551 | } |
| 552 | return err; |
| 553 | |
| 554 | sync_out: |
| 555 | if (allocated) |
| 556 | sync_inode_page(&dn); |
| 557 | f2fs_put_dnode(&dn); |
| 558 | out: |
| 559 | f2fs_unlock_op(sbi); |
| 560 | if (dn.node_changed) |
| 561 | f2fs_balance_fs(sbi); |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
| 567 | * f2fs_map_blocks structure. |
| 568 | * If original data blocks are allocated, then give them to blockdev. |
| 569 | * Otherwise, |
| 570 | * a. preallocate requested block addresses |
| 571 | * b. do not use extent cache for better performance |
| 572 | * c. give the block addresses to blockdev |
| 573 | */ |
| 574 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
| 575 | int create, int flag) |
| 576 | { |
| 577 | unsigned int maxblocks = map->m_len; |
| 578 | struct dnode_of_data dn; |
| 579 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 580 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 581 | pgoff_t pgofs, end_offset; |
| 582 | int err = 0, ofs = 1; |
| 583 | struct extent_info ei; |
| 584 | bool allocated = false; |
| 585 | block_t blkaddr; |
| 586 | |
| 587 | map->m_len = 0; |
| 588 | map->m_flags = 0; |
| 589 | |
| 590 | /* it only supports block size == page size */ |
| 591 | pgofs = (pgoff_t)map->m_lblk; |
| 592 | |
| 593 | if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
| 594 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
| 595 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); |
| 596 | map->m_flags = F2FS_MAP_MAPPED; |
| 597 | goto out; |
| 598 | } |
| 599 | |
| 600 | if (create) |
| 601 | f2fs_lock_op(sbi); |
| 602 | |
| 603 | /* When reading holes, we need its node page */ |
| 604 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 605 | err = get_dnode_of_data(&dn, pgofs, mode); |
| 606 | if (err) { |
| 607 | if (err == -ENOENT) |
| 608 | err = 0; |
| 609 | goto unlock_out; |
| 610 | } |
| 611 | |
| 612 | if (dn.data_blkaddr == NEW_ADDR || dn.data_blkaddr == NULL_ADDR) { |
| 613 | if (create) { |
| 614 | if (unlikely(f2fs_cp_error(sbi))) { |
| 615 | err = -EIO; |
| 616 | goto put_out; |
| 617 | } |
| 618 | err = __allocate_data_block(&dn); |
| 619 | if (err) |
| 620 | goto put_out; |
| 621 | allocated = true; |
| 622 | map->m_flags = F2FS_MAP_NEW; |
| 623 | } else { |
| 624 | if (flag != F2FS_GET_BLOCK_FIEMAP || |
| 625 | dn.data_blkaddr != NEW_ADDR) { |
| 626 | if (flag == F2FS_GET_BLOCK_BMAP) |
| 627 | err = -ENOENT; |
| 628 | goto put_out; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * preallocated unwritten block should be mapped |
| 633 | * for fiemap. |
| 634 | */ |
| 635 | if (dn.data_blkaddr == NEW_ADDR) |
| 636 | map->m_flags = F2FS_MAP_UNWRITTEN; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | map->m_flags |= F2FS_MAP_MAPPED; |
| 641 | map->m_pblk = dn.data_blkaddr; |
| 642 | map->m_len = 1; |
| 643 | |
| 644 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 645 | dn.ofs_in_node++; |
| 646 | pgofs++; |
| 647 | |
| 648 | get_next: |
| 649 | if (map->m_len >= maxblocks) |
| 650 | goto sync_out; |
| 651 | |
| 652 | if (dn.ofs_in_node >= end_offset) { |
| 653 | if (allocated) |
| 654 | sync_inode_page(&dn); |
| 655 | allocated = false; |
| 656 | f2fs_put_dnode(&dn); |
| 657 | |
| 658 | if (create) { |
| 659 | f2fs_unlock_op(sbi); |
| 660 | if (dn.node_changed) |
| 661 | f2fs_balance_fs(sbi); |
| 662 | f2fs_lock_op(sbi); |
| 663 | } |
| 664 | |
| 665 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 666 | err = get_dnode_of_data(&dn, pgofs, mode); |
| 667 | if (err) { |
| 668 | if (err == -ENOENT) |
| 669 | err = 0; |
| 670 | goto unlock_out; |
| 671 | } |
| 672 | |
| 673 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 674 | } |
| 675 | |
| 676 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 677 | |
| 678 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) { |
| 679 | if (create) { |
| 680 | if (unlikely(f2fs_cp_error(sbi))) { |
| 681 | err = -EIO; |
| 682 | goto sync_out; |
| 683 | } |
| 684 | err = __allocate_data_block(&dn); |
| 685 | if (err) |
| 686 | goto sync_out; |
| 687 | allocated = true; |
| 688 | map->m_flags |= F2FS_MAP_NEW; |
| 689 | blkaddr = dn.data_blkaddr; |
| 690 | } else { |
| 691 | /* |
| 692 | * we only merge preallocated unwritten blocks |
| 693 | * for fiemap. |
| 694 | */ |
| 695 | if (flag != F2FS_GET_BLOCK_FIEMAP || |
| 696 | blkaddr != NEW_ADDR) |
| 697 | goto sync_out; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* Give more consecutive addresses for the readahead */ |
| 702 | if ((map->m_pblk != NEW_ADDR && |
| 703 | blkaddr == (map->m_pblk + ofs)) || |
| 704 | (map->m_pblk == NEW_ADDR && |
| 705 | blkaddr == NEW_ADDR)) { |
| 706 | ofs++; |
| 707 | dn.ofs_in_node++; |
| 708 | pgofs++; |
| 709 | map->m_len++; |
| 710 | goto get_next; |
| 711 | } |
| 712 | |
| 713 | sync_out: |
| 714 | if (allocated) |
| 715 | sync_inode_page(&dn); |
| 716 | put_out: |
| 717 | f2fs_put_dnode(&dn); |
| 718 | unlock_out: |
| 719 | if (create) { |
| 720 | f2fs_unlock_op(sbi); |
| 721 | if (dn.node_changed) |
| 722 | f2fs_balance_fs(sbi); |
| 723 | } |
| 724 | out: |
| 725 | trace_f2fs_map_blocks(inode, map, err); |
| 726 | return err; |
| 727 | } |
| 728 | |
| 729 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 730 | struct buffer_head *bh, int create, int flag) |
| 731 | { |
| 732 | struct f2fs_map_blocks map; |
| 733 | int ret; |
| 734 | |
| 735 | map.m_lblk = iblock; |
| 736 | map.m_len = bh->b_size >> inode->i_blkbits; |
| 737 | |
| 738 | ret = f2fs_map_blocks(inode, &map, create, flag); |
| 739 | if (!ret) { |
| 740 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 741 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; |
| 742 | bh->b_size = map.m_len << inode->i_blkbits; |
| 743 | } |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 748 | struct buffer_head *bh_result, int create, int flag) |
| 749 | { |
| 750 | return __get_data_block(inode, iblock, bh_result, create, flag); |
| 751 | } |
| 752 | |
| 753 | static int get_data_block_dio(struct inode *inode, sector_t iblock, |
| 754 | struct buffer_head *bh_result, int create) |
| 755 | { |
| 756 | return __get_data_block(inode, iblock, bh_result, create, |
| 757 | F2FS_GET_BLOCK_DIO); |
| 758 | } |
| 759 | |
| 760 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
| 761 | struct buffer_head *bh_result, int create) |
| 762 | { |
| 763 | /* Block number less than F2FS MAX BLOCKS */ |
| 764 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
| 765 | return -EFBIG; |
| 766 | |
| 767 | return __get_data_block(inode, iblock, bh_result, create, |
| 768 | F2FS_GET_BLOCK_BMAP); |
| 769 | } |
| 770 | |
| 771 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 772 | { |
| 773 | return (offset >> inode->i_blkbits); |
| 774 | } |
| 775 | |
| 776 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 777 | { |
| 778 | return (blk << inode->i_blkbits); |
| 779 | } |
| 780 | |
| 781 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 782 | u64 start, u64 len) |
| 783 | { |
| 784 | struct buffer_head map_bh; |
| 785 | sector_t start_blk, last_blk; |
| 786 | loff_t isize; |
| 787 | u64 logical = 0, phys = 0, size = 0; |
| 788 | u32 flags = 0; |
| 789 | int ret = 0; |
| 790 | |
| 791 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 792 | if (ret) |
| 793 | return ret; |
| 794 | |
| 795 | if (f2fs_has_inline_data(inode)) { |
| 796 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 797 | if (ret != -EAGAIN) |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | mutex_lock(&inode->i_mutex); |
| 802 | |
| 803 | isize = i_size_read(inode); |
| 804 | if (start >= isize) |
| 805 | goto out; |
| 806 | |
| 807 | if (start + len > isize) |
| 808 | len = isize - start; |
| 809 | |
| 810 | if (logical_to_blk(inode, len) == 0) |
| 811 | len = blk_to_logical(inode, 1); |
| 812 | |
| 813 | start_blk = logical_to_blk(inode, start); |
| 814 | last_blk = logical_to_blk(inode, start + len - 1); |
| 815 | |
| 816 | next: |
| 817 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 818 | map_bh.b_size = len; |
| 819 | |
| 820 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
| 821 | F2FS_GET_BLOCK_FIEMAP); |
| 822 | if (ret) |
| 823 | goto out; |
| 824 | |
| 825 | /* HOLE */ |
| 826 | if (!buffer_mapped(&map_bh)) { |
| 827 | /* Go through holes util pass the EOF */ |
| 828 | if (blk_to_logical(inode, start_blk++) < isize) |
| 829 | goto prep_next; |
| 830 | /* Found a hole beyond isize means no more extents. |
| 831 | * Note that the premise is that filesystems don't |
| 832 | * punch holes beyond isize and keep size unchanged. |
| 833 | */ |
| 834 | flags |= FIEMAP_EXTENT_LAST; |
| 835 | } |
| 836 | |
| 837 | if (size) |
| 838 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 839 | phys, size, flags); |
| 840 | |
| 841 | if (start_blk > last_blk || ret) |
| 842 | goto out; |
| 843 | |
| 844 | logical = blk_to_logical(inode, start_blk); |
| 845 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 846 | size = map_bh.b_size; |
| 847 | flags = 0; |
| 848 | if (buffer_unwritten(&map_bh)) |
| 849 | flags = FIEMAP_EXTENT_UNWRITTEN; |
| 850 | |
| 851 | start_blk += logical_to_blk(inode, size); |
| 852 | |
| 853 | prep_next: |
| 854 | cond_resched(); |
| 855 | if (fatal_signal_pending(current)) |
| 856 | ret = -EINTR; |
| 857 | else |
| 858 | goto next; |
| 859 | out: |
| 860 | if (ret == 1) |
| 861 | ret = 0; |
| 862 | |
| 863 | mutex_unlock(&inode->i_mutex); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 869 | * Major change was from block_size == page_size in f2fs by default. |
| 870 | */ |
| 871 | static int f2fs_mpage_readpages(struct address_space *mapping, |
| 872 | struct list_head *pages, struct page *page, |
| 873 | unsigned nr_pages) |
| 874 | { |
| 875 | struct bio *bio = NULL; |
| 876 | unsigned page_idx; |
| 877 | sector_t last_block_in_bio = 0; |
| 878 | struct inode *inode = mapping->host; |
| 879 | const unsigned blkbits = inode->i_blkbits; |
| 880 | const unsigned blocksize = 1 << blkbits; |
| 881 | sector_t block_in_file; |
| 882 | sector_t last_block; |
| 883 | sector_t last_block_in_file; |
| 884 | sector_t block_nr; |
| 885 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 886 | struct f2fs_map_blocks map; |
| 887 | |
| 888 | map.m_pblk = 0; |
| 889 | map.m_lblk = 0; |
| 890 | map.m_len = 0; |
| 891 | map.m_flags = 0; |
| 892 | |
| 893 | for (page_idx = 0; nr_pages; page_idx++, nr_pages--) { |
| 894 | |
| 895 | prefetchw(&page->flags); |
| 896 | if (pages) { |
| 897 | page = list_entry(pages->prev, struct page, lru); |
| 898 | list_del(&page->lru); |
| 899 | if (add_to_page_cache_lru(page, mapping, |
| 900 | page->index, GFP_KERNEL)) |
| 901 | goto next_page; |
| 902 | } |
| 903 | |
| 904 | block_in_file = (sector_t)page->index; |
| 905 | last_block = block_in_file + nr_pages; |
| 906 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> |
| 907 | blkbits; |
| 908 | if (last_block > last_block_in_file) |
| 909 | last_block = last_block_in_file; |
| 910 | |
| 911 | /* |
| 912 | * Map blocks using the previous result first. |
| 913 | */ |
| 914 | if ((map.m_flags & F2FS_MAP_MAPPED) && |
| 915 | block_in_file > map.m_lblk && |
| 916 | block_in_file < (map.m_lblk + map.m_len)) |
| 917 | goto got_it; |
| 918 | |
| 919 | /* |
| 920 | * Then do more f2fs_map_blocks() calls until we are |
| 921 | * done with this page. |
| 922 | */ |
| 923 | map.m_flags = 0; |
| 924 | |
| 925 | if (block_in_file < last_block) { |
| 926 | map.m_lblk = block_in_file; |
| 927 | map.m_len = last_block - block_in_file; |
| 928 | |
| 929 | if (f2fs_map_blocks(inode, &map, 0, |
| 930 | F2FS_GET_BLOCK_READ)) |
| 931 | goto set_error_page; |
| 932 | } |
| 933 | got_it: |
| 934 | if ((map.m_flags & F2FS_MAP_MAPPED)) { |
| 935 | block_nr = map.m_pblk + block_in_file - map.m_lblk; |
| 936 | SetPageMappedToDisk(page); |
| 937 | |
| 938 | if (!PageUptodate(page) && !cleancache_get_page(page)) { |
| 939 | SetPageUptodate(page); |
| 940 | goto confused; |
| 941 | } |
| 942 | } else { |
| 943 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 944 | SetPageUptodate(page); |
| 945 | unlock_page(page); |
| 946 | goto next_page; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * This page will go to BIO. Do we need to send this |
| 951 | * BIO off first? |
| 952 | */ |
| 953 | if (bio && (last_block_in_bio != block_nr - 1)) { |
| 954 | submit_and_realloc: |
| 955 | submit_bio(READ, bio); |
| 956 | bio = NULL; |
| 957 | } |
| 958 | if (bio == NULL) { |
| 959 | struct f2fs_crypto_ctx *ctx = NULL; |
| 960 | |
| 961 | if (f2fs_encrypted_inode(inode) && |
| 962 | S_ISREG(inode->i_mode)) { |
| 963 | |
| 964 | ctx = f2fs_get_crypto_ctx(inode); |
| 965 | if (IS_ERR(ctx)) |
| 966 | goto set_error_page; |
| 967 | |
| 968 | /* wait the page to be moved by cleaning */ |
| 969 | f2fs_wait_on_encrypted_page_writeback( |
| 970 | F2FS_I_SB(inode), block_nr); |
| 971 | } |
| 972 | |
| 973 | bio = bio_alloc(GFP_KERNEL, |
| 974 | min_t(int, nr_pages, BIO_MAX_PAGES)); |
| 975 | if (!bio) { |
| 976 | if (ctx) |
| 977 | f2fs_release_crypto_ctx(ctx); |
| 978 | goto set_error_page; |
| 979 | } |
| 980 | bio->bi_bdev = bdev; |
| 981 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr); |
| 982 | bio->bi_end_io = f2fs_read_end_io; |
| 983 | bio->bi_private = ctx; |
| 984 | } |
| 985 | |
| 986 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
| 987 | goto submit_and_realloc; |
| 988 | |
| 989 | last_block_in_bio = block_nr; |
| 990 | goto next_page; |
| 991 | set_error_page: |
| 992 | SetPageError(page); |
| 993 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 994 | unlock_page(page); |
| 995 | goto next_page; |
| 996 | confused: |
| 997 | if (bio) { |
| 998 | submit_bio(READ, bio); |
| 999 | bio = NULL; |
| 1000 | } |
| 1001 | unlock_page(page); |
| 1002 | next_page: |
| 1003 | if (pages) |
| 1004 | page_cache_release(page); |
| 1005 | } |
| 1006 | BUG_ON(pages && !list_empty(pages)); |
| 1007 | if (bio) |
| 1008 | submit_bio(READ, bio); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 1013 | { |
| 1014 | struct inode *inode = page->mapping->host; |
| 1015 | int ret = -EAGAIN; |
| 1016 | |
| 1017 | trace_f2fs_readpage(page, DATA); |
| 1018 | |
| 1019 | /* If the file has inline data, try to read it directly */ |
| 1020 | if (f2fs_has_inline_data(inode)) |
| 1021 | ret = f2fs_read_inline_data(inode, page); |
| 1022 | if (ret == -EAGAIN) |
| 1023 | ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static int f2fs_read_data_pages(struct file *file, |
| 1028 | struct address_space *mapping, |
| 1029 | struct list_head *pages, unsigned nr_pages) |
| 1030 | { |
| 1031 | struct inode *inode = file->f_mapping->host; |
| 1032 | struct page *page = list_entry(pages->prev, struct page, lru); |
| 1033 | |
| 1034 | trace_f2fs_readpages(inode, page, nr_pages); |
| 1035 | |
| 1036 | /* If the file has inline data, skip readpages */ |
| 1037 | if (f2fs_has_inline_data(inode)) |
| 1038 | return 0; |
| 1039 | |
| 1040 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages); |
| 1041 | } |
| 1042 | |
| 1043 | int do_write_data_page(struct f2fs_io_info *fio) |
| 1044 | { |
| 1045 | struct page *page = fio->page; |
| 1046 | struct inode *inode = page->mapping->host; |
| 1047 | struct dnode_of_data dn; |
| 1048 | int err = 0; |
| 1049 | |
| 1050 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1051 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
| 1052 | if (err) |
| 1053 | return err; |
| 1054 | |
| 1055 | fio->blk_addr = dn.data_blkaddr; |
| 1056 | |
| 1057 | /* This page is already truncated */ |
| 1058 | if (fio->blk_addr == NULL_ADDR) { |
| 1059 | ClearPageUptodate(page); |
| 1060 | goto out_writepage; |
| 1061 | } |
| 1062 | |
| 1063 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { |
| 1064 | |
| 1065 | /* wait for GCed encrypted page writeback */ |
| 1066 | f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), |
| 1067 | fio->blk_addr); |
| 1068 | |
| 1069 | fio->encrypted_page = f2fs_encrypt(inode, fio->page); |
| 1070 | if (IS_ERR(fio->encrypted_page)) { |
| 1071 | err = PTR_ERR(fio->encrypted_page); |
| 1072 | goto out_writepage; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | set_page_writeback(page); |
| 1077 | |
| 1078 | /* |
| 1079 | * If current allocation needs SSR, |
| 1080 | * it had better in-place writes for updated data. |
| 1081 | */ |
| 1082 | if (unlikely(fio->blk_addr != NEW_ADDR && |
| 1083 | !is_cold_data(page) && |
| 1084 | !IS_ATOMIC_WRITTEN_PAGE(page) && |
| 1085 | need_inplace_update(inode))) { |
| 1086 | rewrite_data_page(fio); |
| 1087 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
| 1088 | trace_f2fs_do_write_data_page(page, IPU); |
| 1089 | } else { |
| 1090 | write_data_page(&dn, fio); |
| 1091 | set_data_blkaddr(&dn); |
| 1092 | f2fs_update_extent_cache(&dn); |
| 1093 | trace_f2fs_do_write_data_page(page, OPU); |
| 1094 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
| 1095 | if (page->index == 0) |
| 1096 | set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN); |
| 1097 | } |
| 1098 | out_writepage: |
| 1099 | f2fs_put_dnode(&dn); |
| 1100 | return err; |
| 1101 | } |
| 1102 | |
| 1103 | static int f2fs_write_data_page(struct page *page, |
| 1104 | struct writeback_control *wbc) |
| 1105 | { |
| 1106 | struct inode *inode = page->mapping->host; |
| 1107 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1108 | loff_t i_size = i_size_read(inode); |
| 1109 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 1110 | >> PAGE_CACHE_SHIFT; |
| 1111 | unsigned offset = 0; |
| 1112 | bool need_balance_fs = false; |
| 1113 | int err = 0; |
| 1114 | struct f2fs_io_info fio = { |
| 1115 | .sbi = sbi, |
| 1116 | .type = DATA, |
| 1117 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
| 1118 | .page = page, |
| 1119 | .encrypted_page = NULL, |
| 1120 | }; |
| 1121 | |
| 1122 | trace_f2fs_writepage(page, DATA); |
| 1123 | |
| 1124 | if (page->index < end_index) |
| 1125 | goto write; |
| 1126 | |
| 1127 | /* |
| 1128 | * If the offset is out-of-range of file size, |
| 1129 | * this page does not have to be written to disk. |
| 1130 | */ |
| 1131 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 1132 | if ((page->index >= end_index + 1) || !offset) |
| 1133 | goto out; |
| 1134 | |
| 1135 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 1136 | write: |
| 1137 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1138 | goto redirty_out; |
| 1139 | if (f2fs_is_drop_cache(inode)) |
| 1140 | goto out; |
| 1141 | if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim && |
| 1142 | available_free_memory(sbi, BASE_CHECK)) |
| 1143 | goto redirty_out; |
| 1144 | |
| 1145 | /* Dentry blocks are controlled by checkpoint */ |
| 1146 | if (S_ISDIR(inode->i_mode)) { |
| 1147 | if (unlikely(f2fs_cp_error(sbi))) |
| 1148 | goto redirty_out; |
| 1149 | err = do_write_data_page(&fio); |
| 1150 | goto done; |
| 1151 | } |
| 1152 | |
| 1153 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1154 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1155 | SetPageError(page); |
| 1156 | goto out; |
| 1157 | } |
| 1158 | |
| 1159 | if (!wbc->for_reclaim) |
| 1160 | need_balance_fs = true; |
| 1161 | else if (has_not_enough_free_secs(sbi, 0)) |
| 1162 | goto redirty_out; |
| 1163 | |
| 1164 | err = -EAGAIN; |
| 1165 | f2fs_lock_op(sbi); |
| 1166 | if (f2fs_has_inline_data(inode)) |
| 1167 | err = f2fs_write_inline_data(inode, page); |
| 1168 | if (err == -EAGAIN) |
| 1169 | err = do_write_data_page(&fio); |
| 1170 | f2fs_unlock_op(sbi); |
| 1171 | done: |
| 1172 | if (err && err != -ENOENT) |
| 1173 | goto redirty_out; |
| 1174 | |
| 1175 | clear_cold_data(page); |
| 1176 | out: |
| 1177 | inode_dec_dirty_pages(inode); |
| 1178 | if (err) |
| 1179 | ClearPageUptodate(page); |
| 1180 | unlock_page(page); |
| 1181 | if (need_balance_fs) |
| 1182 | f2fs_balance_fs(sbi); |
| 1183 | if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi))) { |
| 1184 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
| 1185 | remove_dirty_inode(inode); |
| 1186 | } |
| 1187 | return 0; |
| 1188 | |
| 1189 | redirty_out: |
| 1190 | redirty_page_for_writepage(wbc, page); |
| 1191 | return AOP_WRITEPAGE_ACTIVATE; |
| 1192 | } |
| 1193 | |
| 1194 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 1195 | void *data) |
| 1196 | { |
| 1197 | struct address_space *mapping = data; |
| 1198 | int ret = mapping->a_ops->writepage(page, wbc); |
| 1199 | mapping_set_error(mapping, ret); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * This function was copied from write_cche_pages from mm/page-writeback.c. |
| 1205 | * The major change is making write step of cold data page separately from |
| 1206 | * warm/hot data page. |
| 1207 | */ |
| 1208 | static int f2fs_write_cache_pages(struct address_space *mapping, |
| 1209 | struct writeback_control *wbc, writepage_t writepage, |
| 1210 | void *data) |
| 1211 | { |
| 1212 | int ret = 0; |
| 1213 | int done = 0; |
| 1214 | struct pagevec pvec; |
| 1215 | int nr_pages; |
| 1216 | pgoff_t uninitialized_var(writeback_index); |
| 1217 | pgoff_t index; |
| 1218 | pgoff_t end; /* Inclusive */ |
| 1219 | pgoff_t done_index; |
| 1220 | int cycled; |
| 1221 | int range_whole = 0; |
| 1222 | int tag; |
| 1223 | int step = 0; |
| 1224 | |
| 1225 | pagevec_init(&pvec, 0); |
| 1226 | next: |
| 1227 | if (wbc->range_cyclic) { |
| 1228 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 1229 | index = writeback_index; |
| 1230 | if (index == 0) |
| 1231 | cycled = 1; |
| 1232 | else |
| 1233 | cycled = 0; |
| 1234 | end = -1; |
| 1235 | } else { |
| 1236 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1237 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1238 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 1239 | range_whole = 1; |
| 1240 | cycled = 1; /* ignore range_cyclic tests */ |
| 1241 | } |
| 1242 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1243 | tag = PAGECACHE_TAG_TOWRITE; |
| 1244 | else |
| 1245 | tag = PAGECACHE_TAG_DIRTY; |
| 1246 | retry: |
| 1247 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1248 | tag_pages_for_writeback(mapping, index, end); |
| 1249 | done_index = index; |
| 1250 | while (!done && (index <= end)) { |
| 1251 | int i; |
| 1252 | |
| 1253 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 1254 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1); |
| 1255 | if (nr_pages == 0) |
| 1256 | break; |
| 1257 | |
| 1258 | for (i = 0; i < nr_pages; i++) { |
| 1259 | struct page *page = pvec.pages[i]; |
| 1260 | |
| 1261 | if (page->index > end) { |
| 1262 | done = 1; |
| 1263 | break; |
| 1264 | } |
| 1265 | |
| 1266 | done_index = page->index; |
| 1267 | |
| 1268 | lock_page(page); |
| 1269 | |
| 1270 | if (unlikely(page->mapping != mapping)) { |
| 1271 | continue_unlock: |
| 1272 | unlock_page(page); |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
| 1276 | if (!PageDirty(page)) { |
| 1277 | /* someone wrote it for us */ |
| 1278 | goto continue_unlock; |
| 1279 | } |
| 1280 | |
| 1281 | if (step == is_cold_data(page)) |
| 1282 | goto continue_unlock; |
| 1283 | |
| 1284 | if (PageWriteback(page)) { |
| 1285 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 1286 | f2fs_wait_on_page_writeback(page, DATA); |
| 1287 | else |
| 1288 | goto continue_unlock; |
| 1289 | } |
| 1290 | |
| 1291 | BUG_ON(PageWriteback(page)); |
| 1292 | if (!clear_page_dirty_for_io(page)) |
| 1293 | goto continue_unlock; |
| 1294 | |
| 1295 | ret = (*writepage)(page, wbc, data); |
| 1296 | if (unlikely(ret)) { |
| 1297 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 1298 | unlock_page(page); |
| 1299 | ret = 0; |
| 1300 | } else { |
| 1301 | done_index = page->index + 1; |
| 1302 | done = 1; |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | if (--wbc->nr_to_write <= 0 && |
| 1308 | wbc->sync_mode == WB_SYNC_NONE) { |
| 1309 | done = 1; |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | pagevec_release(&pvec); |
| 1314 | cond_resched(); |
| 1315 | } |
| 1316 | |
| 1317 | if (step < 1) { |
| 1318 | step++; |
| 1319 | goto next; |
| 1320 | } |
| 1321 | |
| 1322 | if (!cycled && !done) { |
| 1323 | cycled = 1; |
| 1324 | index = 0; |
| 1325 | end = writeback_index - 1; |
| 1326 | goto retry; |
| 1327 | } |
| 1328 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 1329 | mapping->writeback_index = done_index; |
| 1330 | |
| 1331 | return ret; |
| 1332 | } |
| 1333 | |
| 1334 | static int f2fs_write_data_pages(struct address_space *mapping, |
| 1335 | struct writeback_control *wbc) |
| 1336 | { |
| 1337 | struct inode *inode = mapping->host; |
| 1338 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1339 | bool locked = false; |
| 1340 | int ret; |
| 1341 | long diff; |
| 1342 | |
| 1343 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 1344 | |
| 1345 | /* deal with chardevs and other special file */ |
| 1346 | if (!mapping->a_ops->writepage) |
| 1347 | return 0; |
| 1348 | |
| 1349 | /* skip writing if there is no dirty page in this inode */ |
| 1350 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 1351 | return 0; |
| 1352 | |
| 1353 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
| 1354 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
| 1355 | available_free_memory(sbi, DIRTY_DENTS)) |
| 1356 | goto skip_write; |
| 1357 | |
| 1358 | /* skip writing during file defragment */ |
| 1359 | if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG)) |
| 1360 | goto skip_write; |
| 1361 | |
| 1362 | /* during POR, we don't need to trigger writepage at all. */ |
| 1363 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1364 | goto skip_write; |
| 1365 | |
| 1366 | diff = nr_pages_to_write(sbi, DATA, wbc); |
| 1367 | |
| 1368 | if (!S_ISDIR(inode->i_mode)) { |
| 1369 | mutex_lock(&sbi->writepages); |
| 1370 | locked = true; |
| 1371 | } |
| 1372 | ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
| 1373 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
| 1374 | if (locked) |
| 1375 | mutex_unlock(&sbi->writepages); |
| 1376 | |
| 1377 | remove_dirty_inode(inode); |
| 1378 | |
| 1379 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
| 1380 | return ret; |
| 1381 | |
| 1382 | skip_write: |
| 1383 | wbc->pages_skipped += get_dirty_pages(inode); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 1388 | { |
| 1389 | struct inode *inode = mapping->host; |
| 1390 | loff_t i_size = i_size_read(inode); |
| 1391 | |
| 1392 | if (to > i_size) { |
| 1393 | truncate_pagecache(inode, i_size); |
| 1394 | truncate_blocks(inode, i_size, true); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 1399 | struct page *page, loff_t pos, unsigned len, |
| 1400 | block_t *blk_addr, bool *node_changed) |
| 1401 | { |
| 1402 | struct inode *inode = page->mapping->host; |
| 1403 | pgoff_t index = page->index; |
| 1404 | struct dnode_of_data dn; |
| 1405 | struct page *ipage; |
| 1406 | bool locked = false; |
| 1407 | struct extent_info ei; |
| 1408 | int err = 0; |
| 1409 | |
| 1410 | if (f2fs_has_inline_data(inode) || |
| 1411 | (pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1412 | f2fs_lock_op(sbi); |
| 1413 | locked = true; |
| 1414 | } |
| 1415 | restart: |
| 1416 | /* check inline_data */ |
| 1417 | ipage = get_node_page(sbi, inode->i_ino); |
| 1418 | if (IS_ERR(ipage)) { |
| 1419 | err = PTR_ERR(ipage); |
| 1420 | goto unlock_out; |
| 1421 | } |
| 1422 | |
| 1423 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 1424 | |
| 1425 | if (f2fs_has_inline_data(inode)) { |
| 1426 | if (pos + len <= MAX_INLINE_DATA) { |
| 1427 | read_inline_data(page, ipage); |
| 1428 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 1429 | sync_inode_page(&dn); |
| 1430 | } else { |
| 1431 | err = f2fs_convert_inline_page(&dn, page); |
| 1432 | if (err) |
| 1433 | goto out; |
| 1434 | if (dn.data_blkaddr == NULL_ADDR) |
| 1435 | err = f2fs_get_block(&dn, index); |
| 1436 | } |
| 1437 | } else if (locked) { |
| 1438 | err = f2fs_get_block(&dn, index); |
| 1439 | } else { |
| 1440 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 1441 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 1442 | } else { |
| 1443 | bool restart = false; |
| 1444 | |
| 1445 | /* hole case */ |
| 1446 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 1447 | if (err || (!err && dn.data_blkaddr == NULL_ADDR)) |
| 1448 | restart = true; |
| 1449 | if (restart) { |
| 1450 | f2fs_put_dnode(&dn); |
| 1451 | f2fs_lock_op(sbi); |
| 1452 | locked = true; |
| 1453 | goto restart; |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /* convert_inline_page can make node_changed */ |
| 1459 | *blk_addr = dn.data_blkaddr; |
| 1460 | *node_changed = dn.node_changed; |
| 1461 | out: |
| 1462 | f2fs_put_dnode(&dn); |
| 1463 | unlock_out: |
| 1464 | if (locked) |
| 1465 | f2fs_unlock_op(sbi); |
| 1466 | return err; |
| 1467 | } |
| 1468 | |
| 1469 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 1470 | loff_t pos, unsigned len, unsigned flags, |
| 1471 | struct page **pagep, void **fsdata) |
| 1472 | { |
| 1473 | struct inode *inode = mapping->host; |
| 1474 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1475 | struct page *page = NULL; |
| 1476 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 1477 | bool need_balance = false; |
| 1478 | block_t blkaddr = NULL_ADDR; |
| 1479 | int err = 0; |
| 1480 | |
| 1481 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 1482 | |
| 1483 | /* |
| 1484 | * We should check this at this moment to avoid deadlock on inode page |
| 1485 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1486 | * lock_page(page #0) -> lock_page(inode_page) |
| 1487 | */ |
| 1488 | if (index != 0) { |
| 1489 | err = f2fs_convert_inline_inode(inode); |
| 1490 | if (err) |
| 1491 | goto fail; |
| 1492 | } |
| 1493 | repeat: |
| 1494 | page = grab_cache_page_write_begin(mapping, index, flags); |
| 1495 | if (!page) { |
| 1496 | err = -ENOMEM; |
| 1497 | goto fail; |
| 1498 | } |
| 1499 | |
| 1500 | *pagep = page; |
| 1501 | |
| 1502 | err = prepare_write_begin(sbi, page, pos, len, |
| 1503 | &blkaddr, &need_balance); |
| 1504 | if (err) |
| 1505 | goto fail; |
| 1506 | |
| 1507 | if (need_balance && has_not_enough_free_secs(sbi, 0)) { |
| 1508 | unlock_page(page); |
| 1509 | f2fs_balance_fs(sbi); |
| 1510 | lock_page(page); |
| 1511 | if (page->mapping != mapping) { |
| 1512 | /* The page got truncated from under us */ |
| 1513 | f2fs_put_page(page, 1); |
| 1514 | goto repeat; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | f2fs_wait_on_page_writeback(page, DATA); |
| 1519 | |
| 1520 | /* wait for GCed encrypted page writeback */ |
| 1521 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
| 1522 | f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr); |
| 1523 | |
| 1524 | if (len == PAGE_CACHE_SIZE) |
| 1525 | goto out_update; |
| 1526 | if (PageUptodate(page)) |
| 1527 | goto out_clear; |
| 1528 | |
| 1529 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1530 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 1531 | unsigned end = start + len; |
| 1532 | |
| 1533 | /* Reading beyond i_size is simple: memset to zero */ |
| 1534 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
| 1535 | goto out_update; |
| 1536 | } |
| 1537 | |
| 1538 | if (blkaddr == NEW_ADDR) { |
| 1539 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 1540 | } else { |
| 1541 | struct f2fs_io_info fio = { |
| 1542 | .sbi = sbi, |
| 1543 | .type = DATA, |
| 1544 | .rw = READ_SYNC, |
| 1545 | .blk_addr = blkaddr, |
| 1546 | .page = page, |
| 1547 | .encrypted_page = NULL, |
| 1548 | }; |
| 1549 | err = f2fs_submit_page_bio(&fio); |
| 1550 | if (err) |
| 1551 | goto fail; |
| 1552 | |
| 1553 | lock_page(page); |
| 1554 | if (unlikely(!PageUptodate(page))) { |
| 1555 | err = -EIO; |
| 1556 | goto fail; |
| 1557 | } |
| 1558 | if (unlikely(page->mapping != mapping)) { |
| 1559 | f2fs_put_page(page, 1); |
| 1560 | goto repeat; |
| 1561 | } |
| 1562 | |
| 1563 | /* avoid symlink page */ |
| 1564 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { |
| 1565 | err = f2fs_decrypt_one(inode, page); |
| 1566 | if (err) |
| 1567 | goto fail; |
| 1568 | } |
| 1569 | } |
| 1570 | out_update: |
| 1571 | SetPageUptodate(page); |
| 1572 | out_clear: |
| 1573 | clear_cold_data(page); |
| 1574 | return 0; |
| 1575 | |
| 1576 | fail: |
| 1577 | f2fs_put_page(page, 1); |
| 1578 | f2fs_write_failed(mapping, pos + len); |
| 1579 | return err; |
| 1580 | } |
| 1581 | |
| 1582 | static int f2fs_write_end(struct file *file, |
| 1583 | struct address_space *mapping, |
| 1584 | loff_t pos, unsigned len, unsigned copied, |
| 1585 | struct page *page, void *fsdata) |
| 1586 | { |
| 1587 | struct inode *inode = page->mapping->host; |
| 1588 | |
| 1589 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1590 | |
| 1591 | set_page_dirty(page); |
| 1592 | |
| 1593 | if (pos + copied > i_size_read(inode)) { |
| 1594 | i_size_write(inode, pos + copied); |
| 1595 | mark_inode_dirty(inode); |
| 1596 | update_inode_page(inode); |
| 1597 | } |
| 1598 | |
| 1599 | f2fs_put_page(page, 1); |
| 1600 | return copied; |
| 1601 | } |
| 1602 | |
| 1603 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
| 1604 | loff_t offset) |
| 1605 | { |
| 1606 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
| 1607 | |
| 1608 | if (offset & blocksize_mask) |
| 1609 | return -EINVAL; |
| 1610 | |
| 1611 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1612 | return -EINVAL; |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
| 1618 | loff_t offset) |
| 1619 | { |
| 1620 | struct file *file = iocb->ki_filp; |
| 1621 | struct address_space *mapping = file->f_mapping; |
| 1622 | struct inode *inode = mapping->host; |
| 1623 | size_t count = iov_iter_count(iter); |
| 1624 | int err; |
| 1625 | |
| 1626 | /* we don't need to use inline_data strictly */ |
| 1627 | err = f2fs_convert_inline_inode(inode); |
| 1628 | if (err) |
| 1629 | return err; |
| 1630 | |
| 1631 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
| 1632 | return 0; |
| 1633 | |
| 1634 | err = check_direct_IO(inode, iter, offset); |
| 1635 | if (err) |
| 1636 | return err; |
| 1637 | |
| 1638 | trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter)); |
| 1639 | |
| 1640 | if (iov_iter_rw(iter) == WRITE) { |
| 1641 | err = __allocate_data_blocks(inode, offset, count); |
| 1642 | if (err) |
| 1643 | goto out; |
| 1644 | } |
| 1645 | |
| 1646 | err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio); |
| 1647 | out: |
| 1648 | if (err < 0 && iov_iter_rw(iter) == WRITE) |
| 1649 | f2fs_write_failed(mapping, offset + count); |
| 1650 | |
| 1651 | trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err); |
| 1652 | |
| 1653 | return err; |
| 1654 | } |
| 1655 | |
| 1656 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 1657 | unsigned int length) |
| 1658 | { |
| 1659 | struct inode *inode = page->mapping->host; |
| 1660 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1661 | |
| 1662 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 1663 | (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)) |
| 1664 | return; |
| 1665 | |
| 1666 | if (PageDirty(page)) { |
| 1667 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 1668 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 1669 | else if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 1670 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1671 | else |
| 1672 | inode_dec_dirty_pages(inode); |
| 1673 | } |
| 1674 | |
| 1675 | /* This is atomic written page, keep Private */ |
| 1676 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 1677 | return; |
| 1678 | |
| 1679 | ClearPagePrivate(page); |
| 1680 | } |
| 1681 | |
| 1682 | int f2fs_release_page(struct page *page, gfp_t wait) |
| 1683 | { |
| 1684 | /* If this is dirty page, keep PagePrivate */ |
| 1685 | if (PageDirty(page)) |
| 1686 | return 0; |
| 1687 | |
| 1688 | /* This is atomic written page, keep Private */ |
| 1689 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 1690 | return 0; |
| 1691 | |
| 1692 | ClearPagePrivate(page); |
| 1693 | return 1; |
| 1694 | } |
| 1695 | |
| 1696 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1697 | { |
| 1698 | struct address_space *mapping = page->mapping; |
| 1699 | struct inode *inode = mapping->host; |
| 1700 | |
| 1701 | trace_f2fs_set_page_dirty(page, DATA); |
| 1702 | |
| 1703 | SetPageUptodate(page); |
| 1704 | |
| 1705 | if (f2fs_is_atomic_file(inode)) { |
| 1706 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
| 1707 | register_inmem_page(inode, page); |
| 1708 | return 1; |
| 1709 | } |
| 1710 | /* |
| 1711 | * Previously, this page has been registered, we just |
| 1712 | * return here. |
| 1713 | */ |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | if (!PageDirty(page)) { |
| 1718 | __set_page_dirty_nobuffers(page); |
| 1719 | update_dirty_page(inode, page); |
| 1720 | return 1; |
| 1721 | } |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1726 | { |
| 1727 | struct inode *inode = mapping->host; |
| 1728 | |
| 1729 | if (f2fs_has_inline_data(inode)) |
| 1730 | return 0; |
| 1731 | |
| 1732 | /* make sure allocating whole blocks */ |
| 1733 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 1734 | filemap_write_and_wait(mapping); |
| 1735 | |
| 1736 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
| 1737 | } |
| 1738 | |
| 1739 | const struct address_space_operations f2fs_dblock_aops = { |
| 1740 | .readpage = f2fs_read_data_page, |
| 1741 | .readpages = f2fs_read_data_pages, |
| 1742 | .writepage = f2fs_write_data_page, |
| 1743 | .writepages = f2fs_write_data_pages, |
| 1744 | .write_begin = f2fs_write_begin, |
| 1745 | .write_end = f2fs_write_end, |
| 1746 | .set_page_dirty = f2fs_set_data_page_dirty, |
| 1747 | .invalidatepage = f2fs_invalidate_page, |
| 1748 | .releasepage = f2fs_release_page, |
| 1749 | .direct_IO = f2fs_direct_IO, |
| 1750 | .bmap = f2fs_bmap, |
| 1751 | }; |