| 1 | |
| 2 | |
| 3 | * fs/f2fs/checkpoint.c
|
| 4 | *
|
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
| 6 | * http://www.samsung.com/
|
| 7 |
|
| 8 | #include <linux/fs.h>
|
| 9 | #include <linux/bio.h>
|
| 10 | #include <linux/mpage.h>
|
| 11 | #include <linux/writeback.h>
|
| 12 | #include <linux/blkdev.h>
|
| 13 | #include <linux/f2fs_fs.h>
|
| 14 | #include <linux/folio_batch.h>
|
| 15 | #include <linux/swap.h>
|
| 16 | #include <linux/kthread.h>
|
| 17 | #include <linux/delayacct.h>
|
| 18 | #include <linux/ioprio.h>
|
| 19 | #include <linux/math64.h>
|
| 20 |
|
| 21 | #include "f2fs.h"
|
| 22 | #include "node.h"
|
| 23 | #include "segment.h"
|
| 24 | #include "iostat.h"
|
| 25 | #include <trace/events/f2fs.h>
|
| 26 |
|
| 27 | static inline void get_lock_elapsed_time(struct f2fs_time_stat *ts)
|
| 28 | {
|
| 29 | ts->total_time = ktime_get();
|
| 30 | #ifdef CONFIG_64BIT
|
| 31 | ts->running_time = current->se.sum_exec_runtime;
|
| 32 | #endif
|
| 33 | #if defined(CONFIG_SCHED_INFO) && defined(CONFIG_SCHEDSTATS)
|
| 34 | ts->runnable_time = current->sched_info.run_delay;
|
| 35 | #endif
|
| 36 | #ifdef CONFIG_TASK_DELAY_ACCT
|
| 37 | if (current->delays)
|
| 38 | ts->io_sleep_time = current->delays->blkio_delay;
|
| 39 | #endif
|
| 40 | }
|
| 41 |
|
| 42 | static inline void trace_lock_elapsed_time_start(struct f2fs_rwsem *sem,
|
| 43 | struct f2fs_lock_context *lc)
|
| 44 | {
|
| 45 | lc->lock_trace = trace_f2fs_lock_elapsed_time_enabled();
|
| 46 | if (!lc->lock_trace)
|
| 47 | return;
|
| 48 |
|
| 49 | get_lock_elapsed_time(&lc->ts);
|
| 50 | }
|
| 51 |
|
| 52 | static inline void trace_lock_elapsed_time_end(struct f2fs_rwsem *sem,
|
| 53 | struct f2fs_lock_context *lc, bool is_write)
|
| 54 | {
|
| 55 | struct f2fs_time_stat tts;
|
| 56 | unsigned long long total_time;
|
| 57 | unsigned long long running_time = 0;
|
| 58 | unsigned long long runnable_time = 0;
|
| 59 | unsigned long long io_sleep_time = 0;
|
| 60 | unsigned long long other_time = 0;
|
| 61 | unsigned npm = NSEC_PER_MSEC;
|
| 62 |
|
| 63 | if (!lc->lock_trace)
|
| 64 | return;
|
| 65 |
|
| 66 | if (time_to_inject(sem->sbi, FAULT_LOCK_TIMEOUT))
|
| 67 | f2fs_schedule_timeout_killable(DEFAULT_FAULT_TIMEOUT, true);
|
| 68 |
|
| 69 | get_lock_elapsed_time(&tts);
|
| 70 |
|
| 71 | total_time = div_u64(tts.total_time - lc->ts.total_time, npm);
|
| 72 | if (total_time <= sem->sbi->max_lock_elapsed_time)
|
| 73 | return;
|
| 74 |
|
| 75 | #ifdef CONFIG_64BIT
|
| 76 | running_time = div_u64(tts.running_time - lc->ts.running_time, npm);
|
| 77 | #endif
|
| 78 | #if defined(CONFIG_SCHED_INFO) && defined(CONFIG_SCHEDSTATS)
|
| 79 | runnable_time = div_u64(tts.runnable_time - lc->ts.runnable_time, npm);
|
| 80 | #endif
|
| 81 | #ifdef CONFIG_TASK_DELAY_ACCT
|
| 82 | io_sleep_time = div_u64(tts.io_sleep_time - lc->ts.io_sleep_time, npm);
|
| 83 | #endif
|
| 84 | if (total_time > running_time + io_sleep_time + runnable_time)
|
| 85 | other_time = total_time - running_time -
|
| 86 | io_sleep_time - runnable_time;
|
| 87 |
|
| 88 | trace_f2fs_lock_elapsed_time(sem->sbi, sem->name, is_write, current,
|
| 89 | get_current_ioprio(), total_time, running_time,
|
| 90 | runnable_time, io_sleep_time, other_time);
|
| 91 | }
|
| 92 |
|
| 93 | static bool need_uplift_priority(struct f2fs_rwsem *sem, bool is_write)
|
| 94 | {
|
| 95 | if (!(sem->sbi->adjust_lock_priority & BIT(sem->name - 1)))
|
| 96 | return false;
|
| 97 |
|
| 98 | switch (sem->name) {
|
| 99 | |
| 100 | * writer is checkpoint which has high priority, let's just uplift
|
| 101 | * priority for reader
|
| 102 |
|
| 103 | case LOCK_NAME_CP_RWSEM:
|
| 104 | case LOCK_NAME_NODE_CHANGE:
|
| 105 | case LOCK_NAME_NODE_WRITE:
|
| 106 | return !is_write;
|
| 107 | case LOCK_NAME_GC_LOCK:
|
| 108 | case LOCK_NAME_CP_GLOBAL:
|
| 109 | case LOCK_NAME_IO_RWSEM:
|
| 110 | case LOCK_NAME_NAT_TREE_LOCK:
|
| 111 | return true;
|
| 112 | default:
|
| 113 | f2fs_bug_on(sem->sbi, 1);
|
| 114 | }
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | static void uplift_priority(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc,
|
| 119 | bool is_write)
|
| 120 | {
|
| 121 | lc->need_restore = false;
|
| 122 | if (!sem->sbi->adjust_lock_priority)
|
| 123 | return;
|
| 124 | if (rt_task(current))
|
| 125 | return;
|
| 126 | if (!need_uplift_priority(sem, is_write))
|
| 127 | return;
|
| 128 | lc->orig_nice = task_nice(current);
|
| 129 | lc->new_nice = PRIO_TO_NICE(sem->sbi->lock_duration_priority);
|
| 130 | if (lc->orig_nice <= lc->new_nice)
|
| 131 | return;
|
| 132 | set_user_nice(current, lc->new_nice);
|
| 133 | lc->need_restore = true;
|
| 134 |
|
| 135 | trace_f2fs_priority_uplift(sem->sbi, sem->name, is_write, current,
|
| 136 | NICE_TO_PRIO(lc->orig_nice), NICE_TO_PRIO(lc->new_nice));
|
| 137 | }
|
| 138 |
|
| 139 | static void restore_priority(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc,
|
| 140 | bool is_write)
|
| 141 | {
|
| 142 | if (!lc->need_restore)
|
| 143 | return;
|
| 144 |
|
| 145 | if (task_nice(current) != lc->new_nice)
|
| 146 | return;
|
| 147 | set_user_nice(current, lc->orig_nice);
|
| 148 |
|
| 149 | trace_f2fs_priority_restore(sem->sbi, sem->name, is_write, current,
|
| 150 | NICE_TO_PRIO(lc->orig_nice), NICE_TO_PRIO(lc->new_nice));
|
| 151 | }
|
| 152 |
|
| 153 | void f2fs_down_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 154 | {
|
| 155 | uplift_priority(sem, lc, false);
|
| 156 | f2fs_down_read(sem);
|
| 157 | trace_lock_elapsed_time_start(sem, lc);
|
| 158 | }
|
| 159 |
|
| 160 | int f2fs_down_read_trylock_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 161 | {
|
| 162 | uplift_priority(sem, lc, false);
|
| 163 | if (!f2fs_down_read_trylock(sem)) {
|
| 164 | restore_priority(sem, lc, false);
|
| 165 | return 0;
|
| 166 | }
|
| 167 | trace_lock_elapsed_time_start(sem, lc);
|
| 168 | return 1;
|
| 169 | }
|
| 170 |
|
| 171 | void f2fs_up_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 172 | {
|
| 173 | f2fs_up_read(sem);
|
| 174 | restore_priority(sem, lc, false);
|
| 175 | trace_lock_elapsed_time_end(sem, lc, false);
|
| 176 | }
|
| 177 |
|
| 178 | void f2fs_down_write_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 179 | {
|
| 180 | uplift_priority(sem, lc, true);
|
| 181 | f2fs_down_write(sem);
|
| 182 | trace_lock_elapsed_time_start(sem, lc);
|
| 183 | }
|
| 184 |
|
| 185 | int f2fs_down_write_trylock_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 186 | {
|
| 187 | uplift_priority(sem, lc, true);
|
| 188 | if (!f2fs_down_write_trylock(sem)) {
|
| 189 | restore_priority(sem, lc, true);
|
| 190 | return 0;
|
| 191 | }
|
| 192 | trace_lock_elapsed_time_start(sem, lc);
|
| 193 | return 1;
|
| 194 | }
|
| 195 |
|
| 196 | void f2fs_up_write_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc)
|
| 197 | {
|
| 198 | f2fs_up_write(sem);
|
| 199 | restore_priority(sem, lc, true);
|
| 200 | trace_lock_elapsed_time_end(sem, lc, true);
|
| 201 | }
|
| 202 |
|
| 203 | void f2fs_lock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc)
|
| 204 | {
|
| 205 | f2fs_down_read_trace(&sbi->cp_rwsem, lc);
|
| 206 | }
|
| 207 |
|
| 208 | int f2fs_trylock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc)
|
| 209 | {
|
| 210 | if (time_to_inject(sbi, FAULT_LOCK_OP))
|
| 211 | return 0;
|
| 212 |
|
| 213 | return f2fs_down_read_trylock_trace(&sbi->cp_rwsem, lc);
|
| 214 | }
|
| 215 |
|
| 216 | void f2fs_unlock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc)
|
| 217 | {
|
| 218 | f2fs_up_read_trace(&sbi->cp_rwsem, lc);
|
| 219 | }
|
| 220 |
|
| 221 | static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
|
| 222 | {
|
| 223 | f2fs_down_write(&sbi->cp_rwsem);
|
| 224 | }
|
| 225 |
|
| 226 | static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
|
| 227 | {
|
| 228 | f2fs_up_write(&sbi->cp_rwsem);
|
| 229 | }
|
| 230 |
|
| 231 | #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 3))
|
| 232 |
|
| 233 | static struct kmem_cache *ino_entry_slab;
|
| 234 | struct kmem_cache *f2fs_inode_entry_slab;
|
| 235 |
|
| 236 | |
| 237 | * We guarantee no failure on the returned page.
|
| 238 |
|
| 239 | struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index)
|
| 240 | {
|
| 241 | struct address_space *mapping = META_MAPPING(sbi);
|
| 242 | struct folio *folio;
|
| 243 | repeat:
|
| 244 | folio = f2fs_grab_cache_folio(mapping, index, false);
|
| 245 | if (IS_ERR(folio)) {
|
| 246 | cond_resched();
|
| 247 | goto repeat;
|
| 248 | }
|
| 249 | f2fs_folio_wait_writeback(folio, META, true, true);
|
| 250 | if (!folio_test_uptodate(folio))
|
| 251 | folio_mark_uptodate(folio);
|
| 252 | return folio;
|
| 253 | }
|
| 254 |
|
| 255 | static struct folio *__get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index,
|
| 256 | bool is_meta)
|
| 257 | {
|
| 258 | struct address_space *mapping = META_MAPPING(sbi);
|
| 259 | struct folio *folio;
|
| 260 | struct f2fs_io_info fio = {
|
| 261 | .sbi = sbi,
|
| 262 | .type = META,
|
| 263 | .op = REQ_OP_READ,
|
| 264 | .op_flags = REQ_META | REQ_PRIO,
|
| 265 | .old_blkaddr = index,
|
| 266 | .new_blkaddr = index,
|
| 267 | .encrypted_page = NULL,
|
| 268 | .is_por = !is_meta ? 1 : 0,
|
| 269 | };
|
| 270 | int err;
|
| 271 |
|
| 272 | if (unlikely(!is_meta))
|
| 273 | fio.op_flags &= ~REQ_META;
|
| 274 | repeat:
|
| 275 | folio = f2fs_grab_cache_folio(mapping, index, false);
|
| 276 | if (IS_ERR(folio)) {
|
| 277 | cond_resched();
|
| 278 | goto repeat;
|
| 279 | }
|
| 280 | if (folio_test_uptodate(folio))
|
| 281 | goto out;
|
| 282 |
|
| 283 | fio.folio = folio;
|
| 284 |
|
| 285 | err = f2fs_submit_page_bio(&fio);
|
| 286 | if (err) {
|
| 287 | f2fs_folio_put(folio, true);
|
| 288 | return ERR_PTR(err);
|
| 289 | }
|
| 290 |
|
| 291 | f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE);
|
| 292 |
|
| 293 | folio_lock(folio);
|
| 294 | if (unlikely(!is_meta_folio(folio))) {
|
| 295 | f2fs_folio_put(folio, true);
|
| 296 | goto repeat;
|
| 297 | }
|
| 298 |
|
| 299 | if (unlikely(!folio_test_uptodate(folio))) {
|
| 300 | f2fs_handle_page_eio(sbi, folio, META);
|
| 301 | f2fs_folio_put(folio, true);
|
| 302 | return ERR_PTR(-EIO);
|
| 303 | }
|
| 304 | out:
|
| 305 | return folio;
|
| 306 | }
|
| 307 |
|
| 308 | struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index)
|
| 309 | {
|
| 310 | return __get_meta_folio(sbi, index, true);
|
| 311 | }
|
| 312 |
|
| 313 | struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index)
|
| 314 | {
|
| 315 | struct folio *folio;
|
| 316 | int count = 0;
|
| 317 |
|
| 318 | retry:
|
| 319 | folio = __get_meta_folio(sbi, index, true);
|
| 320 | if (IS_ERR(folio)) {
|
| 321 | if (PTR_ERR(folio) == -EIO &&
|
| 322 | ++count <= DEFAULT_RETRY_IO_COUNT)
|
| 323 | goto retry;
|
| 324 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
|
| 325 | }
|
| 326 | return folio;
|
| 327 | }
|
| 328 |
|
| 329 |
|
| 330 | struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index)
|
| 331 | {
|
| 332 | return __get_meta_folio(sbi, index, false);
|
| 333 | }
|
| 334 |
|
| 335 | static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
|
| 336 | int type)
|
| 337 | {
|
| 338 | struct seg_entry *se;
|
| 339 | unsigned int segno, offset;
|
| 340 | bool exist;
|
| 341 |
|
| 342 | if (type == DATA_GENERIC)
|
| 343 | return true;
|
| 344 |
|
| 345 | segno = GET_SEGNO(sbi, blkaddr);
|
| 346 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
|
| 347 | se = get_seg_entry(sbi, segno);
|
| 348 |
|
| 349 | exist = f2fs_test_bit(offset, se->cur_valid_map);
|
| 350 |
|
| 351 |
|
| 352 | if (unlikely(f2fs_cp_error(sbi)))
|
| 353 | return exist;
|
| 354 |
|
| 355 | if ((exist && type == DATA_GENERIC_ENHANCE_UPDATE) ||
|
| 356 | (!exist && type == DATA_GENERIC_ENHANCE))
|
| 357 | goto out_err;
|
| 358 | if (!exist && type != DATA_GENERIC_ENHANCE_UPDATE)
|
| 359 | goto out_handle;
|
| 360 | return exist;
|
| 361 |
|
| 362 | out_err:
|
| 363 | f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
|
| 364 | blkaddr, exist);
|
| 365 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 366 | dump_stack();
|
| 367 | out_handle:
|
| 368 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
|
| 369 | return exist;
|
| 370 | }
|
| 371 |
|
| 372 | static bool __f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
|
| 373 | block_t blkaddr, int type)
|
| 374 | {
|
| 375 | switch (type) {
|
| 376 | case META_NAT:
|
| 377 | break;
|
| 378 | case META_SIT:
|
| 379 | if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
|
| 380 | goto check_only;
|
| 381 | break;
|
| 382 | case META_SSA:
|
| 383 | if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
|
| 384 | blkaddr < SM_I(sbi)->ssa_blkaddr))
|
| 385 | goto check_only;
|
| 386 | break;
|
| 387 | case META_CP:
|
| 388 | if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
|
| 389 | blkaddr < __start_cp_addr(sbi)))
|
| 390 | goto check_only;
|
| 391 | break;
|
| 392 | case META_POR:
|
| 393 | if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
|
| 394 | blkaddr < MAIN_BLKADDR(sbi)))
|
| 395 | goto check_only;
|
| 396 | break;
|
| 397 | case DATA_GENERIC:
|
| 398 | case DATA_GENERIC_ENHANCE:
|
| 399 | case DATA_GENERIC_ENHANCE_READ:
|
| 400 | case DATA_GENERIC_ENHANCE_UPDATE:
|
| 401 | if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
|
| 402 | blkaddr < MAIN_BLKADDR(sbi))) {
|
| 403 |
|
| 404 |
|
| 405 | if (unlikely(f2fs_cp_error(sbi)))
|
| 406 | return false;
|
| 407 |
|
| 408 | f2fs_warn(sbi, "access invalid blkaddr:%u",
|
| 409 | blkaddr);
|
| 410 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 411 | dump_stack();
|
| 412 | goto err;
|
| 413 | } else {
|
| 414 | return __is_bitmap_valid(sbi, blkaddr, type);
|
| 415 | }
|
| 416 | break;
|
| 417 | case META_GENERIC:
|
| 418 | if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
|
| 419 | blkaddr >= MAIN_BLKADDR(sbi)))
|
| 420 | goto err;
|
| 421 | break;
|
| 422 | default:
|
| 423 | BUG();
|
| 424 | }
|
| 425 |
|
| 426 | return true;
|
| 427 | err:
|
| 428 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
|
| 429 | check_only:
|
| 430 | return false;
|
| 431 | }
|
| 432 |
|
| 433 | bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
|
| 434 | block_t blkaddr, int type)
|
| 435 | {
|
| 436 | if (time_to_inject(sbi, FAULT_BLKADDR_VALIDITY))
|
| 437 | return false;
|
| 438 | return __f2fs_is_valid_blkaddr(sbi, blkaddr, type);
|
| 439 | }
|
| 440 |
|
| 441 | bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
|
| 442 | block_t blkaddr, int type)
|
| 443 | {
|
| 444 | return __f2fs_is_valid_blkaddr(sbi, blkaddr, type);
|
| 445 | }
|
| 446 |
|
| 447 | |
| 448 | * Readahead CP/NAT/SIT/SSA/POR pages
|
| 449 |
|
| 450 | int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
|
| 451 | int type, bool sync)
|
| 452 | {
|
| 453 | block_t blkno = start;
|
| 454 | struct f2fs_io_info fio = {
|
| 455 | .sbi = sbi,
|
| 456 | .type = META,
|
| 457 | .op = REQ_OP_READ,
|
| 458 | .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
|
| 459 | .encrypted_page = NULL,
|
| 460 | .in_list = 0,
|
| 461 | .is_por = (type == META_POR) ? 1 : 0,
|
| 462 | };
|
| 463 | struct blk_plug plug;
|
| 464 | int err;
|
| 465 |
|
| 466 | if (unlikely(type == META_POR))
|
| 467 | fio.op_flags &= ~REQ_META;
|
| 468 |
|
| 469 | blk_start_plug(&plug);
|
| 470 | for (; nrpages-- > 0; blkno++) {
|
| 471 | struct folio *folio;
|
| 472 |
|
| 473 | if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
|
| 474 | goto out;
|
| 475 |
|
| 476 | switch (type) {
|
| 477 | case META_NAT:
|
| 478 | if (unlikely(blkno >=
|
| 479 | NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
|
| 480 | blkno = 0;
|
| 481 |
|
| 482 | fio.new_blkaddr = current_nat_addr(sbi,
|
| 483 | blkno * NAT_ENTRY_PER_BLOCK);
|
| 484 | break;
|
| 485 | case META_SIT:
|
| 486 | if (unlikely(blkno >= TOTAL_SEGS(sbi)))
|
| 487 | goto out;
|
| 488 |
|
| 489 | fio.new_blkaddr = current_sit_addr(sbi,
|
| 490 | blkno * SIT_ENTRY_PER_BLOCK);
|
| 491 | break;
|
| 492 | case META_SSA:
|
| 493 | case META_CP:
|
| 494 | case META_POR:
|
| 495 | fio.new_blkaddr = blkno;
|
| 496 | break;
|
| 497 | default:
|
| 498 | BUG();
|
| 499 | }
|
| 500 |
|
| 501 | folio = f2fs_grab_cache_folio(META_MAPPING(sbi),
|
| 502 | fio.new_blkaddr, false);
|
| 503 | if (IS_ERR(folio))
|
| 504 | continue;
|
| 505 | if (folio_test_uptodate(folio)) {
|
| 506 | f2fs_folio_put(folio, true);
|
| 507 | continue;
|
| 508 | }
|
| 509 |
|
| 510 | fio.folio = folio;
|
| 511 | err = f2fs_submit_page_bio(&fio);
|
| 512 | f2fs_folio_put(folio, err ? true : false);
|
| 513 |
|
| 514 | if (!err)
|
| 515 | f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
|
| 516 | F2FS_BLKSIZE);
|
| 517 | }
|
| 518 | out:
|
| 519 | blk_finish_plug(&plug);
|
| 520 | return blkno - start;
|
| 521 | }
|
| 522 |
|
| 523 | void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
|
| 524 | unsigned int ra_blocks)
|
| 525 | {
|
| 526 | struct folio *folio;
|
| 527 | bool readahead = false;
|
| 528 |
|
| 529 | if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
|
| 530 | return;
|
| 531 |
|
| 532 | folio = filemap_get_folio(META_MAPPING(sbi), index);
|
| 533 | if (IS_ERR(folio) || !folio_test_uptodate(folio))
|
| 534 | readahead = true;
|
| 535 | f2fs_folio_put(folio, false);
|
| 536 |
|
| 537 | if (readahead)
|
| 538 | f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
|
| 539 | }
|
| 540 |
|
| 541 | static bool __f2fs_write_meta_folio(struct folio *folio,
|
| 542 | struct writeback_control *wbc,
|
| 543 | enum iostat_type io_type)
|
| 544 | {
|
| 545 | struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
|
| 546 |
|
| 547 | trace_f2fs_writepage(folio, META);
|
| 548 |
|
| 549 | if (unlikely(f2fs_cp_error(sbi))) {
|
| 550 | if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
|
| 551 | folio_clear_uptodate(folio);
|
| 552 | dec_page_count(sbi, F2FS_DIRTY_META);
|
| 553 | folio_unlock(folio);
|
| 554 | return true;
|
| 555 | }
|
| 556 | goto redirty_out;
|
| 557 | }
|
| 558 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
|
| 559 | goto redirty_out;
|
| 560 |
|
| 561 | f2fs_do_write_meta_page(sbi, folio, io_type);
|
| 562 | dec_page_count(sbi, F2FS_DIRTY_META);
|
| 563 |
|
| 564 | folio_unlock(folio);
|
| 565 |
|
| 566 | if (unlikely(f2fs_cp_error(sbi)))
|
| 567 | f2fs_submit_merged_write(sbi, META);
|
| 568 |
|
| 569 | return true;
|
| 570 |
|
| 571 | redirty_out:
|
| 572 | folio_redirty_for_writepage(wbc, folio);
|
| 573 | return false;
|
| 574 | }
|
| 575 |
|
| 576 | static int f2fs_write_meta_pages(struct address_space *mapping,
|
| 577 | struct writeback_control *wbc)
|
| 578 | {
|
| 579 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
|
| 580 | struct f2fs_lock_context lc;
|
| 581 | long diff, written;
|
| 582 |
|
| 583 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
|
| 584 | goto skip_write;
|
| 585 |
|
| 586 |
|
| 587 | if (wbc->sync_mode != WB_SYNC_ALL &&
|
| 588 | get_pages(sbi, F2FS_DIRTY_META) <
|
| 589 | nr_pages_to_skip(sbi, META))
|
| 590 | goto skip_write;
|
| 591 |
|
| 592 |
|
| 593 | if (!f2fs_down_write_trylock_trace(&sbi->cp_global_sem, &lc))
|
| 594 | goto skip_write;
|
| 595 |
|
| 596 | trace_f2fs_writepages(mapping->host, wbc, META);
|
| 597 | diff = nr_pages_to_write(sbi, META, wbc);
|
| 598 | written = f2fs_sync_meta_pages(sbi, wbc->nr_to_write, FS_META_IO);
|
| 599 | f2fs_up_write_trace(&sbi->cp_global_sem, &lc);
|
| 600 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
|
| 601 | return 0;
|
| 602 |
|
| 603 | skip_write:
|
| 604 | wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
|
| 605 | trace_f2fs_writepages(mapping->host, wbc, META);
|
| 606 | return 0;
|
| 607 | }
|
| 608 |
|
| 609 | long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write,
|
| 610 | enum iostat_type io_type)
|
| 611 | {
|
| 612 | struct address_space *mapping = META_MAPPING(sbi);
|
| 613 | pgoff_t index = 0, prev = ULONG_MAX;
|
| 614 | struct folio_batch fbatch;
|
| 615 | long nwritten = 0;
|
| 616 | int nr_folios;
|
| 617 | struct writeback_control wbc = {};
|
| 618 | struct blk_plug plug;
|
| 619 |
|
| 620 | folio_batch_init(&fbatch);
|
| 621 |
|
| 622 | blk_start_plug(&plug);
|
| 623 |
|
| 624 | while ((nr_folios = filemap_get_folios_tag(mapping, &index,
|
| 625 | (pgoff_t)-1,
|
| 626 | PAGECACHE_TAG_DIRTY, &fbatch))) {
|
| 627 | int i;
|
| 628 |
|
| 629 | for (i = 0; i < nr_folios; i++) {
|
| 630 | struct folio *folio = fbatch.folios[i];
|
| 631 |
|
| 632 | if (nr_to_write != LONG_MAX && i != 0 &&
|
| 633 | folio->index != prev +
|
| 634 | folio_nr_pages(fbatch.folios[i-1])) {
|
| 635 | folio_batch_release(&fbatch);
|
| 636 | goto stop;
|
| 637 | }
|
| 638 |
|
| 639 | folio_lock(folio);
|
| 640 |
|
| 641 | if (unlikely(!is_meta_folio(folio))) {
|
| 642 | continue_unlock:
|
| 643 | folio_unlock(folio);
|
| 644 | continue;
|
| 645 | }
|
| 646 | if (!folio_test_dirty(folio)) {
|
| 647 |
|
| 648 | goto continue_unlock;
|
| 649 | }
|
| 650 |
|
| 651 | f2fs_folio_wait_writeback(folio, META, true, true);
|
| 652 |
|
| 653 | if (!folio_clear_dirty_for_io(folio))
|
| 654 | goto continue_unlock;
|
| 655 |
|
| 656 | if (!__f2fs_write_meta_folio(folio, &wbc,
|
| 657 | io_type)) {
|
| 658 | folio_unlock(folio);
|
| 659 | break;
|
| 660 | }
|
| 661 | nwritten += folio_nr_pages(folio);
|
| 662 | prev = folio->index;
|
| 663 | if (unlikely(nwritten >= nr_to_write))
|
| 664 | break;
|
| 665 | }
|
| 666 | folio_batch_release(&fbatch);
|
| 667 | cond_resched();
|
| 668 | }
|
| 669 | stop:
|
| 670 | if (nwritten)
|
| 671 | f2fs_submit_merged_write(sbi, META);
|
| 672 |
|
| 673 | blk_finish_plug(&plug);
|
| 674 |
|
| 675 | return nwritten;
|
| 676 | }
|
| 677 |
|
| 678 | static bool f2fs_dirty_meta_folio(struct address_space *mapping,
|
| 679 | struct folio *folio)
|
| 680 | {
|
| 681 | trace_f2fs_set_page_dirty(folio, META);
|
| 682 |
|
| 683 | if (!folio_test_uptodate(folio))
|
| 684 | folio_mark_uptodate(folio);
|
| 685 | if (filemap_dirty_folio(mapping, folio)) {
|
| 686 | inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
|
| 687 | folio_set_f2fs_reference(folio);
|
| 688 | return true;
|
| 689 | }
|
| 690 | return false;
|
| 691 | }
|
| 692 |
|
| 693 | const struct address_space_operations f2fs_meta_aops = {
|
| 694 | .writepages = f2fs_write_meta_pages,
|
| 695 | .dirty_folio = f2fs_dirty_meta_folio,
|
| 696 | .invalidate_folio = f2fs_invalidate_folio,
|
| 697 | .release_folio = f2fs_release_folio,
|
| 698 | .migrate_folio = filemap_migrate_folio,
|
| 699 | };
|
| 700 |
|
| 701 | static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
|
| 702 | unsigned int devidx, int type)
|
| 703 | {
|
| 704 | struct inode_management *im = &sbi->im[type];
|
| 705 | struct ino_entry *e = NULL, *new = NULL;
|
| 706 | int ret;
|
| 707 |
|
| 708 | if (type == FLUSH_INO) {
|
| 709 | rcu_read_lock();
|
| 710 | e = radix_tree_lookup(&im->ino_root, ino);
|
| 711 | rcu_read_unlock();
|
| 712 | }
|
| 713 |
|
| 714 | retry:
|
| 715 | if (!e)
|
| 716 | new = f2fs_kmem_cache_alloc(ino_entry_slab,
|
| 717 | GFP_NOFS, true, NULL);
|
| 718 |
|
| 719 | ret = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
|
| 720 | f2fs_bug_on(sbi, ret);
|
| 721 |
|
| 722 | spin_lock(&im->ino_lock);
|
| 723 | e = radix_tree_lookup(&im->ino_root, ino);
|
| 724 | if (!e) {
|
| 725 | if (!new) {
|
| 726 | spin_unlock(&im->ino_lock);
|
| 727 | radix_tree_preload_end();
|
| 728 | goto retry;
|
| 729 | }
|
| 730 | e = new;
|
| 731 | if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
|
| 732 | f2fs_bug_on(sbi, 1);
|
| 733 |
|
| 734 | memset(e, 0, sizeof(struct ino_entry));
|
| 735 | e->ino = ino;
|
| 736 |
|
| 737 | list_add_tail(&e->list, &im->ino_list);
|
| 738 | if (type != ORPHAN_INO)
|
| 739 | im->ino_num++;
|
| 740 | }
|
| 741 |
|
| 742 | if (type == FLUSH_INO)
|
| 743 | f2fs_set_bit(devidx, (char *)&e->dirty_device);
|
| 744 |
|
| 745 | spin_unlock(&im->ino_lock);
|
| 746 | radix_tree_preload_end();
|
| 747 |
|
| 748 | if (new && e != new)
|
| 749 | kmem_cache_free(ino_entry_slab, new);
|
| 750 | }
|
| 751 |
|
| 752 | static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
|
| 753 | {
|
| 754 | struct inode_management *im = &sbi->im[type];
|
| 755 | struct ino_entry *e;
|
| 756 |
|
| 757 | spin_lock(&im->ino_lock);
|
| 758 | e = radix_tree_lookup(&im->ino_root, ino);
|
| 759 | if (e) {
|
| 760 | list_del(&e->list);
|
| 761 | radix_tree_delete(&im->ino_root, ino);
|
| 762 | im->ino_num--;
|
| 763 | spin_unlock(&im->ino_lock);
|
| 764 | kmem_cache_free(ino_entry_slab, e);
|
| 765 | return;
|
| 766 | }
|
| 767 | spin_unlock(&im->ino_lock);
|
| 768 | }
|
| 769 |
|
| 770 | static void __set_ino_bitmap(struct f2fs_sb_info *sbi, nid_t ino, int type)
|
| 771 | {
|
| 772 | struct inode_management *im = &sbi->im[type];
|
| 773 | unsigned long index = INO_SLOT_INDEX(ino);
|
| 774 | unsigned int ofs = INO_BIT_OFFSET(ino);
|
| 775 | void **slot, *entry;
|
| 776 | unsigned long bitmap = 0;
|
| 777 | int ret;
|
| 778 |
|
| 779 | ret = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
|
| 780 | f2fs_bug_on(sbi, ret);
|
| 781 |
|
| 782 | spin_lock(&im->ino_lock);
|
| 783 | slot = radix_tree_lookup_slot(&im->ino_root, index);
|
| 784 | if (slot) {
|
| 785 | entry = radix_tree_deref_slot_protected(slot, &im->ino_lock);
|
| 786 | bitmap = xa_to_value(entry);
|
| 787 | if (!(bitmap & (1UL << ofs))) {
|
| 788 | bitmap |= (1UL << ofs);
|
| 789 | entry = xa_mk_value(bitmap);
|
| 790 | radix_tree_replace_slot(&im->ino_root, slot, entry);
|
| 791 | }
|
| 792 | } else {
|
| 793 | bitmap |= (1UL << ofs);
|
| 794 | entry = xa_mk_value(bitmap);
|
| 795 | if (unlikely(radix_tree_insert(&im->ino_root, index, entry)))
|
| 796 | f2fs_bug_on(sbi, 1);
|
| 797 | }
|
| 798 | spin_unlock(&im->ino_lock);
|
| 799 | radix_tree_preload_end();
|
| 800 | }
|
| 801 |
|
| 802 | static void __clear_ino_bitmap(struct f2fs_sb_info *sbi, nid_t ino, int type)
|
| 803 | {
|
| 804 | struct inode_management *im = &sbi->im[type];
|
| 805 | unsigned long index = INO_SLOT_INDEX(ino);
|
| 806 | unsigned int ofs = INO_BIT_OFFSET(ino);
|
| 807 | void **slot, *entry;
|
| 808 | unsigned long bitmap;
|
| 809 |
|
| 810 | spin_lock(&im->ino_lock);
|
| 811 | slot = radix_tree_lookup_slot(&im->ino_root, index);
|
| 812 | if (slot) {
|
| 813 | entry = radix_tree_deref_slot_protected(slot, &im->ino_lock);
|
| 814 | bitmap = xa_to_value(entry);
|
| 815 | if (bitmap & (1UL << ofs))
|
| 816 | bitmap &= ~(1UL << ofs);
|
| 817 |
|
| 818 | if (bitmap) {
|
| 819 | entry = xa_mk_value(bitmap);
|
| 820 | radix_tree_replace_slot(&im->ino_root, slot, entry);
|
| 821 | } else {
|
| 822 | radix_tree_delete(&im->ino_root, index);
|
| 823 | }
|
| 824 | }
|
| 825 | spin_unlock(&im->ino_lock);
|
| 826 | }
|
| 827 |
|
| 828 | static void f2fs_wait_for_inode_record(struct f2fs_sb_info *sbi, int mode)
|
| 829 | {
|
| 830 | if (mode != APPEND_INO && mode != UPDATE_INO)
|
| 831 | return;
|
| 832 |
|
| 833 |
|
| 834 | flush_workqueue(sbi->evict_wq);
|
| 835 | }
|
| 836 |
|
| 837 | static void __f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
|
| 838 | unsigned int devidx, int type)
|
| 839 | {
|
| 840 | if (type <= FLUSH_INO)
|
| 841 | __add_ino_entry(sbi, ino, devidx, type);
|
| 842 | else
|
| 843 | __set_ino_bitmap(sbi, ino, type);
|
| 844 | }
|
| 845 |
|
| 846 | void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
|
| 847 | {
|
| 848 | __f2fs_add_ino_entry(sbi, ino, 0, type);
|
| 849 | }
|
| 850 |
|
| 851 | void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
|
| 852 | {
|
| 853 | if (type <= FLUSH_INO)
|
| 854 | __remove_ino_entry(sbi, ino, type);
|
| 855 | else
|
| 856 | __clear_ino_bitmap(sbi, ino, type);
|
| 857 | }
|
| 858 |
|
| 859 |
|
| 860 | bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
|
| 861 | {
|
| 862 | struct inode_management *im = &sbi->im[mode];
|
| 863 | unsigned long index = INO_SLOT_INDEX(ino);
|
| 864 | unsigned int ofs = INO_BIT_OFFSET(ino);
|
| 865 | void *entry;
|
| 866 | unsigned long bitmap;
|
| 867 |
|
| 868 | f2fs_bug_on(sbi, mode <= FLUSH_INO);
|
| 869 |
|
| 870 | spin_lock(&im->ino_lock);
|
| 871 | entry = radix_tree_lookup(&im->ino_root, index);
|
| 872 | if (!entry) {
|
| 873 | spin_unlock(&im->ino_lock);
|
| 874 | return false;
|
| 875 | }
|
| 876 | bitmap = xa_to_value(entry);
|
| 877 | spin_unlock(&im->ino_lock);
|
| 878 |
|
| 879 | return bitmap & (1UL << ofs);
|
| 880 | }
|
| 881 |
|
| 882 | void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
|
| 883 | {
|
| 884 | struct ino_entry *e, *tmp;
|
| 885 | int i;
|
| 886 |
|
| 887 | for (i = all ? ORPHAN_INO : FLUSH_INO; i <= FLUSH_INO; i++) {
|
| 888 | struct inode_management *im = &sbi->im[i];
|
| 889 |
|
| 890 | f2fs_wait_for_inode_record(sbi, i);
|
| 891 |
|
| 892 | spin_lock(&im->ino_lock);
|
| 893 | list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
|
| 894 | list_del(&e->list);
|
| 895 | radix_tree_delete(&im->ino_root, e->ino);
|
| 896 | kmem_cache_free(ino_entry_slab, e);
|
| 897 | im->ino_num--;
|
| 898 | }
|
| 899 | spin_unlock(&im->ino_lock);
|
| 900 | }
|
| 901 |
|
| 902 | for (i = APPEND_INO; i < MAX_INO_ENTRY; i++) {
|
| 903 | struct inode_management *im = &sbi->im[i];
|
| 904 |
|
| 905 | spin_lock(&im->ino_lock);
|
| 906 | xa_destroy(&im->ino_root);
|
| 907 | spin_unlock(&im->ino_lock);
|
| 908 | }
|
| 909 | }
|
| 910 |
|
| 911 | void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
|
| 912 | unsigned int devidx, int type)
|
| 913 | {
|
| 914 | __f2fs_add_ino_entry(sbi, ino, devidx, type);
|
| 915 | }
|
| 916 |
|
| 917 | bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
|
| 918 | unsigned int devidx, int type)
|
| 919 | {
|
| 920 | struct inode_management *im = &sbi->im[type];
|
| 921 | struct ino_entry *e;
|
| 922 | bool is_dirty = false;
|
| 923 |
|
| 924 | spin_lock(&im->ino_lock);
|
| 925 | e = radix_tree_lookup(&im->ino_root, ino);
|
| 926 | if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
|
| 927 | is_dirty = true;
|
| 928 | spin_unlock(&im->ino_lock);
|
| 929 | return is_dirty;
|
| 930 | }
|
| 931 |
|
| 932 | int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
|
| 933 | {
|
| 934 | struct inode_management *im = &sbi->im[ORPHAN_INO];
|
| 935 | int err = 0;
|
| 936 |
|
| 937 | spin_lock(&im->ino_lock);
|
| 938 |
|
| 939 | if (time_to_inject(sbi, FAULT_ORPHAN)) {
|
| 940 | spin_unlock(&im->ino_lock);
|
| 941 | return -ENOSPC;
|
| 942 | }
|
| 943 |
|
| 944 | if (unlikely(im->ino_num >= sbi->max_orphans))
|
| 945 | err = -ENOSPC;
|
| 946 | else
|
| 947 | im->ino_num++;
|
| 948 | spin_unlock(&im->ino_lock);
|
| 949 |
|
| 950 | return err;
|
| 951 | }
|
| 952 |
|
| 953 | void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
|
| 954 | {
|
| 955 | struct inode_management *im = &sbi->im[ORPHAN_INO];
|
| 956 |
|
| 957 | spin_lock(&im->ino_lock);
|
| 958 | f2fs_bug_on(sbi, im->ino_num == 0);
|
| 959 | im->ino_num--;
|
| 960 | spin_unlock(&im->ino_lock);
|
| 961 | }
|
| 962 |
|
| 963 | void f2fs_add_orphan_inode(struct inode *inode)
|
| 964 | {
|
| 965 |
|
| 966 | f2fs_add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
|
| 967 | f2fs_update_inode_page(inode);
|
| 968 | }
|
| 969 |
|
| 970 | void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
|
| 971 | {
|
| 972 |
|
| 973 | f2fs_remove_ino_entry(sbi, ino, ORPHAN_INO);
|
| 974 | }
|
| 975 |
|
| 976 | static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
|
| 977 | {
|
| 978 | struct inode *inode;
|
| 979 | struct node_info ni;
|
| 980 | int err;
|
| 981 |
|
| 982 | inode = f2fs_iget_retry(sbi->sb, ino);
|
| 983 | if (IS_ERR(inode)) {
|
| 984 | |
| 985 | * there should be a bug that we can't find the entry
|
| 986 | * to orphan inode.
|
| 987 |
|
| 988 | f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
|
| 989 | return PTR_ERR(inode);
|
| 990 | }
|
| 991 |
|
| 992 | err = f2fs_dquot_initialize(inode);
|
| 993 | if (err) {
|
| 994 | iput(inode);
|
| 995 | goto err_out;
|
| 996 | }
|
| 997 |
|
| 998 | clear_nlink(inode);
|
| 999 |
|
| 1000 |
|
| 1001 | iput(inode);
|
| 1002 |
|
| 1003 | err = f2fs_get_node_info(sbi, ino, &ni, false);
|
| 1004 | if (err)
|
| 1005 | goto err_out;
|
| 1006 |
|
| 1007 |
|
| 1008 | if (ni.blk_addr != NULL_ADDR) {
|
| 1009 | err = -EIO;
|
| 1010 | goto err_out;
|
| 1011 | }
|
| 1012 | return 0;
|
| 1013 |
|
| 1014 | err_out:
|
| 1015 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 1016 | f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
|
| 1017 | __func__, ino);
|
| 1018 | return err;
|
| 1019 | }
|
| 1020 |
|
| 1021 | int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
|
| 1022 | {
|
| 1023 | block_t start_blk, orphan_blocks, i, j;
|
| 1024 | int err = 0;
|
| 1025 |
|
| 1026 | if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
|
| 1027 | return 0;
|
| 1028 |
|
| 1029 | if (f2fs_hw_is_readonly(sbi)) {
|
| 1030 | f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
|
| 1031 | return 0;
|
| 1032 | }
|
| 1033 |
|
| 1034 | if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
|
| 1035 | f2fs_info(sbi, "orphan cleanup on readonly fs");
|
| 1036 |
|
| 1037 | start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
|
| 1038 | orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
|
| 1039 |
|
| 1040 | f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
|
| 1041 |
|
| 1042 | for (i = 0; i < orphan_blocks; i++) {
|
| 1043 | struct folio *folio;
|
| 1044 | struct f2fs_orphan_block *orphan_blk;
|
| 1045 | unsigned int entry_count;
|
| 1046 |
|
| 1047 | folio = f2fs_get_meta_folio(sbi, start_blk + i);
|
| 1048 | if (IS_ERR(folio)) {
|
| 1049 | err = PTR_ERR(folio);
|
| 1050 | goto out;
|
| 1051 | }
|
| 1052 |
|
| 1053 | orphan_blk = folio_address(folio);
|
| 1054 | entry_count = le32_to_cpu(orphan_blk->entry_count);
|
| 1055 | if (entry_count > F2FS_ORPHANS_PER_BLOCK) {
|
| 1056 | f2fs_err(sbi, "invalid orphan inode entry count %u",
|
| 1057 | entry_count);
|
| 1058 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 1059 | f2fs_handle_error(sbi, ERROR_INCONSISTENT_ORPHAN);
|
| 1060 | err = -EFSCORRUPTED;
|
| 1061 | f2fs_folio_put(folio, true);
|
| 1062 | goto out;
|
| 1063 | }
|
| 1064 |
|
| 1065 | for (j = 0; j < entry_count; j++) {
|
| 1066 | nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
|
| 1067 |
|
| 1068 | err = recover_orphan_inode(sbi, ino);
|
| 1069 | if (err) {
|
| 1070 | f2fs_folio_put(folio, true);
|
| 1071 | goto out;
|
| 1072 | }
|
| 1073 | }
|
| 1074 | f2fs_folio_put(folio, true);
|
| 1075 | }
|
| 1076 |
|
| 1077 | clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
|
| 1078 | out:
|
| 1079 | set_sbi_flag(sbi, SBI_IS_RECOVERED);
|
| 1080 |
|
| 1081 | return err;
|
| 1082 | }
|
| 1083 |
|
| 1084 | static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
|
| 1085 | {
|
| 1086 | struct list_head *head;
|
| 1087 | struct f2fs_orphan_block *orphan_blk = NULL;
|
| 1088 | unsigned int nentries = 0;
|
| 1089 | unsigned short index = 1;
|
| 1090 | unsigned short orphan_blocks;
|
| 1091 | struct folio *folio = NULL;
|
| 1092 | struct ino_entry *orphan = NULL;
|
| 1093 | struct inode_management *im = &sbi->im[ORPHAN_INO];
|
| 1094 |
|
| 1095 | orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
|
| 1096 |
|
| 1097 | |
| 1098 | * we don't need to do spin_lock(&im->ino_lock) here, since all the
|
| 1099 | * orphan inode operations are covered under f2fs_lock_op().
|
| 1100 | * And, spin_lock should be avoided due to page operations below.
|
| 1101 |
|
| 1102 | head = &im->ino_list;
|
| 1103 |
|
| 1104 |
|
| 1105 | list_for_each_entry(orphan, head, list) {
|
| 1106 | if (!folio) {
|
| 1107 | folio = f2fs_grab_meta_folio(sbi, start_blk++);
|
| 1108 | orphan_blk = folio_address(folio);
|
| 1109 | memset(orphan_blk, 0, sizeof(*orphan_blk));
|
| 1110 | }
|
| 1111 |
|
| 1112 | orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
|
| 1113 |
|
| 1114 | if (nentries == F2FS_ORPHANS_PER_BLOCK) {
|
| 1115 | |
| 1116 | * an orphan block is full of 1020 entries,
|
| 1117 | * then we need to flush current orphan blocks
|
| 1118 | * and bring another one in memory
|
| 1119 |
|
| 1120 | orphan_blk->blk_addr = cpu_to_le16(index);
|
| 1121 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
|
| 1122 | orphan_blk->entry_count = cpu_to_le32(nentries);
|
| 1123 | folio_mark_dirty(folio);
|
| 1124 | f2fs_folio_put(folio, true);
|
| 1125 | index++;
|
| 1126 | nentries = 0;
|
| 1127 | folio = NULL;
|
| 1128 | }
|
| 1129 | }
|
| 1130 |
|
| 1131 | if (folio) {
|
| 1132 | orphan_blk->blk_addr = cpu_to_le16(index);
|
| 1133 | orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
|
| 1134 | orphan_blk->entry_count = cpu_to_le32(nentries);
|
| 1135 | folio_mark_dirty(folio);
|
| 1136 | f2fs_folio_put(folio, true);
|
| 1137 | }
|
| 1138 | }
|
| 1139 |
|
| 1140 | static __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *ckpt)
|
| 1141 | {
|
| 1142 | unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
|
| 1143 | __u32 chksum;
|
| 1144 |
|
| 1145 | chksum = f2fs_crc32(ckpt, chksum_ofs);
|
| 1146 | if (chksum_ofs < CP_CHKSUM_OFFSET) {
|
| 1147 | chksum_ofs += sizeof(chksum);
|
| 1148 | chksum = f2fs_chksum(chksum, (__u8 *)ckpt + chksum_ofs,
|
| 1149 | F2FS_BLKSIZE - chksum_ofs);
|
| 1150 | }
|
| 1151 | return chksum;
|
| 1152 | }
|
| 1153 |
|
| 1154 | static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
|
| 1155 | struct f2fs_checkpoint **cp_block, struct folio **cp_folio,
|
| 1156 | unsigned long long *version)
|
| 1157 | {
|
| 1158 | size_t crc_offset = 0;
|
| 1159 | __u32 crc;
|
| 1160 |
|
| 1161 | *cp_folio = f2fs_get_meta_folio(sbi, cp_addr);
|
| 1162 | if (IS_ERR(*cp_folio))
|
| 1163 | return PTR_ERR(*cp_folio);
|
| 1164 |
|
| 1165 | *cp_block = folio_address(*cp_folio);
|
| 1166 |
|
| 1167 | crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
|
| 1168 | if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
|
| 1169 | crc_offset > CP_CHKSUM_OFFSET) {
|
| 1170 | f2fs_folio_put(*cp_folio, true);
|
| 1171 | f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
|
| 1172 | return -EINVAL;
|
| 1173 | }
|
| 1174 |
|
| 1175 | crc = f2fs_checkpoint_chksum(*cp_block);
|
| 1176 | if (crc != cur_cp_crc(*cp_block)) {
|
| 1177 | f2fs_folio_put(*cp_folio, true);
|
| 1178 | f2fs_warn(sbi, "invalid crc value");
|
| 1179 | return -EINVAL;
|
| 1180 | }
|
| 1181 |
|
| 1182 | *version = cur_cp_version(*cp_block);
|
| 1183 | return 0;
|
| 1184 | }
|
| 1185 |
|
| 1186 | static struct folio *validate_checkpoint(struct f2fs_sb_info *sbi,
|
| 1187 | block_t cp_addr, unsigned long long *version)
|
| 1188 | {
|
| 1189 | struct folio *cp_folio_1 = NULL, *cp_folio_2 = NULL;
|
| 1190 | struct f2fs_checkpoint *cp_block = NULL;
|
| 1191 | unsigned long long cur_version = 0, pre_version = 0;
|
| 1192 | unsigned int cp_blocks;
|
| 1193 | int err;
|
| 1194 |
|
| 1195 | err = get_checkpoint_version(sbi, cp_addr, &cp_block,
|
| 1196 | &cp_folio_1, version);
|
| 1197 | if (err)
|
| 1198 | return NULL;
|
| 1199 |
|
| 1200 | cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
|
| 1201 |
|
| 1202 | if (cp_blocks > BLKS_PER_SEG(sbi) || cp_blocks <= F2FS_CP_PACKS) {
|
| 1203 | f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
|
| 1204 | le32_to_cpu(cp_block->cp_pack_total_block_count));
|
| 1205 | goto invalid_cp;
|
| 1206 | }
|
| 1207 | pre_version = *version;
|
| 1208 |
|
| 1209 | cp_addr += cp_blocks - 1;
|
| 1210 | err = get_checkpoint_version(sbi, cp_addr, &cp_block,
|
| 1211 | &cp_folio_2, version);
|
| 1212 | if (err)
|
| 1213 | goto invalid_cp;
|
| 1214 | cur_version = *version;
|
| 1215 |
|
| 1216 | if (cur_version == pre_version) {
|
| 1217 | *version = cur_version;
|
| 1218 | f2fs_folio_put(cp_folio_2, true);
|
| 1219 | return cp_folio_1;
|
| 1220 | }
|
| 1221 | f2fs_folio_put(cp_folio_2, true);
|
| 1222 | invalid_cp:
|
| 1223 | f2fs_folio_put(cp_folio_1, true);
|
| 1224 | return NULL;
|
| 1225 | }
|
| 1226 |
|
| 1227 | int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
|
| 1228 | {
|
| 1229 | struct f2fs_checkpoint *cp_block;
|
| 1230 | struct f2fs_super_block *fsb = sbi->raw_super;
|
| 1231 | struct folio *cp1, *cp2, *cur_folio;
|
| 1232 | unsigned long blk_size = sbi->blocksize;
|
| 1233 | unsigned long long cp1_version = 0, cp2_version = 0;
|
| 1234 | unsigned long long cp_start_blk_no;
|
| 1235 | unsigned int cp_blks = 1 + __cp_payload(sbi);
|
| 1236 | block_t cp_blk_no;
|
| 1237 | int i;
|
| 1238 | int err;
|
| 1239 |
|
| 1240 | sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks),
|
| 1241 | GFP_KERNEL);
|
| 1242 | if (!sbi->ckpt)
|
| 1243 | return -ENOMEM;
|
| 1244 | |
| 1245 | * Finding out valid cp block involves read both
|
| 1246 | * sets( cp pack 1 and cp pack 2)
|
| 1247 |
|
| 1248 | cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
|
| 1249 | cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
|
| 1250 |
|
| 1251 |
|
| 1252 | cp_start_blk_no += ((unsigned long long)1) <<
|
| 1253 | le32_to_cpu(fsb->log_blocks_per_seg);
|
| 1254 | cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
|
| 1255 |
|
| 1256 | if (cp1 && cp2) {
|
| 1257 | if (ver_after(cp2_version, cp1_version))
|
| 1258 | cur_folio = cp2;
|
| 1259 | else
|
| 1260 | cur_folio = cp1;
|
| 1261 | } else if (cp1) {
|
| 1262 | cur_folio = cp1;
|
| 1263 | } else if (cp2) {
|
| 1264 | cur_folio = cp2;
|
| 1265 | } else {
|
| 1266 | err = -EFSCORRUPTED;
|
| 1267 | goto fail_no_cp;
|
| 1268 | }
|
| 1269 |
|
| 1270 | cp_block = folio_address(cur_folio);
|
| 1271 | memcpy(sbi->ckpt, cp_block, blk_size);
|
| 1272 |
|
| 1273 | if (cur_folio == cp1)
|
| 1274 | sbi->cur_cp_pack = 1;
|
| 1275 | else
|
| 1276 | sbi->cur_cp_pack = 2;
|
| 1277 |
|
| 1278 |
|
| 1279 | if (f2fs_sanity_check_ckpt(sbi)) {
|
| 1280 | err = -EFSCORRUPTED;
|
| 1281 | goto free_fail_no_cp;
|
| 1282 | }
|
| 1283 |
|
| 1284 | if (cp_blks <= 1)
|
| 1285 | goto done;
|
| 1286 |
|
| 1287 | cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
|
| 1288 | if (cur_folio == cp2)
|
| 1289 | cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg));
|
| 1290 |
|
| 1291 | for (i = 1; i < cp_blks; i++) {
|
| 1292 | void *sit_bitmap_ptr;
|
| 1293 | unsigned char *ckpt = (unsigned char *)sbi->ckpt;
|
| 1294 |
|
| 1295 | cur_folio = f2fs_get_meta_folio(sbi, cp_blk_no + i);
|
| 1296 | if (IS_ERR(cur_folio)) {
|
| 1297 | err = PTR_ERR(cur_folio);
|
| 1298 | goto free_fail_no_cp;
|
| 1299 | }
|
| 1300 | sit_bitmap_ptr = folio_address(cur_folio);
|
| 1301 | memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
|
| 1302 | f2fs_folio_put(cur_folio, true);
|
| 1303 | }
|
| 1304 | done:
|
| 1305 | f2fs_folio_put(cp1, true);
|
| 1306 | f2fs_folio_put(cp2, true);
|
| 1307 | return 0;
|
| 1308 |
|
| 1309 | free_fail_no_cp:
|
| 1310 | f2fs_folio_put(cp1, true);
|
| 1311 | f2fs_folio_put(cp2, true);
|
| 1312 | fail_no_cp:
|
| 1313 | kvfree(sbi->ckpt);
|
| 1314 | return err;
|
| 1315 | }
|
| 1316 |
|
| 1317 | static void __add_dirty_inode(struct inode *inode, enum inode_type type)
|
| 1318 | {
|
| 1319 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1320 | int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
|
| 1321 |
|
| 1322 | if (is_inode_flag_set(inode, flag))
|
| 1323 | return;
|
| 1324 |
|
| 1325 | set_inode_flag(inode, flag);
|
| 1326 | list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
|
| 1327 | stat_inc_dirty_inode(sbi, type);
|
| 1328 | }
|
| 1329 |
|
| 1330 | static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
|
| 1331 | {
|
| 1332 | int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
|
| 1333 |
|
| 1334 | if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
|
| 1335 | return;
|
| 1336 |
|
| 1337 | list_del_init(&F2FS_I(inode)->dirty_list);
|
| 1338 | clear_inode_flag(inode, flag);
|
| 1339 | stat_dec_dirty_inode(F2FS_I_SB(inode), type);
|
| 1340 | }
|
| 1341 |
|
| 1342 | void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio)
|
| 1343 | {
|
| 1344 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1345 | enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
|
| 1346 |
|
| 1347 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
|
| 1348 | !S_ISLNK(inode->i_mode))
|
| 1349 | return;
|
| 1350 |
|
| 1351 | spin_lock(&sbi->inode_lock[type]);
|
| 1352 | if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
|
| 1353 | __add_dirty_inode(inode, type);
|
| 1354 | inode_inc_dirty_pages(inode);
|
| 1355 | spin_unlock(&sbi->inode_lock[type]);
|
| 1356 |
|
| 1357 | folio_set_f2fs_reference(folio);
|
| 1358 | }
|
| 1359 |
|
| 1360 | void f2fs_remove_dirty_inode(struct inode *inode)
|
| 1361 | {
|
| 1362 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1363 | enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
|
| 1364 |
|
| 1365 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
|
| 1366 | !S_ISLNK(inode->i_mode))
|
| 1367 | return;
|
| 1368 |
|
| 1369 | if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
|
| 1370 | return;
|
| 1371 |
|
| 1372 | spin_lock(&sbi->inode_lock[type]);
|
| 1373 | __remove_dirty_inode(inode, type);
|
| 1374 | spin_unlock(&sbi->inode_lock[type]);
|
| 1375 | }
|
| 1376 |
|
| 1377 | int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
|
| 1378 | bool from_cp)
|
| 1379 | {
|
| 1380 | struct list_head *head;
|
| 1381 | struct inode *inode;
|
| 1382 | struct f2fs_inode_info *fi;
|
| 1383 | bool is_dir = (type == DIR_INODE);
|
| 1384 | unsigned long ino = 0;
|
| 1385 |
|
| 1386 | trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
|
| 1387 | get_pages(sbi, is_dir ?
|
| 1388 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
|
| 1389 | retry:
|
| 1390 | if (unlikely(f2fs_cp_error(sbi))) {
|
| 1391 | trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
|
| 1392 | get_pages(sbi, is_dir ?
|
| 1393 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
|
| 1394 | return -EIO;
|
| 1395 | }
|
| 1396 |
|
| 1397 | spin_lock(&sbi->inode_lock[type]);
|
| 1398 |
|
| 1399 | head = &sbi->inode_list[type];
|
| 1400 | if (list_empty(head)) {
|
| 1401 | spin_unlock(&sbi->inode_lock[type]);
|
| 1402 | trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
|
| 1403 | get_pages(sbi, is_dir ?
|
| 1404 | F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
|
| 1405 | return 0;
|
| 1406 | }
|
| 1407 | fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
|
| 1408 | inode = igrab(&fi->vfs_inode);
|
| 1409 | spin_unlock(&sbi->inode_lock[type]);
|
| 1410 | if (inode) {
|
| 1411 | unsigned long cur_ino = inode->i_ino;
|
| 1412 |
|
| 1413 | if (from_cp)
|
| 1414 | F2FS_I(inode)->cp_task = current;
|
| 1415 | F2FS_I(inode)->wb_task = current;
|
| 1416 |
|
| 1417 | filemap_fdatawrite(inode->i_mapping);
|
| 1418 |
|
| 1419 | F2FS_I(inode)->wb_task = NULL;
|
| 1420 | if (from_cp)
|
| 1421 | F2FS_I(inode)->cp_task = NULL;
|
| 1422 |
|
| 1423 | iput(inode);
|
| 1424 |
|
| 1425 | if (ino == cur_ino)
|
| 1426 | cond_resched();
|
| 1427 | else
|
| 1428 | ino = cur_ino;
|
| 1429 | } else {
|
| 1430 | |
| 1431 | * We should submit bio, since it exists several
|
| 1432 | * writebacking dentry pages in the freeing inode.
|
| 1433 |
|
| 1434 | f2fs_submit_merged_write(sbi, DATA);
|
| 1435 | cond_resched();
|
| 1436 | }
|
| 1437 | goto retry;
|
| 1438 | }
|
| 1439 |
|
| 1440 | static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
|
| 1441 | {
|
| 1442 | struct list_head *head = &sbi->inode_list[DIRTY_META];
|
| 1443 | struct inode *inode;
|
| 1444 | struct f2fs_inode_info *fi;
|
| 1445 | s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
|
| 1446 |
|
| 1447 | while (total--) {
|
| 1448 | if (unlikely(f2fs_cp_error(sbi)))
|
| 1449 | return -EIO;
|
| 1450 |
|
| 1451 | spin_lock(&sbi->inode_lock[DIRTY_META]);
|
| 1452 | if (list_empty(head)) {
|
| 1453 | spin_unlock(&sbi->inode_lock[DIRTY_META]);
|
| 1454 | return 0;
|
| 1455 | }
|
| 1456 | fi = list_first_entry(head, struct f2fs_inode_info,
|
| 1457 | gdirty_list);
|
| 1458 | inode = igrab(&fi->vfs_inode);
|
| 1459 | spin_unlock(&sbi->inode_lock[DIRTY_META]);
|
| 1460 | if (inode) {
|
| 1461 | sync_inode_metadata(inode, 0);
|
| 1462 |
|
| 1463 |
|
| 1464 | if (is_inode_flag_set(inode, FI_DIRTY_INODE))
|
| 1465 | f2fs_update_inode_page(inode);
|
| 1466 | iput(inode);
|
| 1467 | }
|
| 1468 | }
|
| 1469 | return 0;
|
| 1470 | }
|
| 1471 |
|
| 1472 | static void __prepare_cp_block(struct f2fs_sb_info *sbi)
|
| 1473 | {
|
| 1474 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
|
| 1475 | struct f2fs_nm_info *nm_i = NM_I(sbi);
|
| 1476 | nid_t last_nid = nm_i->next_scan_nid;
|
| 1477 |
|
| 1478 | next_free_nid(sbi, &last_nid);
|
| 1479 | ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
|
| 1480 | ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
|
| 1481 | ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
|
| 1482 | ckpt->next_free_nid = cpu_to_le32(last_nid);
|
| 1483 |
|
| 1484 |
|
| 1485 | sbi->last_valid_block_count = sbi->total_valid_block_count;
|
| 1486 | percpu_counter_set(&sbi->alloc_valid_block_count, 0);
|
| 1487 | percpu_counter_set(&sbi->rf_node_block_count, 0);
|
| 1488 | }
|
| 1489 |
|
| 1490 | static bool __need_flush_quota(struct f2fs_sb_info *sbi)
|
| 1491 | {
|
| 1492 | bool ret = false;
|
| 1493 |
|
| 1494 | if (!is_journalled_quota(sbi))
|
| 1495 | return false;
|
| 1496 |
|
| 1497 | if (!f2fs_down_write_trylock(&sbi->quota_sem))
|
| 1498 | return true;
|
| 1499 | if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
|
| 1500 | ret = false;
|
| 1501 | } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
|
| 1502 | ret = false;
|
| 1503 | } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
|
| 1504 | clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
|
| 1505 | ret = true;
|
| 1506 | } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
|
| 1507 | ret = true;
|
| 1508 | }
|
| 1509 | f2fs_up_write(&sbi->quota_sem);
|
| 1510 | return ret;
|
| 1511 | }
|
| 1512 |
|
| 1513 | |
| 1514 | * Freeze all the FS-operations for checkpoint.
|
| 1515 |
|
| 1516 | static int block_operations(struct f2fs_sb_info *sbi)
|
| 1517 | {
|
| 1518 | struct writeback_control wbc = {
|
| 1519 | .sync_mode = WB_SYNC_ALL,
|
| 1520 | .nr_to_write = LONG_MAX,
|
| 1521 | };
|
| 1522 | int err = 0, cnt = 0;
|
| 1523 |
|
| 1524 | |
| 1525 | * Let's flush inline_data in dirty node pages.
|
| 1526 |
|
| 1527 | f2fs_flush_inline_data(sbi);
|
| 1528 |
|
| 1529 | retry_flush_quotas:
|
| 1530 | f2fs_lock_all(sbi);
|
| 1531 | if (__need_flush_quota(sbi)) {
|
| 1532 | bool need_lock = sbi->umount_lock_holder != current;
|
| 1533 |
|
| 1534 | if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
|
| 1535 | set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
|
| 1536 | set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
|
| 1537 | goto retry_flush_dents;
|
| 1538 | }
|
| 1539 | f2fs_unlock_all(sbi);
|
| 1540 |
|
| 1541 |
|
| 1542 | if (!need_lock) {
|
| 1543 | f2fs_do_quota_sync(sbi->sb, -1);
|
| 1544 | } else if (down_read_trylock(&sbi->sb->s_umount)) {
|
| 1545 | f2fs_do_quota_sync(sbi->sb, -1);
|
| 1546 | up_read(&sbi->sb->s_umount);
|
| 1547 | }
|
| 1548 | cond_resched();
|
| 1549 | goto retry_flush_quotas;
|
| 1550 | }
|
| 1551 |
|
| 1552 | retry_flush_dents:
|
| 1553 |
|
| 1554 | if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
|
| 1555 | f2fs_unlock_all(sbi);
|
| 1556 | err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
|
| 1557 | if (err)
|
| 1558 | return err;
|
| 1559 | cond_resched();
|
| 1560 | goto retry_flush_quotas;
|
| 1561 | }
|
| 1562 |
|
| 1563 | |
| 1564 | * POR: we should ensure that there are no dirty node pages
|
| 1565 | * until finishing nat/sit flush. inode->i_blocks can be updated.
|
| 1566 |
|
| 1567 | f2fs_down_write(&sbi->node_change);
|
| 1568 |
|
| 1569 | if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
|
| 1570 | f2fs_up_write(&sbi->node_change);
|
| 1571 | f2fs_unlock_all(sbi);
|
| 1572 | err = f2fs_sync_inode_meta(sbi);
|
| 1573 | if (err)
|
| 1574 | return err;
|
| 1575 | cond_resched();
|
| 1576 | goto retry_flush_quotas;
|
| 1577 | }
|
| 1578 |
|
| 1579 | retry_flush_nodes:
|
| 1580 | f2fs_down_write(&sbi->node_write);
|
| 1581 |
|
| 1582 | if (get_pages(sbi, F2FS_DIRTY_NODES)) {
|
| 1583 | f2fs_up_write(&sbi->node_write);
|
| 1584 | atomic_inc(&sbi->wb_sync_req[NODE]);
|
| 1585 | err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
|
| 1586 | atomic_dec(&sbi->wb_sync_req[NODE]);
|
| 1587 | if (err) {
|
| 1588 | f2fs_up_write(&sbi->node_change);
|
| 1589 | f2fs_unlock_all(sbi);
|
| 1590 | return err;
|
| 1591 | }
|
| 1592 | cond_resched();
|
| 1593 | goto retry_flush_nodes;
|
| 1594 | }
|
| 1595 |
|
| 1596 | |
| 1597 | * sbi->node_change is used only for AIO write_begin path which produces
|
| 1598 | * dirty node blocks and some checkpoint values by block allocation.
|
| 1599 |
|
| 1600 | __prepare_cp_block(sbi);
|
| 1601 | f2fs_up_write(&sbi->node_change);
|
| 1602 | return err;
|
| 1603 | }
|
| 1604 |
|
| 1605 | static void unblock_operations(struct f2fs_sb_info *sbi)
|
| 1606 | {
|
| 1607 | f2fs_up_write(&sbi->node_write);
|
| 1608 | f2fs_unlock_all(sbi);
|
| 1609 | }
|
| 1610 |
|
| 1611 | void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
|
| 1612 | {
|
| 1613 | DEFINE_WAIT(wait);
|
| 1614 |
|
| 1615 | for (;;) {
|
| 1616 | if (!get_pages(sbi, type))
|
| 1617 | break;
|
| 1618 |
|
| 1619 | if (unlikely(f2fs_cp_error(sbi) &&
|
| 1620 | !is_sbi_flag_set(sbi, SBI_IS_CLOSE)))
|
| 1621 | break;
|
| 1622 |
|
| 1623 | if (type == F2FS_DIRTY_META)
|
| 1624 | f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
|
| 1625 | else if (type == F2FS_WB_CP_DATA)
|
| 1626 | f2fs_submit_merged_write(sbi, DATA);
|
| 1627 |
|
| 1628 | prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
|
| 1629 | io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
|
| 1630 | }
|
| 1631 | finish_wait(&sbi->cp_wait, &wait);
|
| 1632 | }
|
| 1633 |
|
| 1634 | static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
|
| 1635 | {
|
| 1636 | unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
|
| 1637 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
|
| 1638 | unsigned long flags;
|
| 1639 |
|
| 1640 | spin_lock_irqsave(&sbi->cp_lock, flags);
|
| 1641 |
|
| 1642 | if ((cpc->reason & CP_UMOUNT) &&
|
| 1643 | le32_to_cpu(ckpt->cp_pack_total_block_count) >
|
| 1644 | sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
|
| 1645 | disable_nat_bits(sbi, false);
|
| 1646 |
|
| 1647 | if (cpc->reason & CP_TRIMMED)
|
| 1648 | __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
|
| 1649 | else
|
| 1650 | __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
|
| 1651 |
|
| 1652 | if (cpc->reason & CP_UMOUNT)
|
| 1653 | __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
|
| 1654 | else
|
| 1655 | __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
|
| 1656 |
|
| 1657 | if (cpc->reason & CP_FASTBOOT)
|
| 1658 | __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
|
| 1659 | else
|
| 1660 | __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
|
| 1661 |
|
| 1662 | if (orphan_num)
|
| 1663 | __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
|
| 1664 | else
|
| 1665 | __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
|
| 1666 |
|
| 1667 | if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
|
| 1668 | __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
|
| 1669 |
|
| 1670 | if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
|
| 1671 | __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
|
| 1672 | else
|
| 1673 | __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
|
| 1674 |
|
| 1675 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
|
| 1676 | __set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
|
| 1677 | else
|
| 1678 | __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
|
| 1679 |
|
| 1680 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
|
| 1681 | __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
|
| 1682 | else
|
| 1683 | __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
|
| 1684 |
|
| 1685 | if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
|
| 1686 | __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
|
| 1687 | else
|
| 1688 | __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
|
| 1689 |
|
| 1690 | if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
|
| 1691 | __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
|
| 1692 |
|
| 1693 |
|
| 1694 | __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
|
| 1695 | __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
|
| 1696 |
|
| 1697 | spin_unlock_irqrestore(&sbi->cp_lock, flags);
|
| 1698 | }
|
| 1699 |
|
| 1700 | static void commit_checkpoint(struct f2fs_sb_info *sbi,
|
| 1701 | void *src, block_t blk_addr)
|
| 1702 | {
|
| 1703 | struct writeback_control wbc = {};
|
| 1704 |
|
| 1705 | |
| 1706 | * filemap_get_folios_tag and folio_lock again will take
|
| 1707 | * some extra time. Therefore, f2fs_update_meta_pages and
|
| 1708 | * f2fs_sync_meta_pages are combined in this function.
|
| 1709 |
|
| 1710 | struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr);
|
| 1711 |
|
| 1712 | memcpy(folio_address(folio), src, PAGE_SIZE);
|
| 1713 |
|
| 1714 | folio_mark_dirty(folio);
|
| 1715 | if (unlikely(!folio_clear_dirty_for_io(folio)))
|
| 1716 | f2fs_bug_on(sbi, 1);
|
| 1717 |
|
| 1718 |
|
| 1719 | if (unlikely(!__f2fs_write_meta_folio(folio, &wbc, FS_CP_META_IO))) {
|
| 1720 | if (f2fs_cp_error(sbi)) {
|
| 1721 | f2fs_folio_put(folio, true);
|
| 1722 | return;
|
| 1723 | }
|
| 1724 | f2fs_bug_on(sbi, true);
|
| 1725 | }
|
| 1726 |
|
| 1727 | f2fs_folio_put(folio, false);
|
| 1728 |
|
| 1729 |
|
| 1730 | f2fs_submit_merged_write(sbi, META_FLUSH);
|
| 1731 | }
|
| 1732 |
|
| 1733 | static inline u64 get_sectors_written(struct block_device *bdev)
|
| 1734 | {
|
| 1735 | return (u64)part_stat_read(bdev, sectors[STAT_WRITE]);
|
| 1736 | }
|
| 1737 |
|
| 1738 | u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
|
| 1739 | {
|
| 1740 | if (f2fs_is_multi_device(sbi)) {
|
| 1741 | u64 sectors = 0;
|
| 1742 | int i;
|
| 1743 |
|
| 1744 | for (i = 0; i < sbi->s_ndevs; i++)
|
| 1745 | sectors += get_sectors_written(FDEV(i).bdev);
|
| 1746 |
|
| 1747 | return sectors;
|
| 1748 | }
|
| 1749 |
|
| 1750 | return get_sectors_written(sbi->sb->s_bdev);
|
| 1751 | }
|
| 1752 |
|
| 1753 | static inline void stat_cp_time(struct cp_control *cpc, enum cp_time type)
|
| 1754 | {
|
| 1755 | cpc->stats.times[type] = ktime_get();
|
| 1756 | }
|
| 1757 |
|
| 1758 | static inline void check_cp_time(struct f2fs_sb_info *sbi, struct cp_control *cpc)
|
| 1759 | {
|
| 1760 | unsigned long long sb_diff, cur_diff;
|
| 1761 | enum cp_time ct;
|
| 1762 |
|
| 1763 | sb_diff = (u64)ktime_ms_delta(sbi->cp_stats.times[CP_TIME_END],
|
| 1764 | sbi->cp_stats.times[CP_TIME_START]);
|
| 1765 | cur_diff = (u64)ktime_ms_delta(cpc->stats.times[CP_TIME_END],
|
| 1766 | cpc->stats.times[CP_TIME_START]);
|
| 1767 |
|
| 1768 | if (cur_diff > sb_diff) {
|
| 1769 | sbi->cp_stats = cpc->stats;
|
| 1770 | if (cur_diff < CP_LONG_LATENCY_THRESHOLD)
|
| 1771 | return;
|
| 1772 |
|
| 1773 | f2fs_warn(sbi, "checkpoint was blocked for %llu ms", cur_diff);
|
| 1774 | for (ct = CP_TIME_START; ct < CP_TIME_MAX - 1; ct++)
|
| 1775 | f2fs_warn(sbi, "Step#%d: %llu ms", ct,
|
| 1776 | (u64)ktime_ms_delta(cpc->stats.times[ct + 1],
|
| 1777 | cpc->stats.times[ct]));
|
| 1778 | }
|
| 1779 | }
|
| 1780 |
|
| 1781 | static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
|
| 1782 | {
|
| 1783 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
|
| 1784 | struct f2fs_nm_info *nm_i = NM_I(sbi);
|
| 1785 | unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
|
| 1786 | block_t start_blk;
|
| 1787 | unsigned int data_sum_blocks, orphan_blocks;
|
| 1788 | __u32 crc32 = 0;
|
| 1789 | int i;
|
| 1790 | int cp_payload_blks = __cp_payload(sbi);
|
| 1791 | struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
|
| 1792 | u64 kbytes_written;
|
| 1793 | int err;
|
| 1794 |
|
| 1795 |
|
| 1796 | f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
|
| 1797 |
|
| 1798 | stat_cp_time(cpc, CP_TIME_SYNC_META);
|
| 1799 |
|
| 1800 |
|
| 1801 | ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
|
| 1802 | ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
|
| 1803 | for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
|
| 1804 | struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE);
|
| 1805 |
|
| 1806 | ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno);
|
| 1807 | ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
|
| 1808 | ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type;
|
| 1809 | }
|
| 1810 | for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
|
| 1811 | struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA);
|
| 1812 |
|
| 1813 | ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno);
|
| 1814 | ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
|
| 1815 | ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type;
|
| 1816 | }
|
| 1817 |
|
| 1818 |
|
| 1819 | data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
|
| 1820 | spin_lock_irqsave(&sbi->cp_lock, flags);
|
| 1821 | if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
|
| 1822 | __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
|
| 1823 | else
|
| 1824 | __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
|
| 1825 | spin_unlock_irqrestore(&sbi->cp_lock, flags);
|
| 1826 |
|
| 1827 | orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
|
| 1828 | ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
|
| 1829 | orphan_blocks);
|
| 1830 |
|
| 1831 | if (__remain_node_summaries(cpc->reason))
|
| 1832 | ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
|
| 1833 | cp_payload_blks + data_sum_blocks +
|
| 1834 | orphan_blocks + NR_CURSEG_NODE_TYPE);
|
| 1835 | else
|
| 1836 | ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
|
| 1837 | cp_payload_blks + data_sum_blocks +
|
| 1838 | orphan_blocks);
|
| 1839 |
|
| 1840 |
|
| 1841 | update_ckpt_flags(sbi, cpc);
|
| 1842 |
|
| 1843 |
|
| 1844 | get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
|
| 1845 | get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
|
| 1846 |
|
| 1847 | crc32 = f2fs_checkpoint_chksum(ckpt);
|
| 1848 | *((__le32 *)((unsigned char *)ckpt +
|
| 1849 | le32_to_cpu(ckpt->checksum_offset)))
|
| 1850 | = cpu_to_le32(crc32);
|
| 1851 |
|
| 1852 | start_blk = __start_cp_next_addr(sbi);
|
| 1853 |
|
| 1854 |
|
| 1855 | if (enabled_nat_bits(sbi, cpc)) {
|
| 1856 | __u64 cp_ver = cur_cp_version(ckpt);
|
| 1857 | block_t blk;
|
| 1858 |
|
| 1859 | cp_ver |= ((__u64)crc32 << 32);
|
| 1860 | *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
|
| 1861 |
|
| 1862 | blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks;
|
| 1863 | for (i = 0; i < nm_i->nat_bits_blocks; i++)
|
| 1864 | f2fs_update_meta_page(sbi, nm_i->nat_bits +
|
| 1865 | F2FS_BLK_TO_BYTES(i), blk + i);
|
| 1866 | }
|
| 1867 |
|
| 1868 |
|
| 1869 | f2fs_update_meta_page(sbi, ckpt, start_blk++);
|
| 1870 |
|
| 1871 | for (i = 1; i < 1 + cp_payload_blks; i++)
|
| 1872 | f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
|
| 1873 | start_blk++);
|
| 1874 |
|
| 1875 | if (orphan_num) {
|
| 1876 | write_orphan_inodes(sbi, start_blk);
|
| 1877 | start_blk += orphan_blocks;
|
| 1878 | }
|
| 1879 |
|
| 1880 | f2fs_write_data_summaries(sbi, start_blk);
|
| 1881 | start_blk += data_sum_blocks;
|
| 1882 |
|
| 1883 |
|
| 1884 | kbytes_written = sbi->kbytes_written;
|
| 1885 | kbytes_written += (f2fs_get_sectors_written(sbi) -
|
| 1886 | sbi->sectors_written_start) >> 1;
|
| 1887 | seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
|
| 1888 |
|
| 1889 | if (__remain_node_summaries(cpc->reason)) {
|
| 1890 | f2fs_write_node_summaries(sbi, start_blk);
|
| 1891 | start_blk += NR_CURSEG_NODE_TYPE;
|
| 1892 | }
|
| 1893 |
|
| 1894 |
|
| 1895 | f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
|
| 1896 | stat_cp_time(cpc, CP_TIME_SYNC_CP_META);
|
| 1897 |
|
| 1898 |
|
| 1899 | f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
|
| 1900 | stat_cp_time(cpc, CP_TIME_WAIT_DIRTY_META);
|
| 1901 |
|
| 1902 |
|
| 1903 | f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
|
| 1904 | stat_cp_time(cpc, CP_TIME_WAIT_CP_DATA);
|
| 1905 |
|
| 1906 |
|
| 1907 | err = f2fs_flush_device_cache(sbi);
|
| 1908 | if (err)
|
| 1909 | return err;
|
| 1910 | stat_cp_time(cpc, CP_TIME_FLUSH_DEVICE);
|
| 1911 |
|
| 1912 |
|
| 1913 | commit_checkpoint(sbi, ckpt, start_blk);
|
| 1914 | f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
|
| 1915 | stat_cp_time(cpc, CP_TIME_WAIT_LAST_CP);
|
| 1916 |
|
| 1917 | |
| 1918 | * invalidate intermediate page cache borrowed from meta inode which are
|
| 1919 | * used for migration of encrypted, verity or compressed inode's blocks.
|
| 1920 |
|
| 1921 | if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
|
| 1922 | f2fs_sb_has_compression(sbi))
|
| 1923 | f2fs_bug_on(sbi,
|
| 1924 | invalidate_inode_pages2_range(META_MAPPING(sbi),
|
| 1925 | MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1));
|
| 1926 |
|
| 1927 | f2fs_release_ino_entry(sbi, false);
|
| 1928 |
|
| 1929 | f2fs_reset_fsync_node_info(sbi);
|
| 1930 |
|
| 1931 | clear_sbi_flag(sbi, SBI_IS_DIRTY);
|
| 1932 | clear_sbi_flag(sbi, SBI_NEED_CP);
|
| 1933 | clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
|
| 1934 |
|
| 1935 | spin_lock(&sbi->stat_lock);
|
| 1936 | sbi->unusable_block_count = 0;
|
| 1937 | spin_unlock(&sbi->stat_lock);
|
| 1938 |
|
| 1939 | __set_cp_next_pack(sbi);
|
| 1940 |
|
| 1941 | |
| 1942 | * redirty superblock if metadata like node page or inode cache is
|
| 1943 | * updated during writing checkpoint.
|
| 1944 |
|
| 1945 | if (get_pages(sbi, F2FS_DIRTY_NODES) ||
|
| 1946 | get_pages(sbi, F2FS_DIRTY_IMETA))
|
| 1947 | set_sbi_flag(sbi, SBI_IS_DIRTY);
|
| 1948 |
|
| 1949 | f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
|
| 1950 |
|
| 1951 | return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
|
| 1952 | }
|
| 1953 |
|
| 1954 | int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
|
| 1955 | {
|
| 1956 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
|
| 1957 | struct f2fs_lock_context lc;
|
| 1958 | unsigned long long ckpt_ver;
|
| 1959 | int err = 0;
|
| 1960 |
|
| 1961 | stat_cp_time(cpc, CP_TIME_START);
|
| 1962 |
|
| 1963 | if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
|
| 1964 | return -EROFS;
|
| 1965 |
|
| 1966 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
|
| 1967 | if (cpc->reason != CP_PAUSE)
|
| 1968 | return 0;
|
| 1969 | f2fs_warn(sbi, "Start checkpoint disabled!");
|
| 1970 | }
|
| 1971 | if (cpc->reason != CP_RESIZE)
|
| 1972 | f2fs_down_write_trace(&sbi->cp_global_sem, &lc);
|
| 1973 |
|
| 1974 | stat_cp_time(cpc, CP_TIME_LOCK);
|
| 1975 |
|
| 1976 | if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
|
| 1977 | ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
|
| 1978 | ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
|
| 1979 | goto out;
|
| 1980 | if (unlikely(f2fs_cp_error(sbi))) {
|
| 1981 | err = -EIO;
|
| 1982 | goto out;
|
| 1983 | }
|
| 1984 |
|
| 1985 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, CP_PHASE_START_BLOCK_OPS);
|
| 1986 |
|
| 1987 | err = block_operations(sbi);
|
| 1988 | if (err)
|
| 1989 | goto out;
|
| 1990 |
|
| 1991 | stat_cp_time(cpc, CP_TIME_OP_LOCK);
|
| 1992 |
|
| 1993 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, CP_PHASE_FINISH_BLOCK_OPS);
|
| 1994 |
|
| 1995 | f2fs_flush_merged_writes(sbi);
|
| 1996 |
|
| 1997 |
|
| 1998 | if (cpc->reason & CP_DISCARD) {
|
| 1999 | if (!f2fs_exist_trim_candidates(sbi, cpc)) {
|
| 2000 | unblock_operations(sbi);
|
| 2001 | goto out;
|
| 2002 | }
|
| 2003 |
|
| 2004 | if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
|
| 2005 | SIT_I(sbi)->dirty_sentries == 0 &&
|
| 2006 | prefree_segments(sbi) == 0) {
|
| 2007 | f2fs_flush_sit_entries(sbi, cpc);
|
| 2008 | f2fs_clear_prefree_segments(sbi, cpc);
|
| 2009 | unblock_operations(sbi);
|
| 2010 | goto out;
|
| 2011 | }
|
| 2012 | }
|
| 2013 | stat_cp_time(cpc, CP_TIME_MERGE_WRITE);
|
| 2014 |
|
| 2015 | |
| 2016 | * update checkpoint pack index
|
| 2017 | * Increase the version number so that
|
| 2018 | * SIT entries and seg summaries are written at correct place
|
| 2019 |
|
| 2020 | ckpt_ver = cur_cp_version(ckpt);
|
| 2021 | ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
|
| 2022 |
|
| 2023 |
|
| 2024 | err = f2fs_flush_nat_entries(sbi, cpc);
|
| 2025 | if (err) {
|
| 2026 | f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err);
|
| 2027 | f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
|
| 2028 | goto stop;
|
| 2029 | }
|
| 2030 | stat_cp_time(cpc, CP_TIME_FLUSH_NAT);
|
| 2031 |
|
| 2032 | f2fs_flush_sit_entries(sbi, cpc);
|
| 2033 |
|
| 2034 | stat_cp_time(cpc, CP_TIME_FLUSH_SIT);
|
| 2035 |
|
| 2036 |
|
| 2037 | f2fs_save_inmem_curseg(sbi);
|
| 2038 |
|
| 2039 | err = do_checkpoint(sbi, cpc);
|
| 2040 | if (err) {
|
| 2041 | f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err);
|
| 2042 | f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
|
| 2043 | f2fs_release_discard_addrs(sbi);
|
| 2044 | } else {
|
| 2045 | f2fs_clear_prefree_segments(sbi, cpc);
|
| 2046 | }
|
| 2047 |
|
| 2048 | f2fs_restore_inmem_curseg(sbi);
|
| 2049 | f2fs_reinit_atgc_curseg(sbi);
|
| 2050 | stat_inc_cp_count(sbi);
|
| 2051 | stop:
|
| 2052 | unblock_operations(sbi);
|
| 2053 | stat_cp_time(cpc, CP_TIME_END);
|
| 2054 | check_cp_time(sbi, cpc);
|
| 2055 |
|
| 2056 | if (cpc->reason & CP_RECOVERY)
|
| 2057 | f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
|
| 2058 |
|
| 2059 |
|
| 2060 | f2fs_update_time(sbi, CP_TIME);
|
| 2061 | trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, CP_PHASE_FINISH_CHECKPOINT);
|
| 2062 | out:
|
| 2063 | if (cpc->reason != CP_RESIZE)
|
| 2064 | f2fs_up_write_trace(&sbi->cp_global_sem, &lc);
|
| 2065 | return err;
|
| 2066 | }
|
| 2067 |
|
| 2068 | void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
|
| 2069 | {
|
| 2070 | int i;
|
| 2071 |
|
| 2072 | for (i = 0; i < MAX_INO_ENTRY; i++) {
|
| 2073 | struct inode_management *im = &sbi->im[i];
|
| 2074 |
|
| 2075 | INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
|
| 2076 | spin_lock_init(&im->ino_lock);
|
| 2077 | INIT_LIST_HEAD(&im->ino_list);
|
| 2078 | im->ino_num = 0;
|
| 2079 | }
|
| 2080 |
|
| 2081 | sbi->max_orphans = (BLKS_PER_SEG(sbi) - F2FS_CP_PACKS -
|
| 2082 | NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
|
| 2083 | F2FS_ORPHANS_PER_BLOCK;
|
| 2084 | }
|
| 2085 |
|
| 2086 | int __init f2fs_create_checkpoint_caches(void)
|
| 2087 | {
|
| 2088 | ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
|
| 2089 | sizeof(struct ino_entry));
|
| 2090 | if (!ino_entry_slab)
|
| 2091 | return -ENOMEM;
|
| 2092 | f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
|
| 2093 | sizeof(struct inode_entry));
|
| 2094 | if (!f2fs_inode_entry_slab) {
|
| 2095 | kmem_cache_destroy(ino_entry_slab);
|
| 2096 | return -ENOMEM;
|
| 2097 | }
|
| 2098 | return 0;
|
| 2099 | }
|
| 2100 |
|
| 2101 | void f2fs_destroy_checkpoint_caches(void)
|
| 2102 | {
|
| 2103 | kmem_cache_destroy(ino_entry_slab);
|
| 2104 | kmem_cache_destroy(f2fs_inode_entry_slab);
|
| 2105 | }
|
| 2106 |
|
| 2107 | static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
|
| 2108 | {
|
| 2109 | struct cp_control cpc = { .reason = CP_SYNC, };
|
| 2110 | struct f2fs_lock_context lc;
|
| 2111 | int err;
|
| 2112 |
|
| 2113 | f2fs_down_write_trace(&sbi->gc_lock, &lc);
|
| 2114 | err = f2fs_write_checkpoint(sbi, &cpc);
|
| 2115 | f2fs_up_write_trace(&sbi->gc_lock, &lc);
|
| 2116 |
|
| 2117 | return err;
|
| 2118 | }
|
| 2119 |
|
| 2120 | static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
|
| 2121 | {
|
| 2122 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2123 | struct ckpt_req *req, *next;
|
| 2124 | struct llist_node *dispatch_list;
|
| 2125 | u64 sum_diff = 0, diff, count = 0;
|
| 2126 | int ret;
|
| 2127 |
|
| 2128 | dispatch_list = llist_del_all(&cprc->issue_list);
|
| 2129 | if (!dispatch_list)
|
| 2130 | return;
|
| 2131 | dispatch_list = llist_reverse_order(dispatch_list);
|
| 2132 |
|
| 2133 | ret = __write_checkpoint_sync(sbi);
|
| 2134 | atomic_inc(&cprc->issued_ckpt);
|
| 2135 |
|
| 2136 | llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
|
| 2137 | diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
|
| 2138 | req->ret = ret;
|
| 2139 | req->delta_time = diff;
|
| 2140 | complete(&req->wait);
|
| 2141 |
|
| 2142 | sum_diff += diff;
|
| 2143 | count++;
|
| 2144 | }
|
| 2145 | atomic_sub(count, &cprc->queued_ckpt);
|
| 2146 | atomic_add(count, &cprc->total_ckpt);
|
| 2147 |
|
| 2148 | spin_lock(&cprc->stat_lock);
|
| 2149 | cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
|
| 2150 | if (cprc->peak_time < cprc->cur_time)
|
| 2151 | cprc->peak_time = cprc->cur_time;
|
| 2152 | spin_unlock(&cprc->stat_lock);
|
| 2153 | }
|
| 2154 |
|
| 2155 | static int issue_checkpoint_thread(void *data)
|
| 2156 | {
|
| 2157 | struct f2fs_sb_info *sbi = data;
|
| 2158 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2159 | wait_queue_head_t *q = &cprc->ckpt_wait_queue;
|
| 2160 | repeat:
|
| 2161 | if (kthread_should_stop())
|
| 2162 | return 0;
|
| 2163 |
|
| 2164 | if (!llist_empty(&cprc->issue_list))
|
| 2165 | __checkpoint_and_complete_reqs(sbi);
|
| 2166 |
|
| 2167 | wait_event_interruptible(*q,
|
| 2168 | kthread_should_stop() || !llist_empty(&cprc->issue_list));
|
| 2169 | goto repeat;
|
| 2170 | }
|
| 2171 |
|
| 2172 | static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
|
| 2173 | struct ckpt_req *wait_req)
|
| 2174 | {
|
| 2175 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2176 |
|
| 2177 | if (!llist_empty(&cprc->issue_list)) {
|
| 2178 | __checkpoint_and_complete_reqs(sbi);
|
| 2179 | } else {
|
| 2180 |
|
| 2181 | if (wait_req)
|
| 2182 | wait_for_completion(&wait_req->wait);
|
| 2183 | }
|
| 2184 | }
|
| 2185 |
|
| 2186 | static void init_ckpt_req(struct ckpt_req *req)
|
| 2187 | {
|
| 2188 | memset(req, 0, sizeof(struct ckpt_req));
|
| 2189 |
|
| 2190 | init_completion(&req->wait);
|
| 2191 | req->queue_time = ktime_get();
|
| 2192 | }
|
| 2193 |
|
| 2194 | int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
|
| 2195 | {
|
| 2196 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2197 | struct ckpt_req req;
|
| 2198 | struct cp_control cpc;
|
| 2199 |
|
| 2200 | cpc.reason = __get_cp_reason(sbi);
|
| 2201 | if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC ||
|
| 2202 | sbi->umount_lock_holder == current) {
|
| 2203 | struct f2fs_lock_context lc;
|
| 2204 | int ret;
|
| 2205 |
|
| 2206 | f2fs_down_write_trace(&sbi->gc_lock, &lc);
|
| 2207 | ret = f2fs_write_checkpoint(sbi, &cpc);
|
| 2208 | f2fs_up_write_trace(&sbi->gc_lock, &lc);
|
| 2209 |
|
| 2210 | return ret;
|
| 2211 | }
|
| 2212 |
|
| 2213 | if (!cprc->f2fs_issue_ckpt)
|
| 2214 | return __write_checkpoint_sync(sbi);
|
| 2215 |
|
| 2216 | init_ckpt_req(&req);
|
| 2217 |
|
| 2218 | llist_add(&req.llnode, &cprc->issue_list);
|
| 2219 | atomic_inc(&cprc->queued_ckpt);
|
| 2220 |
|
| 2221 | |
| 2222 | * update issue_list before we wake up issue_checkpoint thread,
|
| 2223 | * this smp_mb() pairs with another barrier in ___wait_event(),
|
| 2224 | * see more details in comments of waitqueue_active().
|
| 2225 |
|
| 2226 | smp_mb();
|
| 2227 |
|
| 2228 | if (waitqueue_active(&cprc->ckpt_wait_queue))
|
| 2229 | wake_up(&cprc->ckpt_wait_queue);
|
| 2230 |
|
| 2231 | if (cprc->f2fs_issue_ckpt)
|
| 2232 | wait_for_completion(&req.wait);
|
| 2233 | else
|
| 2234 | flush_remained_ckpt_reqs(sbi, &req);
|
| 2235 |
|
| 2236 | if (unlikely(req.delta_time >= CP_LONG_LATENCY_THRESHOLD)) {
|
| 2237 | f2fs_warn_ratelimited(sbi,
|
| 2238 | "blocked on checkpoint for %u ms", cprc->peak_time);
|
| 2239 | dump_stack();
|
| 2240 | }
|
| 2241 |
|
| 2242 | return req.ret;
|
| 2243 | }
|
| 2244 |
|
| 2245 | int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
|
| 2246 | {
|
| 2247 | dev_t dev = sbi->sb->s_bdev->bd_dev;
|
| 2248 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2249 |
|
| 2250 | if (cprc->f2fs_issue_ckpt)
|
| 2251 | return 0;
|
| 2252 |
|
| 2253 | cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
|
| 2254 | "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
|
| 2255 | if (IS_ERR(cprc->f2fs_issue_ckpt)) {
|
| 2256 | int err = PTR_ERR(cprc->f2fs_issue_ckpt);
|
| 2257 |
|
| 2258 | cprc->f2fs_issue_ckpt = NULL;
|
| 2259 | return err;
|
| 2260 | }
|
| 2261 |
|
| 2262 | set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
|
| 2263 | set_user_nice(cprc->f2fs_issue_ckpt,
|
| 2264 | PRIO_TO_NICE(sbi->critical_task_priority));
|
| 2265 |
|
| 2266 | return 0;
|
| 2267 | }
|
| 2268 |
|
| 2269 | void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
|
| 2270 | {
|
| 2271 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2272 | struct task_struct *ckpt_task;
|
| 2273 |
|
| 2274 | if (!cprc->f2fs_issue_ckpt)
|
| 2275 | return;
|
| 2276 |
|
| 2277 | ckpt_task = cprc->f2fs_issue_ckpt;
|
| 2278 | cprc->f2fs_issue_ckpt = NULL;
|
| 2279 | kthread_stop(ckpt_task);
|
| 2280 |
|
| 2281 | f2fs_flush_ckpt_thread(sbi);
|
| 2282 | }
|
| 2283 |
|
| 2284 | void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
|
| 2285 | {
|
| 2286 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2287 |
|
| 2288 | flush_remained_ckpt_reqs(sbi, NULL);
|
| 2289 |
|
| 2290 |
|
| 2291 | while (atomic_read(&cprc->queued_ckpt))
|
| 2292 | io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
|
| 2293 | }
|
| 2294 |
|
| 2295 | void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
|
| 2296 | {
|
| 2297 | struct ckpt_req_control *cprc = &sbi->cprc_info;
|
| 2298 |
|
| 2299 | atomic_set(&cprc->issued_ckpt, 0);
|
| 2300 | atomic_set(&cprc->total_ckpt, 0);
|
| 2301 | atomic_set(&cprc->queued_ckpt, 0);
|
| 2302 | cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
|
| 2303 | init_waitqueue_head(&cprc->ckpt_wait_queue);
|
| 2304 | init_llist_head(&cprc->issue_list);
|
| 2305 | spin_lock_init(&cprc->stat_lock);
|
| 2306 | }
|