| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * fs/f2fs/data.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
| 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/buffer_head.h> |
| 11 | #include <linux/mpage.h> |
| 12 | #include <linux/writeback.h> |
| 13 | #include <linux/backing-dev.h> |
| 14 | #include <linux/pagevec.h> |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/bio.h> |
| 17 | #include <linux/swap.h> |
| 18 | #include <linux/prefetch.h> |
| 19 | #include <linux/uio.h> |
| 20 | #include <linux/cleancache.h> |
| 21 | #include <linux/sched/signal.h> |
| 22 | |
| 23 | #include "f2fs.h" |
| 24 | #include "node.h" |
| 25 | #include "segment.h" |
| 26 | #include "trace.h" |
| 27 | #include <trace/events/f2fs.h> |
| 28 | #include <trace/events/android_fs.h> |
| 29 | |
| 30 | #define NUM_PREALLOC_POST_READ_CTXS 128 |
| 31 | |
| 32 | static struct kmem_cache *bio_post_read_ctx_cache; |
| 33 | static struct kmem_cache *bio_entry_slab; |
| 34 | static mempool_t *bio_post_read_ctx_pool; |
| 35 | |
| 36 | static bool __is_cp_guaranteed(struct page *page) |
| 37 | { |
| 38 | struct address_space *mapping = page->mapping; |
| 39 | struct inode *inode; |
| 40 | struct f2fs_sb_info *sbi; |
| 41 | |
| 42 | if (!mapping) |
| 43 | return false; |
| 44 | |
| 45 | inode = mapping->host; |
| 46 | sbi = F2FS_I_SB(inode); |
| 47 | |
| 48 | if (inode->i_ino == F2FS_META_INO(sbi) || |
| 49 | inode->i_ino == F2FS_NODE_INO(sbi) || |
| 50 | S_ISDIR(inode->i_mode) || |
| 51 | (S_ISREG(inode->i_mode) && |
| 52 | (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) || |
| 53 | is_cold_data(page)) |
| 54 | return true; |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | static enum count_type __read_io_type(struct page *page) |
| 59 | { |
| 60 | struct address_space *mapping = page_file_mapping(page); |
| 61 | |
| 62 | if (mapping) { |
| 63 | struct inode *inode = mapping->host; |
| 64 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 65 | |
| 66 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 67 | return F2FS_RD_META; |
| 68 | |
| 69 | if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 70 | return F2FS_RD_NODE; |
| 71 | } |
| 72 | return F2FS_RD_DATA; |
| 73 | } |
| 74 | |
| 75 | /* postprocessing steps for read bios */ |
| 76 | enum bio_post_read_step { |
| 77 | STEP_INITIAL = 0, |
| 78 | STEP_DECRYPT, |
| 79 | STEP_VERITY, |
| 80 | }; |
| 81 | |
| 82 | struct bio_post_read_ctx { |
| 83 | struct bio *bio; |
| 84 | struct work_struct work; |
| 85 | unsigned int cur_step; |
| 86 | unsigned int enabled_steps; |
| 87 | }; |
| 88 | |
| 89 | static void __read_end_io(struct bio *bio) |
| 90 | { |
| 91 | struct page *page; |
| 92 | struct bio_vec *bv; |
| 93 | int i; |
| 94 | |
| 95 | bio_for_each_segment_all(bv, bio, i) { |
| 96 | page = bv->bv_page; |
| 97 | |
| 98 | /* PG_error was set if any post_read step failed */ |
| 99 | if (bio->bi_status || PageError(page)) { |
| 100 | ClearPageUptodate(page); |
| 101 | /* will re-read again later */ |
| 102 | ClearPageError(page); |
| 103 | } else { |
| 104 | SetPageUptodate(page); |
| 105 | } |
| 106 | dec_page_count(F2FS_P_SB(page), __read_io_type(page)); |
| 107 | unlock_page(page); |
| 108 | } |
| 109 | if (bio->bi_private) |
| 110 | mempool_free(bio->bi_private, bio_post_read_ctx_pool); |
| 111 | bio_put(bio); |
| 112 | } |
| 113 | |
| 114 | static void bio_post_read_processing(struct bio_post_read_ctx *ctx); |
| 115 | |
| 116 | static void decrypt_work(struct work_struct *work) |
| 117 | { |
| 118 | struct bio_post_read_ctx *ctx = |
| 119 | container_of(work, struct bio_post_read_ctx, work); |
| 120 | |
| 121 | fscrypt_decrypt_bio(ctx->bio); |
| 122 | |
| 123 | bio_post_read_processing(ctx); |
| 124 | } |
| 125 | |
| 126 | static void verity_work(struct work_struct *work) |
| 127 | { |
| 128 | struct bio_post_read_ctx *ctx = |
| 129 | container_of(work, struct bio_post_read_ctx, work); |
| 130 | |
| 131 | fsverity_verify_bio(ctx->bio); |
| 132 | |
| 133 | bio_post_read_processing(ctx); |
| 134 | } |
| 135 | |
| 136 | static void bio_post_read_processing(struct bio_post_read_ctx *ctx) |
| 137 | { |
| 138 | /* |
| 139 | * We use different work queues for decryption and for verity because |
| 140 | * verity may require reading metadata pages that need decryption, and |
| 141 | * we shouldn't recurse to the same workqueue. |
| 142 | */ |
| 143 | switch (++ctx->cur_step) { |
| 144 | case STEP_DECRYPT: |
| 145 | if (ctx->enabled_steps & (1 << STEP_DECRYPT)) { |
| 146 | INIT_WORK(&ctx->work, decrypt_work); |
| 147 | fscrypt_enqueue_decrypt_work(&ctx->work); |
| 148 | return; |
| 149 | } |
| 150 | ctx->cur_step++; |
| 151 | /* fall-through */ |
| 152 | case STEP_VERITY: |
| 153 | if (ctx->enabled_steps & (1 << STEP_VERITY)) { |
| 154 | INIT_WORK(&ctx->work, verity_work); |
| 155 | fsverity_enqueue_verify_work(&ctx->work); |
| 156 | return; |
| 157 | } |
| 158 | ctx->cur_step++; |
| 159 | /* fall-through */ |
| 160 | default: |
| 161 | __read_end_io(ctx->bio); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static bool f2fs_bio_post_read_required(struct bio *bio) |
| 166 | { |
| 167 | return bio->bi_private && !bio->bi_status; |
| 168 | } |
| 169 | |
| 170 | static void f2fs_read_end_io(struct bio *bio) |
| 171 | { |
| 172 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 173 | struct f2fs_sb_info *sbi = F2FS_P_SB(first_page); |
| 174 | |
| 175 | if (time_to_inject(sbi, FAULT_READ_IO)) { |
| 176 | f2fs_show_injection_info(sbi, FAULT_READ_IO); |
| 177 | bio->bi_status = BLK_STS_IOERR; |
| 178 | } |
| 179 | |
| 180 | if (f2fs_bio_post_read_required(bio)) { |
| 181 | struct bio_post_read_ctx *ctx = bio->bi_private; |
| 182 | |
| 183 | ctx->cur_step = STEP_INITIAL; |
| 184 | bio_post_read_processing(ctx); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (first_page != NULL && |
| 189 | __read_io_type(first_page) == F2FS_RD_DATA) { |
| 190 | trace_android_fs_dataread_end(first_page->mapping->host, |
| 191 | page_offset(first_page), |
| 192 | bio->bi_iter.bi_size); |
| 193 | } |
| 194 | |
| 195 | __read_end_io(bio); |
| 196 | } |
| 197 | |
| 198 | static void f2fs_write_end_io(struct bio *bio) |
| 199 | { |
| 200 | struct f2fs_sb_info *sbi = bio->bi_private; |
| 201 | struct bio_vec *bvec; |
| 202 | int i; |
| 203 | |
| 204 | if (time_to_inject(sbi, FAULT_WRITE_IO)) { |
| 205 | f2fs_show_injection_info(sbi, FAULT_WRITE_IO); |
| 206 | bio->bi_status = BLK_STS_IOERR; |
| 207 | } |
| 208 | |
| 209 | bio_for_each_segment_all(bvec, bio, i) { |
| 210 | struct page *page = bvec->bv_page; |
| 211 | enum count_type type = WB_DATA_TYPE(page); |
| 212 | |
| 213 | if (IS_DUMMY_WRITTEN_PAGE(page)) { |
| 214 | set_page_private(page, (unsigned long)NULL); |
| 215 | ClearPagePrivate(page); |
| 216 | unlock_page(page); |
| 217 | mempool_free(page, sbi->write_io_dummy); |
| 218 | |
| 219 | if (unlikely(bio->bi_status)) |
| 220 | f2fs_stop_checkpoint(sbi, true); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | fscrypt_finalize_bounce_page(&page); |
| 225 | |
| 226 | if (unlikely(bio->bi_status)) { |
| 227 | mapping_set_error(page->mapping, -EIO); |
| 228 | if (type == F2FS_WB_CP_DATA) |
| 229 | f2fs_stop_checkpoint(sbi, true); |
| 230 | } |
| 231 | |
| 232 | f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) && |
| 233 | page->index != nid_of_node(page)); |
| 234 | |
| 235 | dec_page_count(sbi, type); |
| 236 | if (f2fs_in_warm_node_list(sbi, page)) |
| 237 | f2fs_del_fsync_node_entry(sbi, page); |
| 238 | clear_cold_data(page); |
| 239 | end_page_writeback(page); |
| 240 | } |
| 241 | if (!get_pages(sbi, F2FS_WB_CP_DATA) && |
| 242 | wq_has_sleeper(&sbi->cp_wait)) |
| 243 | wake_up(&sbi->cp_wait); |
| 244 | |
| 245 | bio_put(bio); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Return true, if pre_bio's bdev is same as its target device. |
| 250 | */ |
| 251 | struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, |
| 252 | block_t blk_addr, struct bio *bio) |
| 253 | { |
| 254 | struct block_device *bdev = sbi->sb->s_bdev; |
| 255 | int i; |
| 256 | |
| 257 | if (f2fs_is_multi_device(sbi)) { |
| 258 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 259 | if (FDEV(i).start_blk <= blk_addr && |
| 260 | FDEV(i).end_blk >= blk_addr) { |
| 261 | blk_addr -= FDEV(i).start_blk; |
| 262 | bdev = FDEV(i).bdev; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | if (bio) { |
| 268 | bio_set_dev(bio, bdev); |
| 269 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
| 270 | } |
| 271 | return bdev; |
| 272 | } |
| 273 | |
| 274 | int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 275 | { |
| 276 | int i; |
| 277 | |
| 278 | if (!f2fs_is_multi_device(sbi)) |
| 279 | return 0; |
| 280 | |
| 281 | for (i = 0; i < sbi->s_ndevs; i++) |
| 282 | if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) |
| 283 | return i; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static bool __same_bdev(struct f2fs_sb_info *sbi, |
| 288 | block_t blk_addr, struct bio *bio) |
| 289 | { |
| 290 | struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL); |
| 291 | return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Low-level block read/write IO operations. |
| 296 | */ |
| 297 | static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages) |
| 298 | { |
| 299 | struct f2fs_sb_info *sbi = fio->sbi; |
| 300 | struct bio *bio; |
| 301 | |
| 302 | bio = f2fs_bio_alloc(sbi, npages, true); |
| 303 | |
| 304 | f2fs_target_device(sbi, fio->new_blkaddr, bio); |
| 305 | if (is_read_io(fio->op)) { |
| 306 | bio->bi_end_io = f2fs_read_end_io; |
| 307 | bio->bi_private = NULL; |
| 308 | } else { |
| 309 | bio->bi_end_io = f2fs_write_end_io; |
| 310 | bio->bi_private = sbi; |
| 311 | bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, |
| 312 | fio->type, fio->temp); |
| 313 | } |
| 314 | if (fio->io_wbc) |
| 315 | wbc_init_bio(fio->io_wbc, bio); |
| 316 | |
| 317 | return bio; |
| 318 | } |
| 319 | |
| 320 | static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, |
| 321 | pgoff_t first_idx, |
| 322 | const struct f2fs_io_info *fio, |
| 323 | gfp_t gfp_mask) |
| 324 | { |
| 325 | /* |
| 326 | * The f2fs garbage collector sets ->encrypted_page when it wants to |
| 327 | * read/write raw data without encryption. |
| 328 | */ |
| 329 | if (!fio || !fio->encrypted_page) |
| 330 | fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask); |
| 331 | } |
| 332 | |
| 333 | static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
| 334 | pgoff_t next_idx, |
| 335 | const struct f2fs_io_info *fio) |
| 336 | { |
| 337 | /* |
| 338 | * The f2fs garbage collector sets ->encrypted_page when it wants to |
| 339 | * read/write raw data without encryption. |
| 340 | */ |
| 341 | if (fio && fio->encrypted_page) |
| 342 | return !bio_has_crypt_ctx(bio); |
| 343 | |
| 344 | return fscrypt_mergeable_bio(bio, inode, next_idx); |
| 345 | } |
| 346 | |
| 347 | static inline void __submit_bio(struct f2fs_sb_info *sbi, |
| 348 | struct bio *bio, enum page_type type) |
| 349 | { |
| 350 | if (!is_read_io(bio_op(bio))) { |
| 351 | unsigned int start; |
| 352 | |
| 353 | if (type != DATA && type != NODE) |
| 354 | goto submit_io; |
| 355 | |
| 356 | if (test_opt(sbi, LFS) && current->plug) |
| 357 | blk_finish_plug(current->plug); |
| 358 | |
| 359 | if (F2FS_IO_ALIGNED(sbi)) |
| 360 | goto submit_io; |
| 361 | |
| 362 | start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS; |
| 363 | start %= F2FS_IO_SIZE(sbi); |
| 364 | |
| 365 | if (start == 0) |
| 366 | goto submit_io; |
| 367 | |
| 368 | /* fill dummy pages */ |
| 369 | for (; start < F2FS_IO_SIZE(sbi); start++) { |
| 370 | struct page *page = |
| 371 | mempool_alloc(sbi->write_io_dummy, |
| 372 | GFP_NOIO | __GFP_NOFAIL); |
| 373 | f2fs_bug_on(sbi, !page); |
| 374 | |
| 375 | zero_user_segment(page, 0, PAGE_SIZE); |
| 376 | SetPagePrivate(page); |
| 377 | set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE); |
| 378 | lock_page(page); |
| 379 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
| 380 | f2fs_bug_on(sbi, 1); |
| 381 | } |
| 382 | /* |
| 383 | * In the NODE case, we lose next block address chain. So, we |
| 384 | * need to do checkpoint in f2fs_sync_file. |
| 385 | */ |
| 386 | if (type == NODE) |
| 387 | set_sbi_flag(sbi, SBI_NEED_CP); |
| 388 | } |
| 389 | submit_io: |
| 390 | if (is_read_io(bio_op(bio))) |
| 391 | trace_f2fs_submit_read_bio(sbi->sb, type, bio); |
| 392 | else |
| 393 | trace_f2fs_submit_write_bio(sbi->sb, type, bio); |
| 394 | submit_bio(bio); |
| 395 | } |
| 396 | |
| 397 | static void __f2fs_submit_read_bio(struct f2fs_sb_info *sbi, |
| 398 | struct bio *bio, enum page_type type) |
| 399 | { |
| 400 | if (trace_android_fs_dataread_start_enabled() && (type == DATA)) { |
| 401 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 402 | |
| 403 | if (first_page != NULL && |
| 404 | __read_io_type(first_page) == F2FS_RD_DATA) { |
| 405 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 406 | |
| 407 | path = android_fstrace_get_pathname(pathbuf, |
| 408 | MAX_TRACE_PATHBUF_LEN, |
| 409 | first_page->mapping->host); |
| 410 | |
| 411 | trace_android_fs_dataread_start( |
| 412 | first_page->mapping->host, |
| 413 | page_offset(first_page), |
| 414 | bio->bi_iter.bi_size, |
| 415 | current->pid, |
| 416 | path, |
| 417 | current->comm); |
| 418 | } |
| 419 | } |
| 420 | __submit_bio(sbi, bio, type); |
| 421 | } |
| 422 | |
| 423 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
| 424 | { |
| 425 | struct f2fs_io_info *fio = &io->fio; |
| 426 | |
| 427 | if (!io->bio) |
| 428 | return; |
| 429 | |
| 430 | bio_set_op_attrs(io->bio, fio->op, fio->op_flags); |
| 431 | |
| 432 | if (is_read_io(fio->op)) |
| 433 | trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
| 434 | else |
| 435 | trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
| 436 | |
| 437 | __submit_bio(io->sbi, io->bio, fio->type); |
| 438 | io->bio = NULL; |
| 439 | } |
| 440 | |
| 441 | static bool __has_merged_page(struct bio *bio, struct inode *inode, |
| 442 | struct page *page, nid_t ino) |
| 443 | { |
| 444 | struct bio_vec *bvec; |
| 445 | struct page *target; |
| 446 | int i; |
| 447 | |
| 448 | if (!bio) |
| 449 | return false; |
| 450 | |
| 451 | if (!inode && !page && !ino) |
| 452 | return true; |
| 453 | |
| 454 | bio_for_each_segment_all(bvec, bio, i) { |
| 455 | |
| 456 | target = bvec->bv_page; |
| 457 | if (fscrypt_is_bounce_page(target)) |
| 458 | target = fscrypt_pagecache_page(target); |
| 459 | |
| 460 | if (inode && inode == target->mapping->host) |
| 461 | return true; |
| 462 | if (page && page == target) |
| 463 | return true; |
| 464 | if (ino && ino == ino_of_node(target)) |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, |
| 472 | enum page_type type, enum temp_type temp) |
| 473 | { |
| 474 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 475 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
| 476 | |
| 477 | down_write(&io->io_rwsem); |
| 478 | |
| 479 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 480 | if (type >= META_FLUSH) { |
| 481 | io->fio.type = META_FLUSH; |
| 482 | io->fio.op = REQ_OP_WRITE; |
| 483 | io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC; |
| 484 | if (!test_opt(sbi, NOBARRIER)) |
| 485 | io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA; |
| 486 | } |
| 487 | __submit_merged_bio(io); |
| 488 | up_write(&io->io_rwsem); |
| 489 | } |
| 490 | |
| 491 | static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, |
| 492 | struct inode *inode, struct page *page, |
| 493 | nid_t ino, enum page_type type, bool force) |
| 494 | { |
| 495 | enum temp_type temp; |
| 496 | bool ret = true; |
| 497 | |
| 498 | for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { |
| 499 | if (!force) { |
| 500 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 501 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
| 502 | |
| 503 | down_read(&io->io_rwsem); |
| 504 | ret = __has_merged_page(io->bio, inode, page, ino); |
| 505 | up_read(&io->io_rwsem); |
| 506 | } |
| 507 | if (ret) |
| 508 | __f2fs_submit_merged_write(sbi, type, temp); |
| 509 | |
| 510 | /* TODO: use HOT temp only for meta pages now. */ |
| 511 | if (type >= META) |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) |
| 517 | { |
| 518 | __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true); |
| 519 | } |
| 520 | |
| 521 | void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, |
| 522 | struct inode *inode, struct page *page, |
| 523 | nid_t ino, enum page_type type) |
| 524 | { |
| 525 | __submit_merged_write_cond(sbi, inode, page, ino, type, false); |
| 526 | } |
| 527 | |
| 528 | void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) |
| 529 | { |
| 530 | f2fs_submit_merged_write(sbi, DATA); |
| 531 | f2fs_submit_merged_write(sbi, NODE); |
| 532 | f2fs_submit_merged_write(sbi, META); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Fill the locked page with data located in the block address. |
| 537 | * A caller needs to unlock the page on failure. |
| 538 | */ |
| 539 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
| 540 | { |
| 541 | struct bio *bio; |
| 542 | struct page *page = fio->encrypted_page ? |
| 543 | fio->encrypted_page : fio->page; |
| 544 | |
| 545 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
| 546 | fio->is_por ? META_POR : (__is_meta_io(fio) ? |
| 547 | META_GENERIC : DATA_GENERIC_ENHANCE))) |
| 548 | return -EFSCORRUPTED; |
| 549 | |
| 550 | trace_f2fs_submit_page_bio(page, fio); |
| 551 | f2fs_trace_ios(fio, 0); |
| 552 | |
| 553 | /* Allocate a new bio */ |
| 554 | bio = __bio_alloc(fio, 1); |
| 555 | |
| 556 | f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host, |
| 557 | fio->page->index, fio, GFP_NOIO); |
| 558 | |
| 559 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
| 560 | bio_put(bio); |
| 561 | return -EFAULT; |
| 562 | } |
| 563 | |
| 564 | if (fio->io_wbc && !is_read_io(fio->op)) |
| 565 | wbc_account_io(fio->io_wbc, page, PAGE_SIZE); |
| 566 | |
| 567 | bio_set_op_attrs(bio, fio->op, fio->op_flags); |
| 568 | |
| 569 | inc_page_count(fio->sbi, is_read_io(fio->op) ? |
| 570 | __read_io_type(page): WB_DATA_TYPE(fio->page)); |
| 571 | |
| 572 | if (is_read_io(fio->op)) |
| 573 | __f2fs_submit_read_bio(fio->sbi, bio, fio->type); |
| 574 | else |
| 575 | __submit_bio(fio->sbi, bio, fio->type); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, |
| 580 | block_t last_blkaddr, block_t cur_blkaddr) |
| 581 | { |
| 582 | if (last_blkaddr + 1 != cur_blkaddr) |
| 583 | return false; |
| 584 | return __same_bdev(sbi, cur_blkaddr, bio); |
| 585 | } |
| 586 | |
| 587 | static bool io_type_is_mergeable(struct f2fs_bio_info *io, |
| 588 | struct f2fs_io_info *fio) |
| 589 | { |
| 590 | if (io->fio.op != fio->op) |
| 591 | return false; |
| 592 | return io->fio.op_flags == fio->op_flags; |
| 593 | } |
| 594 | |
| 595 | static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, |
| 596 | struct f2fs_bio_info *io, |
| 597 | struct f2fs_io_info *fio, |
| 598 | block_t last_blkaddr, |
| 599 | block_t cur_blkaddr) |
| 600 | { |
| 601 | if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) { |
| 602 | unsigned int filled_blocks = |
| 603 | F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size); |
| 604 | unsigned int io_size = F2FS_IO_SIZE(sbi); |
| 605 | unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt; |
| 606 | |
| 607 | /* IOs in bio is aligned and left space of vectors is not enough */ |
| 608 | if (!(filled_blocks % io_size) && left_vecs < io_size) |
| 609 | return false; |
| 610 | } |
| 611 | if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr)) |
| 612 | return false; |
| 613 | return io_type_is_mergeable(io, fio); |
| 614 | } |
| 615 | |
| 616 | static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio, |
| 617 | struct page *page, enum temp_type temp) |
| 618 | { |
| 619 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 620 | struct bio_entry *be; |
| 621 | |
| 622 | be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS); |
| 623 | be->bio = bio; |
| 624 | bio_get(bio); |
| 625 | |
| 626 | if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) |
| 627 | f2fs_bug_on(sbi, 1); |
| 628 | |
| 629 | down_write(&io->bio_list_lock); |
| 630 | list_add_tail(&be->list, &io->bio_list); |
| 631 | up_write(&io->bio_list_lock); |
| 632 | } |
| 633 | |
| 634 | static void del_bio_entry(struct bio_entry *be) |
| 635 | { |
| 636 | list_del(&be->list); |
| 637 | kmem_cache_free(bio_entry_slab, be); |
| 638 | } |
| 639 | |
| 640 | static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio, |
| 641 | struct page *page) |
| 642 | { |
| 643 | enum temp_type temp; |
| 644 | bool found = false; |
| 645 | int ret = -EAGAIN; |
| 646 | |
| 647 | for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { |
| 648 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 649 | struct list_head *head = &io->bio_list; |
| 650 | struct bio_entry *be; |
| 651 | |
| 652 | down_write(&io->bio_list_lock); |
| 653 | list_for_each_entry(be, head, list) { |
| 654 | if (be->bio != *bio) |
| 655 | continue; |
| 656 | |
| 657 | found = true; |
| 658 | |
| 659 | if (bio_add_page(*bio, page, PAGE_SIZE, 0) == PAGE_SIZE) { |
| 660 | ret = 0; |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | /* bio is full */ |
| 665 | del_bio_entry(be); |
| 666 | __submit_bio(sbi, *bio, DATA); |
| 667 | break; |
| 668 | } |
| 669 | up_write(&io->bio_list_lock); |
| 670 | } |
| 671 | |
| 672 | if (ret) { |
| 673 | bio_put(*bio); |
| 674 | *bio = NULL; |
| 675 | } |
| 676 | |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, |
| 681 | struct bio **bio, struct page *page) |
| 682 | { |
| 683 | enum temp_type temp; |
| 684 | bool found = false; |
| 685 | struct bio *target = bio ? *bio : NULL; |
| 686 | |
| 687 | for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { |
| 688 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 689 | struct list_head *head = &io->bio_list; |
| 690 | struct bio_entry *be; |
| 691 | |
| 692 | if (list_empty(head)) |
| 693 | continue; |
| 694 | |
| 695 | down_read(&io->bio_list_lock); |
| 696 | list_for_each_entry(be, head, list) { |
| 697 | if (target) |
| 698 | found = (target == be->bio); |
| 699 | else |
| 700 | found = __has_merged_page(be->bio, NULL, |
| 701 | page, 0); |
| 702 | if (found) |
| 703 | break; |
| 704 | } |
| 705 | up_read(&io->bio_list_lock); |
| 706 | |
| 707 | if (!found) |
| 708 | continue; |
| 709 | |
| 710 | found = false; |
| 711 | |
| 712 | down_write(&io->bio_list_lock); |
| 713 | list_for_each_entry(be, head, list) { |
| 714 | if (target) |
| 715 | found = (target == be->bio); |
| 716 | else |
| 717 | found = __has_merged_page(be->bio, NULL, |
| 718 | page, 0); |
| 719 | if (found) { |
| 720 | target = be->bio; |
| 721 | del_bio_entry(be); |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | up_write(&io->bio_list_lock); |
| 726 | } |
| 727 | |
| 728 | if (found) |
| 729 | __submit_bio(sbi, target, DATA); |
| 730 | if (bio && *bio) { |
| 731 | bio_put(*bio); |
| 732 | *bio = NULL; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | int f2fs_merge_page_bio(struct f2fs_io_info *fio) |
| 737 | { |
| 738 | struct bio *bio = *fio->bio; |
| 739 | struct page *page = fio->encrypted_page ? |
| 740 | fio->encrypted_page : fio->page; |
| 741 | |
| 742 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
| 743 | __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) |
| 744 | return -EFSCORRUPTED; |
| 745 | |
| 746 | trace_f2fs_submit_page_bio(page, fio); |
| 747 | f2fs_trace_ios(fio, 0); |
| 748 | |
| 749 | if (bio && (!page_is_mergeable(fio->sbi, bio, *fio->last_block, |
| 750 | fio->new_blkaddr) || |
| 751 | !f2fs_crypt_mergeable_bio(bio, fio->page->mapping->host, |
| 752 | fio->page->index, fio))) |
| 753 | f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); |
| 754 | alloc_new: |
| 755 | if (!bio) { |
| 756 | bio = __bio_alloc(fio, BIO_MAX_PAGES); |
| 757 | f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host, |
| 758 | fio->page->index, fio, |
| 759 | GFP_NOIO); |
| 760 | bio_set_op_attrs(bio, fio->op, fio->op_flags); |
| 761 | |
| 762 | add_bio_entry(fio->sbi, bio, page, fio->temp); |
| 763 | } else { |
| 764 | if (add_ipu_page(fio->sbi, &bio, page)) |
| 765 | goto alloc_new; |
| 766 | } |
| 767 | |
| 768 | if (fio->io_wbc) |
| 769 | wbc_account_io(fio->io_wbc, page, PAGE_SIZE); |
| 770 | |
| 771 | inc_page_count(fio->sbi, WB_DATA_TYPE(page)); |
| 772 | |
| 773 | *fio->last_block = fio->new_blkaddr; |
| 774 | *fio->bio = bio; |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | void f2fs_submit_page_write(struct f2fs_io_info *fio) |
| 780 | { |
| 781 | struct f2fs_sb_info *sbi = fio->sbi; |
| 782 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
| 783 | struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; |
| 784 | struct page *bio_page; |
| 785 | |
| 786 | f2fs_bug_on(sbi, is_read_io(fio->op)); |
| 787 | |
| 788 | down_write(&io->io_rwsem); |
| 789 | next: |
| 790 | if (fio->in_list) { |
| 791 | spin_lock(&io->io_lock); |
| 792 | if (list_empty(&io->io_list)) { |
| 793 | spin_unlock(&io->io_lock); |
| 794 | goto out; |
| 795 | } |
| 796 | fio = list_first_entry(&io->io_list, |
| 797 | struct f2fs_io_info, list); |
| 798 | list_del(&fio->list); |
| 799 | spin_unlock(&io->io_lock); |
| 800 | } |
| 801 | |
| 802 | verify_fio_blkaddr(fio); |
| 803 | |
| 804 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 805 | |
| 806 | /* set submitted = true as a return value */ |
| 807 | fio->submitted = true; |
| 808 | |
| 809 | inc_page_count(sbi, WB_DATA_TYPE(bio_page)); |
| 810 | |
| 811 | if (io->bio && |
| 812 | (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, |
| 813 | fio->new_blkaddr) || |
| 814 | !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host, |
| 815 | fio->page->index, fio))) |
| 816 | __submit_merged_bio(io); |
| 817 | alloc_new: |
| 818 | if (io->bio == NULL) { |
| 819 | if (F2FS_IO_ALIGNED(sbi) && |
| 820 | (fio->type == DATA || fio->type == NODE) && |
| 821 | fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) { |
| 822 | dec_page_count(sbi, WB_DATA_TYPE(bio_page)); |
| 823 | fio->retry = true; |
| 824 | goto skip; |
| 825 | } |
| 826 | io->bio = __bio_alloc(fio, BIO_MAX_PAGES); |
| 827 | f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host, |
| 828 | fio->page->index, fio, |
| 829 | GFP_NOIO); |
| 830 | io->fio = *fio; |
| 831 | } |
| 832 | |
| 833 | if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) { |
| 834 | __submit_merged_bio(io); |
| 835 | goto alloc_new; |
| 836 | } |
| 837 | |
| 838 | if (fio->io_wbc) |
| 839 | wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE); |
| 840 | |
| 841 | io->last_block_in_bio = fio->new_blkaddr; |
| 842 | f2fs_trace_ios(fio, 0); |
| 843 | |
| 844 | trace_f2fs_submit_page_write(fio->page, fio); |
| 845 | skip: |
| 846 | if (fio->in_list) |
| 847 | goto next; |
| 848 | out: |
| 849 | if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || |
| 850 | !f2fs_is_checkpoint_ready(sbi)) |
| 851 | __submit_merged_bio(io); |
| 852 | up_write(&io->io_rwsem); |
| 853 | } |
| 854 | |
| 855 | static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx) |
| 856 | { |
| 857 | return fsverity_active(inode) && |
| 858 | idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE); |
| 859 | } |
| 860 | |
| 861 | static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, |
| 862 | unsigned nr_pages, unsigned op_flag, |
| 863 | pgoff_t first_idx) |
| 864 | { |
| 865 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 866 | struct bio *bio; |
| 867 | struct bio_post_read_ctx *ctx; |
| 868 | unsigned int post_read_steps = 0; |
| 869 | |
| 870 | bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false); |
| 871 | if (!bio) |
| 872 | return ERR_PTR(-ENOMEM); |
| 873 | |
| 874 | f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS); |
| 875 | |
| 876 | f2fs_target_device(sbi, blkaddr, bio); |
| 877 | bio->bi_end_io = f2fs_read_end_io; |
| 878 | bio_set_op_attrs(bio, REQ_OP_READ, op_flag); |
| 879 | |
| 880 | if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
| 881 | post_read_steps |= 1 << STEP_DECRYPT; |
| 882 | |
| 883 | if (f2fs_need_verity(inode, first_idx)) |
| 884 | post_read_steps |= 1 << STEP_VERITY; |
| 885 | |
| 886 | if (post_read_steps) { |
| 887 | ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); |
| 888 | if (!ctx) { |
| 889 | bio_put(bio); |
| 890 | return ERR_PTR(-ENOMEM); |
| 891 | } |
| 892 | ctx->bio = bio; |
| 893 | ctx->enabled_steps = post_read_steps; |
| 894 | bio->bi_private = ctx; |
| 895 | } |
| 896 | |
| 897 | return bio; |
| 898 | } |
| 899 | |
| 900 | /* This can handle encryption stuffs */ |
| 901 | static int f2fs_submit_page_read(struct inode *inode, struct page *page, |
| 902 | block_t blkaddr) |
| 903 | { |
| 904 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 905 | struct bio *bio; |
| 906 | |
| 907 | bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index); |
| 908 | if (IS_ERR(bio)) |
| 909 | return PTR_ERR(bio); |
| 910 | |
| 911 | /* wait for GCed page writeback via META_MAPPING */ |
| 912 | f2fs_wait_on_block_writeback(inode, blkaddr); |
| 913 | |
| 914 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
| 915 | bio_put(bio); |
| 916 | return -EFAULT; |
| 917 | } |
| 918 | ClearPageError(page); |
| 919 | inc_page_count(sbi, F2FS_RD_DATA); |
| 920 | __f2fs_submit_read_bio(sbi, bio, DATA); |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
| 925 | { |
| 926 | struct f2fs_node *rn = F2FS_NODE(dn->node_page); |
| 927 | __le32 *addr_array; |
| 928 | int base = 0; |
| 929 | |
| 930 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) |
| 931 | base = get_extra_isize(dn->inode); |
| 932 | |
| 933 | /* Get physical address of data block */ |
| 934 | addr_array = blkaddr_in_node(rn); |
| 935 | addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Lock ordering for the change of data block address: |
| 940 | * ->data_page |
| 941 | * ->node_page |
| 942 | * update block addresses in the node page |
| 943 | */ |
| 944 | void f2fs_set_data_blkaddr(struct dnode_of_data *dn) |
| 945 | { |
| 946 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
| 947 | __set_data_blkaddr(dn); |
| 948 | if (set_page_dirty(dn->node_page)) |
| 949 | dn->node_changed = true; |
| 950 | } |
| 951 | |
| 952 | void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 953 | { |
| 954 | dn->data_blkaddr = blkaddr; |
| 955 | f2fs_set_data_blkaddr(dn); |
| 956 | f2fs_update_extent_cache(dn); |
| 957 | } |
| 958 | |
| 959 | /* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
| 960 | int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) |
| 961 | { |
| 962 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 963 | int err; |
| 964 | |
| 965 | if (!count) |
| 966 | return 0; |
| 967 | |
| 968 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
| 969 | return -EPERM; |
| 970 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 971 | return err; |
| 972 | |
| 973 | trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
| 974 | dn->ofs_in_node, count); |
| 975 | |
| 976 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
| 977 | |
| 978 | for (; count > 0; dn->ofs_in_node++) { |
| 979 | block_t blkaddr = datablock_addr(dn->inode, |
| 980 | dn->node_page, dn->ofs_in_node); |
| 981 | if (blkaddr == NULL_ADDR) { |
| 982 | dn->data_blkaddr = NEW_ADDR; |
| 983 | __set_data_blkaddr(dn); |
| 984 | count--; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if (set_page_dirty(dn->node_page)) |
| 989 | dn->node_changed = true; |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | /* Should keep dn->ofs_in_node unchanged */ |
| 994 | int f2fs_reserve_new_block(struct dnode_of_data *dn) |
| 995 | { |
| 996 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 997 | int ret; |
| 998 | |
| 999 | ret = f2fs_reserve_new_blocks(dn, 1); |
| 1000 | dn->ofs_in_node = ofs_in_node; |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 1005 | { |
| 1006 | bool need_put = dn->inode_page ? false : true; |
| 1007 | int err; |
| 1008 | |
| 1009 | err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); |
| 1010 | if (err) |
| 1011 | return err; |
| 1012 | |
| 1013 | if (dn->data_blkaddr == NULL_ADDR) |
| 1014 | err = f2fs_reserve_new_block(dn); |
| 1015 | if (err || need_put) |
| 1016 | f2fs_put_dnode(dn); |
| 1017 | return err; |
| 1018 | } |
| 1019 | |
| 1020 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
| 1021 | { |
| 1022 | struct extent_info ei = {0,0,0}; |
| 1023 | struct inode *inode = dn->inode; |
| 1024 | |
| 1025 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 1026 | dn->data_blkaddr = ei.blk + index - ei.fofs; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | return f2fs_reserve_block(dn, index); |
| 1031 | } |
| 1032 | |
| 1033 | struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index, |
| 1034 | int op_flags, bool for_write) |
| 1035 | { |
| 1036 | struct address_space *mapping = inode->i_mapping; |
| 1037 | struct dnode_of_data dn; |
| 1038 | struct page *page; |
| 1039 | struct extent_info ei = {0,0,0}; |
| 1040 | int err; |
| 1041 | |
| 1042 | page = f2fs_grab_cache_page(mapping, index, for_write); |
| 1043 | if (!page) |
| 1044 | return ERR_PTR(-ENOMEM); |
| 1045 | |
| 1046 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 1047 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 1048 | if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, |
| 1049 | DATA_GENERIC_ENHANCE_READ)) { |
| 1050 | err = -EFSCORRUPTED; |
| 1051 | goto put_err; |
| 1052 | } |
| 1053 | goto got_it; |
| 1054 | } |
| 1055 | |
| 1056 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1057 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 1058 | if (err) |
| 1059 | goto put_err; |
| 1060 | f2fs_put_dnode(&dn); |
| 1061 | |
| 1062 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
| 1063 | err = -ENOENT; |
| 1064 | goto put_err; |
| 1065 | } |
| 1066 | if (dn.data_blkaddr != NEW_ADDR && |
| 1067 | !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), |
| 1068 | dn.data_blkaddr, |
| 1069 | DATA_GENERIC_ENHANCE)) { |
| 1070 | err = -EFSCORRUPTED; |
| 1071 | goto put_err; |
| 1072 | } |
| 1073 | got_it: |
| 1074 | if (PageUptodate(page)) { |
| 1075 | unlock_page(page); |
| 1076 | return page; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * A new dentry page is allocated but not able to be written, since its |
| 1081 | * new inode page couldn't be allocated due to -ENOSPC. |
| 1082 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 1083 | * see, f2fs_add_link -> f2fs_get_new_data_page -> |
| 1084 | * f2fs_init_inode_metadata. |
| 1085 | */ |
| 1086 | if (dn.data_blkaddr == NEW_ADDR) { |
| 1087 | zero_user_segment(page, 0, PAGE_SIZE); |
| 1088 | if (!PageUptodate(page)) |
| 1089 | SetPageUptodate(page); |
| 1090 | unlock_page(page); |
| 1091 | return page; |
| 1092 | } |
| 1093 | |
| 1094 | err = f2fs_submit_page_read(inode, page, dn.data_blkaddr); |
| 1095 | if (err) |
| 1096 | goto put_err; |
| 1097 | return page; |
| 1098 | |
| 1099 | put_err: |
| 1100 | f2fs_put_page(page, 1); |
| 1101 | return ERR_PTR(err); |
| 1102 | } |
| 1103 | |
| 1104 | struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index) |
| 1105 | { |
| 1106 | struct address_space *mapping = inode->i_mapping; |
| 1107 | struct page *page; |
| 1108 | |
| 1109 | page = find_get_page(mapping, index); |
| 1110 | if (page && PageUptodate(page)) |
| 1111 | return page; |
| 1112 | f2fs_put_page(page, 0); |
| 1113 | |
| 1114 | page = f2fs_get_read_data_page(inode, index, 0, false); |
| 1115 | if (IS_ERR(page)) |
| 1116 | return page; |
| 1117 | |
| 1118 | if (PageUptodate(page)) |
| 1119 | return page; |
| 1120 | |
| 1121 | wait_on_page_locked(page); |
| 1122 | if (unlikely(!PageUptodate(page))) { |
| 1123 | f2fs_put_page(page, 0); |
| 1124 | return ERR_PTR(-EIO); |
| 1125 | } |
| 1126 | return page; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * If it tries to access a hole, return an error. |
| 1131 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 1132 | * whether this page exists or not. |
| 1133 | */ |
| 1134 | struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index, |
| 1135 | bool for_write) |
| 1136 | { |
| 1137 | struct address_space *mapping = inode->i_mapping; |
| 1138 | struct page *page; |
| 1139 | repeat: |
| 1140 | page = f2fs_get_read_data_page(inode, index, 0, for_write); |
| 1141 | if (IS_ERR(page)) |
| 1142 | return page; |
| 1143 | |
| 1144 | /* wait for read completion */ |
| 1145 | lock_page(page); |
| 1146 | if (unlikely(page->mapping != mapping)) { |
| 1147 | f2fs_put_page(page, 1); |
| 1148 | goto repeat; |
| 1149 | } |
| 1150 | if (unlikely(!PageUptodate(page))) { |
| 1151 | f2fs_put_page(page, 1); |
| 1152 | return ERR_PTR(-EIO); |
| 1153 | } |
| 1154 | return page; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Caller ensures that this data page is never allocated. |
| 1159 | * A new zero-filled data page is allocated in the page cache. |
| 1160 | * |
| 1161 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 1162 | * f2fs_unlock_op(). |
| 1163 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
| 1164 | * ipage should be released by this function. |
| 1165 | */ |
| 1166 | struct page *f2fs_get_new_data_page(struct inode *inode, |
| 1167 | struct page *ipage, pgoff_t index, bool new_i_size) |
| 1168 | { |
| 1169 | struct address_space *mapping = inode->i_mapping; |
| 1170 | struct page *page; |
| 1171 | struct dnode_of_data dn; |
| 1172 | int err; |
| 1173 | |
| 1174 | page = f2fs_grab_cache_page(mapping, index, true); |
| 1175 | if (!page) { |
| 1176 | /* |
| 1177 | * before exiting, we should make sure ipage will be released |
| 1178 | * if any error occur. |
| 1179 | */ |
| 1180 | f2fs_put_page(ipage, 1); |
| 1181 | return ERR_PTR(-ENOMEM); |
| 1182 | } |
| 1183 | |
| 1184 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
| 1185 | err = f2fs_reserve_block(&dn, index); |
| 1186 | if (err) { |
| 1187 | f2fs_put_page(page, 1); |
| 1188 | return ERR_PTR(err); |
| 1189 | } |
| 1190 | if (!ipage) |
| 1191 | f2fs_put_dnode(&dn); |
| 1192 | |
| 1193 | if (PageUptodate(page)) |
| 1194 | goto got_it; |
| 1195 | |
| 1196 | if (dn.data_blkaddr == NEW_ADDR) { |
| 1197 | zero_user_segment(page, 0, PAGE_SIZE); |
| 1198 | if (!PageUptodate(page)) |
| 1199 | SetPageUptodate(page); |
| 1200 | } else { |
| 1201 | f2fs_put_page(page, 1); |
| 1202 | |
| 1203 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
| 1204 | f2fs_bug_on(F2FS_I_SB(inode), ipage); |
| 1205 | page = f2fs_get_lock_data_page(inode, index, true); |
| 1206 | if (IS_ERR(page)) |
| 1207 | return page; |
| 1208 | } |
| 1209 | got_it: |
| 1210 | if (new_i_size && i_size_read(inode) < |
| 1211 | ((loff_t)(index + 1) << PAGE_SHIFT)) |
| 1212 | f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
| 1213 | return page; |
| 1214 | } |
| 1215 | |
| 1216 | static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) |
| 1217 | { |
| 1218 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 1219 | struct f2fs_summary sum; |
| 1220 | struct node_info ni; |
| 1221 | block_t old_blkaddr; |
| 1222 | blkcnt_t count = 1; |
| 1223 | int err; |
| 1224 | |
| 1225 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
| 1226 | return -EPERM; |
| 1227 | |
| 1228 | err = f2fs_get_node_info(sbi, dn->nid, &ni); |
| 1229 | if (err) |
| 1230 | return err; |
| 1231 | |
| 1232 | dn->data_blkaddr = datablock_addr(dn->inode, |
| 1233 | dn->node_page, dn->ofs_in_node); |
| 1234 | if (dn->data_blkaddr != NULL_ADDR) |
| 1235 | goto alloc; |
| 1236 | |
| 1237 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 1238 | return err; |
| 1239 | |
| 1240 | alloc: |
| 1241 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 1242 | old_blkaddr = dn->data_blkaddr; |
| 1243 | f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr, |
| 1244 | &sum, seg_type, NULL, false); |
| 1245 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 1246 | invalidate_mapping_pages(META_MAPPING(sbi), |
| 1247 | old_blkaddr, old_blkaddr); |
| 1248 | f2fs_update_data_blkaddr(dn, dn->data_blkaddr); |
| 1249 | |
| 1250 | /* |
| 1251 | * i_size will be updated by direct_IO. Otherwise, we'll get stale |
| 1252 | * data from unwritten block via dio_read. |
| 1253 | */ |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) |
| 1258 | { |
| 1259 | struct inode *inode = file_inode(iocb->ki_filp); |
| 1260 | struct f2fs_map_blocks map; |
| 1261 | int flag; |
| 1262 | int err = 0; |
| 1263 | bool direct_io = iocb->ki_flags & IOCB_DIRECT; |
| 1264 | |
| 1265 | /* convert inline data for Direct I/O*/ |
| 1266 | if (direct_io) { |
| 1267 | err = f2fs_convert_inline_inode(inode); |
| 1268 | if (err) |
| 1269 | return err; |
| 1270 | } |
| 1271 | |
| 1272 | if (direct_io && allow_outplace_dio(inode, iocb, from)) |
| 1273 | return 0; |
| 1274 | |
| 1275 | if (is_inode_flag_set(inode, FI_NO_PREALLOC)) |
| 1276 | return 0; |
| 1277 | |
| 1278 | map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); |
| 1279 | map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); |
| 1280 | if (map.m_len > map.m_lblk) |
| 1281 | map.m_len -= map.m_lblk; |
| 1282 | else |
| 1283 | map.m_len = 0; |
| 1284 | |
| 1285 | map.m_next_pgofs = NULL; |
| 1286 | map.m_next_extent = NULL; |
| 1287 | map.m_seg_type = NO_CHECK_TYPE; |
| 1288 | map.m_may_create = true; |
| 1289 | |
| 1290 | if (direct_io) { |
| 1291 | map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint); |
| 1292 | flag = f2fs_force_buffered_io(inode, iocb, from) ? |
| 1293 | F2FS_GET_BLOCK_PRE_AIO : |
| 1294 | F2FS_GET_BLOCK_PRE_DIO; |
| 1295 | goto map_blocks; |
| 1296 | } |
| 1297 | if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) { |
| 1298 | err = f2fs_convert_inline_inode(inode); |
| 1299 | if (err) |
| 1300 | return err; |
| 1301 | } |
| 1302 | if (f2fs_has_inline_data(inode)) |
| 1303 | return err; |
| 1304 | |
| 1305 | flag = F2FS_GET_BLOCK_PRE_AIO; |
| 1306 | |
| 1307 | map_blocks: |
| 1308 | err = f2fs_map_blocks(inode, &map, 1, flag); |
| 1309 | if (map.m_len > 0 && err == -ENOSPC) { |
| 1310 | if (!direct_io) |
| 1311 | set_inode_flag(inode, FI_NO_PREALLOC); |
| 1312 | err = 0; |
| 1313 | } |
| 1314 | return err; |
| 1315 | } |
| 1316 | |
| 1317 | void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock) |
| 1318 | { |
| 1319 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
| 1320 | if (lock) |
| 1321 | down_read(&sbi->node_change); |
| 1322 | else |
| 1323 | up_read(&sbi->node_change); |
| 1324 | } else { |
| 1325 | if (lock) |
| 1326 | f2fs_lock_op(sbi); |
| 1327 | else |
| 1328 | f2fs_unlock_op(sbi); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
| 1334 | * f2fs_map_blocks structure. |
| 1335 | * If original data blocks are allocated, then give them to blockdev. |
| 1336 | * Otherwise, |
| 1337 | * a. preallocate requested block addresses |
| 1338 | * b. do not use extent cache for better performance |
| 1339 | * c. give the block addresses to blockdev |
| 1340 | */ |
| 1341 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
| 1342 | int create, int flag) |
| 1343 | { |
| 1344 | unsigned int maxblocks = map->m_len; |
| 1345 | struct dnode_of_data dn; |
| 1346 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1347 | int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; |
| 1348 | pgoff_t pgofs, end_offset, end; |
| 1349 | int err = 0, ofs = 1; |
| 1350 | unsigned int ofs_in_node, last_ofs_in_node; |
| 1351 | blkcnt_t prealloc; |
| 1352 | struct extent_info ei = {0,0,0}; |
| 1353 | block_t blkaddr; |
| 1354 | unsigned int start_pgofs; |
| 1355 | |
| 1356 | if (!maxblocks) |
| 1357 | return 0; |
| 1358 | |
| 1359 | map->m_len = 0; |
| 1360 | map->m_flags = 0; |
| 1361 | |
| 1362 | /* it only supports block size == page size */ |
| 1363 | pgofs = (pgoff_t)map->m_lblk; |
| 1364 | end = pgofs + maxblocks; |
| 1365 | |
| 1366 | if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
| 1367 | if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && |
| 1368 | map->m_may_create) |
| 1369 | goto next_dnode; |
| 1370 | |
| 1371 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
| 1372 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); |
| 1373 | map->m_flags = F2FS_MAP_MAPPED; |
| 1374 | if (map->m_next_extent) |
| 1375 | *map->m_next_extent = pgofs + map->m_len; |
| 1376 | |
| 1377 | /* for hardware encryption, but to avoid potential issue in future */ |
| 1378 | if (flag == F2FS_GET_BLOCK_DIO) |
| 1379 | f2fs_wait_on_block_writeback_range(inode, |
| 1380 | map->m_pblk, map->m_len); |
| 1381 | goto out; |
| 1382 | } |
| 1383 | |
| 1384 | next_dnode: |
| 1385 | if (map->m_may_create) |
| 1386 | __do_map_lock(sbi, flag, true); |
| 1387 | |
| 1388 | /* When reading holes, we need its node page */ |
| 1389 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1390 | err = f2fs_get_dnode_of_data(&dn, pgofs, mode); |
| 1391 | if (err) { |
| 1392 | if (flag == F2FS_GET_BLOCK_BMAP) |
| 1393 | map->m_pblk = 0; |
| 1394 | if (err == -ENOENT) { |
| 1395 | err = 0; |
| 1396 | if (map->m_next_pgofs) |
| 1397 | *map->m_next_pgofs = |
| 1398 | f2fs_get_next_page_offset(&dn, pgofs); |
| 1399 | if (map->m_next_extent) |
| 1400 | *map->m_next_extent = |
| 1401 | f2fs_get_next_page_offset(&dn, pgofs); |
| 1402 | } |
| 1403 | goto unlock_out; |
| 1404 | } |
| 1405 | |
| 1406 | start_pgofs = pgofs; |
| 1407 | prealloc = 0; |
| 1408 | last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
| 1409 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
| 1410 | |
| 1411 | next_block: |
| 1412 | blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node); |
| 1413 | |
| 1414 | if (__is_valid_data_blkaddr(blkaddr) && |
| 1415 | !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) { |
| 1416 | err = -EFSCORRUPTED; |
| 1417 | goto sync_out; |
| 1418 | } |
| 1419 | |
| 1420 | if (__is_valid_data_blkaddr(blkaddr)) { |
| 1421 | /* use out-place-update for driect IO under LFS mode */ |
| 1422 | if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && |
| 1423 | map->m_may_create) { |
| 1424 | err = __allocate_data_block(&dn, map->m_seg_type); |
| 1425 | if (err) |
| 1426 | goto sync_out; |
| 1427 | blkaddr = dn.data_blkaddr; |
| 1428 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 1429 | } |
| 1430 | } else { |
| 1431 | if (create) { |
| 1432 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1433 | err = -EIO; |
| 1434 | goto sync_out; |
| 1435 | } |
| 1436 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
| 1437 | if (blkaddr == NULL_ADDR) { |
| 1438 | prealloc++; |
| 1439 | last_ofs_in_node = dn.ofs_in_node; |
| 1440 | } |
| 1441 | } else { |
| 1442 | WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO && |
| 1443 | flag != F2FS_GET_BLOCK_DIO); |
| 1444 | err = __allocate_data_block(&dn, |
| 1445 | map->m_seg_type); |
| 1446 | if (!err) |
| 1447 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 1448 | } |
| 1449 | if (err) |
| 1450 | goto sync_out; |
| 1451 | map->m_flags |= F2FS_MAP_NEW; |
| 1452 | blkaddr = dn.data_blkaddr; |
| 1453 | } else { |
| 1454 | if (flag == F2FS_GET_BLOCK_BMAP) { |
| 1455 | map->m_pblk = 0; |
| 1456 | goto sync_out; |
| 1457 | } |
| 1458 | if (flag == F2FS_GET_BLOCK_PRECACHE) |
| 1459 | goto sync_out; |
| 1460 | if (flag == F2FS_GET_BLOCK_FIEMAP && |
| 1461 | blkaddr == NULL_ADDR) { |
| 1462 | if (map->m_next_pgofs) |
| 1463 | *map->m_next_pgofs = pgofs + 1; |
| 1464 | goto sync_out; |
| 1465 | } |
| 1466 | if (flag != F2FS_GET_BLOCK_FIEMAP) { |
| 1467 | /* for defragment case */ |
| 1468 | if (map->m_next_pgofs) |
| 1469 | *map->m_next_pgofs = pgofs + 1; |
| 1470 | goto sync_out; |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 1476 | goto skip; |
| 1477 | |
| 1478 | if (map->m_len == 0) { |
| 1479 | /* preallocated unwritten block should be mapped for fiemap. */ |
| 1480 | if (blkaddr == NEW_ADDR) |
| 1481 | map->m_flags |= F2FS_MAP_UNWRITTEN; |
| 1482 | map->m_flags |= F2FS_MAP_MAPPED; |
| 1483 | |
| 1484 | map->m_pblk = blkaddr; |
| 1485 | map->m_len = 1; |
| 1486 | } else if ((map->m_pblk != NEW_ADDR && |
| 1487 | blkaddr == (map->m_pblk + ofs)) || |
| 1488 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || |
| 1489 | flag == F2FS_GET_BLOCK_PRE_DIO) { |
| 1490 | ofs++; |
| 1491 | map->m_len++; |
| 1492 | } else { |
| 1493 | goto sync_out; |
| 1494 | } |
| 1495 | |
| 1496 | skip: |
| 1497 | dn.ofs_in_node++; |
| 1498 | pgofs++; |
| 1499 | |
| 1500 | /* preallocate blocks in batch for one dnode page */ |
| 1501 | if (flag == F2FS_GET_BLOCK_PRE_AIO && |
| 1502 | (pgofs == end || dn.ofs_in_node == end_offset)) { |
| 1503 | |
| 1504 | dn.ofs_in_node = ofs_in_node; |
| 1505 | err = f2fs_reserve_new_blocks(&dn, prealloc); |
| 1506 | if (err) |
| 1507 | goto sync_out; |
| 1508 | |
| 1509 | map->m_len += dn.ofs_in_node - ofs_in_node; |
| 1510 | if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { |
| 1511 | err = -ENOSPC; |
| 1512 | goto sync_out; |
| 1513 | } |
| 1514 | dn.ofs_in_node = end_offset; |
| 1515 | } |
| 1516 | |
| 1517 | if (pgofs >= end) |
| 1518 | goto sync_out; |
| 1519 | else if (dn.ofs_in_node < end_offset) |
| 1520 | goto next_block; |
| 1521 | |
| 1522 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1523 | if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1524 | unsigned int ofs = start_pgofs - map->m_lblk; |
| 1525 | |
| 1526 | f2fs_update_extent_cache_range(&dn, |
| 1527 | start_pgofs, map->m_pblk + ofs, |
| 1528 | map->m_len - ofs); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | f2fs_put_dnode(&dn); |
| 1533 | |
| 1534 | if (map->m_may_create) { |
| 1535 | __do_map_lock(sbi, flag, false); |
| 1536 | f2fs_balance_fs(sbi, dn.node_changed); |
| 1537 | } |
| 1538 | goto next_dnode; |
| 1539 | |
| 1540 | sync_out: |
| 1541 | |
| 1542 | /* for hardware encryption, but to avoid potential issue in future */ |
| 1543 | if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) |
| 1544 | f2fs_wait_on_block_writeback_range(inode, |
| 1545 | map->m_pblk, map->m_len); |
| 1546 | |
| 1547 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1548 | if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1549 | unsigned int ofs = start_pgofs - map->m_lblk; |
| 1550 | |
| 1551 | f2fs_update_extent_cache_range(&dn, |
| 1552 | start_pgofs, map->m_pblk + ofs, |
| 1553 | map->m_len - ofs); |
| 1554 | } |
| 1555 | if (map->m_next_extent) |
| 1556 | *map->m_next_extent = pgofs + 1; |
| 1557 | } |
| 1558 | f2fs_put_dnode(&dn); |
| 1559 | unlock_out: |
| 1560 | if (map->m_may_create) { |
| 1561 | __do_map_lock(sbi, flag, false); |
| 1562 | f2fs_balance_fs(sbi, dn.node_changed); |
| 1563 | } |
| 1564 | out: |
| 1565 | trace_f2fs_map_blocks(inode, map, err); |
| 1566 | return err; |
| 1567 | } |
| 1568 | |
| 1569 | bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) |
| 1570 | { |
| 1571 | struct f2fs_map_blocks map; |
| 1572 | block_t last_lblk; |
| 1573 | int err; |
| 1574 | |
| 1575 | if (pos + len > i_size_read(inode)) |
| 1576 | return false; |
| 1577 | |
| 1578 | map.m_lblk = F2FS_BYTES_TO_BLK(pos); |
| 1579 | map.m_next_pgofs = NULL; |
| 1580 | map.m_next_extent = NULL; |
| 1581 | map.m_seg_type = NO_CHECK_TYPE; |
| 1582 | map.m_may_create = false; |
| 1583 | last_lblk = F2FS_BLK_ALIGN(pos + len); |
| 1584 | |
| 1585 | while (map.m_lblk < last_lblk) { |
| 1586 | map.m_len = last_lblk - map.m_lblk; |
| 1587 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
| 1588 | if (err || map.m_len == 0) |
| 1589 | return false; |
| 1590 | map.m_lblk += map.m_len; |
| 1591 | } |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 1596 | struct buffer_head *bh, int create, int flag, |
| 1597 | pgoff_t *next_pgofs, int seg_type, bool may_write) |
| 1598 | { |
| 1599 | struct f2fs_map_blocks map; |
| 1600 | int err; |
| 1601 | |
| 1602 | map.m_lblk = iblock; |
| 1603 | map.m_len = bh->b_size >> inode->i_blkbits; |
| 1604 | map.m_next_pgofs = next_pgofs; |
| 1605 | map.m_next_extent = NULL; |
| 1606 | map.m_seg_type = seg_type; |
| 1607 | map.m_may_create = may_write; |
| 1608 | |
| 1609 | err = f2fs_map_blocks(inode, &map, create, flag); |
| 1610 | if (!err) { |
| 1611 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 1612 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; |
| 1613 | bh->b_size = (u64)map.m_len << inode->i_blkbits; |
| 1614 | } |
| 1615 | return err; |
| 1616 | } |
| 1617 | |
| 1618 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 1619 | struct buffer_head *bh_result, int create, int flag, |
| 1620 | pgoff_t *next_pgofs) |
| 1621 | { |
| 1622 | return __get_data_block(inode, iblock, bh_result, create, |
| 1623 | flag, next_pgofs, |
| 1624 | NO_CHECK_TYPE, create); |
| 1625 | } |
| 1626 | |
| 1627 | static int get_data_block_dio_write(struct inode *inode, sector_t iblock, |
| 1628 | struct buffer_head *bh_result, int create) |
| 1629 | { |
| 1630 | return __get_data_block(inode, iblock, bh_result, create, |
| 1631 | F2FS_GET_BLOCK_DIO, NULL, |
| 1632 | f2fs_rw_hint_to_seg_type(inode->i_write_hint), |
| 1633 | IS_SWAPFILE(inode) ? false : true); |
| 1634 | } |
| 1635 | |
| 1636 | static int get_data_block_dio(struct inode *inode, sector_t iblock, |
| 1637 | struct buffer_head *bh_result, int create) |
| 1638 | { |
| 1639 | return __get_data_block(inode, iblock, bh_result, create, |
| 1640 | F2FS_GET_BLOCK_DIO, NULL, |
| 1641 | f2fs_rw_hint_to_seg_type(inode->i_write_hint), |
| 1642 | false); |
| 1643 | } |
| 1644 | |
| 1645 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
| 1646 | struct buffer_head *bh_result, int create) |
| 1647 | { |
| 1648 | /* Block number less than F2FS MAX BLOCKS */ |
| 1649 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
| 1650 | return -EFBIG; |
| 1651 | |
| 1652 | return __get_data_block(inode, iblock, bh_result, create, |
| 1653 | F2FS_GET_BLOCK_BMAP, NULL, |
| 1654 | NO_CHECK_TYPE, create); |
| 1655 | } |
| 1656 | |
| 1657 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 1658 | { |
| 1659 | return (offset >> inode->i_blkbits); |
| 1660 | } |
| 1661 | |
| 1662 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 1663 | { |
| 1664 | return (blk << inode->i_blkbits); |
| 1665 | } |
| 1666 | |
| 1667 | static int f2fs_xattr_fiemap(struct inode *inode, |
| 1668 | struct fiemap_extent_info *fieinfo) |
| 1669 | { |
| 1670 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1671 | struct page *page; |
| 1672 | struct node_info ni; |
| 1673 | __u64 phys = 0, len; |
| 1674 | __u32 flags; |
| 1675 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 1676 | int err = 0; |
| 1677 | |
| 1678 | if (f2fs_has_inline_xattr(inode)) { |
| 1679 | int offset; |
| 1680 | |
| 1681 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), |
| 1682 | inode->i_ino, false); |
| 1683 | if (!page) |
| 1684 | return -ENOMEM; |
| 1685 | |
| 1686 | err = f2fs_get_node_info(sbi, inode->i_ino, &ni); |
| 1687 | if (err) { |
| 1688 | f2fs_put_page(page, 1); |
| 1689 | return err; |
| 1690 | } |
| 1691 | |
| 1692 | phys = (__u64)blk_to_logical(inode, ni.blk_addr); |
| 1693 | offset = offsetof(struct f2fs_inode, i_addr) + |
| 1694 | sizeof(__le32) * (DEF_ADDRS_PER_INODE - |
| 1695 | get_inline_xattr_addrs(inode)); |
| 1696 | |
| 1697 | phys += offset; |
| 1698 | len = inline_xattr_size(inode); |
| 1699 | |
| 1700 | f2fs_put_page(page, 1); |
| 1701 | |
| 1702 | flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; |
| 1703 | |
| 1704 | if (!xnid) |
| 1705 | flags |= FIEMAP_EXTENT_LAST; |
| 1706 | |
| 1707 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 1708 | if (err || err == 1) |
| 1709 | return err; |
| 1710 | } |
| 1711 | |
| 1712 | if (xnid) { |
| 1713 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false); |
| 1714 | if (!page) |
| 1715 | return -ENOMEM; |
| 1716 | |
| 1717 | err = f2fs_get_node_info(sbi, xnid, &ni); |
| 1718 | if (err) { |
| 1719 | f2fs_put_page(page, 1); |
| 1720 | return err; |
| 1721 | } |
| 1722 | |
| 1723 | phys = (__u64)blk_to_logical(inode, ni.blk_addr); |
| 1724 | len = inode->i_sb->s_blocksize; |
| 1725 | |
| 1726 | f2fs_put_page(page, 1); |
| 1727 | |
| 1728 | flags = FIEMAP_EXTENT_LAST; |
| 1729 | } |
| 1730 | |
| 1731 | if (phys) |
| 1732 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 1733 | |
| 1734 | return (err < 0 ? err : 0); |
| 1735 | } |
| 1736 | |
| 1737 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1738 | u64 start, u64 len) |
| 1739 | { |
| 1740 | struct buffer_head map_bh; |
| 1741 | sector_t start_blk, last_blk; |
| 1742 | pgoff_t next_pgofs; |
| 1743 | u64 logical = 0, phys = 0, size = 0; |
| 1744 | u32 flags = 0; |
| 1745 | int ret = 0; |
| 1746 | |
| 1747 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 1748 | ret = f2fs_precache_extents(inode); |
| 1749 | if (ret) |
| 1750 | return ret; |
| 1751 | } |
| 1752 | |
| 1753 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR); |
| 1754 | if (ret) |
| 1755 | return ret; |
| 1756 | |
| 1757 | inode_lock(inode); |
| 1758 | |
| 1759 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 1760 | ret = f2fs_xattr_fiemap(inode, fieinfo); |
| 1761 | goto out; |
| 1762 | } |
| 1763 | |
| 1764 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
| 1765 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 1766 | if (ret != -EAGAIN) |
| 1767 | goto out; |
| 1768 | } |
| 1769 | |
| 1770 | if (logical_to_blk(inode, len) == 0) |
| 1771 | len = blk_to_logical(inode, 1); |
| 1772 | |
| 1773 | start_blk = logical_to_blk(inode, start); |
| 1774 | last_blk = logical_to_blk(inode, start + len - 1); |
| 1775 | |
| 1776 | next: |
| 1777 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 1778 | map_bh.b_size = len; |
| 1779 | |
| 1780 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
| 1781 | F2FS_GET_BLOCK_FIEMAP, &next_pgofs); |
| 1782 | if (ret) |
| 1783 | goto out; |
| 1784 | |
| 1785 | /* HOLE */ |
| 1786 | if (!buffer_mapped(&map_bh)) { |
| 1787 | start_blk = next_pgofs; |
| 1788 | |
| 1789 | if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, |
| 1790 | F2FS_I_SB(inode)->max_file_blocks)) |
| 1791 | goto prep_next; |
| 1792 | |
| 1793 | flags |= FIEMAP_EXTENT_LAST; |
| 1794 | } |
| 1795 | |
| 1796 | if (size) { |
| 1797 | if (IS_ENCRYPTED(inode)) |
| 1798 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
| 1799 | |
| 1800 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 1801 | phys, size, flags); |
| 1802 | } |
| 1803 | |
| 1804 | if (start_blk > last_blk || ret) |
| 1805 | goto out; |
| 1806 | |
| 1807 | logical = blk_to_logical(inode, start_blk); |
| 1808 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 1809 | size = map_bh.b_size; |
| 1810 | flags = 0; |
| 1811 | if (buffer_unwritten(&map_bh)) |
| 1812 | flags = FIEMAP_EXTENT_UNWRITTEN; |
| 1813 | |
| 1814 | start_blk += logical_to_blk(inode, size); |
| 1815 | |
| 1816 | prep_next: |
| 1817 | cond_resched(); |
| 1818 | if (fatal_signal_pending(current)) |
| 1819 | ret = -EINTR; |
| 1820 | else |
| 1821 | goto next; |
| 1822 | out: |
| 1823 | if (ret == 1) |
| 1824 | ret = 0; |
| 1825 | |
| 1826 | inode_unlock(inode); |
| 1827 | return ret; |
| 1828 | } |
| 1829 | |
| 1830 | static inline loff_t f2fs_readpage_limit(struct inode *inode) |
| 1831 | { |
| 1832 | if (IS_ENABLED(CONFIG_FS_VERITY) && |
| 1833 | (IS_VERITY(inode) || f2fs_verity_in_progress(inode))) |
| 1834 | return inode->i_sb->s_maxbytes; |
| 1835 | |
| 1836 | return i_size_read(inode); |
| 1837 | } |
| 1838 | |
| 1839 | static int f2fs_read_single_page(struct inode *inode, struct page *page, |
| 1840 | unsigned nr_pages, |
| 1841 | struct f2fs_map_blocks *map, |
| 1842 | struct bio **bio_ret, |
| 1843 | sector_t *last_block_in_bio, |
| 1844 | bool is_readahead) |
| 1845 | { |
| 1846 | struct bio *bio = *bio_ret; |
| 1847 | const unsigned blkbits = inode->i_blkbits; |
| 1848 | const unsigned blocksize = 1 << blkbits; |
| 1849 | sector_t block_in_file; |
| 1850 | sector_t last_block; |
| 1851 | sector_t last_block_in_file; |
| 1852 | sector_t block_nr; |
| 1853 | int ret = 0; |
| 1854 | |
| 1855 | block_in_file = (sector_t)page_index(page); |
| 1856 | last_block = block_in_file + nr_pages; |
| 1857 | last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >> |
| 1858 | blkbits; |
| 1859 | if (last_block > last_block_in_file) |
| 1860 | last_block = last_block_in_file; |
| 1861 | |
| 1862 | /* just zeroing out page which is beyond EOF */ |
| 1863 | if (block_in_file >= last_block) |
| 1864 | goto zero_out; |
| 1865 | /* |
| 1866 | * Map blocks using the previous result first. |
| 1867 | */ |
| 1868 | if ((map->m_flags & F2FS_MAP_MAPPED) && |
| 1869 | block_in_file > map->m_lblk && |
| 1870 | block_in_file < (map->m_lblk + map->m_len)) |
| 1871 | goto got_it; |
| 1872 | |
| 1873 | /* |
| 1874 | * Then do more f2fs_map_blocks() calls until we are |
| 1875 | * done with this page. |
| 1876 | */ |
| 1877 | map->m_lblk = block_in_file; |
| 1878 | map->m_len = last_block - block_in_file; |
| 1879 | |
| 1880 | ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT); |
| 1881 | if (ret) |
| 1882 | goto out; |
| 1883 | got_it: |
| 1884 | if ((map->m_flags & F2FS_MAP_MAPPED)) { |
| 1885 | block_nr = map->m_pblk + block_in_file - map->m_lblk; |
| 1886 | SetPageMappedToDisk(page); |
| 1887 | |
| 1888 | if (!PageUptodate(page) && (!PageSwapCache(page) && |
| 1889 | !cleancache_get_page(page))) { |
| 1890 | SetPageUptodate(page); |
| 1891 | goto confused; |
| 1892 | } |
| 1893 | |
| 1894 | if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, |
| 1895 | DATA_GENERIC_ENHANCE_READ)) { |
| 1896 | ret = -EFSCORRUPTED; |
| 1897 | goto out; |
| 1898 | } |
| 1899 | } else { |
| 1900 | zero_out: |
| 1901 | zero_user_segment(page, 0, PAGE_SIZE); |
| 1902 | if (f2fs_need_verity(inode, page->index) && |
| 1903 | !fsverity_verify_page(page)) { |
| 1904 | ret = -EIO; |
| 1905 | goto out; |
| 1906 | } |
| 1907 | if (!PageUptodate(page)) |
| 1908 | SetPageUptodate(page); |
| 1909 | unlock_page(page); |
| 1910 | goto out; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | * This page will go to BIO. Do we need to send this |
| 1915 | * BIO off first? |
| 1916 | */ |
| 1917 | if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, |
| 1918 | *last_block_in_bio, block_nr) || |
| 1919 | !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) { |
| 1920 | submit_and_realloc: |
| 1921 | __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 1922 | bio = NULL; |
| 1923 | } |
| 1924 | if (bio == NULL) { |
| 1925 | bio = f2fs_grab_read_bio(inode, block_nr, nr_pages, |
| 1926 | is_readahead ? REQ_RAHEAD : 0, page->index); |
| 1927 | if (IS_ERR(bio)) { |
| 1928 | ret = PTR_ERR(bio); |
| 1929 | bio = NULL; |
| 1930 | goto out; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * If the page is under writeback, we need to wait for |
| 1936 | * its completion to see the correct decrypted data. |
| 1937 | */ |
| 1938 | f2fs_wait_on_block_writeback(inode, block_nr); |
| 1939 | |
| 1940 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
| 1941 | goto submit_and_realloc; |
| 1942 | |
| 1943 | inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); |
| 1944 | ClearPageError(page); |
| 1945 | *last_block_in_bio = block_nr; |
| 1946 | goto out; |
| 1947 | confused: |
| 1948 | if (bio) { |
| 1949 | __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 1950 | bio = NULL; |
| 1951 | } |
| 1952 | unlock_page(page); |
| 1953 | out: |
| 1954 | *bio_ret = bio; |
| 1955 | return ret; |
| 1956 | } |
| 1957 | |
| 1958 | /* |
| 1959 | * This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 1960 | * Major change was from block_size == page_size in f2fs by default. |
| 1961 | * |
| 1962 | * Note that the aops->readpages() function is ONLY used for read-ahead. If |
| 1963 | * this function ever deviates from doing just read-ahead, it should either |
| 1964 | * use ->readpage() or do the necessary surgery to decouple ->readpages() |
| 1965 | * from read-ahead. |
| 1966 | */ |
| 1967 | static int f2fs_mpage_readpages(struct address_space *mapping, |
| 1968 | struct list_head *pages, struct page *page, |
| 1969 | unsigned nr_pages, bool is_readahead) |
| 1970 | { |
| 1971 | struct bio *bio = NULL; |
| 1972 | sector_t last_block_in_bio = 0; |
| 1973 | struct inode *inode = mapping->host; |
| 1974 | struct f2fs_map_blocks map; |
| 1975 | int ret = 0; |
| 1976 | |
| 1977 | map.m_pblk = 0; |
| 1978 | map.m_lblk = 0; |
| 1979 | map.m_len = 0; |
| 1980 | map.m_flags = 0; |
| 1981 | map.m_next_pgofs = NULL; |
| 1982 | map.m_next_extent = NULL; |
| 1983 | map.m_seg_type = NO_CHECK_TYPE; |
| 1984 | map.m_may_create = false; |
| 1985 | |
| 1986 | for (; nr_pages; nr_pages--) { |
| 1987 | if (pages) { |
| 1988 | page = list_last_entry(pages, struct page, lru); |
| 1989 | |
| 1990 | prefetchw(&page->flags); |
| 1991 | list_del(&page->lru); |
| 1992 | if (add_to_page_cache_lru(page, mapping, |
| 1993 | page_index(page), |
| 1994 | readahead_gfp_mask(mapping))) |
| 1995 | goto next_page; |
| 1996 | } |
| 1997 | |
| 1998 | ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio, |
| 1999 | &last_block_in_bio, is_readahead); |
| 2000 | if (ret) { |
| 2001 | SetPageError(page); |
| 2002 | zero_user_segment(page, 0, PAGE_SIZE); |
| 2003 | unlock_page(page); |
| 2004 | } |
| 2005 | next_page: |
| 2006 | if (pages) |
| 2007 | put_page(page); |
| 2008 | } |
| 2009 | BUG_ON(pages && !list_empty(pages)); |
| 2010 | if (bio) |
| 2011 | __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 2012 | return pages ? 0 : ret; |
| 2013 | } |
| 2014 | |
| 2015 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 2016 | { |
| 2017 | struct inode *inode = page_file_mapping(page)->host; |
| 2018 | int ret = -EAGAIN; |
| 2019 | |
| 2020 | trace_f2fs_readpage(page, DATA); |
| 2021 | |
| 2022 | /* If the file has inline data, try to read it directly */ |
| 2023 | if (f2fs_has_inline_data(inode)) |
| 2024 | ret = f2fs_read_inline_data(inode, page); |
| 2025 | if (ret == -EAGAIN) |
| 2026 | ret = f2fs_mpage_readpages(page_file_mapping(page), |
| 2027 | NULL, page, 1, false); |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
| 2031 | static int f2fs_read_data_pages(struct file *file, |
| 2032 | struct address_space *mapping, |
| 2033 | struct list_head *pages, unsigned nr_pages) |
| 2034 | { |
| 2035 | struct inode *inode = mapping->host; |
| 2036 | struct page *page = list_last_entry(pages, struct page, lru); |
| 2037 | |
| 2038 | trace_f2fs_readpages(inode, page, nr_pages); |
| 2039 | |
| 2040 | /* If the file has inline data, skip readpages */ |
| 2041 | if (f2fs_has_inline_data(inode)) |
| 2042 | return 0; |
| 2043 | |
| 2044 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true); |
| 2045 | } |
| 2046 | |
| 2047 | static int encrypt_one_page(struct f2fs_io_info *fio) |
| 2048 | { |
| 2049 | struct inode *inode = fio->page->mapping->host; |
| 2050 | struct page *mpage; |
| 2051 | gfp_t gfp_flags = GFP_NOFS; |
| 2052 | |
| 2053 | if (!f2fs_encrypted_file(inode)) |
| 2054 | return 0; |
| 2055 | |
| 2056 | /* wait for GCed page writeback via META_MAPPING */ |
| 2057 | f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); |
| 2058 | |
| 2059 | if (fscrypt_inode_uses_inline_crypto(inode)) |
| 2060 | return 0; |
| 2061 | |
| 2062 | retry_encrypt: |
| 2063 | fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page, |
| 2064 | PAGE_SIZE, 0, |
| 2065 | gfp_flags); |
| 2066 | if (IS_ERR(fio->encrypted_page)) { |
| 2067 | /* flush pending IOs and wait for a while in the ENOMEM case */ |
| 2068 | if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { |
| 2069 | f2fs_flush_merged_writes(fio->sbi); |
| 2070 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 2071 | gfp_flags |= __GFP_NOFAIL; |
| 2072 | goto retry_encrypt; |
| 2073 | } |
| 2074 | return PTR_ERR(fio->encrypted_page); |
| 2075 | } |
| 2076 | |
| 2077 | mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr); |
| 2078 | if (mpage) { |
| 2079 | if (PageUptodate(mpage)) |
| 2080 | memcpy(page_address(mpage), |
| 2081 | page_address(fio->encrypted_page), PAGE_SIZE); |
| 2082 | f2fs_put_page(mpage, 1); |
| 2083 | } |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | static inline bool check_inplace_update_policy(struct inode *inode, |
| 2088 | struct f2fs_io_info *fio) |
| 2089 | { |
| 2090 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2091 | unsigned int policy = SM_I(sbi)->ipu_policy; |
| 2092 | |
| 2093 | if (policy & (0x1 << F2FS_IPU_FORCE)) |
| 2094 | return true; |
| 2095 | if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi)) |
| 2096 | return true; |
| 2097 | if (policy & (0x1 << F2FS_IPU_UTIL) && |
| 2098 | utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 2099 | return true; |
| 2100 | if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) && |
| 2101 | utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 2102 | return true; |
| 2103 | |
| 2104 | /* |
| 2105 | * IPU for rewrite async pages |
| 2106 | */ |
| 2107 | if (policy & (0x1 << F2FS_IPU_ASYNC) && |
| 2108 | fio && fio->op == REQ_OP_WRITE && |
| 2109 | !(fio->op_flags & REQ_SYNC) && |
| 2110 | !IS_ENCRYPTED(inode)) |
| 2111 | return true; |
| 2112 | |
| 2113 | /* this is only set during fdatasync */ |
| 2114 | if (policy & (0x1 << F2FS_IPU_FSYNC) && |
| 2115 | is_inode_flag_set(inode, FI_NEED_IPU)) |
| 2116 | return true; |
| 2117 | |
| 2118 | if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 2119 | !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 2120 | return true; |
| 2121 | |
| 2122 | return false; |
| 2123 | } |
| 2124 | |
| 2125 | bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) |
| 2126 | { |
| 2127 | if (f2fs_is_pinned_file(inode)) |
| 2128 | return true; |
| 2129 | |
| 2130 | /* if this is cold file, we should overwrite to avoid fragmentation */ |
| 2131 | if (file_is_cold(inode)) |
| 2132 | return true; |
| 2133 | |
| 2134 | return check_inplace_update_policy(inode, fio); |
| 2135 | } |
| 2136 | |
| 2137 | bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) |
| 2138 | { |
| 2139 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2140 | |
| 2141 | if (test_opt(sbi, LFS)) |
| 2142 | return true; |
| 2143 | if (S_ISDIR(inode->i_mode)) |
| 2144 | return true; |
| 2145 | if (IS_NOQUOTA(inode)) |
| 2146 | return true; |
| 2147 | if (f2fs_is_atomic_file(inode)) |
| 2148 | return true; |
| 2149 | if (fio) { |
| 2150 | if (is_cold_data(fio->page)) |
| 2151 | return true; |
| 2152 | if (IS_ATOMIC_WRITTEN_PAGE(fio->page)) |
| 2153 | return true; |
| 2154 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 2155 | f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 2156 | return true; |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | static inline bool need_inplace_update(struct f2fs_io_info *fio) |
| 2162 | { |
| 2163 | struct inode *inode = fio->page->mapping->host; |
| 2164 | |
| 2165 | if (f2fs_should_update_outplace(inode, fio)) |
| 2166 | return false; |
| 2167 | |
| 2168 | return f2fs_should_update_inplace(inode, fio); |
| 2169 | } |
| 2170 | |
| 2171 | int f2fs_do_write_data_page(struct f2fs_io_info *fio) |
| 2172 | { |
| 2173 | struct page *page = fio->page; |
| 2174 | struct inode *inode = page->mapping->host; |
| 2175 | struct dnode_of_data dn; |
| 2176 | struct extent_info ei = {0,0,0}; |
| 2177 | struct node_info ni; |
| 2178 | bool ipu_force = false; |
| 2179 | int err = 0; |
| 2180 | |
| 2181 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 2182 | if (need_inplace_update(fio) && |
| 2183 | f2fs_lookup_extent_cache(inode, page->index, &ei)) { |
| 2184 | fio->old_blkaddr = ei.blk + page->index - ei.fofs; |
| 2185 | |
| 2186 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 2187 | DATA_GENERIC_ENHANCE)) |
| 2188 | return -EFSCORRUPTED; |
| 2189 | |
| 2190 | ipu_force = true; |
| 2191 | fio->need_lock = LOCK_DONE; |
| 2192 | goto got_it; |
| 2193 | } |
| 2194 | |
| 2195 | /* Deadlock due to between page->lock and f2fs_lock_op */ |
| 2196 | if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) |
| 2197 | return -EAGAIN; |
| 2198 | |
| 2199 | err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
| 2200 | if (err) |
| 2201 | goto out; |
| 2202 | |
| 2203 | fio->old_blkaddr = dn.data_blkaddr; |
| 2204 | |
| 2205 | /* This page is already truncated */ |
| 2206 | if (fio->old_blkaddr == NULL_ADDR) { |
| 2207 | ClearPageUptodate(page); |
| 2208 | clear_cold_data(page); |
| 2209 | goto out_writepage; |
| 2210 | } |
| 2211 | got_it: |
| 2212 | if (__is_valid_data_blkaddr(fio->old_blkaddr) && |
| 2213 | !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 2214 | DATA_GENERIC_ENHANCE)) { |
| 2215 | err = -EFSCORRUPTED; |
| 2216 | goto out_writepage; |
| 2217 | } |
| 2218 | /* |
| 2219 | * If current allocation needs SSR, |
| 2220 | * it had better in-place writes for updated data. |
| 2221 | */ |
| 2222 | if (ipu_force || |
| 2223 | (__is_valid_data_blkaddr(fio->old_blkaddr) && |
| 2224 | need_inplace_update(fio))) { |
| 2225 | err = encrypt_one_page(fio); |
| 2226 | if (err) |
| 2227 | goto out_writepage; |
| 2228 | |
| 2229 | set_page_writeback(page); |
| 2230 | ClearPageError(page); |
| 2231 | f2fs_put_dnode(&dn); |
| 2232 | if (fio->need_lock == LOCK_REQ) |
| 2233 | f2fs_unlock_op(fio->sbi); |
| 2234 | err = f2fs_inplace_write_data(fio); |
| 2235 | if (err) { |
| 2236 | if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
| 2237 | fscrypt_finalize_bounce_page(&fio->encrypted_page); |
| 2238 | if (PageWriteback(page)) |
| 2239 | end_page_writeback(page); |
| 2240 | } else { |
| 2241 | set_inode_flag(inode, FI_UPDATE_WRITE); |
| 2242 | } |
| 2243 | trace_f2fs_do_write_data_page(fio->page, IPU); |
| 2244 | return err; |
| 2245 | } |
| 2246 | |
| 2247 | if (fio->need_lock == LOCK_RETRY) { |
| 2248 | if (!f2fs_trylock_op(fio->sbi)) { |
| 2249 | err = -EAGAIN; |
| 2250 | goto out_writepage; |
| 2251 | } |
| 2252 | fio->need_lock = LOCK_REQ; |
| 2253 | } |
| 2254 | |
| 2255 | err = f2fs_get_node_info(fio->sbi, dn.nid, &ni); |
| 2256 | if (err) |
| 2257 | goto out_writepage; |
| 2258 | |
| 2259 | fio->version = ni.version; |
| 2260 | |
| 2261 | err = encrypt_one_page(fio); |
| 2262 | if (err) |
| 2263 | goto out_writepage; |
| 2264 | |
| 2265 | set_page_writeback(page); |
| 2266 | ClearPageError(page); |
| 2267 | |
| 2268 | /* LFS mode write path */ |
| 2269 | f2fs_outplace_write_data(&dn, fio); |
| 2270 | trace_f2fs_do_write_data_page(page, OPU); |
| 2271 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 2272 | if (page->index == 0) |
| 2273 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
| 2274 | out_writepage: |
| 2275 | f2fs_put_dnode(&dn); |
| 2276 | out: |
| 2277 | if (fio->need_lock == LOCK_REQ) |
| 2278 | f2fs_unlock_op(fio->sbi); |
| 2279 | return err; |
| 2280 | } |
| 2281 | |
| 2282 | static int __write_data_page(struct page *page, bool *submitted, |
| 2283 | struct bio **bio, |
| 2284 | sector_t *last_block, |
| 2285 | struct writeback_control *wbc, |
| 2286 | enum iostat_type io_type) |
| 2287 | { |
| 2288 | struct inode *inode = page->mapping->host; |
| 2289 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2290 | loff_t i_size = i_size_read(inode); |
| 2291 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 2292 | >> PAGE_SHIFT; |
| 2293 | loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT; |
| 2294 | unsigned offset = 0; |
| 2295 | bool need_balance_fs = false; |
| 2296 | int err = 0; |
| 2297 | struct f2fs_io_info fio = { |
| 2298 | .sbi = sbi, |
| 2299 | .ino = inode->i_ino, |
| 2300 | .type = DATA, |
| 2301 | .op = REQ_OP_WRITE, |
| 2302 | .op_flags = wbc_to_write_flags(wbc), |
| 2303 | .old_blkaddr = NULL_ADDR, |
| 2304 | .page = page, |
| 2305 | .encrypted_page = NULL, |
| 2306 | .submitted = false, |
| 2307 | .need_lock = LOCK_RETRY, |
| 2308 | .io_type = io_type, |
| 2309 | .io_wbc = wbc, |
| 2310 | .bio = bio, |
| 2311 | .last_block = last_block, |
| 2312 | }; |
| 2313 | |
| 2314 | trace_f2fs_writepage(page, DATA); |
| 2315 | |
| 2316 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 2317 | if (unlikely(f2fs_cp_error(sbi))) { |
| 2318 | mapping_set_error(page->mapping, -EIO); |
| 2319 | /* |
| 2320 | * don't drop any dirty dentry pages for keeping lastest |
| 2321 | * directory structure. |
| 2322 | */ |
| 2323 | if (S_ISDIR(inode->i_mode)) |
| 2324 | goto redirty_out; |
| 2325 | goto out; |
| 2326 | } |
| 2327 | |
| 2328 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 2329 | goto redirty_out; |
| 2330 | |
| 2331 | if (page->index < end_index || f2fs_verity_in_progress(inode)) |
| 2332 | goto write; |
| 2333 | |
| 2334 | /* |
| 2335 | * If the offset is out-of-range of file size, |
| 2336 | * this page does not have to be written to disk. |
| 2337 | */ |
| 2338 | offset = i_size & (PAGE_SIZE - 1); |
| 2339 | if ((page->index >= end_index + 1) || !offset) |
| 2340 | goto out; |
| 2341 | |
| 2342 | zero_user_segment(page, offset, PAGE_SIZE); |
| 2343 | write: |
| 2344 | if (f2fs_is_drop_cache(inode)) |
| 2345 | goto out; |
| 2346 | /* we should not write 0'th page having journal header */ |
| 2347 | if (f2fs_is_volatile_file(inode) && (!page->index || |
| 2348 | (!wbc->for_reclaim && |
| 2349 | f2fs_available_free_memory(sbi, BASE_CHECK)))) |
| 2350 | goto redirty_out; |
| 2351 | |
| 2352 | /* Dentry blocks are controlled by checkpoint */ |
| 2353 | if (S_ISDIR(inode->i_mode)) { |
| 2354 | fio.need_lock = LOCK_DONE; |
| 2355 | err = f2fs_do_write_data_page(&fio); |
| 2356 | goto done; |
| 2357 | } |
| 2358 | |
| 2359 | if (!wbc->for_reclaim) |
| 2360 | need_balance_fs = true; |
| 2361 | else if (has_not_enough_free_secs(sbi, 0, 0)) |
| 2362 | goto redirty_out; |
| 2363 | else |
| 2364 | set_inode_flag(inode, FI_HOT_DATA); |
| 2365 | |
| 2366 | err = -EAGAIN; |
| 2367 | if (f2fs_has_inline_data(inode)) { |
| 2368 | err = f2fs_write_inline_data(inode, page); |
| 2369 | if (!err) |
| 2370 | goto out; |
| 2371 | } |
| 2372 | |
| 2373 | if (err == -EAGAIN) { |
| 2374 | err = f2fs_do_write_data_page(&fio); |
| 2375 | if (err == -EAGAIN) { |
| 2376 | fio.need_lock = LOCK_REQ; |
| 2377 | err = f2fs_do_write_data_page(&fio); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (err) { |
| 2382 | file_set_keep_isize(inode); |
| 2383 | } else { |
| 2384 | down_write(&F2FS_I(inode)->i_sem); |
| 2385 | if (F2FS_I(inode)->last_disk_size < psize) |
| 2386 | F2FS_I(inode)->last_disk_size = psize; |
| 2387 | up_write(&F2FS_I(inode)->i_sem); |
| 2388 | } |
| 2389 | |
| 2390 | done: |
| 2391 | if (err && err != -ENOENT) |
| 2392 | goto redirty_out; |
| 2393 | |
| 2394 | out: |
| 2395 | inode_dec_dirty_pages(inode); |
| 2396 | if (err) { |
| 2397 | ClearPageUptodate(page); |
| 2398 | clear_cold_data(page); |
| 2399 | } |
| 2400 | |
| 2401 | if (wbc->for_reclaim) { |
| 2402 | f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA); |
| 2403 | clear_inode_flag(inode, FI_HOT_DATA); |
| 2404 | f2fs_remove_dirty_inode(inode); |
| 2405 | submitted = NULL; |
| 2406 | } |
| 2407 | |
| 2408 | unlock_page(page); |
| 2409 | if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && |
| 2410 | !F2FS_I(inode)->cp_task) |
| 2411 | f2fs_balance_fs(sbi, need_balance_fs); |
| 2412 | |
| 2413 | if (unlikely(f2fs_cp_error(sbi))) { |
| 2414 | f2fs_submit_merged_write(sbi, DATA); |
| 2415 | f2fs_submit_merged_ipu_write(sbi, bio, NULL); |
| 2416 | submitted = NULL; |
| 2417 | } |
| 2418 | |
| 2419 | if (submitted) |
| 2420 | *submitted = fio.submitted; |
| 2421 | |
| 2422 | return 0; |
| 2423 | |
| 2424 | redirty_out: |
| 2425 | redirty_page_for_writepage(wbc, page); |
| 2426 | /* |
| 2427 | * pageout() in MM traslates EAGAIN, so calls handle_write_error() |
| 2428 | * -> mapping_set_error() -> set_bit(AS_EIO, ...). |
| 2429 | * file_write_and_wait_range() will see EIO error, which is critical |
| 2430 | * to return value of fsync() followed by atomic_write failure to user. |
| 2431 | */ |
| 2432 | if (!err || wbc->for_reclaim) |
| 2433 | return AOP_WRITEPAGE_ACTIVATE; |
| 2434 | unlock_page(page); |
| 2435 | return err; |
| 2436 | } |
| 2437 | |
| 2438 | static int f2fs_write_data_page(struct page *page, |
| 2439 | struct writeback_control *wbc) |
| 2440 | { |
| 2441 | return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO); |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * This function was copied from write_cche_pages from mm/page-writeback.c. |
| 2446 | * The major change is making write step of cold data page separately from |
| 2447 | * warm/hot data page. |
| 2448 | */ |
| 2449 | static int f2fs_write_cache_pages(struct address_space *mapping, |
| 2450 | struct writeback_control *wbc, |
| 2451 | enum iostat_type io_type) |
| 2452 | { |
| 2453 | int ret = 0; |
| 2454 | int done = 0; |
| 2455 | struct pagevec pvec; |
| 2456 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
| 2457 | struct bio *bio = NULL; |
| 2458 | sector_t last_block; |
| 2459 | int nr_pages; |
| 2460 | pgoff_t uninitialized_var(writeback_index); |
| 2461 | pgoff_t index; |
| 2462 | pgoff_t end; /* Inclusive */ |
| 2463 | pgoff_t done_index; |
| 2464 | int cycled; |
| 2465 | int range_whole = 0; |
| 2466 | int tag; |
| 2467 | int nwritten = 0; |
| 2468 | |
| 2469 | pagevec_init(&pvec); |
| 2470 | |
| 2471 | if (get_dirty_pages(mapping->host) <= |
| 2472 | SM_I(F2FS_M_SB(mapping))->min_hot_blocks) |
| 2473 | set_inode_flag(mapping->host, FI_HOT_DATA); |
| 2474 | else |
| 2475 | clear_inode_flag(mapping->host, FI_HOT_DATA); |
| 2476 | |
| 2477 | if (wbc->range_cyclic) { |
| 2478 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 2479 | index = writeback_index; |
| 2480 | if (index == 0) |
| 2481 | cycled = 1; |
| 2482 | else |
| 2483 | cycled = 0; |
| 2484 | end = -1; |
| 2485 | } else { |
| 2486 | index = wbc->range_start >> PAGE_SHIFT; |
| 2487 | end = wbc->range_end >> PAGE_SHIFT; |
| 2488 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2489 | range_whole = 1; |
| 2490 | cycled = 1; /* ignore range_cyclic tests */ |
| 2491 | } |
| 2492 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2493 | tag = PAGECACHE_TAG_TOWRITE; |
| 2494 | else |
| 2495 | tag = PAGECACHE_TAG_DIRTY; |
| 2496 | retry: |
| 2497 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2498 | tag_pages_for_writeback(mapping, index, end); |
| 2499 | done_index = index; |
| 2500 | while (!done && (index <= end)) { |
| 2501 | int i; |
| 2502 | |
| 2503 | nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
| 2504 | tag); |
| 2505 | if (nr_pages == 0) |
| 2506 | break; |
| 2507 | |
| 2508 | for (i = 0; i < nr_pages; i++) { |
| 2509 | struct page *page = pvec.pages[i]; |
| 2510 | bool submitted = false; |
| 2511 | |
| 2512 | /* give a priority to WB_SYNC threads */ |
| 2513 | if (atomic_read(&sbi->wb_sync_req[DATA]) && |
| 2514 | wbc->sync_mode == WB_SYNC_NONE) { |
| 2515 | done = 1; |
| 2516 | break; |
| 2517 | } |
| 2518 | |
| 2519 | done_index = page->index; |
| 2520 | retry_write: |
| 2521 | lock_page(page); |
| 2522 | |
| 2523 | if (unlikely(page->mapping != mapping)) { |
| 2524 | continue_unlock: |
| 2525 | unlock_page(page); |
| 2526 | continue; |
| 2527 | } |
| 2528 | |
| 2529 | if (!PageDirty(page)) { |
| 2530 | /* someone wrote it for us */ |
| 2531 | goto continue_unlock; |
| 2532 | } |
| 2533 | |
| 2534 | if (PageWriteback(page)) { |
| 2535 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 2536 | f2fs_wait_on_page_writeback(page, |
| 2537 | DATA, true, true); |
| 2538 | else |
| 2539 | goto continue_unlock; |
| 2540 | } |
| 2541 | |
| 2542 | if (!clear_page_dirty_for_io(page)) |
| 2543 | goto continue_unlock; |
| 2544 | |
| 2545 | ret = __write_data_page(page, &submitted, &bio, |
| 2546 | &last_block, wbc, io_type); |
| 2547 | if (unlikely(ret)) { |
| 2548 | /* |
| 2549 | * keep nr_to_write, since vfs uses this to |
| 2550 | * get # of written pages. |
| 2551 | */ |
| 2552 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 2553 | unlock_page(page); |
| 2554 | ret = 0; |
| 2555 | continue; |
| 2556 | } else if (ret == -EAGAIN) { |
| 2557 | ret = 0; |
| 2558 | if (wbc->sync_mode == WB_SYNC_ALL) { |
| 2559 | cond_resched(); |
| 2560 | congestion_wait(BLK_RW_ASYNC, |
| 2561 | HZ/50); |
| 2562 | goto retry_write; |
| 2563 | } |
| 2564 | continue; |
| 2565 | } |
| 2566 | done_index = page->index + 1; |
| 2567 | done = 1; |
| 2568 | break; |
| 2569 | } else if (submitted) { |
| 2570 | nwritten++; |
| 2571 | } |
| 2572 | |
| 2573 | if (--wbc->nr_to_write <= 0 && |
| 2574 | wbc->sync_mode == WB_SYNC_NONE) { |
| 2575 | done = 1; |
| 2576 | break; |
| 2577 | } |
| 2578 | } |
| 2579 | pagevec_release(&pvec); |
| 2580 | cond_resched(); |
| 2581 | } |
| 2582 | |
| 2583 | if (!cycled && !done) { |
| 2584 | cycled = 1; |
| 2585 | index = 0; |
| 2586 | end = writeback_index - 1; |
| 2587 | goto retry; |
| 2588 | } |
| 2589 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2590 | mapping->writeback_index = done_index; |
| 2591 | |
| 2592 | if (nwritten) |
| 2593 | f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, |
| 2594 | NULL, 0, DATA); |
| 2595 | /* submit cached bio of IPU write */ |
| 2596 | if (bio) |
| 2597 | f2fs_submit_merged_ipu_write(sbi, &bio, NULL); |
| 2598 | |
| 2599 | return ret; |
| 2600 | } |
| 2601 | |
| 2602 | static inline bool __should_serialize_io(struct inode *inode, |
| 2603 | struct writeback_control *wbc) |
| 2604 | { |
| 2605 | if (!S_ISREG(inode->i_mode)) |
| 2606 | return false; |
| 2607 | if (IS_NOQUOTA(inode)) |
| 2608 | return false; |
| 2609 | /* to avoid deadlock in path of data flush */ |
| 2610 | if (F2FS_I(inode)->cp_task) |
| 2611 | return false; |
| 2612 | if (wbc->sync_mode != WB_SYNC_ALL) |
| 2613 | return true; |
| 2614 | if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) |
| 2615 | return true; |
| 2616 | return false; |
| 2617 | } |
| 2618 | |
| 2619 | static int __f2fs_write_data_pages(struct address_space *mapping, |
| 2620 | struct writeback_control *wbc, |
| 2621 | enum iostat_type io_type) |
| 2622 | { |
| 2623 | struct inode *inode = mapping->host; |
| 2624 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2625 | struct blk_plug plug; |
| 2626 | int ret; |
| 2627 | bool locked = false; |
| 2628 | |
| 2629 | /* deal with chardevs and other special file */ |
| 2630 | if (!mapping->a_ops->writepage) |
| 2631 | return 0; |
| 2632 | |
| 2633 | /* skip writing if there is no dirty page in this inode */ |
| 2634 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 2635 | return 0; |
| 2636 | |
| 2637 | /* during POR, we don't need to trigger writepage at all. */ |
| 2638 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 2639 | goto skip_write; |
| 2640 | |
| 2641 | if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && |
| 2642 | wbc->sync_mode == WB_SYNC_NONE && |
| 2643 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
| 2644 | f2fs_available_free_memory(sbi, DIRTY_DENTS)) |
| 2645 | goto skip_write; |
| 2646 | |
| 2647 | /* skip writing during file defragment */ |
| 2648 | if (is_inode_flag_set(inode, FI_DO_DEFRAG)) |
| 2649 | goto skip_write; |
| 2650 | |
| 2651 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 2652 | |
| 2653 | /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ |
| 2654 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 2655 | atomic_inc(&sbi->wb_sync_req[DATA]); |
| 2656 | else if (atomic_read(&sbi->wb_sync_req[DATA])) |
| 2657 | goto skip_write; |
| 2658 | |
| 2659 | if (__should_serialize_io(inode, wbc)) { |
| 2660 | mutex_lock(&sbi->writepages); |
| 2661 | locked = true; |
| 2662 | } |
| 2663 | |
| 2664 | blk_start_plug(&plug); |
| 2665 | ret = f2fs_write_cache_pages(mapping, wbc, io_type); |
| 2666 | blk_finish_plug(&plug); |
| 2667 | |
| 2668 | if (locked) |
| 2669 | mutex_unlock(&sbi->writepages); |
| 2670 | |
| 2671 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 2672 | atomic_dec(&sbi->wb_sync_req[DATA]); |
| 2673 | /* |
| 2674 | * if some pages were truncated, we cannot guarantee its mapping->host |
| 2675 | * to detect pending bios. |
| 2676 | */ |
| 2677 | |
| 2678 | f2fs_remove_dirty_inode(inode); |
| 2679 | return ret; |
| 2680 | |
| 2681 | skip_write: |
| 2682 | wbc->pages_skipped += get_dirty_pages(inode); |
| 2683 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
| 2687 | static int f2fs_write_data_pages(struct address_space *mapping, |
| 2688 | struct writeback_control *wbc) |
| 2689 | { |
| 2690 | struct inode *inode = mapping->host; |
| 2691 | |
| 2692 | return __f2fs_write_data_pages(mapping, wbc, |
| 2693 | F2FS_I(inode)->cp_task == current ? |
| 2694 | FS_CP_DATA_IO : FS_DATA_IO); |
| 2695 | } |
| 2696 | |
| 2697 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 2698 | { |
| 2699 | struct inode *inode = mapping->host; |
| 2700 | loff_t i_size = i_size_read(inode); |
| 2701 | |
| 2702 | /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ |
| 2703 | if (to > i_size && !f2fs_verity_in_progress(inode)) { |
| 2704 | down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 2705 | down_write(&F2FS_I(inode)->i_mmap_sem); |
| 2706 | |
| 2707 | truncate_pagecache(inode, i_size); |
| 2708 | if (!IS_NOQUOTA(inode)) |
| 2709 | f2fs_truncate_blocks(inode, i_size, true); |
| 2710 | |
| 2711 | up_write(&F2FS_I(inode)->i_mmap_sem); |
| 2712 | up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 2717 | struct page *page, loff_t pos, unsigned len, |
| 2718 | block_t *blk_addr, bool *node_changed) |
| 2719 | { |
| 2720 | struct inode *inode = page->mapping->host; |
| 2721 | pgoff_t index = page->index; |
| 2722 | struct dnode_of_data dn; |
| 2723 | struct page *ipage; |
| 2724 | bool locked = false; |
| 2725 | struct extent_info ei = {0,0,0}; |
| 2726 | int err = 0; |
| 2727 | int flag; |
| 2728 | |
| 2729 | /* |
| 2730 | * we already allocated all the blocks, so we don't need to get |
| 2731 | * the block addresses when there is no need to fill the page. |
| 2732 | */ |
| 2733 | if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE && |
| 2734 | !is_inode_flag_set(inode, FI_NO_PREALLOC) && |
| 2735 | !f2fs_verity_in_progress(inode)) |
| 2736 | return 0; |
| 2737 | |
| 2738 | /* f2fs_lock_op avoids race between write CP and convert_inline_page */ |
| 2739 | if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode)) |
| 2740 | flag = F2FS_GET_BLOCK_DEFAULT; |
| 2741 | else |
| 2742 | flag = F2FS_GET_BLOCK_PRE_AIO; |
| 2743 | |
| 2744 | if (f2fs_has_inline_data(inode) || |
| 2745 | (pos & PAGE_MASK) >= i_size_read(inode)) { |
| 2746 | __do_map_lock(sbi, flag, true); |
| 2747 | locked = true; |
| 2748 | } |
| 2749 | restart: |
| 2750 | /* check inline_data */ |
| 2751 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
| 2752 | if (IS_ERR(ipage)) { |
| 2753 | err = PTR_ERR(ipage); |
| 2754 | goto unlock_out; |
| 2755 | } |
| 2756 | |
| 2757 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 2758 | |
| 2759 | if (f2fs_has_inline_data(inode)) { |
| 2760 | if (pos + len <= MAX_INLINE_DATA(inode)) { |
| 2761 | f2fs_do_read_inline_data(page, ipage); |
| 2762 | set_inode_flag(inode, FI_DATA_EXIST); |
| 2763 | if (inode->i_nlink) |
| 2764 | set_inline_node(ipage); |
| 2765 | } else { |
| 2766 | err = f2fs_convert_inline_page(&dn, page); |
| 2767 | if (err) |
| 2768 | goto out; |
| 2769 | if (dn.data_blkaddr == NULL_ADDR) |
| 2770 | err = f2fs_get_block(&dn, index); |
| 2771 | } |
| 2772 | } else if (locked) { |
| 2773 | err = f2fs_get_block(&dn, index); |
| 2774 | } else { |
| 2775 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 2776 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 2777 | } else { |
| 2778 | /* hole case */ |
| 2779 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 2780 | if (err || dn.data_blkaddr == NULL_ADDR) { |
| 2781 | f2fs_put_dnode(&dn); |
| 2782 | __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, |
| 2783 | true); |
| 2784 | WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); |
| 2785 | locked = true; |
| 2786 | goto restart; |
| 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | /* convert_inline_page can make node_changed */ |
| 2792 | *blk_addr = dn.data_blkaddr; |
| 2793 | *node_changed = dn.node_changed; |
| 2794 | out: |
| 2795 | f2fs_put_dnode(&dn); |
| 2796 | unlock_out: |
| 2797 | if (locked) |
| 2798 | __do_map_lock(sbi, flag, false); |
| 2799 | return err; |
| 2800 | } |
| 2801 | |
| 2802 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 2803 | loff_t pos, unsigned len, unsigned flags, |
| 2804 | struct page **pagep, void **fsdata) |
| 2805 | { |
| 2806 | struct inode *inode = mapping->host; |
| 2807 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2808 | struct page *page = NULL; |
| 2809 | pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; |
| 2810 | bool need_balance = false, drop_atomic = false; |
| 2811 | block_t blkaddr = NULL_ADDR; |
| 2812 | int err = 0; |
| 2813 | |
| 2814 | if (trace_android_fs_datawrite_start_enabled()) { |
| 2815 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 2816 | |
| 2817 | path = android_fstrace_get_pathname(pathbuf, |
| 2818 | MAX_TRACE_PATHBUF_LEN, |
| 2819 | inode); |
| 2820 | trace_android_fs_datawrite_start(inode, pos, len, |
| 2821 | current->pid, path, |
| 2822 | current->comm); |
| 2823 | } |
| 2824 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 2825 | |
| 2826 | if (!f2fs_is_checkpoint_ready(sbi)) { |
| 2827 | err = -ENOSPC; |
| 2828 | goto fail; |
| 2829 | } |
| 2830 | |
| 2831 | if ((f2fs_is_atomic_file(inode) && |
| 2832 | !f2fs_available_free_memory(sbi, INMEM_PAGES)) || |
| 2833 | is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) { |
| 2834 | err = -ENOMEM; |
| 2835 | drop_atomic = true; |
| 2836 | goto fail; |
| 2837 | } |
| 2838 | |
| 2839 | /* |
| 2840 | * We should check this at this moment to avoid deadlock on inode page |
| 2841 | * and #0 page. The locking rule for inline_data conversion should be: |
| 2842 | * lock_page(page #0) -> lock_page(inode_page) |
| 2843 | */ |
| 2844 | if (index != 0) { |
| 2845 | err = f2fs_convert_inline_inode(inode); |
| 2846 | if (err) |
| 2847 | goto fail; |
| 2848 | } |
| 2849 | repeat: |
| 2850 | /* |
| 2851 | * Do not use grab_cache_page_write_begin() to avoid deadlock due to |
| 2852 | * wait_for_stable_page. Will wait that below with our IO control. |
| 2853 | */ |
| 2854 | page = f2fs_pagecache_get_page(mapping, index, |
| 2855 | FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); |
| 2856 | if (!page) { |
| 2857 | err = -ENOMEM; |
| 2858 | goto fail; |
| 2859 | } |
| 2860 | |
| 2861 | *pagep = page; |
| 2862 | |
| 2863 | err = prepare_write_begin(sbi, page, pos, len, |
| 2864 | &blkaddr, &need_balance); |
| 2865 | if (err) |
| 2866 | goto fail; |
| 2867 | |
| 2868 | if (need_balance && !IS_NOQUOTA(inode) && |
| 2869 | has_not_enough_free_secs(sbi, 0, 0)) { |
| 2870 | unlock_page(page); |
| 2871 | f2fs_balance_fs(sbi, true); |
| 2872 | lock_page(page); |
| 2873 | if (page->mapping != mapping) { |
| 2874 | /* The page got truncated from under us */ |
| 2875 | f2fs_put_page(page, 1); |
| 2876 | goto repeat; |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | f2fs_wait_on_page_writeback(page, DATA, false, true); |
| 2881 | |
| 2882 | if (len == PAGE_SIZE || PageUptodate(page)) |
| 2883 | return 0; |
| 2884 | |
| 2885 | if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && |
| 2886 | !f2fs_verity_in_progress(inode)) { |
| 2887 | zero_user_segment(page, len, PAGE_SIZE); |
| 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | if (blkaddr == NEW_ADDR) { |
| 2892 | zero_user_segment(page, 0, PAGE_SIZE); |
| 2893 | SetPageUptodate(page); |
| 2894 | } else { |
| 2895 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, |
| 2896 | DATA_GENERIC_ENHANCE_READ)) { |
| 2897 | err = -EFSCORRUPTED; |
| 2898 | goto fail; |
| 2899 | } |
| 2900 | err = f2fs_submit_page_read(inode, page, blkaddr); |
| 2901 | if (err) |
| 2902 | goto fail; |
| 2903 | |
| 2904 | lock_page(page); |
| 2905 | if (unlikely(page->mapping != mapping)) { |
| 2906 | f2fs_put_page(page, 1); |
| 2907 | goto repeat; |
| 2908 | } |
| 2909 | if (unlikely(!PageUptodate(page))) { |
| 2910 | err = -EIO; |
| 2911 | goto fail; |
| 2912 | } |
| 2913 | } |
| 2914 | return 0; |
| 2915 | |
| 2916 | fail: |
| 2917 | f2fs_put_page(page, 1); |
| 2918 | f2fs_write_failed(mapping, pos + len); |
| 2919 | if (drop_atomic) |
| 2920 | f2fs_drop_inmem_pages_all(sbi, false); |
| 2921 | return err; |
| 2922 | } |
| 2923 | |
| 2924 | static int f2fs_write_end(struct file *file, |
| 2925 | struct address_space *mapping, |
| 2926 | loff_t pos, unsigned len, unsigned copied, |
| 2927 | struct page *page, void *fsdata) |
| 2928 | { |
| 2929 | struct inode *inode = page->mapping->host; |
| 2930 | |
| 2931 | trace_android_fs_datawrite_end(inode, pos, len); |
| 2932 | trace_f2fs_write_end(inode, pos, len, copied); |
| 2933 | |
| 2934 | /* |
| 2935 | * This should be come from len == PAGE_SIZE, and we expect copied |
| 2936 | * should be PAGE_SIZE. Otherwise, we treat it with zero copied and |
| 2937 | * let generic_perform_write() try to copy data again through copied=0. |
| 2938 | */ |
| 2939 | if (!PageUptodate(page)) { |
| 2940 | if (unlikely(copied != len)) |
| 2941 | copied = 0; |
| 2942 | else |
| 2943 | SetPageUptodate(page); |
| 2944 | } |
| 2945 | if (!copied) |
| 2946 | goto unlock_out; |
| 2947 | |
| 2948 | set_page_dirty(page); |
| 2949 | |
| 2950 | if (pos + copied > i_size_read(inode) && |
| 2951 | !f2fs_verity_in_progress(inode)) |
| 2952 | f2fs_i_size_write(inode, pos + copied); |
| 2953 | unlock_out: |
| 2954 | f2fs_put_page(page, 1); |
| 2955 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
| 2956 | return copied; |
| 2957 | } |
| 2958 | |
| 2959 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
| 2960 | loff_t offset) |
| 2961 | { |
| 2962 | unsigned i_blkbits = READ_ONCE(inode->i_blkbits); |
| 2963 | unsigned blkbits = i_blkbits; |
| 2964 | unsigned blocksize_mask = (1 << blkbits) - 1; |
| 2965 | unsigned long align = offset | iov_iter_alignment(iter); |
| 2966 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 2967 | |
| 2968 | if (align & blocksize_mask) { |
| 2969 | if (bdev) |
| 2970 | blkbits = blksize_bits(bdev_logical_block_size(bdev)); |
| 2971 | blocksize_mask = (1 << blkbits) - 1; |
| 2972 | if (align & blocksize_mask) |
| 2973 | return -EINVAL; |
| 2974 | return 1; |
| 2975 | } |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | static void f2fs_dio_end_io(struct bio *bio) |
| 2980 | { |
| 2981 | struct f2fs_private_dio *dio = bio->bi_private; |
| 2982 | |
| 2983 | dec_page_count(F2FS_I_SB(dio->inode), |
| 2984 | dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ); |
| 2985 | |
| 2986 | bio->bi_private = dio->orig_private; |
| 2987 | bio->bi_end_io = dio->orig_end_io; |
| 2988 | |
| 2989 | kvfree(dio); |
| 2990 | |
| 2991 | bio_endio(bio); |
| 2992 | } |
| 2993 | |
| 2994 | static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode, |
| 2995 | loff_t file_offset) |
| 2996 | { |
| 2997 | struct f2fs_private_dio *dio; |
| 2998 | bool write = (bio_op(bio) == REQ_OP_WRITE); |
| 2999 | |
| 3000 | dio = f2fs_kzalloc(F2FS_I_SB(inode), |
| 3001 | sizeof(struct f2fs_private_dio), GFP_NOFS); |
| 3002 | if (!dio) |
| 3003 | goto out; |
| 3004 | |
| 3005 | dio->inode = inode; |
| 3006 | dio->orig_end_io = bio->bi_end_io; |
| 3007 | dio->orig_private = bio->bi_private; |
| 3008 | dio->write = write; |
| 3009 | |
| 3010 | bio->bi_end_io = f2fs_dio_end_io; |
| 3011 | bio->bi_private = dio; |
| 3012 | |
| 3013 | inc_page_count(F2FS_I_SB(inode), |
| 3014 | write ? F2FS_DIO_WRITE : F2FS_DIO_READ); |
| 3015 | |
| 3016 | submit_bio(bio); |
| 3017 | return; |
| 3018 | out: |
| 3019 | bio->bi_status = BLK_STS_IOERR; |
| 3020 | bio_endio(bio); |
| 3021 | } |
| 3022 | |
| 3023 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
| 3024 | { |
| 3025 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
| 3026 | struct inode *inode = mapping->host; |
| 3027 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3028 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 3029 | size_t count = iov_iter_count(iter); |
| 3030 | loff_t offset = iocb->ki_pos; |
| 3031 | int rw = iov_iter_rw(iter); |
| 3032 | int err; |
| 3033 | enum rw_hint hint = iocb->ki_hint; |
| 3034 | int whint_mode = F2FS_OPTION(sbi).whint_mode; |
| 3035 | bool do_opu; |
| 3036 | |
| 3037 | err = check_direct_IO(inode, iter, offset); |
| 3038 | if (err) |
| 3039 | return err < 0 ? err : 0; |
| 3040 | |
| 3041 | if (f2fs_force_buffered_io(inode, iocb, iter)) |
| 3042 | return 0; |
| 3043 | |
| 3044 | do_opu = allow_outplace_dio(inode, iocb, iter); |
| 3045 | |
| 3046 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
| 3047 | |
| 3048 | if (trace_android_fs_dataread_start_enabled() && |
| 3049 | (rw == READ)) { |
| 3050 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 3051 | |
| 3052 | path = android_fstrace_get_pathname(pathbuf, |
| 3053 | MAX_TRACE_PATHBUF_LEN, |
| 3054 | inode); |
| 3055 | trace_android_fs_dataread_start(inode, offset, |
| 3056 | count, current->pid, path, |
| 3057 | current->comm); |
| 3058 | } |
| 3059 | if (trace_android_fs_datawrite_start_enabled() && |
| 3060 | (rw == WRITE)) { |
| 3061 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 3062 | |
| 3063 | path = android_fstrace_get_pathname(pathbuf, |
| 3064 | MAX_TRACE_PATHBUF_LEN, |
| 3065 | inode); |
| 3066 | trace_android_fs_datawrite_start(inode, offset, count, |
| 3067 | current->pid, path, |
| 3068 | current->comm); |
| 3069 | } |
| 3070 | |
| 3071 | if (rw == WRITE && whint_mode == WHINT_MODE_OFF) |
| 3072 | iocb->ki_hint = WRITE_LIFE_NOT_SET; |
| 3073 | |
| 3074 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 3075 | if (!down_read_trylock(&fi->i_gc_rwsem[rw])) { |
| 3076 | iocb->ki_hint = hint; |
| 3077 | err = -EAGAIN; |
| 3078 | goto out; |
| 3079 | } |
| 3080 | if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) { |
| 3081 | up_read(&fi->i_gc_rwsem[rw]); |
| 3082 | iocb->ki_hint = hint; |
| 3083 | err = -EAGAIN; |
| 3084 | goto out; |
| 3085 | } |
| 3086 | } else { |
| 3087 | down_read(&fi->i_gc_rwsem[rw]); |
| 3088 | if (do_opu) |
| 3089 | down_read(&fi->i_gc_rwsem[READ]); |
| 3090 | } |
| 3091 | |
| 3092 | err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, |
| 3093 | iter, rw == WRITE ? get_data_block_dio_write : |
| 3094 | get_data_block_dio, NULL, f2fs_dio_submit_bio, |
| 3095 | DIO_LOCKING | DIO_SKIP_HOLES); |
| 3096 | |
| 3097 | if (do_opu) |
| 3098 | up_read(&fi->i_gc_rwsem[READ]); |
| 3099 | |
| 3100 | up_read(&fi->i_gc_rwsem[rw]); |
| 3101 | |
| 3102 | if (rw == WRITE) { |
| 3103 | if (whint_mode == WHINT_MODE_OFF) |
| 3104 | iocb->ki_hint = hint; |
| 3105 | if (err > 0) { |
| 3106 | f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO, |
| 3107 | err); |
| 3108 | if (!do_opu) |
| 3109 | set_inode_flag(inode, FI_UPDATE_WRITE); |
| 3110 | } else if (err < 0) { |
| 3111 | f2fs_write_failed(mapping, offset + count); |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | out: |
| 3116 | if (trace_android_fs_dataread_start_enabled() && |
| 3117 | (rw == READ)) |
| 3118 | trace_android_fs_dataread_end(inode, offset, count); |
| 3119 | if (trace_android_fs_datawrite_start_enabled() && |
| 3120 | (rw == WRITE)) |
| 3121 | trace_android_fs_datawrite_end(inode, offset, count); |
| 3122 | |
| 3123 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
| 3124 | |
| 3125 | return err; |
| 3126 | } |
| 3127 | |
| 3128 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 3129 | unsigned int length) |
| 3130 | { |
| 3131 | struct inode *inode = page->mapping->host; |
| 3132 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3133 | |
| 3134 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 3135 | (offset % PAGE_SIZE || length != PAGE_SIZE)) |
| 3136 | return; |
| 3137 | |
| 3138 | if (PageDirty(page)) { |
| 3139 | if (inode->i_ino == F2FS_META_INO(sbi)) { |
| 3140 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 3141 | } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
| 3142 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 3143 | } else { |
| 3144 | inode_dec_dirty_pages(inode); |
| 3145 | f2fs_remove_dirty_inode(inode); |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | clear_cold_data(page); |
| 3150 | |
| 3151 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 3152 | return f2fs_drop_inmem_page(inode, page); |
| 3153 | |
| 3154 | f2fs_clear_page_private(page); |
| 3155 | } |
| 3156 | |
| 3157 | int f2fs_release_page(struct page *page, gfp_t wait) |
| 3158 | { |
| 3159 | /* If this is dirty page, keep PagePrivate */ |
| 3160 | if (PageDirty(page)) |
| 3161 | return 0; |
| 3162 | |
| 3163 | /* This is atomic written page, keep Private */ |
| 3164 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 3165 | return 0; |
| 3166 | |
| 3167 | clear_cold_data(page); |
| 3168 | f2fs_clear_page_private(page); |
| 3169 | return 1; |
| 3170 | } |
| 3171 | |
| 3172 | static int f2fs_set_data_page_dirty(struct page *page) |
| 3173 | { |
| 3174 | struct inode *inode = page_file_mapping(page)->host; |
| 3175 | |
| 3176 | trace_f2fs_set_page_dirty(page, DATA); |
| 3177 | |
| 3178 | if (!PageUptodate(page)) |
| 3179 | SetPageUptodate(page); |
| 3180 | if (PageSwapCache(page)) |
| 3181 | return __set_page_dirty_nobuffers(page); |
| 3182 | |
| 3183 | if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) { |
| 3184 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
| 3185 | f2fs_register_inmem_page(inode, page); |
| 3186 | return 1; |
| 3187 | } |
| 3188 | /* |
| 3189 | * Previously, this page has been registered, we just |
| 3190 | * return here. |
| 3191 | */ |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
| 3195 | if (!PageDirty(page)) { |
| 3196 | __set_page_dirty_nobuffers(page); |
| 3197 | f2fs_update_dirty_page(inode, page); |
| 3198 | return 1; |
| 3199 | } |
| 3200 | return 0; |
| 3201 | } |
| 3202 | |
| 3203 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 3204 | { |
| 3205 | struct inode *inode = mapping->host; |
| 3206 | |
| 3207 | if (f2fs_has_inline_data(inode)) |
| 3208 | return 0; |
| 3209 | |
| 3210 | /* make sure allocating whole blocks */ |
| 3211 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 3212 | filemap_write_and_wait(mapping); |
| 3213 | |
| 3214 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
| 3215 | } |
| 3216 | |
| 3217 | #ifdef CONFIG_MIGRATION |
| 3218 | #include <linux/migrate.h> |
| 3219 | |
| 3220 | int f2fs_migrate_page(struct address_space *mapping, |
| 3221 | struct page *newpage, struct page *page, enum migrate_mode mode) |
| 3222 | { |
| 3223 | int rc, extra_count; |
| 3224 | struct f2fs_inode_info *fi = F2FS_I(mapping->host); |
| 3225 | bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page); |
| 3226 | |
| 3227 | BUG_ON(PageWriteback(page)); |
| 3228 | |
| 3229 | /* migrating an atomic written page is safe with the inmem_lock hold */ |
| 3230 | if (atomic_written) { |
| 3231 | if (mode != MIGRATE_SYNC) |
| 3232 | return -EBUSY; |
| 3233 | if (!mutex_trylock(&fi->inmem_lock)) |
| 3234 | return -EAGAIN; |
| 3235 | } |
| 3236 | |
| 3237 | /* one extra reference was held for atomic_write page */ |
| 3238 | extra_count = atomic_written ? 1 : 0; |
| 3239 | rc = migrate_page_move_mapping(mapping, newpage, |
| 3240 | page, NULL, mode, extra_count); |
| 3241 | if (rc != MIGRATEPAGE_SUCCESS) { |
| 3242 | if (atomic_written) |
| 3243 | mutex_unlock(&fi->inmem_lock); |
| 3244 | return rc; |
| 3245 | } |
| 3246 | |
| 3247 | if (atomic_written) { |
| 3248 | struct inmem_pages *cur; |
| 3249 | list_for_each_entry(cur, &fi->inmem_pages, list) |
| 3250 | if (cur->page == page) { |
| 3251 | cur->page = newpage; |
| 3252 | break; |
| 3253 | } |
| 3254 | mutex_unlock(&fi->inmem_lock); |
| 3255 | put_page(page); |
| 3256 | get_page(newpage); |
| 3257 | } |
| 3258 | |
| 3259 | if (PagePrivate(page)) { |
| 3260 | f2fs_set_page_private(newpage, page_private(page)); |
| 3261 | f2fs_clear_page_private(page); |
| 3262 | } |
| 3263 | |
| 3264 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 3265 | migrate_page_copy(newpage, page); |
| 3266 | else |
| 3267 | migrate_page_states(newpage, page); |
| 3268 | |
| 3269 | return MIGRATEPAGE_SUCCESS; |
| 3270 | } |
| 3271 | #endif |
| 3272 | |
| 3273 | #ifdef CONFIG_SWAP |
| 3274 | /* Copied from generic_swapfile_activate() to check any holes */ |
| 3275 | static int check_swap_activate(struct file *swap_file, unsigned int max) |
| 3276 | { |
| 3277 | struct address_space *mapping = swap_file->f_mapping; |
| 3278 | struct inode *inode = mapping->host; |
| 3279 | unsigned blocks_per_page; |
| 3280 | unsigned long page_no; |
| 3281 | unsigned blkbits; |
| 3282 | sector_t probe_block; |
| 3283 | sector_t last_block; |
| 3284 | sector_t lowest_block = -1; |
| 3285 | sector_t highest_block = 0; |
| 3286 | |
| 3287 | blkbits = inode->i_blkbits; |
| 3288 | blocks_per_page = PAGE_SIZE >> blkbits; |
| 3289 | |
| 3290 | /* |
| 3291 | * Map all the blocks into the extent list. This code doesn't try |
| 3292 | * to be very smart. |
| 3293 | */ |
| 3294 | probe_block = 0; |
| 3295 | page_no = 0; |
| 3296 | last_block = i_size_read(inode) >> blkbits; |
| 3297 | while ((probe_block + blocks_per_page) <= last_block && page_no < max) { |
| 3298 | unsigned block_in_page; |
| 3299 | sector_t first_block; |
| 3300 | |
| 3301 | cond_resched(); |
| 3302 | |
| 3303 | first_block = bmap(inode, probe_block); |
| 3304 | if (first_block == 0) |
| 3305 | goto bad_bmap; |
| 3306 | |
| 3307 | /* |
| 3308 | * It must be PAGE_SIZE aligned on-disk |
| 3309 | */ |
| 3310 | if (first_block & (blocks_per_page - 1)) { |
| 3311 | probe_block++; |
| 3312 | goto reprobe; |
| 3313 | } |
| 3314 | |
| 3315 | for (block_in_page = 1; block_in_page < blocks_per_page; |
| 3316 | block_in_page++) { |
| 3317 | sector_t block; |
| 3318 | |
| 3319 | block = bmap(inode, probe_block + block_in_page); |
| 3320 | if (block == 0) |
| 3321 | goto bad_bmap; |
| 3322 | if (block != first_block + block_in_page) { |
| 3323 | /* Discontiguity */ |
| 3324 | probe_block++; |
| 3325 | goto reprobe; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | first_block >>= (PAGE_SHIFT - blkbits); |
| 3330 | if (page_no) { /* exclude the header page */ |
| 3331 | if (first_block < lowest_block) |
| 3332 | lowest_block = first_block; |
| 3333 | if (first_block > highest_block) |
| 3334 | highest_block = first_block; |
| 3335 | } |
| 3336 | |
| 3337 | page_no++; |
| 3338 | probe_block += blocks_per_page; |
| 3339 | reprobe: |
| 3340 | continue; |
| 3341 | } |
| 3342 | return 0; |
| 3343 | |
| 3344 | bad_bmap: |
| 3345 | pr_err("swapon: swapfile has holes\n"); |
| 3346 | return -EINVAL; |
| 3347 | } |
| 3348 | |
| 3349 | static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 3350 | sector_t *span) |
| 3351 | { |
| 3352 | struct inode *inode = file_inode(file); |
| 3353 | int ret; |
| 3354 | |
| 3355 | if (!S_ISREG(inode->i_mode)) |
| 3356 | return -EINVAL; |
| 3357 | |
| 3358 | if (f2fs_readonly(F2FS_I_SB(inode)->sb)) |
| 3359 | return -EROFS; |
| 3360 | |
| 3361 | ret = f2fs_convert_inline_inode(inode); |
| 3362 | if (ret) |
| 3363 | return ret; |
| 3364 | |
| 3365 | ret = check_swap_activate(file, sis->max); |
| 3366 | if (ret) |
| 3367 | return ret; |
| 3368 | |
| 3369 | set_inode_flag(inode, FI_PIN_FILE); |
| 3370 | f2fs_precache_extents(inode); |
| 3371 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
| 3375 | static void f2fs_swap_deactivate(struct file *file) |
| 3376 | { |
| 3377 | struct inode *inode = file_inode(file); |
| 3378 | |
| 3379 | clear_inode_flag(inode, FI_PIN_FILE); |
| 3380 | } |
| 3381 | #else |
| 3382 | static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 3383 | sector_t *span) |
| 3384 | { |
| 3385 | return -EOPNOTSUPP; |
| 3386 | } |
| 3387 | |
| 3388 | static void f2fs_swap_deactivate(struct file *file) |
| 3389 | { |
| 3390 | } |
| 3391 | #endif |
| 3392 | |
| 3393 | const struct address_space_operations f2fs_dblock_aops = { |
| 3394 | .readpage = f2fs_read_data_page, |
| 3395 | .readpages = f2fs_read_data_pages, |
| 3396 | .writepage = f2fs_write_data_page, |
| 3397 | .writepages = f2fs_write_data_pages, |
| 3398 | .write_begin = f2fs_write_begin, |
| 3399 | .write_end = f2fs_write_end, |
| 3400 | .set_page_dirty = f2fs_set_data_page_dirty, |
| 3401 | .invalidatepage = f2fs_invalidate_page, |
| 3402 | .releasepage = f2fs_release_page, |
| 3403 | .direct_IO = f2fs_direct_IO, |
| 3404 | .bmap = f2fs_bmap, |
| 3405 | .swap_activate = f2fs_swap_activate, |
| 3406 | .swap_deactivate = f2fs_swap_deactivate, |
| 3407 | #ifdef CONFIG_MIGRATION |
| 3408 | .migratepage = f2fs_migrate_page, |
| 3409 | #endif |
| 3410 | }; |
| 3411 | |
| 3412 | void f2fs_clear_radix_tree_dirty_tag(struct page *page) |
| 3413 | { |
| 3414 | struct address_space *mapping = page_mapping(page); |
| 3415 | unsigned long flags; |
| 3416 | |
| 3417 | xa_lock_irqsave(&mapping->i_pages, flags); |
| 3418 | radix_tree_tag_clear(&mapping->i_pages, page_index(page), |
| 3419 | PAGECACHE_TAG_DIRTY); |
| 3420 | xa_unlock_irqrestore(&mapping->i_pages, flags); |
| 3421 | } |
| 3422 | |
| 3423 | int __init f2fs_init_post_read_processing(void) |
| 3424 | { |
| 3425 | bio_post_read_ctx_cache = |
| 3426 | kmem_cache_create("f2fs_bio_post_read_ctx", |
| 3427 | sizeof(struct bio_post_read_ctx), 0, 0, NULL); |
| 3428 | if (!bio_post_read_ctx_cache) |
| 3429 | goto fail; |
| 3430 | bio_post_read_ctx_pool = |
| 3431 | mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, |
| 3432 | bio_post_read_ctx_cache); |
| 3433 | if (!bio_post_read_ctx_pool) |
| 3434 | goto fail_free_cache; |
| 3435 | return 0; |
| 3436 | |
| 3437 | fail_free_cache: |
| 3438 | kmem_cache_destroy(bio_post_read_ctx_cache); |
| 3439 | fail: |
| 3440 | return -ENOMEM; |
| 3441 | } |
| 3442 | |
| 3443 | void f2fs_destroy_post_read_processing(void) |
| 3444 | { |
| 3445 | mempool_destroy(bio_post_read_ctx_pool); |
| 3446 | kmem_cache_destroy(bio_post_read_ctx_cache); |
| 3447 | } |
| 3448 | |
| 3449 | int __init f2fs_init_bio_entry_cache(void) |
| 3450 | { |
| 3451 | bio_entry_slab = f2fs_kmem_cache_create("bio_entry_slab", |
| 3452 | sizeof(struct bio_entry)); |
| 3453 | if (!bio_entry_slab) |
| 3454 | return -ENOMEM; |
| 3455 | return 0; |
| 3456 | } |
| 3457 | |
| 3458 | void __exit f2fs_destroy_bio_entry_cache(void) |
| 3459 | { |
| 3460 | kmem_cache_destroy(bio_entry_slab); |
| 3461 | } |