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