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