1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/node.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/mpage.h> 11 #include <linux/backing-dev.h> 12 #include <linux/blkdev.h> 13 #include <linux/pagevec.h> 14 #include <linux/swap.h> 15 16 #include "f2fs.h" 17 #include "node.h" 18 #include "segment.h" 19 #include "xattr.h" 20 #include <trace/events/f2fs.h> 21 22 #define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock) 23 24 static struct kmem_cache *nat_entry_slab; 25 static struct kmem_cache *free_nid_slab; 26 static struct kmem_cache *nat_entry_set_slab; 27 static struct kmem_cache *fsync_node_entry_slab; 28 29 /* 30 * Check whether the given nid is within node id range. 31 */ 32 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid) 33 { 34 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) { 35 set_sbi_flag(sbi, SBI_NEED_FSCK); 36 f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.", 37 __func__, nid); 38 return -EFSCORRUPTED; 39 } 40 return 0; 41 } 42 43 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type) 44 { 45 struct f2fs_nm_info *nm_i = NM_I(sbi); 46 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 47 struct sysinfo val; 48 unsigned long avail_ram; 49 unsigned long mem_size = 0; 50 bool res = false; 51 52 if (!nm_i) 53 return true; 54 55 si_meminfo(&val); 56 57 /* only uses low memory */ 58 avail_ram = val.totalram - val.totalhigh; 59 60 /* 61 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively 62 */ 63 if (type == FREE_NIDS) { 64 mem_size = (nm_i->nid_cnt[FREE_NID] * 65 sizeof(struct free_nid)) >> PAGE_SHIFT; 66 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); 67 } else if (type == NAT_ENTRIES) { 68 mem_size = (nm_i->nat_cnt[TOTAL_NAT] * 69 sizeof(struct nat_entry)) >> PAGE_SHIFT; 70 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); 71 if (excess_cached_nats(sbi)) 72 res = false; 73 } else if (type == DIRTY_DENTS) { 74 if (sbi->sb->s_bdi->wb.dirty_exceeded) 75 return false; 76 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS); 77 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); 78 } else if (type == INO_ENTRIES) { 79 int i; 80 81 for (i = 0; i < MAX_INO_ENTRY; i++) 82 mem_size += sbi->im[i].ino_num * 83 sizeof(struct ino_entry); 84 mem_size >>= PAGE_SHIFT; 85 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); 86 } else if (type == EXTENT_CACHE) { 87 mem_size = (atomic_read(&sbi->total_ext_tree) * 88 sizeof(struct extent_tree) + 89 atomic_read(&sbi->total_ext_node) * 90 sizeof(struct extent_node)) >> PAGE_SHIFT; 91 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); 92 } else if (type == INMEM_PAGES) { 93 /* it allows 20% / total_ram for inmemory pages */ 94 mem_size = get_pages(sbi, F2FS_INMEM_PAGES); 95 res = mem_size < (val.totalram / 5); 96 } else if (type == DISCARD_CACHE) { 97 mem_size = (atomic_read(&dcc->discard_cmd_cnt) * 98 sizeof(struct discard_cmd)) >> PAGE_SHIFT; 99 res = mem_size < (avail_ram * nm_i->ram_thresh / 100); 100 } else if (type == COMPRESS_PAGE) { 101 #ifdef CONFIG_F2FS_FS_COMPRESSION 102 unsigned long free_ram = val.freeram; 103 104 /* 105 * free memory is lower than watermark or cached page count 106 * exceed threshold, deny caching compress page. 107 */ 108 res = (free_ram > avail_ram * sbi->compress_watermark / 100) && 109 (COMPRESS_MAPPING(sbi)->nrpages < 110 free_ram * sbi->compress_percent / 100); 111 #else 112 res = false; 113 #endif 114 } else { 115 if (!sbi->sb->s_bdi->wb.dirty_exceeded) 116 return true; 117 } 118 return res; 119 } 120 121 static void clear_node_page_dirty(struct page *page) 122 { 123 if (PageDirty(page)) { 124 f2fs_clear_page_cache_dirty_tag(page); 125 clear_page_dirty_for_io(page); 126 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); 127 } 128 ClearPageUptodate(page); 129 } 130 131 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 132 { 133 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid)); 134 } 135 136 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 137 { 138 struct page *src_page; 139 struct page *dst_page; 140 pgoff_t dst_off; 141 void *src_addr; 142 void *dst_addr; 143 struct f2fs_nm_info *nm_i = NM_I(sbi); 144 145 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid)); 146 147 /* get current nat block page with lock */ 148 src_page = get_current_nat_page(sbi, nid); 149 if (IS_ERR(src_page)) 150 return src_page; 151 dst_page = f2fs_grab_meta_page(sbi, dst_off); 152 f2fs_bug_on(sbi, PageDirty(src_page)); 153 154 src_addr = page_address(src_page); 155 dst_addr = page_address(dst_page); 156 memcpy(dst_addr, src_addr, PAGE_SIZE); 157 set_page_dirty(dst_page); 158 f2fs_put_page(src_page, 1); 159 160 set_to_next_nat(nm_i, nid); 161 162 return dst_page; 163 } 164 165 static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail) 166 { 167 struct nat_entry *new; 168 169 if (no_fail) 170 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO); 171 else 172 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO); 173 if (new) { 174 nat_set_nid(new, nid); 175 nat_reset_flag(new); 176 } 177 return new; 178 } 179 180 static void __free_nat_entry(struct nat_entry *e) 181 { 182 kmem_cache_free(nat_entry_slab, e); 183 } 184 185 /* must be locked by nat_tree_lock */ 186 static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i, 187 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail) 188 { 189 if (no_fail) 190 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne); 191 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne)) 192 return NULL; 193 194 if (raw_ne) 195 node_info_from_raw_nat(&ne->ni, raw_ne); 196 197 spin_lock(&nm_i->nat_list_lock); 198 list_add_tail(&ne->list, &nm_i->nat_entries); 199 spin_unlock(&nm_i->nat_list_lock); 200 201 nm_i->nat_cnt[TOTAL_NAT]++; 202 nm_i->nat_cnt[RECLAIMABLE_NAT]++; 203 return ne; 204 } 205 206 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n) 207 { 208 struct nat_entry *ne; 209 210 ne = radix_tree_lookup(&nm_i->nat_root, n); 211 212 /* for recent accessed nat entry, move it to tail of lru list */ 213 if (ne && !get_nat_flag(ne, IS_DIRTY)) { 214 spin_lock(&nm_i->nat_list_lock); 215 if (!list_empty(&ne->list)) 216 list_move_tail(&ne->list, &nm_i->nat_entries); 217 spin_unlock(&nm_i->nat_list_lock); 218 } 219 220 return ne; 221 } 222 223 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i, 224 nid_t start, unsigned int nr, struct nat_entry **ep) 225 { 226 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr); 227 } 228 229 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) 230 { 231 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); 232 nm_i->nat_cnt[TOTAL_NAT]--; 233 nm_i->nat_cnt[RECLAIMABLE_NAT]--; 234 __free_nat_entry(e); 235 } 236 237 static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i, 238 struct nat_entry *ne) 239 { 240 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid); 241 struct nat_entry_set *head; 242 243 head = radix_tree_lookup(&nm_i->nat_set_root, set); 244 if (!head) { 245 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS); 246 247 INIT_LIST_HEAD(&head->entry_list); 248 INIT_LIST_HEAD(&head->set_list); 249 head->set = set; 250 head->entry_cnt = 0; 251 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head); 252 } 253 return head; 254 } 255 256 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i, 257 struct nat_entry *ne) 258 { 259 struct nat_entry_set *head; 260 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR; 261 262 if (!new_ne) 263 head = __grab_nat_entry_set(nm_i, ne); 264 265 /* 266 * update entry_cnt in below condition: 267 * 1. update NEW_ADDR to valid block address; 268 * 2. update old block address to new one; 269 */ 270 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) || 271 !get_nat_flag(ne, IS_DIRTY))) 272 head->entry_cnt++; 273 274 set_nat_flag(ne, IS_PREALLOC, new_ne); 275 276 if (get_nat_flag(ne, IS_DIRTY)) 277 goto refresh_list; 278 279 nm_i->nat_cnt[DIRTY_NAT]++; 280 nm_i->nat_cnt[RECLAIMABLE_NAT]--; 281 set_nat_flag(ne, IS_DIRTY, true); 282 refresh_list: 283 spin_lock(&nm_i->nat_list_lock); 284 if (new_ne) 285 list_del_init(&ne->list); 286 else 287 list_move_tail(&ne->list, &head->entry_list); 288 spin_unlock(&nm_i->nat_list_lock); 289 } 290 291 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i, 292 struct nat_entry_set *set, struct nat_entry *ne) 293 { 294 spin_lock(&nm_i->nat_list_lock); 295 list_move_tail(&ne->list, &nm_i->nat_entries); 296 spin_unlock(&nm_i->nat_list_lock); 297 298 set_nat_flag(ne, IS_DIRTY, false); 299 set->entry_cnt--; 300 nm_i->nat_cnt[DIRTY_NAT]--; 301 nm_i->nat_cnt[RECLAIMABLE_NAT]++; 302 } 303 304 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, 305 nid_t start, unsigned int nr, struct nat_entry_set **ep) 306 { 307 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep, 308 start, nr); 309 } 310 311 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page) 312 { 313 return NODE_MAPPING(sbi) == page->mapping && 314 IS_DNODE(page) && is_cold_node(page); 315 } 316 317 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi) 318 { 319 spin_lock_init(&sbi->fsync_node_lock); 320 INIT_LIST_HEAD(&sbi->fsync_node_list); 321 sbi->fsync_seg_id = 0; 322 sbi->fsync_node_num = 0; 323 } 324 325 static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi, 326 struct page *page) 327 { 328 struct fsync_node_entry *fn; 329 unsigned long flags; 330 unsigned int seq_id; 331 332 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS); 333 334 get_page(page); 335 fn->page = page; 336 INIT_LIST_HEAD(&fn->list); 337 338 spin_lock_irqsave(&sbi->fsync_node_lock, flags); 339 list_add_tail(&fn->list, &sbi->fsync_node_list); 340 fn->seq_id = sbi->fsync_seg_id++; 341 seq_id = fn->seq_id; 342 sbi->fsync_node_num++; 343 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 344 345 return seq_id; 346 } 347 348 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page) 349 { 350 struct fsync_node_entry *fn; 351 unsigned long flags; 352 353 spin_lock_irqsave(&sbi->fsync_node_lock, flags); 354 list_for_each_entry(fn, &sbi->fsync_node_list, list) { 355 if (fn->page == page) { 356 list_del(&fn->list); 357 sbi->fsync_node_num--; 358 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 359 kmem_cache_free(fsync_node_entry_slab, fn); 360 put_page(page); 361 return; 362 } 363 } 364 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 365 f2fs_bug_on(sbi, 1); 366 } 367 368 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi) 369 { 370 unsigned long flags; 371 372 spin_lock_irqsave(&sbi->fsync_node_lock, flags); 373 sbi->fsync_seg_id = 0; 374 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 375 } 376 377 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid) 378 { 379 struct f2fs_nm_info *nm_i = NM_I(sbi); 380 struct nat_entry *e; 381 bool need = false; 382 383 down_read(&nm_i->nat_tree_lock); 384 e = __lookup_nat_cache(nm_i, nid); 385 if (e) { 386 if (!get_nat_flag(e, IS_CHECKPOINTED) && 387 !get_nat_flag(e, HAS_FSYNCED_INODE)) 388 need = true; 389 } 390 up_read(&nm_i->nat_tree_lock); 391 return need; 392 } 393 394 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid) 395 { 396 struct f2fs_nm_info *nm_i = NM_I(sbi); 397 struct nat_entry *e; 398 bool is_cp = true; 399 400 down_read(&nm_i->nat_tree_lock); 401 e = __lookup_nat_cache(nm_i, nid); 402 if (e && !get_nat_flag(e, IS_CHECKPOINTED)) 403 is_cp = false; 404 up_read(&nm_i->nat_tree_lock); 405 return is_cp; 406 } 407 408 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino) 409 { 410 struct f2fs_nm_info *nm_i = NM_I(sbi); 411 struct nat_entry *e; 412 bool need_update = true; 413 414 down_read(&nm_i->nat_tree_lock); 415 e = __lookup_nat_cache(nm_i, ino); 416 if (e && get_nat_flag(e, HAS_LAST_FSYNC) && 417 (get_nat_flag(e, IS_CHECKPOINTED) || 418 get_nat_flag(e, HAS_FSYNCED_INODE))) 419 need_update = false; 420 up_read(&nm_i->nat_tree_lock); 421 return need_update; 422 } 423 424 /* must be locked by nat_tree_lock */ 425 static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, 426 struct f2fs_nat_entry *ne) 427 { 428 struct f2fs_nm_info *nm_i = NM_I(sbi); 429 struct nat_entry *new, *e; 430 431 new = __alloc_nat_entry(nid, false); 432 if (!new) 433 return; 434 435 down_write(&nm_i->nat_tree_lock); 436 e = __lookup_nat_cache(nm_i, nid); 437 if (!e) 438 e = __init_nat_entry(nm_i, new, ne, false); 439 else 440 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) || 441 nat_get_blkaddr(e) != 442 le32_to_cpu(ne->block_addr) || 443 nat_get_version(e) != ne->version); 444 up_write(&nm_i->nat_tree_lock); 445 if (e != new) 446 __free_nat_entry(new); 447 } 448 449 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni, 450 block_t new_blkaddr, bool fsync_done) 451 { 452 struct f2fs_nm_info *nm_i = NM_I(sbi); 453 struct nat_entry *e; 454 struct nat_entry *new = __alloc_nat_entry(ni->nid, true); 455 456 down_write(&nm_i->nat_tree_lock); 457 e = __lookup_nat_cache(nm_i, ni->nid); 458 if (!e) { 459 e = __init_nat_entry(nm_i, new, NULL, true); 460 copy_node_info(&e->ni, ni); 461 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR); 462 } else if (new_blkaddr == NEW_ADDR) { 463 /* 464 * when nid is reallocated, 465 * previous nat entry can be remained in nat cache. 466 * So, reinitialize it with new information. 467 */ 468 copy_node_info(&e->ni, ni); 469 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR); 470 } 471 /* let's free early to reduce memory consumption */ 472 if (e != new) 473 __free_nat_entry(new); 474 475 /* sanity check */ 476 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr); 477 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR && 478 new_blkaddr == NULL_ADDR); 479 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR && 480 new_blkaddr == NEW_ADDR); 481 f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) && 482 new_blkaddr == NEW_ADDR); 483 484 /* increment version no as node is removed */ 485 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) { 486 unsigned char version = nat_get_version(e); 487 488 nat_set_version(e, inc_node_version(version)); 489 } 490 491 /* change address */ 492 nat_set_blkaddr(e, new_blkaddr); 493 if (!__is_valid_data_blkaddr(new_blkaddr)) 494 set_nat_flag(e, IS_CHECKPOINTED, false); 495 __set_nat_cache_dirty(nm_i, e); 496 497 /* update fsync_mark if its inode nat entry is still alive */ 498 if (ni->nid != ni->ino) 499 e = __lookup_nat_cache(nm_i, ni->ino); 500 if (e) { 501 if (fsync_done && ni->nid == ni->ino) 502 set_nat_flag(e, HAS_FSYNCED_INODE, true); 503 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done); 504 } 505 up_write(&nm_i->nat_tree_lock); 506 } 507 508 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink) 509 { 510 struct f2fs_nm_info *nm_i = NM_I(sbi); 511 int nr = nr_shrink; 512 513 if (!down_write_trylock(&nm_i->nat_tree_lock)) 514 return 0; 515 516 spin_lock(&nm_i->nat_list_lock); 517 while (nr_shrink) { 518 struct nat_entry *ne; 519 520 if (list_empty(&nm_i->nat_entries)) 521 break; 522 523 ne = list_first_entry(&nm_i->nat_entries, 524 struct nat_entry, list); 525 list_del(&ne->list); 526 spin_unlock(&nm_i->nat_list_lock); 527 528 __del_from_nat_cache(nm_i, ne); 529 nr_shrink--; 530 531 spin_lock(&nm_i->nat_list_lock); 532 } 533 spin_unlock(&nm_i->nat_list_lock); 534 535 up_write(&nm_i->nat_tree_lock); 536 return nr - nr_shrink; 537 } 538 539 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid, 540 struct node_info *ni) 541 { 542 struct f2fs_nm_info *nm_i = NM_I(sbi); 543 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 544 struct f2fs_journal *journal = curseg->journal; 545 nid_t start_nid = START_NID(nid); 546 struct f2fs_nat_block *nat_blk; 547 struct page *page = NULL; 548 struct f2fs_nat_entry ne; 549 struct nat_entry *e; 550 pgoff_t index; 551 block_t blkaddr; 552 int i; 553 554 ni->nid = nid; 555 556 /* Check nat cache */ 557 down_read(&nm_i->nat_tree_lock); 558 e = __lookup_nat_cache(nm_i, nid); 559 if (e) { 560 ni->ino = nat_get_ino(e); 561 ni->blk_addr = nat_get_blkaddr(e); 562 ni->version = nat_get_version(e); 563 up_read(&nm_i->nat_tree_lock); 564 return 0; 565 } 566 567 memset(&ne, 0, sizeof(struct f2fs_nat_entry)); 568 569 /* Check current segment summary */ 570 down_read(&curseg->journal_rwsem); 571 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0); 572 if (i >= 0) { 573 ne = nat_in_journal(journal, i); 574 node_info_from_raw_nat(ni, &ne); 575 } 576 up_read(&curseg->journal_rwsem); 577 if (i >= 0) { 578 up_read(&nm_i->nat_tree_lock); 579 goto cache; 580 } 581 582 /* Fill node_info from nat page */ 583 index = current_nat_addr(sbi, nid); 584 up_read(&nm_i->nat_tree_lock); 585 586 page = f2fs_get_meta_page(sbi, index); 587 if (IS_ERR(page)) 588 return PTR_ERR(page); 589 590 nat_blk = (struct f2fs_nat_block *)page_address(page); 591 ne = nat_blk->entries[nid - start_nid]; 592 node_info_from_raw_nat(ni, &ne); 593 f2fs_put_page(page, 1); 594 cache: 595 blkaddr = le32_to_cpu(ne.block_addr); 596 if (__is_valid_data_blkaddr(blkaddr) && 597 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) 598 return -EFAULT; 599 600 /* cache nat entry */ 601 cache_nat_entry(sbi, nid, &ne); 602 return 0; 603 } 604 605 /* 606 * readahead MAX_RA_NODE number of node pages. 607 */ 608 static void f2fs_ra_node_pages(struct page *parent, int start, int n) 609 { 610 struct f2fs_sb_info *sbi = F2FS_P_SB(parent); 611 struct blk_plug plug; 612 int i, end; 613 nid_t nid; 614 615 blk_start_plug(&plug); 616 617 /* Then, try readahead for siblings of the desired node */ 618 end = start + n; 619 end = min(end, NIDS_PER_BLOCK); 620 for (i = start; i < end; i++) { 621 nid = get_nid(parent, i, false); 622 f2fs_ra_node_page(sbi, nid); 623 } 624 625 blk_finish_plug(&plug); 626 } 627 628 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs) 629 { 630 const long direct_index = ADDRS_PER_INODE(dn->inode); 631 const long direct_blks = ADDRS_PER_BLOCK(dn->inode); 632 const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK; 633 unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode); 634 int cur_level = dn->cur_level; 635 int max_level = dn->max_level; 636 pgoff_t base = 0; 637 638 if (!dn->max_level) 639 return pgofs + 1; 640 641 while (max_level-- > cur_level) 642 skipped_unit *= NIDS_PER_BLOCK; 643 644 switch (dn->max_level) { 645 case 3: 646 base += 2 * indirect_blks; 647 fallthrough; 648 case 2: 649 base += 2 * direct_blks; 650 fallthrough; 651 case 1: 652 base += direct_index; 653 break; 654 default: 655 f2fs_bug_on(F2FS_I_SB(dn->inode), 1); 656 } 657 658 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base; 659 } 660 661 /* 662 * The maximum depth is four. 663 * Offset[0] will have raw inode offset. 664 */ 665 static int get_node_path(struct inode *inode, long block, 666 int offset[4], unsigned int noffset[4]) 667 { 668 const long direct_index = ADDRS_PER_INODE(inode); 669 const long direct_blks = ADDRS_PER_BLOCK(inode); 670 const long dptrs_per_blk = NIDS_PER_BLOCK; 671 const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK; 672 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK; 673 int n = 0; 674 int level = 0; 675 676 noffset[0] = 0; 677 678 if (block < direct_index) { 679 offset[n] = block; 680 goto got; 681 } 682 block -= direct_index; 683 if (block < direct_blks) { 684 offset[n++] = NODE_DIR1_BLOCK; 685 noffset[n] = 1; 686 offset[n] = block; 687 level = 1; 688 goto got; 689 } 690 block -= direct_blks; 691 if (block < direct_blks) { 692 offset[n++] = NODE_DIR2_BLOCK; 693 noffset[n] = 2; 694 offset[n] = block; 695 level = 1; 696 goto got; 697 } 698 block -= direct_blks; 699 if (block < indirect_blks) { 700 offset[n++] = NODE_IND1_BLOCK; 701 noffset[n] = 3; 702 offset[n++] = block / direct_blks; 703 noffset[n] = 4 + offset[n - 1]; 704 offset[n] = block % direct_blks; 705 level = 2; 706 goto got; 707 } 708 block -= indirect_blks; 709 if (block < indirect_blks) { 710 offset[n++] = NODE_IND2_BLOCK; 711 noffset[n] = 4 + dptrs_per_blk; 712 offset[n++] = block / direct_blks; 713 noffset[n] = 5 + dptrs_per_blk + offset[n - 1]; 714 offset[n] = block % direct_blks; 715 level = 2; 716 goto got; 717 } 718 block -= indirect_blks; 719 if (block < dindirect_blks) { 720 offset[n++] = NODE_DIND_BLOCK; 721 noffset[n] = 5 + (dptrs_per_blk * 2); 722 offset[n++] = block / indirect_blks; 723 noffset[n] = 6 + (dptrs_per_blk * 2) + 724 offset[n - 1] * (dptrs_per_blk + 1); 725 offset[n++] = (block / direct_blks) % dptrs_per_blk; 726 noffset[n] = 7 + (dptrs_per_blk * 2) + 727 offset[n - 2] * (dptrs_per_blk + 1) + 728 offset[n - 1]; 729 offset[n] = block % direct_blks; 730 level = 3; 731 goto got; 732 } else { 733 return -E2BIG; 734 } 735 got: 736 return level; 737 } 738 739 /* 740 * Caller should call f2fs_put_dnode(dn). 741 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and 742 * f2fs_unlock_op() only if mode is set with ALLOC_NODE. 743 */ 744 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode) 745 { 746 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 747 struct page *npage[4]; 748 struct page *parent = NULL; 749 int offset[4]; 750 unsigned int noffset[4]; 751 nid_t nids[4]; 752 int level, i = 0; 753 int err = 0; 754 755 level = get_node_path(dn->inode, index, offset, noffset); 756 if (level < 0) 757 return level; 758 759 nids[0] = dn->inode->i_ino; 760 npage[0] = dn->inode_page; 761 762 if (!npage[0]) { 763 npage[0] = f2fs_get_node_page(sbi, nids[0]); 764 if (IS_ERR(npage[0])) 765 return PTR_ERR(npage[0]); 766 } 767 768 /* if inline_data is set, should not report any block indices */ 769 if (f2fs_has_inline_data(dn->inode) && index) { 770 err = -ENOENT; 771 f2fs_put_page(npage[0], 1); 772 goto release_out; 773 } 774 775 parent = npage[0]; 776 if (level != 0) 777 nids[1] = get_nid(parent, offset[0], true); 778 dn->inode_page = npage[0]; 779 dn->inode_page_locked = true; 780 781 /* get indirect or direct nodes */ 782 for (i = 1; i <= level; i++) { 783 bool done = false; 784 785 if (!nids[i] && mode == ALLOC_NODE) { 786 /* alloc new node */ 787 if (!f2fs_alloc_nid(sbi, &(nids[i]))) { 788 err = -ENOSPC; 789 goto release_pages; 790 } 791 792 dn->nid = nids[i]; 793 npage[i] = f2fs_new_node_page(dn, noffset[i]); 794 if (IS_ERR(npage[i])) { 795 f2fs_alloc_nid_failed(sbi, nids[i]); 796 err = PTR_ERR(npage[i]); 797 goto release_pages; 798 } 799 800 set_nid(parent, offset[i - 1], nids[i], i == 1); 801 f2fs_alloc_nid_done(sbi, nids[i]); 802 done = true; 803 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) { 804 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]); 805 if (IS_ERR(npage[i])) { 806 err = PTR_ERR(npage[i]); 807 goto release_pages; 808 } 809 done = true; 810 } 811 if (i == 1) { 812 dn->inode_page_locked = false; 813 unlock_page(parent); 814 } else { 815 f2fs_put_page(parent, 1); 816 } 817 818 if (!done) { 819 npage[i] = f2fs_get_node_page(sbi, nids[i]); 820 if (IS_ERR(npage[i])) { 821 err = PTR_ERR(npage[i]); 822 f2fs_put_page(npage[0], 0); 823 goto release_out; 824 } 825 } 826 if (i < level) { 827 parent = npage[i]; 828 nids[i + 1] = get_nid(parent, offset[i], false); 829 } 830 } 831 dn->nid = nids[level]; 832 dn->ofs_in_node = offset[level]; 833 dn->node_page = npage[level]; 834 dn->data_blkaddr = f2fs_data_blkaddr(dn); 835 return 0; 836 837 release_pages: 838 f2fs_put_page(parent, 1); 839 if (i > 1) 840 f2fs_put_page(npage[0], 0); 841 release_out: 842 dn->inode_page = NULL; 843 dn->node_page = NULL; 844 if (err == -ENOENT) { 845 dn->cur_level = i; 846 dn->max_level = level; 847 dn->ofs_in_node = offset[level]; 848 } 849 return err; 850 } 851 852 static int truncate_node(struct dnode_of_data *dn) 853 { 854 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 855 struct node_info ni; 856 int err; 857 pgoff_t index; 858 859 err = f2fs_get_node_info(sbi, dn->nid, &ni); 860 if (err) 861 return err; 862 863 /* Deallocate node address */ 864 f2fs_invalidate_blocks(sbi, ni.blk_addr); 865 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino); 866 set_node_addr(sbi, &ni, NULL_ADDR, false); 867 868 if (dn->nid == dn->inode->i_ino) { 869 f2fs_remove_orphan_inode(sbi, dn->nid); 870 dec_valid_inode_count(sbi); 871 f2fs_inode_synced(dn->inode); 872 } 873 874 clear_node_page_dirty(dn->node_page); 875 set_sbi_flag(sbi, SBI_IS_DIRTY); 876 877 index = dn->node_page->index; 878 f2fs_put_page(dn->node_page, 1); 879 880 invalidate_mapping_pages(NODE_MAPPING(sbi), 881 index, index); 882 883 dn->node_page = NULL; 884 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr); 885 886 return 0; 887 } 888 889 static int truncate_dnode(struct dnode_of_data *dn) 890 { 891 struct page *page; 892 int err; 893 894 if (dn->nid == 0) 895 return 1; 896 897 /* get direct node */ 898 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid); 899 if (PTR_ERR(page) == -ENOENT) 900 return 1; 901 else if (IS_ERR(page)) 902 return PTR_ERR(page); 903 904 /* Make dnode_of_data for parameter */ 905 dn->node_page = page; 906 dn->ofs_in_node = 0; 907 f2fs_truncate_data_blocks(dn); 908 err = truncate_node(dn); 909 if (err) 910 return err; 911 912 return 1; 913 } 914 915 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs, 916 int ofs, int depth) 917 { 918 struct dnode_of_data rdn = *dn; 919 struct page *page; 920 struct f2fs_node *rn; 921 nid_t child_nid; 922 unsigned int child_nofs; 923 int freed = 0; 924 int i, ret; 925 926 if (dn->nid == 0) 927 return NIDS_PER_BLOCK + 1; 928 929 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr); 930 931 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid); 932 if (IS_ERR(page)) { 933 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page)); 934 return PTR_ERR(page); 935 } 936 937 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK); 938 939 rn = F2FS_NODE(page); 940 if (depth < 3) { 941 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) { 942 child_nid = le32_to_cpu(rn->in.nid[i]); 943 if (child_nid == 0) 944 continue; 945 rdn.nid = child_nid; 946 ret = truncate_dnode(&rdn); 947 if (ret < 0) 948 goto out_err; 949 if (set_nid(page, i, 0, false)) 950 dn->node_changed = true; 951 } 952 } else { 953 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1; 954 for (i = ofs; i < NIDS_PER_BLOCK; i++) { 955 child_nid = le32_to_cpu(rn->in.nid[i]); 956 if (child_nid == 0) { 957 child_nofs += NIDS_PER_BLOCK + 1; 958 continue; 959 } 960 rdn.nid = child_nid; 961 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1); 962 if (ret == (NIDS_PER_BLOCK + 1)) { 963 if (set_nid(page, i, 0, false)) 964 dn->node_changed = true; 965 child_nofs += ret; 966 } else if (ret < 0 && ret != -ENOENT) { 967 goto out_err; 968 } 969 } 970 freed = child_nofs; 971 } 972 973 if (!ofs) { 974 /* remove current indirect node */ 975 dn->node_page = page; 976 ret = truncate_node(dn); 977 if (ret) 978 goto out_err; 979 freed++; 980 } else { 981 f2fs_put_page(page, 1); 982 } 983 trace_f2fs_truncate_nodes_exit(dn->inode, freed); 984 return freed; 985 986 out_err: 987 f2fs_put_page(page, 1); 988 trace_f2fs_truncate_nodes_exit(dn->inode, ret); 989 return ret; 990 } 991 992 static int truncate_partial_nodes(struct dnode_of_data *dn, 993 struct f2fs_inode *ri, int *offset, int depth) 994 { 995 struct page *pages[2]; 996 nid_t nid[3]; 997 nid_t child_nid; 998 int err = 0; 999 int i; 1000 int idx = depth - 2; 1001 1002 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); 1003 if (!nid[0]) 1004 return 0; 1005 1006 /* get indirect nodes in the path */ 1007 for (i = 0; i < idx + 1; i++) { 1008 /* reference count'll be increased */ 1009 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]); 1010 if (IS_ERR(pages[i])) { 1011 err = PTR_ERR(pages[i]); 1012 idx = i - 1; 1013 goto fail; 1014 } 1015 nid[i + 1] = get_nid(pages[i], offset[i + 1], false); 1016 } 1017 1018 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK); 1019 1020 /* free direct nodes linked to a partial indirect node */ 1021 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) { 1022 child_nid = get_nid(pages[idx], i, false); 1023 if (!child_nid) 1024 continue; 1025 dn->nid = child_nid; 1026 err = truncate_dnode(dn); 1027 if (err < 0) 1028 goto fail; 1029 if (set_nid(pages[idx], i, 0, false)) 1030 dn->node_changed = true; 1031 } 1032 1033 if (offset[idx + 1] == 0) { 1034 dn->node_page = pages[idx]; 1035 dn->nid = nid[idx]; 1036 err = truncate_node(dn); 1037 if (err) 1038 goto fail; 1039 } else { 1040 f2fs_put_page(pages[idx], 1); 1041 } 1042 offset[idx]++; 1043 offset[idx + 1] = 0; 1044 idx--; 1045 fail: 1046 for (i = idx; i >= 0; i--) 1047 f2fs_put_page(pages[i], 1); 1048 1049 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err); 1050 1051 return err; 1052 } 1053 1054 /* 1055 * All the block addresses of data and nodes should be nullified. 1056 */ 1057 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from) 1058 { 1059 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1060 int err = 0, cont = 1; 1061 int level, offset[4], noffset[4]; 1062 unsigned int nofs = 0; 1063 struct f2fs_inode *ri; 1064 struct dnode_of_data dn; 1065 struct page *page; 1066 1067 trace_f2fs_truncate_inode_blocks_enter(inode, from); 1068 1069 level = get_node_path(inode, from, offset, noffset); 1070 if (level < 0) { 1071 trace_f2fs_truncate_inode_blocks_exit(inode, level); 1072 return level; 1073 } 1074 1075 page = f2fs_get_node_page(sbi, inode->i_ino); 1076 if (IS_ERR(page)) { 1077 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page)); 1078 return PTR_ERR(page); 1079 } 1080 1081 set_new_dnode(&dn, inode, page, NULL, 0); 1082 unlock_page(page); 1083 1084 ri = F2FS_INODE(page); 1085 switch (level) { 1086 case 0: 1087 case 1: 1088 nofs = noffset[1]; 1089 break; 1090 case 2: 1091 nofs = noffset[1]; 1092 if (!offset[level - 1]) 1093 goto skip_partial; 1094 err = truncate_partial_nodes(&dn, ri, offset, level); 1095 if (err < 0 && err != -ENOENT) 1096 goto fail; 1097 nofs += 1 + NIDS_PER_BLOCK; 1098 break; 1099 case 3: 1100 nofs = 5 + 2 * NIDS_PER_BLOCK; 1101 if (!offset[level - 1]) 1102 goto skip_partial; 1103 err = truncate_partial_nodes(&dn, ri, offset, level); 1104 if (err < 0 && err != -ENOENT) 1105 goto fail; 1106 break; 1107 default: 1108 BUG(); 1109 } 1110 1111 skip_partial: 1112 while (cont) { 1113 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); 1114 switch (offset[0]) { 1115 case NODE_DIR1_BLOCK: 1116 case NODE_DIR2_BLOCK: 1117 err = truncate_dnode(&dn); 1118 break; 1119 1120 case NODE_IND1_BLOCK: 1121 case NODE_IND2_BLOCK: 1122 err = truncate_nodes(&dn, nofs, offset[1], 2); 1123 break; 1124 1125 case NODE_DIND_BLOCK: 1126 err = truncate_nodes(&dn, nofs, offset[1], 3); 1127 cont = 0; 1128 break; 1129 1130 default: 1131 BUG(); 1132 } 1133 if (err < 0 && err != -ENOENT) 1134 goto fail; 1135 if (offset[1] == 0 && 1136 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) { 1137 lock_page(page); 1138 BUG_ON(page->mapping != NODE_MAPPING(sbi)); 1139 f2fs_wait_on_page_writeback(page, NODE, true, true); 1140 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0; 1141 set_page_dirty(page); 1142 unlock_page(page); 1143 } 1144 offset[1] = 0; 1145 offset[0]++; 1146 nofs += err; 1147 } 1148 fail: 1149 f2fs_put_page(page, 0); 1150 trace_f2fs_truncate_inode_blocks_exit(inode, err); 1151 return err > 0 ? 0 : err; 1152 } 1153 1154 /* caller must lock inode page */ 1155 int f2fs_truncate_xattr_node(struct inode *inode) 1156 { 1157 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1158 nid_t nid = F2FS_I(inode)->i_xattr_nid; 1159 struct dnode_of_data dn; 1160 struct page *npage; 1161 int err; 1162 1163 if (!nid) 1164 return 0; 1165 1166 npage = f2fs_get_node_page(sbi, nid); 1167 if (IS_ERR(npage)) 1168 return PTR_ERR(npage); 1169 1170 set_new_dnode(&dn, inode, NULL, npage, nid); 1171 err = truncate_node(&dn); 1172 if (err) { 1173 f2fs_put_page(npage, 1); 1174 return err; 1175 } 1176 1177 f2fs_i_xnid_write(inode, 0); 1178 1179 return 0; 1180 } 1181 1182 /* 1183 * Caller should grab and release a rwsem by calling f2fs_lock_op() and 1184 * f2fs_unlock_op(). 1185 */ 1186 int f2fs_remove_inode_page(struct inode *inode) 1187 { 1188 struct dnode_of_data dn; 1189 int err; 1190 1191 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 1192 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE); 1193 if (err) 1194 return err; 1195 1196 err = f2fs_truncate_xattr_node(inode); 1197 if (err) { 1198 f2fs_put_dnode(&dn); 1199 return err; 1200 } 1201 1202 /* remove potential inline_data blocks */ 1203 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 1204 S_ISLNK(inode->i_mode)) 1205 f2fs_truncate_data_blocks_range(&dn, 1); 1206 1207 /* 0 is possible, after f2fs_new_inode() has failed */ 1208 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) { 1209 f2fs_put_dnode(&dn); 1210 return -EIO; 1211 } 1212 1213 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) { 1214 f2fs_warn(F2FS_I_SB(inode), 1215 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu", 1216 inode->i_ino, (unsigned long long)inode->i_blocks); 1217 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 1218 } 1219 1220 /* will put inode & node pages */ 1221 err = truncate_node(&dn); 1222 if (err) { 1223 f2fs_put_dnode(&dn); 1224 return err; 1225 } 1226 return 0; 1227 } 1228 1229 struct page *f2fs_new_inode_page(struct inode *inode) 1230 { 1231 struct dnode_of_data dn; 1232 1233 /* allocate inode page for new inode */ 1234 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 1235 1236 /* caller should f2fs_put_page(page, 1); */ 1237 return f2fs_new_node_page(&dn, 0); 1238 } 1239 1240 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs) 1241 { 1242 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1243 struct node_info new_ni; 1244 struct page *page; 1245 int err; 1246 1247 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 1248 return ERR_PTR(-EPERM); 1249 1250 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false); 1251 if (!page) 1252 return ERR_PTR(-ENOMEM); 1253 1254 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs)))) 1255 goto fail; 1256 1257 #ifdef CONFIG_F2FS_CHECK_FS 1258 err = f2fs_get_node_info(sbi, dn->nid, &new_ni); 1259 if (err) { 1260 dec_valid_node_count(sbi, dn->inode, !ofs); 1261 goto fail; 1262 } 1263 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR); 1264 #endif 1265 new_ni.nid = dn->nid; 1266 new_ni.ino = dn->inode->i_ino; 1267 new_ni.blk_addr = NULL_ADDR; 1268 new_ni.flag = 0; 1269 new_ni.version = 0; 1270 set_node_addr(sbi, &new_ni, NEW_ADDR, false); 1271 1272 f2fs_wait_on_page_writeback(page, NODE, true, true); 1273 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true); 1274 set_cold_node(page, S_ISDIR(dn->inode->i_mode)); 1275 if (!PageUptodate(page)) 1276 SetPageUptodate(page); 1277 if (set_page_dirty(page)) 1278 dn->node_changed = true; 1279 1280 if (f2fs_has_xattr_block(ofs)) 1281 f2fs_i_xnid_write(dn->inode, dn->nid); 1282 1283 if (ofs == 0) 1284 inc_valid_inode_count(sbi); 1285 return page; 1286 1287 fail: 1288 clear_node_page_dirty(page); 1289 f2fs_put_page(page, 1); 1290 return ERR_PTR(err); 1291 } 1292 1293 /* 1294 * Caller should do after getting the following values. 1295 * 0: f2fs_put_page(page, 0) 1296 * LOCKED_PAGE or error: f2fs_put_page(page, 1) 1297 */ 1298 static int read_node_page(struct page *page, int op_flags) 1299 { 1300 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1301 struct node_info ni; 1302 struct f2fs_io_info fio = { 1303 .sbi = sbi, 1304 .type = NODE, 1305 .op = REQ_OP_READ, 1306 .op_flags = op_flags, 1307 .page = page, 1308 .encrypted_page = NULL, 1309 }; 1310 int err; 1311 1312 if (PageUptodate(page)) { 1313 if (!f2fs_inode_chksum_verify(sbi, page)) { 1314 ClearPageUptodate(page); 1315 return -EFSBADCRC; 1316 } 1317 return LOCKED_PAGE; 1318 } 1319 1320 err = f2fs_get_node_info(sbi, page->index, &ni); 1321 if (err) 1322 return err; 1323 1324 if (unlikely(ni.blk_addr == NULL_ADDR) || 1325 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) { 1326 ClearPageUptodate(page); 1327 return -ENOENT; 1328 } 1329 1330 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr; 1331 1332 err = f2fs_submit_page_bio(&fio); 1333 1334 if (!err) 1335 f2fs_update_iostat(sbi, FS_NODE_READ_IO, F2FS_BLKSIZE); 1336 1337 return err; 1338 } 1339 1340 /* 1341 * Readahead a node page 1342 */ 1343 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid) 1344 { 1345 struct page *apage; 1346 int err; 1347 1348 if (!nid) 1349 return; 1350 if (f2fs_check_nid_range(sbi, nid)) 1351 return; 1352 1353 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid); 1354 if (apage) 1355 return; 1356 1357 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false); 1358 if (!apage) 1359 return; 1360 1361 err = read_node_page(apage, REQ_RAHEAD); 1362 f2fs_put_page(apage, err ? 1 : 0); 1363 } 1364 1365 static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid, 1366 struct page *parent, int start) 1367 { 1368 struct page *page; 1369 int err; 1370 1371 if (!nid) 1372 return ERR_PTR(-ENOENT); 1373 if (f2fs_check_nid_range(sbi, nid)) 1374 return ERR_PTR(-EINVAL); 1375 repeat: 1376 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false); 1377 if (!page) 1378 return ERR_PTR(-ENOMEM); 1379 1380 err = read_node_page(page, 0); 1381 if (err < 0) { 1382 f2fs_put_page(page, 1); 1383 return ERR_PTR(err); 1384 } else if (err == LOCKED_PAGE) { 1385 err = 0; 1386 goto page_hit; 1387 } 1388 1389 if (parent) 1390 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE); 1391 1392 lock_page(page); 1393 1394 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1395 f2fs_put_page(page, 1); 1396 goto repeat; 1397 } 1398 1399 if (unlikely(!PageUptodate(page))) { 1400 err = -EIO; 1401 goto out_err; 1402 } 1403 1404 if (!f2fs_inode_chksum_verify(sbi, page)) { 1405 err = -EFSBADCRC; 1406 goto out_err; 1407 } 1408 page_hit: 1409 if (unlikely(nid != nid_of_node(page))) { 1410 f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]", 1411 nid, nid_of_node(page), ino_of_node(page), 1412 ofs_of_node(page), cpver_of_node(page), 1413 next_blkaddr_of_node(page)); 1414 err = -EINVAL; 1415 out_err: 1416 ClearPageUptodate(page); 1417 f2fs_put_page(page, 1); 1418 return ERR_PTR(err); 1419 } 1420 return page; 1421 } 1422 1423 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid) 1424 { 1425 return __get_node_page(sbi, nid, NULL, 0); 1426 } 1427 1428 struct page *f2fs_get_node_page_ra(struct page *parent, int start) 1429 { 1430 struct f2fs_sb_info *sbi = F2FS_P_SB(parent); 1431 nid_t nid = get_nid(parent, start, false); 1432 1433 return __get_node_page(sbi, nid, parent, start); 1434 } 1435 1436 static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino) 1437 { 1438 struct inode *inode; 1439 struct page *page; 1440 int ret; 1441 1442 /* should flush inline_data before evict_inode */ 1443 inode = ilookup(sbi->sb, ino); 1444 if (!inode) 1445 return; 1446 1447 page = f2fs_pagecache_get_page(inode->i_mapping, 0, 1448 FGP_LOCK|FGP_NOWAIT, 0); 1449 if (!page) 1450 goto iput_out; 1451 1452 if (!PageUptodate(page)) 1453 goto page_out; 1454 1455 if (!PageDirty(page)) 1456 goto page_out; 1457 1458 if (!clear_page_dirty_for_io(page)) 1459 goto page_out; 1460 1461 ret = f2fs_write_inline_data(inode, page); 1462 inode_dec_dirty_pages(inode); 1463 f2fs_remove_dirty_inode(inode); 1464 if (ret) 1465 set_page_dirty(page); 1466 page_out: 1467 f2fs_put_page(page, 1); 1468 iput_out: 1469 iput(inode); 1470 } 1471 1472 static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino) 1473 { 1474 pgoff_t index; 1475 struct pagevec pvec; 1476 struct page *last_page = NULL; 1477 int nr_pages; 1478 1479 pagevec_init(&pvec); 1480 index = 0; 1481 1482 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, 1483 PAGECACHE_TAG_DIRTY))) { 1484 int i; 1485 1486 for (i = 0; i < nr_pages; i++) { 1487 struct page *page = pvec.pages[i]; 1488 1489 if (unlikely(f2fs_cp_error(sbi))) { 1490 f2fs_put_page(last_page, 0); 1491 pagevec_release(&pvec); 1492 return ERR_PTR(-EIO); 1493 } 1494 1495 if (!IS_DNODE(page) || !is_cold_node(page)) 1496 continue; 1497 if (ino_of_node(page) != ino) 1498 continue; 1499 1500 lock_page(page); 1501 1502 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1503 continue_unlock: 1504 unlock_page(page); 1505 continue; 1506 } 1507 if (ino_of_node(page) != ino) 1508 goto continue_unlock; 1509 1510 if (!PageDirty(page)) { 1511 /* someone wrote it for us */ 1512 goto continue_unlock; 1513 } 1514 1515 if (last_page) 1516 f2fs_put_page(last_page, 0); 1517 1518 get_page(page); 1519 last_page = page; 1520 unlock_page(page); 1521 } 1522 pagevec_release(&pvec); 1523 cond_resched(); 1524 } 1525 return last_page; 1526 } 1527 1528 static int __write_node_page(struct page *page, bool atomic, bool *submitted, 1529 struct writeback_control *wbc, bool do_balance, 1530 enum iostat_type io_type, unsigned int *seq_id) 1531 { 1532 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1533 nid_t nid; 1534 struct node_info ni; 1535 struct f2fs_io_info fio = { 1536 .sbi = sbi, 1537 .ino = ino_of_node(page), 1538 .type = NODE, 1539 .op = REQ_OP_WRITE, 1540 .op_flags = wbc_to_write_flags(wbc), 1541 .page = page, 1542 .encrypted_page = NULL, 1543 .submitted = false, 1544 .io_type = io_type, 1545 .io_wbc = wbc, 1546 }; 1547 unsigned int seq; 1548 1549 trace_f2fs_writepage(page, NODE); 1550 1551 if (unlikely(f2fs_cp_error(sbi))) { 1552 ClearPageUptodate(page); 1553 dec_page_count(sbi, F2FS_DIRTY_NODES); 1554 unlock_page(page); 1555 return 0; 1556 } 1557 1558 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 1559 goto redirty_out; 1560 1561 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 1562 wbc->sync_mode == WB_SYNC_NONE && 1563 IS_DNODE(page) && is_cold_node(page)) 1564 goto redirty_out; 1565 1566 /* get old block addr of this node page */ 1567 nid = nid_of_node(page); 1568 f2fs_bug_on(sbi, page->index != nid); 1569 1570 if (f2fs_get_node_info(sbi, nid, &ni)) 1571 goto redirty_out; 1572 1573 if (wbc->for_reclaim) { 1574 if (!down_read_trylock(&sbi->node_write)) 1575 goto redirty_out; 1576 } else { 1577 down_read(&sbi->node_write); 1578 } 1579 1580 /* This page is already truncated */ 1581 if (unlikely(ni.blk_addr == NULL_ADDR)) { 1582 ClearPageUptodate(page); 1583 dec_page_count(sbi, F2FS_DIRTY_NODES); 1584 up_read(&sbi->node_write); 1585 unlock_page(page); 1586 return 0; 1587 } 1588 1589 if (__is_valid_data_blkaddr(ni.blk_addr) && 1590 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, 1591 DATA_GENERIC_ENHANCE)) { 1592 up_read(&sbi->node_write); 1593 goto redirty_out; 1594 } 1595 1596 if (atomic && !test_opt(sbi, NOBARRIER)) 1597 fio.op_flags |= REQ_PREFLUSH | REQ_FUA; 1598 1599 /* should add to global list before clearing PAGECACHE status */ 1600 if (f2fs_in_warm_node_list(sbi, page)) { 1601 seq = f2fs_add_fsync_node_entry(sbi, page); 1602 if (seq_id) 1603 *seq_id = seq; 1604 } 1605 1606 set_page_writeback(page); 1607 ClearPageError(page); 1608 1609 fio.old_blkaddr = ni.blk_addr; 1610 f2fs_do_write_node_page(nid, &fio); 1611 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page)); 1612 dec_page_count(sbi, F2FS_DIRTY_NODES); 1613 up_read(&sbi->node_write); 1614 1615 if (wbc->for_reclaim) { 1616 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE); 1617 submitted = NULL; 1618 } 1619 1620 unlock_page(page); 1621 1622 if (unlikely(f2fs_cp_error(sbi))) { 1623 f2fs_submit_merged_write(sbi, NODE); 1624 submitted = NULL; 1625 } 1626 if (submitted) 1627 *submitted = fio.submitted; 1628 1629 if (do_balance) 1630 f2fs_balance_fs(sbi, false); 1631 return 0; 1632 1633 redirty_out: 1634 redirty_page_for_writepage(wbc, page); 1635 return AOP_WRITEPAGE_ACTIVATE; 1636 } 1637 1638 int f2fs_move_node_page(struct page *node_page, int gc_type) 1639 { 1640 int err = 0; 1641 1642 if (gc_type == FG_GC) { 1643 struct writeback_control wbc = { 1644 .sync_mode = WB_SYNC_ALL, 1645 .nr_to_write = 1, 1646 .for_reclaim = 0, 1647 }; 1648 1649 f2fs_wait_on_page_writeback(node_page, NODE, true, true); 1650 1651 set_page_dirty(node_page); 1652 1653 if (!clear_page_dirty_for_io(node_page)) { 1654 err = -EAGAIN; 1655 goto out_page; 1656 } 1657 1658 if (__write_node_page(node_page, false, NULL, 1659 &wbc, false, FS_GC_NODE_IO, NULL)) { 1660 err = -EAGAIN; 1661 unlock_page(node_page); 1662 } 1663 goto release_page; 1664 } else { 1665 /* set page dirty and write it */ 1666 if (!PageWriteback(node_page)) 1667 set_page_dirty(node_page); 1668 } 1669 out_page: 1670 unlock_page(node_page); 1671 release_page: 1672 f2fs_put_page(node_page, 0); 1673 return err; 1674 } 1675 1676 static int f2fs_write_node_page(struct page *page, 1677 struct writeback_control *wbc) 1678 { 1679 return __write_node_page(page, false, NULL, wbc, false, 1680 FS_NODE_IO, NULL); 1681 } 1682 1683 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, 1684 struct writeback_control *wbc, bool atomic, 1685 unsigned int *seq_id) 1686 { 1687 pgoff_t index; 1688 struct pagevec pvec; 1689 int ret = 0; 1690 struct page *last_page = NULL; 1691 bool marked = false; 1692 nid_t ino = inode->i_ino; 1693 int nr_pages; 1694 int nwritten = 0; 1695 1696 if (atomic) { 1697 last_page = last_fsync_dnode(sbi, ino); 1698 if (IS_ERR_OR_NULL(last_page)) 1699 return PTR_ERR_OR_ZERO(last_page); 1700 } 1701 retry: 1702 pagevec_init(&pvec); 1703 index = 0; 1704 1705 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, 1706 PAGECACHE_TAG_DIRTY))) { 1707 int i; 1708 1709 for (i = 0; i < nr_pages; i++) { 1710 struct page *page = pvec.pages[i]; 1711 bool submitted = false; 1712 1713 if (unlikely(f2fs_cp_error(sbi))) { 1714 f2fs_put_page(last_page, 0); 1715 pagevec_release(&pvec); 1716 ret = -EIO; 1717 goto out; 1718 } 1719 1720 if (!IS_DNODE(page) || !is_cold_node(page)) 1721 continue; 1722 if (ino_of_node(page) != ino) 1723 continue; 1724 1725 lock_page(page); 1726 1727 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1728 continue_unlock: 1729 unlock_page(page); 1730 continue; 1731 } 1732 if (ino_of_node(page) != ino) 1733 goto continue_unlock; 1734 1735 if (!PageDirty(page) && page != last_page) { 1736 /* someone wrote it for us */ 1737 goto continue_unlock; 1738 } 1739 1740 f2fs_wait_on_page_writeback(page, NODE, true, true); 1741 1742 set_fsync_mark(page, 0); 1743 set_dentry_mark(page, 0); 1744 1745 if (!atomic || page == last_page) { 1746 set_fsync_mark(page, 1); 1747 if (IS_INODE(page)) { 1748 if (is_inode_flag_set(inode, 1749 FI_DIRTY_INODE)) 1750 f2fs_update_inode(inode, page); 1751 set_dentry_mark(page, 1752 f2fs_need_dentry_mark(sbi, ino)); 1753 } 1754 /* may be written by other thread */ 1755 if (!PageDirty(page)) 1756 set_page_dirty(page); 1757 } 1758 1759 if (!clear_page_dirty_for_io(page)) 1760 goto continue_unlock; 1761 1762 ret = __write_node_page(page, atomic && 1763 page == last_page, 1764 &submitted, wbc, true, 1765 FS_NODE_IO, seq_id); 1766 if (ret) { 1767 unlock_page(page); 1768 f2fs_put_page(last_page, 0); 1769 break; 1770 } else if (submitted) { 1771 nwritten++; 1772 } 1773 1774 if (page == last_page) { 1775 f2fs_put_page(page, 0); 1776 marked = true; 1777 break; 1778 } 1779 } 1780 pagevec_release(&pvec); 1781 cond_resched(); 1782 1783 if (ret || marked) 1784 break; 1785 } 1786 if (!ret && atomic && !marked) { 1787 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx", 1788 ino, last_page->index); 1789 lock_page(last_page); 1790 f2fs_wait_on_page_writeback(last_page, NODE, true, true); 1791 set_page_dirty(last_page); 1792 unlock_page(last_page); 1793 goto retry; 1794 } 1795 out: 1796 if (nwritten) 1797 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE); 1798 return ret ? -EIO : 0; 1799 } 1800 1801 static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data) 1802 { 1803 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1804 bool clean; 1805 1806 if (inode->i_ino != ino) 1807 return 0; 1808 1809 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) 1810 return 0; 1811 1812 spin_lock(&sbi->inode_lock[DIRTY_META]); 1813 clean = list_empty(&F2FS_I(inode)->gdirty_list); 1814 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1815 1816 if (clean) 1817 return 0; 1818 1819 inode = igrab(inode); 1820 if (!inode) 1821 return 0; 1822 return 1; 1823 } 1824 1825 static bool flush_dirty_inode(struct page *page) 1826 { 1827 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1828 struct inode *inode; 1829 nid_t ino = ino_of_node(page); 1830 1831 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL); 1832 if (!inode) 1833 return false; 1834 1835 f2fs_update_inode(inode, page); 1836 unlock_page(page); 1837 1838 iput(inode); 1839 return true; 1840 } 1841 1842 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi) 1843 { 1844 pgoff_t index = 0; 1845 struct pagevec pvec; 1846 int nr_pages; 1847 1848 pagevec_init(&pvec); 1849 1850 while ((nr_pages = pagevec_lookup_tag(&pvec, 1851 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) { 1852 int i; 1853 1854 for (i = 0; i < nr_pages; i++) { 1855 struct page *page = pvec.pages[i]; 1856 1857 if (!IS_DNODE(page)) 1858 continue; 1859 1860 lock_page(page); 1861 1862 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1863 continue_unlock: 1864 unlock_page(page); 1865 continue; 1866 } 1867 1868 if (!PageDirty(page)) { 1869 /* someone wrote it for us */ 1870 goto continue_unlock; 1871 } 1872 1873 /* flush inline_data, if it's async context. */ 1874 if (page_private_inline(page)) { 1875 clear_page_private_inline(page); 1876 unlock_page(page); 1877 flush_inline_data(sbi, ino_of_node(page)); 1878 continue; 1879 } 1880 unlock_page(page); 1881 } 1882 pagevec_release(&pvec); 1883 cond_resched(); 1884 } 1885 } 1886 1887 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi, 1888 struct writeback_control *wbc, 1889 bool do_balance, enum iostat_type io_type) 1890 { 1891 pgoff_t index; 1892 struct pagevec pvec; 1893 int step = 0; 1894 int nwritten = 0; 1895 int ret = 0; 1896 int nr_pages, done = 0; 1897 1898 pagevec_init(&pvec); 1899 1900 next_step: 1901 index = 0; 1902 1903 while (!done && (nr_pages = pagevec_lookup_tag(&pvec, 1904 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) { 1905 int i; 1906 1907 for (i = 0; i < nr_pages; i++) { 1908 struct page *page = pvec.pages[i]; 1909 bool submitted = false; 1910 bool may_dirty = true; 1911 1912 /* give a priority to WB_SYNC threads */ 1913 if (atomic_read(&sbi->wb_sync_req[NODE]) && 1914 wbc->sync_mode == WB_SYNC_NONE) { 1915 done = 1; 1916 break; 1917 } 1918 1919 /* 1920 * flushing sequence with step: 1921 * 0. indirect nodes 1922 * 1. dentry dnodes 1923 * 2. file dnodes 1924 */ 1925 if (step == 0 && IS_DNODE(page)) 1926 continue; 1927 if (step == 1 && (!IS_DNODE(page) || 1928 is_cold_node(page))) 1929 continue; 1930 if (step == 2 && (!IS_DNODE(page) || 1931 !is_cold_node(page))) 1932 continue; 1933 lock_node: 1934 if (wbc->sync_mode == WB_SYNC_ALL) 1935 lock_page(page); 1936 else if (!trylock_page(page)) 1937 continue; 1938 1939 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1940 continue_unlock: 1941 unlock_page(page); 1942 continue; 1943 } 1944 1945 if (!PageDirty(page)) { 1946 /* someone wrote it for us */ 1947 goto continue_unlock; 1948 } 1949 1950 /* flush inline_data/inode, if it's async context. */ 1951 if (!do_balance) 1952 goto write_node; 1953 1954 /* flush inline_data */ 1955 if (page_private_inline(page)) { 1956 clear_page_private_inline(page); 1957 unlock_page(page); 1958 flush_inline_data(sbi, ino_of_node(page)); 1959 goto lock_node; 1960 } 1961 1962 /* flush dirty inode */ 1963 if (IS_INODE(page) && may_dirty) { 1964 may_dirty = false; 1965 if (flush_dirty_inode(page)) 1966 goto lock_node; 1967 } 1968 write_node: 1969 f2fs_wait_on_page_writeback(page, NODE, true, true); 1970 1971 if (!clear_page_dirty_for_io(page)) 1972 goto continue_unlock; 1973 1974 set_fsync_mark(page, 0); 1975 set_dentry_mark(page, 0); 1976 1977 ret = __write_node_page(page, false, &submitted, 1978 wbc, do_balance, io_type, NULL); 1979 if (ret) 1980 unlock_page(page); 1981 else if (submitted) 1982 nwritten++; 1983 1984 if (--wbc->nr_to_write == 0) 1985 break; 1986 } 1987 pagevec_release(&pvec); 1988 cond_resched(); 1989 1990 if (wbc->nr_to_write == 0) { 1991 step = 2; 1992 break; 1993 } 1994 } 1995 1996 if (step < 2) { 1997 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 1998 wbc->sync_mode == WB_SYNC_NONE && step == 1) 1999 goto out; 2000 step++; 2001 goto next_step; 2002 } 2003 out: 2004 if (nwritten) 2005 f2fs_submit_merged_write(sbi, NODE); 2006 2007 if (unlikely(f2fs_cp_error(sbi))) 2008 return -EIO; 2009 return ret; 2010 } 2011 2012 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, 2013 unsigned int seq_id) 2014 { 2015 struct fsync_node_entry *fn; 2016 struct page *page; 2017 struct list_head *head = &sbi->fsync_node_list; 2018 unsigned long flags; 2019 unsigned int cur_seq_id = 0; 2020 int ret2, ret = 0; 2021 2022 while (seq_id && cur_seq_id < seq_id) { 2023 spin_lock_irqsave(&sbi->fsync_node_lock, flags); 2024 if (list_empty(head)) { 2025 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 2026 break; 2027 } 2028 fn = list_first_entry(head, struct fsync_node_entry, list); 2029 if (fn->seq_id > seq_id) { 2030 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 2031 break; 2032 } 2033 cur_seq_id = fn->seq_id; 2034 page = fn->page; 2035 get_page(page); 2036 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); 2037 2038 f2fs_wait_on_page_writeback(page, NODE, true, false); 2039 if (TestClearPageError(page)) 2040 ret = -EIO; 2041 2042 put_page(page); 2043 2044 if (ret) 2045 break; 2046 } 2047 2048 ret2 = filemap_check_errors(NODE_MAPPING(sbi)); 2049 if (!ret) 2050 ret = ret2; 2051 2052 return ret; 2053 } 2054 2055 static int f2fs_write_node_pages(struct address_space *mapping, 2056 struct writeback_control *wbc) 2057 { 2058 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 2059 struct blk_plug plug; 2060 long diff; 2061 2062 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2063 goto skip_write; 2064 2065 /* balancing f2fs's metadata in background */ 2066 f2fs_balance_fs_bg(sbi, true); 2067 2068 /* collect a number of dirty node pages and write together */ 2069 if (wbc->sync_mode != WB_SYNC_ALL && 2070 get_pages(sbi, F2FS_DIRTY_NODES) < 2071 nr_pages_to_skip(sbi, NODE)) 2072 goto skip_write; 2073 2074 if (wbc->sync_mode == WB_SYNC_ALL) 2075 atomic_inc(&sbi->wb_sync_req[NODE]); 2076 else if (atomic_read(&sbi->wb_sync_req[NODE])) 2077 goto skip_write; 2078 2079 trace_f2fs_writepages(mapping->host, wbc, NODE); 2080 2081 diff = nr_pages_to_write(sbi, NODE, wbc); 2082 blk_start_plug(&plug); 2083 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO); 2084 blk_finish_plug(&plug); 2085 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); 2086 2087 if (wbc->sync_mode == WB_SYNC_ALL) 2088 atomic_dec(&sbi->wb_sync_req[NODE]); 2089 return 0; 2090 2091 skip_write: 2092 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES); 2093 trace_f2fs_writepages(mapping->host, wbc, NODE); 2094 return 0; 2095 } 2096 2097 static int f2fs_set_node_page_dirty(struct page *page) 2098 { 2099 trace_f2fs_set_page_dirty(page, NODE); 2100 2101 if (!PageUptodate(page)) 2102 SetPageUptodate(page); 2103 #ifdef CONFIG_F2FS_CHECK_FS 2104 if (IS_INODE(page)) 2105 f2fs_inode_chksum_set(F2FS_P_SB(page), page); 2106 #endif 2107 if (!PageDirty(page)) { 2108 __set_page_dirty_nobuffers(page); 2109 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); 2110 set_page_private_reference(page); 2111 return 1; 2112 } 2113 return 0; 2114 } 2115 2116 /* 2117 * Structure of the f2fs node operations 2118 */ 2119 const struct address_space_operations f2fs_node_aops = { 2120 .writepage = f2fs_write_node_page, 2121 .writepages = f2fs_write_node_pages, 2122 .set_page_dirty = f2fs_set_node_page_dirty, 2123 .invalidatepage = f2fs_invalidate_page, 2124 .releasepage = f2fs_release_page, 2125 #ifdef CONFIG_MIGRATION 2126 .migratepage = f2fs_migrate_page, 2127 #endif 2128 }; 2129 2130 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i, 2131 nid_t n) 2132 { 2133 return radix_tree_lookup(&nm_i->free_nid_root, n); 2134 } 2135 2136 static int __insert_free_nid(struct f2fs_sb_info *sbi, 2137 struct free_nid *i) 2138 { 2139 struct f2fs_nm_info *nm_i = NM_I(sbi); 2140 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i); 2141 2142 if (err) 2143 return err; 2144 2145 nm_i->nid_cnt[FREE_NID]++; 2146 list_add_tail(&i->list, &nm_i->free_nid_list); 2147 return 0; 2148 } 2149 2150 static void __remove_free_nid(struct f2fs_sb_info *sbi, 2151 struct free_nid *i, enum nid_state state) 2152 { 2153 struct f2fs_nm_info *nm_i = NM_I(sbi); 2154 2155 f2fs_bug_on(sbi, state != i->state); 2156 nm_i->nid_cnt[state]--; 2157 if (state == FREE_NID) 2158 list_del(&i->list); 2159 radix_tree_delete(&nm_i->free_nid_root, i->nid); 2160 } 2161 2162 static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i, 2163 enum nid_state org_state, enum nid_state dst_state) 2164 { 2165 struct f2fs_nm_info *nm_i = NM_I(sbi); 2166 2167 f2fs_bug_on(sbi, org_state != i->state); 2168 i->state = dst_state; 2169 nm_i->nid_cnt[org_state]--; 2170 nm_i->nid_cnt[dst_state]++; 2171 2172 switch (dst_state) { 2173 case PREALLOC_NID: 2174 list_del(&i->list); 2175 break; 2176 case FREE_NID: 2177 list_add_tail(&i->list, &nm_i->free_nid_list); 2178 break; 2179 default: 2180 BUG_ON(1); 2181 } 2182 } 2183 2184 static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid, 2185 bool set, bool build) 2186 { 2187 struct f2fs_nm_info *nm_i = NM_I(sbi); 2188 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid); 2189 unsigned int nid_ofs = nid - START_NID(nid); 2190 2191 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap)) 2192 return; 2193 2194 if (set) { 2195 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs])) 2196 return; 2197 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]); 2198 nm_i->free_nid_count[nat_ofs]++; 2199 } else { 2200 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs])) 2201 return; 2202 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]); 2203 if (!build) 2204 nm_i->free_nid_count[nat_ofs]--; 2205 } 2206 } 2207 2208 /* return if the nid is recognized as free */ 2209 static bool add_free_nid(struct f2fs_sb_info *sbi, 2210 nid_t nid, bool build, bool update) 2211 { 2212 struct f2fs_nm_info *nm_i = NM_I(sbi); 2213 struct free_nid *i, *e; 2214 struct nat_entry *ne; 2215 int err = -EINVAL; 2216 bool ret = false; 2217 2218 /* 0 nid should not be used */ 2219 if (unlikely(nid == 0)) 2220 return false; 2221 2222 if (unlikely(f2fs_check_nid_range(sbi, nid))) 2223 return false; 2224 2225 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS); 2226 i->nid = nid; 2227 i->state = FREE_NID; 2228 2229 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); 2230 2231 spin_lock(&nm_i->nid_list_lock); 2232 2233 if (build) { 2234 /* 2235 * Thread A Thread B 2236 * - f2fs_create 2237 * - f2fs_new_inode 2238 * - f2fs_alloc_nid 2239 * - __insert_nid_to_list(PREALLOC_NID) 2240 * - f2fs_balance_fs_bg 2241 * - f2fs_build_free_nids 2242 * - __f2fs_build_free_nids 2243 * - scan_nat_page 2244 * - add_free_nid 2245 * - __lookup_nat_cache 2246 * - f2fs_add_link 2247 * - f2fs_init_inode_metadata 2248 * - f2fs_new_inode_page 2249 * - f2fs_new_node_page 2250 * - set_node_addr 2251 * - f2fs_alloc_nid_done 2252 * - __remove_nid_from_list(PREALLOC_NID) 2253 * - __insert_nid_to_list(FREE_NID) 2254 */ 2255 ne = __lookup_nat_cache(nm_i, nid); 2256 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) || 2257 nat_get_blkaddr(ne) != NULL_ADDR)) 2258 goto err_out; 2259 2260 e = __lookup_free_nid_list(nm_i, nid); 2261 if (e) { 2262 if (e->state == FREE_NID) 2263 ret = true; 2264 goto err_out; 2265 } 2266 } 2267 ret = true; 2268 err = __insert_free_nid(sbi, i); 2269 err_out: 2270 if (update) { 2271 update_free_nid_bitmap(sbi, nid, ret, build); 2272 if (!build) 2273 nm_i->available_nids++; 2274 } 2275 spin_unlock(&nm_i->nid_list_lock); 2276 radix_tree_preload_end(); 2277 2278 if (err) 2279 kmem_cache_free(free_nid_slab, i); 2280 return ret; 2281 } 2282 2283 static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid) 2284 { 2285 struct f2fs_nm_info *nm_i = NM_I(sbi); 2286 struct free_nid *i; 2287 bool need_free = false; 2288 2289 spin_lock(&nm_i->nid_list_lock); 2290 i = __lookup_free_nid_list(nm_i, nid); 2291 if (i && i->state == FREE_NID) { 2292 __remove_free_nid(sbi, i, FREE_NID); 2293 need_free = true; 2294 } 2295 spin_unlock(&nm_i->nid_list_lock); 2296 2297 if (need_free) 2298 kmem_cache_free(free_nid_slab, i); 2299 } 2300 2301 static int scan_nat_page(struct f2fs_sb_info *sbi, 2302 struct page *nat_page, nid_t start_nid) 2303 { 2304 struct f2fs_nm_info *nm_i = NM_I(sbi); 2305 struct f2fs_nat_block *nat_blk = page_address(nat_page); 2306 block_t blk_addr; 2307 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid); 2308 int i; 2309 2310 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap); 2311 2312 i = start_nid % NAT_ENTRY_PER_BLOCK; 2313 2314 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) { 2315 if (unlikely(start_nid >= nm_i->max_nid)) 2316 break; 2317 2318 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr); 2319 2320 if (blk_addr == NEW_ADDR) 2321 return -EINVAL; 2322 2323 if (blk_addr == NULL_ADDR) { 2324 add_free_nid(sbi, start_nid, true, true); 2325 } else { 2326 spin_lock(&NM_I(sbi)->nid_list_lock); 2327 update_free_nid_bitmap(sbi, start_nid, false, true); 2328 spin_unlock(&NM_I(sbi)->nid_list_lock); 2329 } 2330 } 2331 2332 return 0; 2333 } 2334 2335 static void scan_curseg_cache(struct f2fs_sb_info *sbi) 2336 { 2337 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2338 struct f2fs_journal *journal = curseg->journal; 2339 int i; 2340 2341 down_read(&curseg->journal_rwsem); 2342 for (i = 0; i < nats_in_cursum(journal); i++) { 2343 block_t addr; 2344 nid_t nid; 2345 2346 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr); 2347 nid = le32_to_cpu(nid_in_journal(journal, i)); 2348 if (addr == NULL_ADDR) 2349 add_free_nid(sbi, nid, true, false); 2350 else 2351 remove_free_nid(sbi, nid); 2352 } 2353 up_read(&curseg->journal_rwsem); 2354 } 2355 2356 static void scan_free_nid_bits(struct f2fs_sb_info *sbi) 2357 { 2358 struct f2fs_nm_info *nm_i = NM_I(sbi); 2359 unsigned int i, idx; 2360 nid_t nid; 2361 2362 down_read(&nm_i->nat_tree_lock); 2363 2364 for (i = 0; i < nm_i->nat_blocks; i++) { 2365 if (!test_bit_le(i, nm_i->nat_block_bitmap)) 2366 continue; 2367 if (!nm_i->free_nid_count[i]) 2368 continue; 2369 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) { 2370 idx = find_next_bit_le(nm_i->free_nid_bitmap[i], 2371 NAT_ENTRY_PER_BLOCK, idx); 2372 if (idx >= NAT_ENTRY_PER_BLOCK) 2373 break; 2374 2375 nid = i * NAT_ENTRY_PER_BLOCK + idx; 2376 add_free_nid(sbi, nid, true, false); 2377 2378 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS) 2379 goto out; 2380 } 2381 } 2382 out: 2383 scan_curseg_cache(sbi); 2384 2385 up_read(&nm_i->nat_tree_lock); 2386 } 2387 2388 static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi, 2389 bool sync, bool mount) 2390 { 2391 struct f2fs_nm_info *nm_i = NM_I(sbi); 2392 int i = 0, ret; 2393 nid_t nid = nm_i->next_scan_nid; 2394 2395 if (unlikely(nid >= nm_i->max_nid)) 2396 nid = 0; 2397 2398 if (unlikely(nid % NAT_ENTRY_PER_BLOCK)) 2399 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK; 2400 2401 /* Enough entries */ 2402 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK) 2403 return 0; 2404 2405 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS)) 2406 return 0; 2407 2408 if (!mount) { 2409 /* try to find free nids in free_nid_bitmap */ 2410 scan_free_nid_bits(sbi); 2411 2412 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK) 2413 return 0; 2414 } 2415 2416 /* readahead nat pages to be scanned */ 2417 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, 2418 META_NAT, true); 2419 2420 down_read(&nm_i->nat_tree_lock); 2421 2422 while (1) { 2423 if (!test_bit_le(NAT_BLOCK_OFFSET(nid), 2424 nm_i->nat_block_bitmap)) { 2425 struct page *page = get_current_nat_page(sbi, nid); 2426 2427 if (IS_ERR(page)) { 2428 ret = PTR_ERR(page); 2429 } else { 2430 ret = scan_nat_page(sbi, page, nid); 2431 f2fs_put_page(page, 1); 2432 } 2433 2434 if (ret) { 2435 up_read(&nm_i->nat_tree_lock); 2436 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it"); 2437 return ret; 2438 } 2439 } 2440 2441 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK)); 2442 if (unlikely(nid >= nm_i->max_nid)) 2443 nid = 0; 2444 2445 if (++i >= FREE_NID_PAGES) 2446 break; 2447 } 2448 2449 /* go to the next free nat pages to find free nids abundantly */ 2450 nm_i->next_scan_nid = nid; 2451 2452 /* find free nids from current sum_pages */ 2453 scan_curseg_cache(sbi); 2454 2455 up_read(&nm_i->nat_tree_lock); 2456 2457 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid), 2458 nm_i->ra_nid_pages, META_NAT, false); 2459 2460 return 0; 2461 } 2462 2463 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount) 2464 { 2465 int ret; 2466 2467 mutex_lock(&NM_I(sbi)->build_lock); 2468 ret = __f2fs_build_free_nids(sbi, sync, mount); 2469 mutex_unlock(&NM_I(sbi)->build_lock); 2470 2471 return ret; 2472 } 2473 2474 /* 2475 * If this function returns success, caller can obtain a new nid 2476 * from second parameter of this function. 2477 * The returned nid could be used ino as well as nid when inode is created. 2478 */ 2479 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid) 2480 { 2481 struct f2fs_nm_info *nm_i = NM_I(sbi); 2482 struct free_nid *i = NULL; 2483 retry: 2484 if (time_to_inject(sbi, FAULT_ALLOC_NID)) { 2485 f2fs_show_injection_info(sbi, FAULT_ALLOC_NID); 2486 return false; 2487 } 2488 2489 spin_lock(&nm_i->nid_list_lock); 2490 2491 if (unlikely(nm_i->available_nids == 0)) { 2492 spin_unlock(&nm_i->nid_list_lock); 2493 return false; 2494 } 2495 2496 /* We should not use stale free nids created by f2fs_build_free_nids */ 2497 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) { 2498 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list)); 2499 i = list_first_entry(&nm_i->free_nid_list, 2500 struct free_nid, list); 2501 *nid = i->nid; 2502 2503 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID); 2504 nm_i->available_nids--; 2505 2506 update_free_nid_bitmap(sbi, *nid, false, false); 2507 2508 spin_unlock(&nm_i->nid_list_lock); 2509 return true; 2510 } 2511 spin_unlock(&nm_i->nid_list_lock); 2512 2513 /* Let's scan nat pages and its caches to get free nids */ 2514 if (!f2fs_build_free_nids(sbi, true, false)) 2515 goto retry; 2516 return false; 2517 } 2518 2519 /* 2520 * f2fs_alloc_nid() should be called prior to this function. 2521 */ 2522 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid) 2523 { 2524 struct f2fs_nm_info *nm_i = NM_I(sbi); 2525 struct free_nid *i; 2526 2527 spin_lock(&nm_i->nid_list_lock); 2528 i = __lookup_free_nid_list(nm_i, nid); 2529 f2fs_bug_on(sbi, !i); 2530 __remove_free_nid(sbi, i, PREALLOC_NID); 2531 spin_unlock(&nm_i->nid_list_lock); 2532 2533 kmem_cache_free(free_nid_slab, i); 2534 } 2535 2536 /* 2537 * f2fs_alloc_nid() should be called prior to this function. 2538 */ 2539 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid) 2540 { 2541 struct f2fs_nm_info *nm_i = NM_I(sbi); 2542 struct free_nid *i; 2543 bool need_free = false; 2544 2545 if (!nid) 2546 return; 2547 2548 spin_lock(&nm_i->nid_list_lock); 2549 i = __lookup_free_nid_list(nm_i, nid); 2550 f2fs_bug_on(sbi, !i); 2551 2552 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) { 2553 __remove_free_nid(sbi, i, PREALLOC_NID); 2554 need_free = true; 2555 } else { 2556 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID); 2557 } 2558 2559 nm_i->available_nids++; 2560 2561 update_free_nid_bitmap(sbi, nid, true, false); 2562 2563 spin_unlock(&nm_i->nid_list_lock); 2564 2565 if (need_free) 2566 kmem_cache_free(free_nid_slab, i); 2567 } 2568 2569 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink) 2570 { 2571 struct f2fs_nm_info *nm_i = NM_I(sbi); 2572 int nr = nr_shrink; 2573 2574 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS) 2575 return 0; 2576 2577 if (!mutex_trylock(&nm_i->build_lock)) 2578 return 0; 2579 2580 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) { 2581 struct free_nid *i, *next; 2582 unsigned int batch = SHRINK_NID_BATCH_SIZE; 2583 2584 spin_lock(&nm_i->nid_list_lock); 2585 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) { 2586 if (!nr_shrink || !batch || 2587 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS) 2588 break; 2589 __remove_free_nid(sbi, i, FREE_NID); 2590 kmem_cache_free(free_nid_slab, i); 2591 nr_shrink--; 2592 batch--; 2593 } 2594 spin_unlock(&nm_i->nid_list_lock); 2595 } 2596 2597 mutex_unlock(&nm_i->build_lock); 2598 2599 return nr - nr_shrink; 2600 } 2601 2602 int f2fs_recover_inline_xattr(struct inode *inode, struct page *page) 2603 { 2604 void *src_addr, *dst_addr; 2605 size_t inline_size; 2606 struct page *ipage; 2607 struct f2fs_inode *ri; 2608 2609 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 2610 if (IS_ERR(ipage)) 2611 return PTR_ERR(ipage); 2612 2613 ri = F2FS_INODE(page); 2614 if (ri->i_inline & F2FS_INLINE_XATTR) { 2615 if (!f2fs_has_inline_xattr(inode)) { 2616 set_inode_flag(inode, FI_INLINE_XATTR); 2617 stat_inc_inline_xattr(inode); 2618 } 2619 } else { 2620 if (f2fs_has_inline_xattr(inode)) { 2621 stat_dec_inline_xattr(inode); 2622 clear_inode_flag(inode, FI_INLINE_XATTR); 2623 } 2624 goto update_inode; 2625 } 2626 2627 dst_addr = inline_xattr_addr(inode, ipage); 2628 src_addr = inline_xattr_addr(inode, page); 2629 inline_size = inline_xattr_size(inode); 2630 2631 f2fs_wait_on_page_writeback(ipage, NODE, true, true); 2632 memcpy(dst_addr, src_addr, inline_size); 2633 update_inode: 2634 f2fs_update_inode(inode, ipage); 2635 f2fs_put_page(ipage, 1); 2636 return 0; 2637 } 2638 2639 int f2fs_recover_xattr_data(struct inode *inode, struct page *page) 2640 { 2641 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2642 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid; 2643 nid_t new_xnid; 2644 struct dnode_of_data dn; 2645 struct node_info ni; 2646 struct page *xpage; 2647 int err; 2648 2649 if (!prev_xnid) 2650 goto recover_xnid; 2651 2652 /* 1: invalidate the previous xattr nid */ 2653 err = f2fs_get_node_info(sbi, prev_xnid, &ni); 2654 if (err) 2655 return err; 2656 2657 f2fs_invalidate_blocks(sbi, ni.blk_addr); 2658 dec_valid_node_count(sbi, inode, false); 2659 set_node_addr(sbi, &ni, NULL_ADDR, false); 2660 2661 recover_xnid: 2662 /* 2: update xattr nid in inode */ 2663 if (!f2fs_alloc_nid(sbi, &new_xnid)) 2664 return -ENOSPC; 2665 2666 set_new_dnode(&dn, inode, NULL, NULL, new_xnid); 2667 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); 2668 if (IS_ERR(xpage)) { 2669 f2fs_alloc_nid_failed(sbi, new_xnid); 2670 return PTR_ERR(xpage); 2671 } 2672 2673 f2fs_alloc_nid_done(sbi, new_xnid); 2674 f2fs_update_inode_page(inode); 2675 2676 /* 3: update and set xattr node page dirty */ 2677 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE); 2678 2679 set_page_dirty(xpage); 2680 f2fs_put_page(xpage, 1); 2681 2682 return 0; 2683 } 2684 2685 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page) 2686 { 2687 struct f2fs_inode *src, *dst; 2688 nid_t ino = ino_of_node(page); 2689 struct node_info old_ni, new_ni; 2690 struct page *ipage; 2691 int err; 2692 2693 err = f2fs_get_node_info(sbi, ino, &old_ni); 2694 if (err) 2695 return err; 2696 2697 if (unlikely(old_ni.blk_addr != NULL_ADDR)) 2698 return -EINVAL; 2699 retry: 2700 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false); 2701 if (!ipage) { 2702 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT); 2703 goto retry; 2704 } 2705 2706 /* Should not use this inode from free nid list */ 2707 remove_free_nid(sbi, ino); 2708 2709 if (!PageUptodate(ipage)) 2710 SetPageUptodate(ipage); 2711 fill_node_footer(ipage, ino, ino, 0, true); 2712 set_cold_node(ipage, false); 2713 2714 src = F2FS_INODE(page); 2715 dst = F2FS_INODE(ipage); 2716 2717 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext)); 2718 dst->i_size = 0; 2719 dst->i_blocks = cpu_to_le64(1); 2720 dst->i_links = cpu_to_le32(1); 2721 dst->i_xattr_nid = 0; 2722 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR); 2723 if (dst->i_inline & F2FS_EXTRA_ATTR) { 2724 dst->i_extra_isize = src->i_extra_isize; 2725 2726 if (f2fs_sb_has_flexible_inline_xattr(sbi) && 2727 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), 2728 i_inline_xattr_size)) 2729 dst->i_inline_xattr_size = src->i_inline_xattr_size; 2730 2731 if (f2fs_sb_has_project_quota(sbi) && 2732 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), 2733 i_projid)) 2734 dst->i_projid = src->i_projid; 2735 2736 if (f2fs_sb_has_inode_crtime(sbi) && 2737 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), 2738 i_crtime_nsec)) { 2739 dst->i_crtime = src->i_crtime; 2740 dst->i_crtime_nsec = src->i_crtime_nsec; 2741 } 2742 } 2743 2744 new_ni = old_ni; 2745 new_ni.ino = ino; 2746 2747 if (unlikely(inc_valid_node_count(sbi, NULL, true))) 2748 WARN_ON(1); 2749 set_node_addr(sbi, &new_ni, NEW_ADDR, false); 2750 inc_valid_inode_count(sbi); 2751 set_page_dirty(ipage); 2752 f2fs_put_page(ipage, 1); 2753 return 0; 2754 } 2755 2756 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi, 2757 unsigned int segno, struct f2fs_summary_block *sum) 2758 { 2759 struct f2fs_node *rn; 2760 struct f2fs_summary *sum_entry; 2761 block_t addr; 2762 int i, idx, last_offset, nrpages; 2763 2764 /* scan the node segment */ 2765 last_offset = sbi->blocks_per_seg; 2766 addr = START_BLOCK(sbi, segno); 2767 sum_entry = &sum->entries[0]; 2768 2769 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) { 2770 nrpages = bio_max_segs(last_offset - i); 2771 2772 /* readahead node pages */ 2773 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true); 2774 2775 for (idx = addr; idx < addr + nrpages; idx++) { 2776 struct page *page = f2fs_get_tmp_page(sbi, idx); 2777 2778 if (IS_ERR(page)) 2779 return PTR_ERR(page); 2780 2781 rn = F2FS_NODE(page); 2782 sum_entry->nid = rn->footer.nid; 2783 sum_entry->version = 0; 2784 sum_entry->ofs_in_node = 0; 2785 sum_entry++; 2786 f2fs_put_page(page, 1); 2787 } 2788 2789 invalidate_mapping_pages(META_MAPPING(sbi), addr, 2790 addr + nrpages); 2791 } 2792 return 0; 2793 } 2794 2795 static void remove_nats_in_journal(struct f2fs_sb_info *sbi) 2796 { 2797 struct f2fs_nm_info *nm_i = NM_I(sbi); 2798 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2799 struct f2fs_journal *journal = curseg->journal; 2800 int i; 2801 2802 down_write(&curseg->journal_rwsem); 2803 for (i = 0; i < nats_in_cursum(journal); i++) { 2804 struct nat_entry *ne; 2805 struct f2fs_nat_entry raw_ne; 2806 nid_t nid = le32_to_cpu(nid_in_journal(journal, i)); 2807 2808 if (f2fs_check_nid_range(sbi, nid)) 2809 continue; 2810 2811 raw_ne = nat_in_journal(journal, i); 2812 2813 ne = __lookup_nat_cache(nm_i, nid); 2814 if (!ne) { 2815 ne = __alloc_nat_entry(nid, true); 2816 __init_nat_entry(nm_i, ne, &raw_ne, true); 2817 } 2818 2819 /* 2820 * if a free nat in journal has not been used after last 2821 * checkpoint, we should remove it from available nids, 2822 * since later we will add it again. 2823 */ 2824 if (!get_nat_flag(ne, IS_DIRTY) && 2825 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) { 2826 spin_lock(&nm_i->nid_list_lock); 2827 nm_i->available_nids--; 2828 spin_unlock(&nm_i->nid_list_lock); 2829 } 2830 2831 __set_nat_cache_dirty(nm_i, ne); 2832 } 2833 update_nats_in_cursum(journal, -i); 2834 up_write(&curseg->journal_rwsem); 2835 } 2836 2837 static void __adjust_nat_entry_set(struct nat_entry_set *nes, 2838 struct list_head *head, int max) 2839 { 2840 struct nat_entry_set *cur; 2841 2842 if (nes->entry_cnt >= max) 2843 goto add_out; 2844 2845 list_for_each_entry(cur, head, set_list) { 2846 if (cur->entry_cnt >= nes->entry_cnt) { 2847 list_add(&nes->set_list, cur->set_list.prev); 2848 return; 2849 } 2850 } 2851 add_out: 2852 list_add_tail(&nes->set_list, head); 2853 } 2854 2855 static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid, 2856 struct page *page) 2857 { 2858 struct f2fs_nm_info *nm_i = NM_I(sbi); 2859 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK; 2860 struct f2fs_nat_block *nat_blk = page_address(page); 2861 int valid = 0; 2862 int i = 0; 2863 2864 if (!enabled_nat_bits(sbi, NULL)) 2865 return; 2866 2867 if (nat_index == 0) { 2868 valid = 1; 2869 i = 1; 2870 } 2871 for (; i < NAT_ENTRY_PER_BLOCK; i++) { 2872 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR) 2873 valid++; 2874 } 2875 if (valid == 0) { 2876 __set_bit_le(nat_index, nm_i->empty_nat_bits); 2877 __clear_bit_le(nat_index, nm_i->full_nat_bits); 2878 return; 2879 } 2880 2881 __clear_bit_le(nat_index, nm_i->empty_nat_bits); 2882 if (valid == NAT_ENTRY_PER_BLOCK) 2883 __set_bit_le(nat_index, nm_i->full_nat_bits); 2884 else 2885 __clear_bit_le(nat_index, nm_i->full_nat_bits); 2886 } 2887 2888 static int __flush_nat_entry_set(struct f2fs_sb_info *sbi, 2889 struct nat_entry_set *set, struct cp_control *cpc) 2890 { 2891 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2892 struct f2fs_journal *journal = curseg->journal; 2893 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK; 2894 bool to_journal = true; 2895 struct f2fs_nat_block *nat_blk; 2896 struct nat_entry *ne, *cur; 2897 struct page *page = NULL; 2898 2899 /* 2900 * there are two steps to flush nat entries: 2901 * #1, flush nat entries to journal in current hot data summary block. 2902 * #2, flush nat entries to nat page. 2903 */ 2904 if (enabled_nat_bits(sbi, cpc) || 2905 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL)) 2906 to_journal = false; 2907 2908 if (to_journal) { 2909 down_write(&curseg->journal_rwsem); 2910 } else { 2911 page = get_next_nat_page(sbi, start_nid); 2912 if (IS_ERR(page)) 2913 return PTR_ERR(page); 2914 2915 nat_blk = page_address(page); 2916 f2fs_bug_on(sbi, !nat_blk); 2917 } 2918 2919 /* flush dirty nats in nat entry set */ 2920 list_for_each_entry_safe(ne, cur, &set->entry_list, list) { 2921 struct f2fs_nat_entry *raw_ne; 2922 nid_t nid = nat_get_nid(ne); 2923 int offset; 2924 2925 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR); 2926 2927 if (to_journal) { 2928 offset = f2fs_lookup_journal_in_cursum(journal, 2929 NAT_JOURNAL, nid, 1); 2930 f2fs_bug_on(sbi, offset < 0); 2931 raw_ne = &nat_in_journal(journal, offset); 2932 nid_in_journal(journal, offset) = cpu_to_le32(nid); 2933 } else { 2934 raw_ne = &nat_blk->entries[nid - start_nid]; 2935 } 2936 raw_nat_from_node_info(raw_ne, &ne->ni); 2937 nat_reset_flag(ne); 2938 __clear_nat_cache_dirty(NM_I(sbi), set, ne); 2939 if (nat_get_blkaddr(ne) == NULL_ADDR) { 2940 add_free_nid(sbi, nid, false, true); 2941 } else { 2942 spin_lock(&NM_I(sbi)->nid_list_lock); 2943 update_free_nid_bitmap(sbi, nid, false, false); 2944 spin_unlock(&NM_I(sbi)->nid_list_lock); 2945 } 2946 } 2947 2948 if (to_journal) { 2949 up_write(&curseg->journal_rwsem); 2950 } else { 2951 __update_nat_bits(sbi, start_nid, page); 2952 f2fs_put_page(page, 1); 2953 } 2954 2955 /* Allow dirty nats by node block allocation in write_begin */ 2956 if (!set->entry_cnt) { 2957 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set); 2958 kmem_cache_free(nat_entry_set_slab, set); 2959 } 2960 return 0; 2961 } 2962 2963 /* 2964 * This function is called during the checkpointing process. 2965 */ 2966 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) 2967 { 2968 struct f2fs_nm_info *nm_i = NM_I(sbi); 2969 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2970 struct f2fs_journal *journal = curseg->journal; 2971 struct nat_entry_set *setvec[SETVEC_SIZE]; 2972 struct nat_entry_set *set, *tmp; 2973 unsigned int found; 2974 nid_t set_idx = 0; 2975 LIST_HEAD(sets); 2976 int err = 0; 2977 2978 /* 2979 * during unmount, let's flush nat_bits before checking 2980 * nat_cnt[DIRTY_NAT]. 2981 */ 2982 if (enabled_nat_bits(sbi, cpc)) { 2983 down_write(&nm_i->nat_tree_lock); 2984 remove_nats_in_journal(sbi); 2985 up_write(&nm_i->nat_tree_lock); 2986 } 2987 2988 if (!nm_i->nat_cnt[DIRTY_NAT]) 2989 return 0; 2990 2991 down_write(&nm_i->nat_tree_lock); 2992 2993 /* 2994 * if there are no enough space in journal to store dirty nat 2995 * entries, remove all entries from journal and merge them 2996 * into nat entry set. 2997 */ 2998 if (enabled_nat_bits(sbi, cpc) || 2999 !__has_cursum_space(journal, 3000 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL)) 3001 remove_nats_in_journal(sbi); 3002 3003 while ((found = __gang_lookup_nat_set(nm_i, 3004 set_idx, SETVEC_SIZE, setvec))) { 3005 unsigned idx; 3006 3007 set_idx = setvec[found - 1]->set + 1; 3008 for (idx = 0; idx < found; idx++) 3009 __adjust_nat_entry_set(setvec[idx], &sets, 3010 MAX_NAT_JENTRIES(journal)); 3011 } 3012 3013 /* flush dirty nats in nat entry set */ 3014 list_for_each_entry_safe(set, tmp, &sets, set_list) { 3015 err = __flush_nat_entry_set(sbi, set, cpc); 3016 if (err) 3017 break; 3018 } 3019 3020 up_write(&nm_i->nat_tree_lock); 3021 /* Allow dirty nats by node block allocation in write_begin */ 3022 3023 return err; 3024 } 3025 3026 static int __get_nat_bitmaps(struct f2fs_sb_info *sbi) 3027 { 3028 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 3029 struct f2fs_nm_info *nm_i = NM_I(sbi); 3030 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE; 3031 unsigned int i; 3032 __u64 cp_ver = cur_cp_version(ckpt); 3033 block_t nat_bits_addr; 3034 3035 if (!enabled_nat_bits(sbi, NULL)) 3036 return 0; 3037 3038 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8); 3039 nm_i->nat_bits = f2fs_kvzalloc(sbi, 3040 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL); 3041 if (!nm_i->nat_bits) 3042 return -ENOMEM; 3043 3044 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg - 3045 nm_i->nat_bits_blocks; 3046 for (i = 0; i < nm_i->nat_bits_blocks; i++) { 3047 struct page *page; 3048 3049 page = f2fs_get_meta_page(sbi, nat_bits_addr++); 3050 if (IS_ERR(page)) 3051 return PTR_ERR(page); 3052 3053 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS), 3054 page_address(page), F2FS_BLKSIZE); 3055 f2fs_put_page(page, 1); 3056 } 3057 3058 cp_ver |= (cur_cp_crc(ckpt) << 32); 3059 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) { 3060 disable_nat_bits(sbi, true); 3061 return 0; 3062 } 3063 3064 nm_i->full_nat_bits = nm_i->nat_bits + 8; 3065 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes; 3066 3067 f2fs_notice(sbi, "Found nat_bits in checkpoint"); 3068 return 0; 3069 } 3070 3071 static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi) 3072 { 3073 struct f2fs_nm_info *nm_i = NM_I(sbi); 3074 unsigned int i = 0; 3075 nid_t nid, last_nid; 3076 3077 if (!enabled_nat_bits(sbi, NULL)) 3078 return; 3079 3080 for (i = 0; i < nm_i->nat_blocks; i++) { 3081 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i); 3082 if (i >= nm_i->nat_blocks) 3083 break; 3084 3085 __set_bit_le(i, nm_i->nat_block_bitmap); 3086 3087 nid = i * NAT_ENTRY_PER_BLOCK; 3088 last_nid = nid + NAT_ENTRY_PER_BLOCK; 3089 3090 spin_lock(&NM_I(sbi)->nid_list_lock); 3091 for (; nid < last_nid; nid++) 3092 update_free_nid_bitmap(sbi, nid, true, true); 3093 spin_unlock(&NM_I(sbi)->nid_list_lock); 3094 } 3095 3096 for (i = 0; i < nm_i->nat_blocks; i++) { 3097 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i); 3098 if (i >= nm_i->nat_blocks) 3099 break; 3100 3101 __set_bit_le(i, nm_i->nat_block_bitmap); 3102 } 3103 } 3104 3105 static int init_node_manager(struct f2fs_sb_info *sbi) 3106 { 3107 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); 3108 struct f2fs_nm_info *nm_i = NM_I(sbi); 3109 unsigned char *version_bitmap; 3110 unsigned int nat_segs; 3111 int err; 3112 3113 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); 3114 3115 /* segment_count_nat includes pair segment so divide to 2. */ 3116 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; 3117 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); 3118 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks; 3119 3120 /* not used nids: 0, node, meta, (and root counted as valid node) */ 3121 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count - 3122 F2FS_RESERVED_NODE_NUM; 3123 nm_i->nid_cnt[FREE_NID] = 0; 3124 nm_i->nid_cnt[PREALLOC_NID] = 0; 3125 nm_i->ram_thresh = DEF_RAM_THRESHOLD; 3126 nm_i->ra_nid_pages = DEF_RA_NID_PAGES; 3127 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD; 3128 3129 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC); 3130 INIT_LIST_HEAD(&nm_i->free_nid_list); 3131 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO); 3132 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO); 3133 INIT_LIST_HEAD(&nm_i->nat_entries); 3134 spin_lock_init(&nm_i->nat_list_lock); 3135 3136 mutex_init(&nm_i->build_lock); 3137 spin_lock_init(&nm_i->nid_list_lock); 3138 init_rwsem(&nm_i->nat_tree_lock); 3139 3140 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 3141 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 3142 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 3143 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size, 3144 GFP_KERNEL); 3145 if (!nm_i->nat_bitmap) 3146 return -ENOMEM; 3147 3148 err = __get_nat_bitmaps(sbi); 3149 if (err) 3150 return err; 3151 3152 #ifdef CONFIG_F2FS_CHECK_FS 3153 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size, 3154 GFP_KERNEL); 3155 if (!nm_i->nat_bitmap_mir) 3156 return -ENOMEM; 3157 #endif 3158 3159 return 0; 3160 } 3161 3162 static int init_free_nid_cache(struct f2fs_sb_info *sbi) 3163 { 3164 struct f2fs_nm_info *nm_i = NM_I(sbi); 3165 int i; 3166 3167 nm_i->free_nid_bitmap = 3168 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *), 3169 nm_i->nat_blocks), 3170 GFP_KERNEL); 3171 if (!nm_i->free_nid_bitmap) 3172 return -ENOMEM; 3173 3174 for (i = 0; i < nm_i->nat_blocks; i++) { 3175 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi, 3176 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL); 3177 if (!nm_i->free_nid_bitmap[i]) 3178 return -ENOMEM; 3179 } 3180 3181 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8, 3182 GFP_KERNEL); 3183 if (!nm_i->nat_block_bitmap) 3184 return -ENOMEM; 3185 3186 nm_i->free_nid_count = 3187 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short), 3188 nm_i->nat_blocks), 3189 GFP_KERNEL); 3190 if (!nm_i->free_nid_count) 3191 return -ENOMEM; 3192 return 0; 3193 } 3194 3195 int f2fs_build_node_manager(struct f2fs_sb_info *sbi) 3196 { 3197 int err; 3198 3199 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info), 3200 GFP_KERNEL); 3201 if (!sbi->nm_info) 3202 return -ENOMEM; 3203 3204 err = init_node_manager(sbi); 3205 if (err) 3206 return err; 3207 3208 err = init_free_nid_cache(sbi); 3209 if (err) 3210 return err; 3211 3212 /* load free nid status from nat_bits table */ 3213 load_free_nid_bitmap(sbi); 3214 3215 return f2fs_build_free_nids(sbi, true, true); 3216 } 3217 3218 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi) 3219 { 3220 struct f2fs_nm_info *nm_i = NM_I(sbi); 3221 struct free_nid *i, *next_i; 3222 struct nat_entry *natvec[NATVEC_SIZE]; 3223 struct nat_entry_set *setvec[SETVEC_SIZE]; 3224 nid_t nid = 0; 3225 unsigned int found; 3226 3227 if (!nm_i) 3228 return; 3229 3230 /* destroy free nid list */ 3231 spin_lock(&nm_i->nid_list_lock); 3232 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) { 3233 __remove_free_nid(sbi, i, FREE_NID); 3234 spin_unlock(&nm_i->nid_list_lock); 3235 kmem_cache_free(free_nid_slab, i); 3236 spin_lock(&nm_i->nid_list_lock); 3237 } 3238 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]); 3239 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]); 3240 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list)); 3241 spin_unlock(&nm_i->nid_list_lock); 3242 3243 /* destroy nat cache */ 3244 down_write(&nm_i->nat_tree_lock); 3245 while ((found = __gang_lookup_nat_cache(nm_i, 3246 nid, NATVEC_SIZE, natvec))) { 3247 unsigned idx; 3248 3249 nid = nat_get_nid(natvec[found - 1]) + 1; 3250 for (idx = 0; idx < found; idx++) { 3251 spin_lock(&nm_i->nat_list_lock); 3252 list_del(&natvec[idx]->list); 3253 spin_unlock(&nm_i->nat_list_lock); 3254 3255 __del_from_nat_cache(nm_i, natvec[idx]); 3256 } 3257 } 3258 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]); 3259 3260 /* destroy nat set cache */ 3261 nid = 0; 3262 while ((found = __gang_lookup_nat_set(nm_i, 3263 nid, SETVEC_SIZE, setvec))) { 3264 unsigned idx; 3265 3266 nid = setvec[found - 1]->set + 1; 3267 for (idx = 0; idx < found; idx++) { 3268 /* entry_cnt is not zero, when cp_error was occurred */ 3269 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list)); 3270 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set); 3271 kmem_cache_free(nat_entry_set_slab, setvec[idx]); 3272 } 3273 } 3274 up_write(&nm_i->nat_tree_lock); 3275 3276 kvfree(nm_i->nat_block_bitmap); 3277 if (nm_i->free_nid_bitmap) { 3278 int i; 3279 3280 for (i = 0; i < nm_i->nat_blocks; i++) 3281 kvfree(nm_i->free_nid_bitmap[i]); 3282 kvfree(nm_i->free_nid_bitmap); 3283 } 3284 kvfree(nm_i->free_nid_count); 3285 3286 kvfree(nm_i->nat_bitmap); 3287 kvfree(nm_i->nat_bits); 3288 #ifdef CONFIG_F2FS_CHECK_FS 3289 kvfree(nm_i->nat_bitmap_mir); 3290 #endif 3291 sbi->nm_info = NULL; 3292 kfree(nm_i); 3293 } 3294 3295 int __init f2fs_create_node_manager_caches(void) 3296 { 3297 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry", 3298 sizeof(struct nat_entry)); 3299 if (!nat_entry_slab) 3300 goto fail; 3301 3302 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid", 3303 sizeof(struct free_nid)); 3304 if (!free_nid_slab) 3305 goto destroy_nat_entry; 3306 3307 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set", 3308 sizeof(struct nat_entry_set)); 3309 if (!nat_entry_set_slab) 3310 goto destroy_free_nid; 3311 3312 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry", 3313 sizeof(struct fsync_node_entry)); 3314 if (!fsync_node_entry_slab) 3315 goto destroy_nat_entry_set; 3316 return 0; 3317 3318 destroy_nat_entry_set: 3319 kmem_cache_destroy(nat_entry_set_slab); 3320 destroy_free_nid: 3321 kmem_cache_destroy(free_nid_slab); 3322 destroy_nat_entry: 3323 kmem_cache_destroy(nat_entry_slab); 3324 fail: 3325 return -ENOMEM; 3326 } 3327 3328 void f2fs_destroy_node_manager_caches(void) 3329 { 3330 kmem_cache_destroy(fsync_node_entry_slab); 3331 kmem_cache_destroy(nat_entry_set_slab); 3332 kmem_cache_destroy(free_nid_slab); 3333 kmem_cache_destroy(nat_entry_slab); 3334 } 3335