1 /* 2 * fs/f2fs/node.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 #include <linux/mpage.h> 14 #include <linux/backing-dev.h> 15 #include <linux/blkdev.h> 16 #include <linux/pagevec.h> 17 #include <linux/swap.h> 18 19 #include "f2fs.h" 20 #include "node.h" 21 #include "segment.h" 22 #include <trace/events/f2fs.h> 23 24 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock) 25 26 static struct kmem_cache *nat_entry_slab; 27 static struct kmem_cache *free_nid_slab; 28 static struct kmem_cache *nat_entry_set_slab; 29 30 bool available_free_memory(struct f2fs_sb_info *sbi, int type) 31 { 32 struct f2fs_nm_info *nm_i = NM_I(sbi); 33 struct sysinfo val; 34 unsigned long mem_size = 0; 35 bool res = false; 36 37 si_meminfo(&val); 38 /* give 25%, 25%, 50% memory for each components respectively */ 39 if (type == FREE_NIDS) { 40 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12; 41 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2); 42 } else if (type == NAT_ENTRIES) { 43 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12; 44 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2); 45 } else if (type == DIRTY_DENTS) { 46 if (sbi->sb->s_bdi->dirty_exceeded) 47 return false; 48 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS); 49 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1); 50 } 51 return res; 52 } 53 54 static void clear_node_page_dirty(struct page *page) 55 { 56 struct address_space *mapping = page->mapping; 57 unsigned int long flags; 58 59 if (PageDirty(page)) { 60 spin_lock_irqsave(&mapping->tree_lock, flags); 61 radix_tree_tag_clear(&mapping->page_tree, 62 page_index(page), 63 PAGECACHE_TAG_DIRTY); 64 spin_unlock_irqrestore(&mapping->tree_lock, flags); 65 66 clear_page_dirty_for_io(page); 67 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES); 68 } 69 ClearPageUptodate(page); 70 } 71 72 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 73 { 74 pgoff_t index = current_nat_addr(sbi, nid); 75 return get_meta_page(sbi, index); 76 } 77 78 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 79 { 80 struct page *src_page; 81 struct page *dst_page; 82 pgoff_t src_off; 83 pgoff_t dst_off; 84 void *src_addr; 85 void *dst_addr; 86 struct f2fs_nm_info *nm_i = NM_I(sbi); 87 88 src_off = current_nat_addr(sbi, nid); 89 dst_off = next_nat_addr(sbi, src_off); 90 91 /* get current nat block page with lock */ 92 src_page = get_meta_page(sbi, src_off); 93 dst_page = grab_meta_page(sbi, dst_off); 94 f2fs_bug_on(sbi, PageDirty(src_page)); 95 96 src_addr = page_address(src_page); 97 dst_addr = page_address(dst_page); 98 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); 99 set_page_dirty(dst_page); 100 f2fs_put_page(src_page, 1); 101 102 set_to_next_nat(nm_i, nid); 103 104 return dst_page; 105 } 106 107 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n) 108 { 109 return radix_tree_lookup(&nm_i->nat_root, n); 110 } 111 112 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i, 113 nid_t start, unsigned int nr, struct nat_entry **ep) 114 { 115 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr); 116 } 117 118 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) 119 { 120 list_del(&e->list); 121 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); 122 nm_i->nat_cnt--; 123 kmem_cache_free(nat_entry_slab, e); 124 } 125 126 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i, 127 struct nat_entry *ne) 128 { 129 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid); 130 struct nat_entry_set *head; 131 132 if (get_nat_flag(ne, IS_DIRTY)) 133 return; 134 retry: 135 head = radix_tree_lookup(&nm_i->nat_set_root, set); 136 if (!head) { 137 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC); 138 139 INIT_LIST_HEAD(&head->entry_list); 140 INIT_LIST_HEAD(&head->set_list); 141 head->set = set; 142 head->entry_cnt = 0; 143 144 if (radix_tree_insert(&nm_i->nat_set_root, set, head)) { 145 cond_resched(); 146 goto retry; 147 } 148 } 149 list_move_tail(&ne->list, &head->entry_list); 150 nm_i->dirty_nat_cnt++; 151 head->entry_cnt++; 152 set_nat_flag(ne, IS_DIRTY, true); 153 } 154 155 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i, 156 struct nat_entry *ne) 157 { 158 nid_t set = ne->ni.nid / NAT_ENTRY_PER_BLOCK; 159 struct nat_entry_set *head; 160 161 head = radix_tree_lookup(&nm_i->nat_set_root, set); 162 if (head) { 163 list_move_tail(&ne->list, &nm_i->nat_entries); 164 set_nat_flag(ne, IS_DIRTY, false); 165 head->entry_cnt--; 166 nm_i->dirty_nat_cnt--; 167 } 168 } 169 170 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, 171 nid_t start, unsigned int nr, struct nat_entry_set **ep) 172 { 173 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep, 174 start, nr); 175 } 176 177 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid) 178 { 179 struct f2fs_nm_info *nm_i = NM_I(sbi); 180 struct nat_entry *e; 181 bool is_cp = true; 182 183 read_lock(&nm_i->nat_tree_lock); 184 e = __lookup_nat_cache(nm_i, nid); 185 if (e && !get_nat_flag(e, IS_CHECKPOINTED)) 186 is_cp = false; 187 read_unlock(&nm_i->nat_tree_lock); 188 return is_cp; 189 } 190 191 bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino) 192 { 193 struct f2fs_nm_info *nm_i = NM_I(sbi); 194 struct nat_entry *e; 195 bool fsynced = false; 196 197 read_lock(&nm_i->nat_tree_lock); 198 e = __lookup_nat_cache(nm_i, ino); 199 if (e && get_nat_flag(e, HAS_FSYNCED_INODE)) 200 fsynced = true; 201 read_unlock(&nm_i->nat_tree_lock); 202 return fsynced; 203 } 204 205 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino) 206 { 207 struct f2fs_nm_info *nm_i = NM_I(sbi); 208 struct nat_entry *e; 209 bool need_update = true; 210 211 read_lock(&nm_i->nat_tree_lock); 212 e = __lookup_nat_cache(nm_i, ino); 213 if (e && get_nat_flag(e, HAS_LAST_FSYNC) && 214 (get_nat_flag(e, IS_CHECKPOINTED) || 215 get_nat_flag(e, HAS_FSYNCED_INODE))) 216 need_update = false; 217 read_unlock(&nm_i->nat_tree_lock); 218 return need_update; 219 } 220 221 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid) 222 { 223 struct nat_entry *new; 224 225 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC); 226 if (!new) 227 return NULL; 228 if (radix_tree_insert(&nm_i->nat_root, nid, new)) { 229 kmem_cache_free(nat_entry_slab, new); 230 return NULL; 231 } 232 memset(new, 0, sizeof(struct nat_entry)); 233 nat_set_nid(new, nid); 234 nat_reset_flag(new); 235 list_add_tail(&new->list, &nm_i->nat_entries); 236 nm_i->nat_cnt++; 237 return new; 238 } 239 240 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid, 241 struct f2fs_nat_entry *ne) 242 { 243 struct nat_entry *e; 244 retry: 245 write_lock(&nm_i->nat_tree_lock); 246 e = __lookup_nat_cache(nm_i, nid); 247 if (!e) { 248 e = grab_nat_entry(nm_i, nid); 249 if (!e) { 250 write_unlock(&nm_i->nat_tree_lock); 251 goto retry; 252 } 253 node_info_from_raw_nat(&e->ni, ne); 254 } 255 write_unlock(&nm_i->nat_tree_lock); 256 } 257 258 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni, 259 block_t new_blkaddr, bool fsync_done) 260 { 261 struct f2fs_nm_info *nm_i = NM_I(sbi); 262 struct nat_entry *e; 263 retry: 264 write_lock(&nm_i->nat_tree_lock); 265 e = __lookup_nat_cache(nm_i, ni->nid); 266 if (!e) { 267 e = grab_nat_entry(nm_i, ni->nid); 268 if (!e) { 269 write_unlock(&nm_i->nat_tree_lock); 270 goto retry; 271 } 272 e->ni = *ni; 273 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR); 274 } else if (new_blkaddr == NEW_ADDR) { 275 /* 276 * when nid is reallocated, 277 * previous nat entry can be remained in nat cache. 278 * So, reinitialize it with new information. 279 */ 280 e->ni = *ni; 281 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR); 282 } 283 284 /* sanity check */ 285 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr); 286 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR && 287 new_blkaddr == NULL_ADDR); 288 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR && 289 new_blkaddr == NEW_ADDR); 290 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR && 291 nat_get_blkaddr(e) != NULL_ADDR && 292 new_blkaddr == NEW_ADDR); 293 294 /* increment version no as node is removed */ 295 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) { 296 unsigned char version = nat_get_version(e); 297 nat_set_version(e, inc_node_version(version)); 298 } 299 300 /* change address */ 301 nat_set_blkaddr(e, new_blkaddr); 302 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR) 303 set_nat_flag(e, IS_CHECKPOINTED, false); 304 __set_nat_cache_dirty(nm_i, e); 305 306 /* update fsync_mark if its inode nat entry is still alive */ 307 e = __lookup_nat_cache(nm_i, ni->ino); 308 if (e) { 309 if (fsync_done && ni->nid == ni->ino) 310 set_nat_flag(e, HAS_FSYNCED_INODE, true); 311 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done); 312 } 313 write_unlock(&nm_i->nat_tree_lock); 314 } 315 316 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink) 317 { 318 struct f2fs_nm_info *nm_i = NM_I(sbi); 319 320 if (available_free_memory(sbi, NAT_ENTRIES)) 321 return 0; 322 323 write_lock(&nm_i->nat_tree_lock); 324 while (nr_shrink && !list_empty(&nm_i->nat_entries)) { 325 struct nat_entry *ne; 326 ne = list_first_entry(&nm_i->nat_entries, 327 struct nat_entry, list); 328 __del_from_nat_cache(nm_i, ne); 329 nr_shrink--; 330 } 331 write_unlock(&nm_i->nat_tree_lock); 332 return nr_shrink; 333 } 334 335 /* 336 * This function always returns success 337 */ 338 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) 339 { 340 struct f2fs_nm_info *nm_i = NM_I(sbi); 341 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 342 struct f2fs_summary_block *sum = curseg->sum_blk; 343 nid_t start_nid = START_NID(nid); 344 struct f2fs_nat_block *nat_blk; 345 struct page *page = NULL; 346 struct f2fs_nat_entry ne; 347 struct nat_entry *e; 348 int i; 349 350 memset(&ne, 0, sizeof(struct f2fs_nat_entry)); 351 ni->nid = nid; 352 353 /* Check nat cache */ 354 read_lock(&nm_i->nat_tree_lock); 355 e = __lookup_nat_cache(nm_i, nid); 356 if (e) { 357 ni->ino = nat_get_ino(e); 358 ni->blk_addr = nat_get_blkaddr(e); 359 ni->version = nat_get_version(e); 360 } 361 read_unlock(&nm_i->nat_tree_lock); 362 if (e) 363 return; 364 365 /* Check current segment summary */ 366 mutex_lock(&curseg->curseg_mutex); 367 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0); 368 if (i >= 0) { 369 ne = nat_in_journal(sum, i); 370 node_info_from_raw_nat(ni, &ne); 371 } 372 mutex_unlock(&curseg->curseg_mutex); 373 if (i >= 0) 374 goto cache; 375 376 /* Fill node_info from nat page */ 377 page = get_current_nat_page(sbi, start_nid); 378 nat_blk = (struct f2fs_nat_block *)page_address(page); 379 ne = nat_blk->entries[nid - start_nid]; 380 node_info_from_raw_nat(ni, &ne); 381 f2fs_put_page(page, 1); 382 cache: 383 /* cache nat entry */ 384 cache_nat_entry(NM_I(sbi), nid, &ne); 385 } 386 387 /* 388 * The maximum depth is four. 389 * Offset[0] will have raw inode offset. 390 */ 391 static int get_node_path(struct f2fs_inode_info *fi, long block, 392 int offset[4], unsigned int noffset[4]) 393 { 394 const long direct_index = ADDRS_PER_INODE(fi); 395 const long direct_blks = ADDRS_PER_BLOCK; 396 const long dptrs_per_blk = NIDS_PER_BLOCK; 397 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK; 398 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK; 399 int n = 0; 400 int level = 0; 401 402 noffset[0] = 0; 403 404 if (block < direct_index) { 405 offset[n] = block; 406 goto got; 407 } 408 block -= direct_index; 409 if (block < direct_blks) { 410 offset[n++] = NODE_DIR1_BLOCK; 411 noffset[n] = 1; 412 offset[n] = block; 413 level = 1; 414 goto got; 415 } 416 block -= direct_blks; 417 if (block < direct_blks) { 418 offset[n++] = NODE_DIR2_BLOCK; 419 noffset[n] = 2; 420 offset[n] = block; 421 level = 1; 422 goto got; 423 } 424 block -= direct_blks; 425 if (block < indirect_blks) { 426 offset[n++] = NODE_IND1_BLOCK; 427 noffset[n] = 3; 428 offset[n++] = block / direct_blks; 429 noffset[n] = 4 + offset[n - 1]; 430 offset[n] = block % direct_blks; 431 level = 2; 432 goto got; 433 } 434 block -= indirect_blks; 435 if (block < indirect_blks) { 436 offset[n++] = NODE_IND2_BLOCK; 437 noffset[n] = 4 + dptrs_per_blk; 438 offset[n++] = block / direct_blks; 439 noffset[n] = 5 + dptrs_per_blk + offset[n - 1]; 440 offset[n] = block % direct_blks; 441 level = 2; 442 goto got; 443 } 444 block -= indirect_blks; 445 if (block < dindirect_blks) { 446 offset[n++] = NODE_DIND_BLOCK; 447 noffset[n] = 5 + (dptrs_per_blk * 2); 448 offset[n++] = block / indirect_blks; 449 noffset[n] = 6 + (dptrs_per_blk * 2) + 450 offset[n - 1] * (dptrs_per_blk + 1); 451 offset[n++] = (block / direct_blks) % dptrs_per_blk; 452 noffset[n] = 7 + (dptrs_per_blk * 2) + 453 offset[n - 2] * (dptrs_per_blk + 1) + 454 offset[n - 1]; 455 offset[n] = block % direct_blks; 456 level = 3; 457 goto got; 458 } else { 459 BUG(); 460 } 461 got: 462 return level; 463 } 464 465 /* 466 * Caller should call f2fs_put_dnode(dn). 467 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and 468 * f2fs_unlock_op() only if ro is not set RDONLY_NODE. 469 * In the case of RDONLY_NODE, we don't need to care about mutex. 470 */ 471 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode) 472 { 473 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 474 struct page *npage[4]; 475 struct page *parent; 476 int offset[4]; 477 unsigned int noffset[4]; 478 nid_t nids[4]; 479 int level, i; 480 int err = 0; 481 482 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset); 483 484 nids[0] = dn->inode->i_ino; 485 npage[0] = dn->inode_page; 486 487 if (!npage[0]) { 488 npage[0] = get_node_page(sbi, nids[0]); 489 if (IS_ERR(npage[0])) 490 return PTR_ERR(npage[0]); 491 } 492 parent = npage[0]; 493 if (level != 0) 494 nids[1] = get_nid(parent, offset[0], true); 495 dn->inode_page = npage[0]; 496 dn->inode_page_locked = true; 497 498 /* get indirect or direct nodes */ 499 for (i = 1; i <= level; i++) { 500 bool done = false; 501 502 if (!nids[i] && mode == ALLOC_NODE) { 503 /* alloc new node */ 504 if (!alloc_nid(sbi, &(nids[i]))) { 505 err = -ENOSPC; 506 goto release_pages; 507 } 508 509 dn->nid = nids[i]; 510 npage[i] = new_node_page(dn, noffset[i], NULL); 511 if (IS_ERR(npage[i])) { 512 alloc_nid_failed(sbi, nids[i]); 513 err = PTR_ERR(npage[i]); 514 goto release_pages; 515 } 516 517 set_nid(parent, offset[i - 1], nids[i], i == 1); 518 alloc_nid_done(sbi, nids[i]); 519 done = true; 520 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) { 521 npage[i] = get_node_page_ra(parent, offset[i - 1]); 522 if (IS_ERR(npage[i])) { 523 err = PTR_ERR(npage[i]); 524 goto release_pages; 525 } 526 done = true; 527 } 528 if (i == 1) { 529 dn->inode_page_locked = false; 530 unlock_page(parent); 531 } else { 532 f2fs_put_page(parent, 1); 533 } 534 535 if (!done) { 536 npage[i] = get_node_page(sbi, nids[i]); 537 if (IS_ERR(npage[i])) { 538 err = PTR_ERR(npage[i]); 539 f2fs_put_page(npage[0], 0); 540 goto release_out; 541 } 542 } 543 if (i < level) { 544 parent = npage[i]; 545 nids[i + 1] = get_nid(parent, offset[i], false); 546 } 547 } 548 dn->nid = nids[level]; 549 dn->ofs_in_node = offset[level]; 550 dn->node_page = npage[level]; 551 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); 552 return 0; 553 554 release_pages: 555 f2fs_put_page(parent, 1); 556 if (i > 1) 557 f2fs_put_page(npage[0], 0); 558 release_out: 559 dn->inode_page = NULL; 560 dn->node_page = NULL; 561 return err; 562 } 563 564 static void truncate_node(struct dnode_of_data *dn) 565 { 566 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 567 struct node_info ni; 568 569 get_node_info(sbi, dn->nid, &ni); 570 if (dn->inode->i_blocks == 0) { 571 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR); 572 goto invalidate; 573 } 574 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); 575 576 /* Deallocate node address */ 577 invalidate_blocks(sbi, ni.blk_addr); 578 dec_valid_node_count(sbi, dn->inode); 579 set_node_addr(sbi, &ni, NULL_ADDR, false); 580 581 if (dn->nid == dn->inode->i_ino) { 582 remove_orphan_inode(sbi, dn->nid); 583 dec_valid_inode_count(sbi); 584 } else { 585 sync_inode_page(dn); 586 } 587 invalidate: 588 clear_node_page_dirty(dn->node_page); 589 F2FS_SET_SB_DIRT(sbi); 590 591 f2fs_put_page(dn->node_page, 1); 592 593 invalidate_mapping_pages(NODE_MAPPING(sbi), 594 dn->node_page->index, dn->node_page->index); 595 596 dn->node_page = NULL; 597 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr); 598 } 599 600 static int truncate_dnode(struct dnode_of_data *dn) 601 { 602 struct page *page; 603 604 if (dn->nid == 0) 605 return 1; 606 607 /* get direct node */ 608 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid); 609 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT) 610 return 1; 611 else if (IS_ERR(page)) 612 return PTR_ERR(page); 613 614 /* Make dnode_of_data for parameter */ 615 dn->node_page = page; 616 dn->ofs_in_node = 0; 617 truncate_data_blocks(dn); 618 truncate_node(dn); 619 return 1; 620 } 621 622 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs, 623 int ofs, int depth) 624 { 625 struct dnode_of_data rdn = *dn; 626 struct page *page; 627 struct f2fs_node *rn; 628 nid_t child_nid; 629 unsigned int child_nofs; 630 int freed = 0; 631 int i, ret; 632 633 if (dn->nid == 0) 634 return NIDS_PER_BLOCK + 1; 635 636 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr); 637 638 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid); 639 if (IS_ERR(page)) { 640 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page)); 641 return PTR_ERR(page); 642 } 643 644 rn = F2FS_NODE(page); 645 if (depth < 3) { 646 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) { 647 child_nid = le32_to_cpu(rn->in.nid[i]); 648 if (child_nid == 0) 649 continue; 650 rdn.nid = child_nid; 651 ret = truncate_dnode(&rdn); 652 if (ret < 0) 653 goto out_err; 654 set_nid(page, i, 0, false); 655 } 656 } else { 657 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1; 658 for (i = ofs; i < NIDS_PER_BLOCK; i++) { 659 child_nid = le32_to_cpu(rn->in.nid[i]); 660 if (child_nid == 0) { 661 child_nofs += NIDS_PER_BLOCK + 1; 662 continue; 663 } 664 rdn.nid = child_nid; 665 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1); 666 if (ret == (NIDS_PER_BLOCK + 1)) { 667 set_nid(page, i, 0, false); 668 child_nofs += ret; 669 } else if (ret < 0 && ret != -ENOENT) { 670 goto out_err; 671 } 672 } 673 freed = child_nofs; 674 } 675 676 if (!ofs) { 677 /* remove current indirect node */ 678 dn->node_page = page; 679 truncate_node(dn); 680 freed++; 681 } else { 682 f2fs_put_page(page, 1); 683 } 684 trace_f2fs_truncate_nodes_exit(dn->inode, freed); 685 return freed; 686 687 out_err: 688 f2fs_put_page(page, 1); 689 trace_f2fs_truncate_nodes_exit(dn->inode, ret); 690 return ret; 691 } 692 693 static int truncate_partial_nodes(struct dnode_of_data *dn, 694 struct f2fs_inode *ri, int *offset, int depth) 695 { 696 struct page *pages[2]; 697 nid_t nid[3]; 698 nid_t child_nid; 699 int err = 0; 700 int i; 701 int idx = depth - 2; 702 703 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); 704 if (!nid[0]) 705 return 0; 706 707 /* get indirect nodes in the path */ 708 for (i = 0; i < idx + 1; i++) { 709 /* reference count'll be increased */ 710 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]); 711 if (IS_ERR(pages[i])) { 712 err = PTR_ERR(pages[i]); 713 idx = i - 1; 714 goto fail; 715 } 716 nid[i + 1] = get_nid(pages[i], offset[i + 1], false); 717 } 718 719 /* free direct nodes linked to a partial indirect node */ 720 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) { 721 child_nid = get_nid(pages[idx], i, false); 722 if (!child_nid) 723 continue; 724 dn->nid = child_nid; 725 err = truncate_dnode(dn); 726 if (err < 0) 727 goto fail; 728 set_nid(pages[idx], i, 0, false); 729 } 730 731 if (offset[idx + 1] == 0) { 732 dn->node_page = pages[idx]; 733 dn->nid = nid[idx]; 734 truncate_node(dn); 735 } else { 736 f2fs_put_page(pages[idx], 1); 737 } 738 offset[idx]++; 739 offset[idx + 1] = 0; 740 idx--; 741 fail: 742 for (i = idx; i >= 0; i--) 743 f2fs_put_page(pages[i], 1); 744 745 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err); 746 747 return err; 748 } 749 750 /* 751 * All the block addresses of data and nodes should be nullified. 752 */ 753 int truncate_inode_blocks(struct inode *inode, pgoff_t from) 754 { 755 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 756 int err = 0, cont = 1; 757 int level, offset[4], noffset[4]; 758 unsigned int nofs = 0; 759 struct f2fs_inode *ri; 760 struct dnode_of_data dn; 761 struct page *page; 762 763 trace_f2fs_truncate_inode_blocks_enter(inode, from); 764 765 level = get_node_path(F2FS_I(inode), from, offset, noffset); 766 restart: 767 page = get_node_page(sbi, inode->i_ino); 768 if (IS_ERR(page)) { 769 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page)); 770 return PTR_ERR(page); 771 } 772 773 set_new_dnode(&dn, inode, page, NULL, 0); 774 unlock_page(page); 775 776 ri = F2FS_INODE(page); 777 switch (level) { 778 case 0: 779 case 1: 780 nofs = noffset[1]; 781 break; 782 case 2: 783 nofs = noffset[1]; 784 if (!offset[level - 1]) 785 goto skip_partial; 786 err = truncate_partial_nodes(&dn, ri, offset, level); 787 if (err < 0 && err != -ENOENT) 788 goto fail; 789 nofs += 1 + NIDS_PER_BLOCK; 790 break; 791 case 3: 792 nofs = 5 + 2 * NIDS_PER_BLOCK; 793 if (!offset[level - 1]) 794 goto skip_partial; 795 err = truncate_partial_nodes(&dn, ri, offset, level); 796 if (err < 0 && err != -ENOENT) 797 goto fail; 798 break; 799 default: 800 BUG(); 801 } 802 803 skip_partial: 804 while (cont) { 805 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); 806 switch (offset[0]) { 807 case NODE_DIR1_BLOCK: 808 case NODE_DIR2_BLOCK: 809 err = truncate_dnode(&dn); 810 break; 811 812 case NODE_IND1_BLOCK: 813 case NODE_IND2_BLOCK: 814 err = truncate_nodes(&dn, nofs, offset[1], 2); 815 break; 816 817 case NODE_DIND_BLOCK: 818 err = truncate_nodes(&dn, nofs, offset[1], 3); 819 cont = 0; 820 break; 821 822 default: 823 BUG(); 824 } 825 if (err < 0 && err != -ENOENT) 826 goto fail; 827 if (offset[1] == 0 && 828 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) { 829 lock_page(page); 830 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 831 f2fs_put_page(page, 1); 832 goto restart; 833 } 834 f2fs_wait_on_page_writeback(page, NODE); 835 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0; 836 set_page_dirty(page); 837 unlock_page(page); 838 } 839 offset[1] = 0; 840 offset[0]++; 841 nofs += err; 842 } 843 fail: 844 f2fs_put_page(page, 0); 845 trace_f2fs_truncate_inode_blocks_exit(inode, err); 846 return err > 0 ? 0 : err; 847 } 848 849 int truncate_xattr_node(struct inode *inode, struct page *page) 850 { 851 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 852 nid_t nid = F2FS_I(inode)->i_xattr_nid; 853 struct dnode_of_data dn; 854 struct page *npage; 855 856 if (!nid) 857 return 0; 858 859 npage = get_node_page(sbi, nid); 860 if (IS_ERR(npage)) 861 return PTR_ERR(npage); 862 863 F2FS_I(inode)->i_xattr_nid = 0; 864 865 /* need to do checkpoint during fsync */ 866 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi)); 867 868 set_new_dnode(&dn, inode, page, npage, nid); 869 870 if (page) 871 dn.inode_page_locked = true; 872 truncate_node(&dn); 873 return 0; 874 } 875 876 /* 877 * Caller should grab and release a rwsem by calling f2fs_lock_op() and 878 * f2fs_unlock_op(). 879 */ 880 void remove_inode_page(struct inode *inode) 881 { 882 struct dnode_of_data dn; 883 884 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 885 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE)) 886 return; 887 888 if (truncate_xattr_node(inode, dn.inode_page)) { 889 f2fs_put_dnode(&dn); 890 return; 891 } 892 893 /* remove potential inline_data blocks */ 894 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 895 S_ISLNK(inode->i_mode)) 896 truncate_data_blocks_range(&dn, 1); 897 898 /* 0 is possible, after f2fs_new_inode() has failed */ 899 f2fs_bug_on(F2FS_I_SB(inode), 900 inode->i_blocks != 0 && inode->i_blocks != 1); 901 902 /* will put inode & node pages */ 903 truncate_node(&dn); 904 } 905 906 struct page *new_inode_page(struct inode *inode) 907 { 908 struct dnode_of_data dn; 909 910 /* allocate inode page for new inode */ 911 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 912 913 /* caller should f2fs_put_page(page, 1); */ 914 return new_node_page(&dn, 0, NULL); 915 } 916 917 struct page *new_node_page(struct dnode_of_data *dn, 918 unsigned int ofs, struct page *ipage) 919 { 920 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 921 struct node_info old_ni, new_ni; 922 struct page *page; 923 int err; 924 925 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) 926 return ERR_PTR(-EPERM); 927 928 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid); 929 if (!page) 930 return ERR_PTR(-ENOMEM); 931 932 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) { 933 err = -ENOSPC; 934 goto fail; 935 } 936 937 get_node_info(sbi, dn->nid, &old_ni); 938 939 /* Reinitialize old_ni with new node page */ 940 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR); 941 new_ni = old_ni; 942 new_ni.ino = dn->inode->i_ino; 943 set_node_addr(sbi, &new_ni, NEW_ADDR, false); 944 945 f2fs_wait_on_page_writeback(page, NODE); 946 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true); 947 set_cold_node(dn->inode, page); 948 SetPageUptodate(page); 949 set_page_dirty(page); 950 951 if (f2fs_has_xattr_block(ofs)) 952 F2FS_I(dn->inode)->i_xattr_nid = dn->nid; 953 954 dn->node_page = page; 955 if (ipage) 956 update_inode(dn->inode, ipage); 957 else 958 sync_inode_page(dn); 959 if (ofs == 0) 960 inc_valid_inode_count(sbi); 961 962 return page; 963 964 fail: 965 clear_node_page_dirty(page); 966 f2fs_put_page(page, 1); 967 return ERR_PTR(err); 968 } 969 970 /* 971 * Caller should do after getting the following values. 972 * 0: f2fs_put_page(page, 0) 973 * LOCKED_PAGE: f2fs_put_page(page, 1) 974 * error: nothing 975 */ 976 static int read_node_page(struct page *page, int rw) 977 { 978 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 979 struct node_info ni; 980 981 get_node_info(sbi, page->index, &ni); 982 983 if (unlikely(ni.blk_addr == NULL_ADDR)) { 984 f2fs_put_page(page, 1); 985 return -ENOENT; 986 } 987 988 if (PageUptodate(page)) 989 return LOCKED_PAGE; 990 991 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw); 992 } 993 994 /* 995 * Readahead a node page 996 */ 997 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid) 998 { 999 struct page *apage; 1000 int err; 1001 1002 apage = find_get_page(NODE_MAPPING(sbi), nid); 1003 if (apage && PageUptodate(apage)) { 1004 f2fs_put_page(apage, 0); 1005 return; 1006 } 1007 f2fs_put_page(apage, 0); 1008 1009 apage = grab_cache_page(NODE_MAPPING(sbi), nid); 1010 if (!apage) 1011 return; 1012 1013 err = read_node_page(apage, READA); 1014 if (err == 0) 1015 f2fs_put_page(apage, 0); 1016 else if (err == LOCKED_PAGE) 1017 f2fs_put_page(apage, 1); 1018 } 1019 1020 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid) 1021 { 1022 struct page *page; 1023 int err; 1024 repeat: 1025 page = grab_cache_page(NODE_MAPPING(sbi), nid); 1026 if (!page) 1027 return ERR_PTR(-ENOMEM); 1028 1029 err = read_node_page(page, READ_SYNC); 1030 if (err < 0) 1031 return ERR_PTR(err); 1032 else if (err == LOCKED_PAGE) 1033 goto got_it; 1034 1035 lock_page(page); 1036 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) { 1037 f2fs_put_page(page, 1); 1038 return ERR_PTR(-EIO); 1039 } 1040 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1041 f2fs_put_page(page, 1); 1042 goto repeat; 1043 } 1044 got_it: 1045 return page; 1046 } 1047 1048 /* 1049 * Return a locked page for the desired node page. 1050 * And, readahead MAX_RA_NODE number of node pages. 1051 */ 1052 struct page *get_node_page_ra(struct page *parent, int start) 1053 { 1054 struct f2fs_sb_info *sbi = F2FS_P_SB(parent); 1055 struct blk_plug plug; 1056 struct page *page; 1057 int err, i, end; 1058 nid_t nid; 1059 1060 /* First, try getting the desired direct node. */ 1061 nid = get_nid(parent, start, false); 1062 if (!nid) 1063 return ERR_PTR(-ENOENT); 1064 repeat: 1065 page = grab_cache_page(NODE_MAPPING(sbi), nid); 1066 if (!page) 1067 return ERR_PTR(-ENOMEM); 1068 1069 err = read_node_page(page, READ_SYNC); 1070 if (err < 0) 1071 return ERR_PTR(err); 1072 else if (err == LOCKED_PAGE) 1073 goto page_hit; 1074 1075 blk_start_plug(&plug); 1076 1077 /* Then, try readahead for siblings of the desired node */ 1078 end = start + MAX_RA_NODE; 1079 end = min(end, NIDS_PER_BLOCK); 1080 for (i = start + 1; i < end; i++) { 1081 nid = get_nid(parent, i, false); 1082 if (!nid) 1083 continue; 1084 ra_node_page(sbi, nid); 1085 } 1086 1087 blk_finish_plug(&plug); 1088 1089 lock_page(page); 1090 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1091 f2fs_put_page(page, 1); 1092 goto repeat; 1093 } 1094 page_hit: 1095 if (unlikely(!PageUptodate(page))) { 1096 f2fs_put_page(page, 1); 1097 return ERR_PTR(-EIO); 1098 } 1099 return page; 1100 } 1101 1102 void sync_inode_page(struct dnode_of_data *dn) 1103 { 1104 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) { 1105 update_inode(dn->inode, dn->node_page); 1106 } else if (dn->inode_page) { 1107 if (!dn->inode_page_locked) 1108 lock_page(dn->inode_page); 1109 update_inode(dn->inode, dn->inode_page); 1110 if (!dn->inode_page_locked) 1111 unlock_page(dn->inode_page); 1112 } else { 1113 update_inode_page(dn->inode); 1114 } 1115 } 1116 1117 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino, 1118 struct writeback_control *wbc) 1119 { 1120 pgoff_t index, end; 1121 struct pagevec pvec; 1122 int step = ino ? 2 : 0; 1123 int nwritten = 0, wrote = 0; 1124 1125 pagevec_init(&pvec, 0); 1126 1127 next_step: 1128 index = 0; 1129 end = LONG_MAX; 1130 1131 while (index <= end) { 1132 int i, nr_pages; 1133 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, 1134 PAGECACHE_TAG_DIRTY, 1135 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 1136 if (nr_pages == 0) 1137 break; 1138 1139 for (i = 0; i < nr_pages; i++) { 1140 struct page *page = pvec.pages[i]; 1141 1142 /* 1143 * flushing sequence with step: 1144 * 0. indirect nodes 1145 * 1. dentry dnodes 1146 * 2. file dnodes 1147 */ 1148 if (step == 0 && IS_DNODE(page)) 1149 continue; 1150 if (step == 1 && (!IS_DNODE(page) || 1151 is_cold_node(page))) 1152 continue; 1153 if (step == 2 && (!IS_DNODE(page) || 1154 !is_cold_node(page))) 1155 continue; 1156 1157 /* 1158 * If an fsync mode, 1159 * we should not skip writing node pages. 1160 */ 1161 if (ino && ino_of_node(page) == ino) 1162 lock_page(page); 1163 else if (!trylock_page(page)) 1164 continue; 1165 1166 if (unlikely(page->mapping != NODE_MAPPING(sbi))) { 1167 continue_unlock: 1168 unlock_page(page); 1169 continue; 1170 } 1171 if (ino && ino_of_node(page) != ino) 1172 goto continue_unlock; 1173 1174 if (!PageDirty(page)) { 1175 /* someone wrote it for us */ 1176 goto continue_unlock; 1177 } 1178 1179 if (!clear_page_dirty_for_io(page)) 1180 goto continue_unlock; 1181 1182 /* called by fsync() */ 1183 if (ino && IS_DNODE(page)) { 1184 set_fsync_mark(page, 1); 1185 if (IS_INODE(page)) { 1186 if (!is_checkpointed_node(sbi, ino) && 1187 !has_fsynced_inode(sbi, ino)) 1188 set_dentry_mark(page, 1); 1189 else 1190 set_dentry_mark(page, 0); 1191 } 1192 nwritten++; 1193 } else { 1194 set_fsync_mark(page, 0); 1195 set_dentry_mark(page, 0); 1196 } 1197 1198 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc)) 1199 unlock_page(page); 1200 else 1201 wrote++; 1202 1203 if (--wbc->nr_to_write == 0) 1204 break; 1205 } 1206 pagevec_release(&pvec); 1207 cond_resched(); 1208 1209 if (wbc->nr_to_write == 0) { 1210 step = 2; 1211 break; 1212 } 1213 } 1214 1215 if (step < 2) { 1216 step++; 1217 goto next_step; 1218 } 1219 1220 if (wrote) 1221 f2fs_submit_merged_bio(sbi, NODE, WRITE); 1222 return nwritten; 1223 } 1224 1225 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino) 1226 { 1227 pgoff_t index = 0, end = LONG_MAX; 1228 struct pagevec pvec; 1229 int ret2 = 0, ret = 0; 1230 1231 pagevec_init(&pvec, 0); 1232 1233 while (index <= end) { 1234 int i, nr_pages; 1235 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, 1236 PAGECACHE_TAG_WRITEBACK, 1237 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 1238 if (nr_pages == 0) 1239 break; 1240 1241 for (i = 0; i < nr_pages; i++) { 1242 struct page *page = pvec.pages[i]; 1243 1244 /* until radix tree lookup accepts end_index */ 1245 if (unlikely(page->index > end)) 1246 continue; 1247 1248 if (ino && ino_of_node(page) == ino) { 1249 f2fs_wait_on_page_writeback(page, NODE); 1250 if (TestClearPageError(page)) 1251 ret = -EIO; 1252 } 1253 } 1254 pagevec_release(&pvec); 1255 cond_resched(); 1256 } 1257 1258 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags))) 1259 ret2 = -ENOSPC; 1260 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags))) 1261 ret2 = -EIO; 1262 if (!ret) 1263 ret = ret2; 1264 return ret; 1265 } 1266 1267 static int f2fs_write_node_page(struct page *page, 1268 struct writeback_control *wbc) 1269 { 1270 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1271 nid_t nid; 1272 block_t new_addr; 1273 struct node_info ni; 1274 struct f2fs_io_info fio = { 1275 .type = NODE, 1276 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, 1277 }; 1278 1279 trace_f2fs_writepage(page, NODE); 1280 1281 if (unlikely(sbi->por_doing)) 1282 goto redirty_out; 1283 if (unlikely(f2fs_cp_error(sbi))) 1284 goto redirty_out; 1285 1286 f2fs_wait_on_page_writeback(page, NODE); 1287 1288 /* get old block addr of this node page */ 1289 nid = nid_of_node(page); 1290 f2fs_bug_on(sbi, page->index != nid); 1291 1292 get_node_info(sbi, nid, &ni); 1293 1294 /* This page is already truncated */ 1295 if (unlikely(ni.blk_addr == NULL_ADDR)) { 1296 dec_page_count(sbi, F2FS_DIRTY_NODES); 1297 unlock_page(page); 1298 return 0; 1299 } 1300 1301 if (wbc->for_reclaim) 1302 goto redirty_out; 1303 1304 down_read(&sbi->node_write); 1305 set_page_writeback(page); 1306 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr); 1307 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page)); 1308 dec_page_count(sbi, F2FS_DIRTY_NODES); 1309 up_read(&sbi->node_write); 1310 unlock_page(page); 1311 return 0; 1312 1313 redirty_out: 1314 redirty_page_for_writepage(wbc, page); 1315 return AOP_WRITEPAGE_ACTIVATE; 1316 } 1317 1318 static int f2fs_write_node_pages(struct address_space *mapping, 1319 struct writeback_control *wbc) 1320 { 1321 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); 1322 long diff; 1323 1324 trace_f2fs_writepages(mapping->host, wbc, NODE); 1325 1326 /* balancing f2fs's metadata in background */ 1327 f2fs_balance_fs_bg(sbi); 1328 1329 /* collect a number of dirty node pages and write together */ 1330 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE)) 1331 goto skip_write; 1332 1333 diff = nr_pages_to_write(sbi, NODE, wbc); 1334 wbc->sync_mode = WB_SYNC_NONE; 1335 sync_node_pages(sbi, 0, wbc); 1336 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); 1337 return 0; 1338 1339 skip_write: 1340 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES); 1341 return 0; 1342 } 1343 1344 static int f2fs_set_node_page_dirty(struct page *page) 1345 { 1346 trace_f2fs_set_page_dirty(page, NODE); 1347 1348 SetPageUptodate(page); 1349 if (!PageDirty(page)) { 1350 __set_page_dirty_nobuffers(page); 1351 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); 1352 SetPagePrivate(page); 1353 return 1; 1354 } 1355 return 0; 1356 } 1357 1358 static void f2fs_invalidate_node_page(struct page *page, unsigned int offset, 1359 unsigned int length) 1360 { 1361 struct inode *inode = page->mapping->host; 1362 if (PageDirty(page)) 1363 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES); 1364 ClearPagePrivate(page); 1365 } 1366 1367 static int f2fs_release_node_page(struct page *page, gfp_t wait) 1368 { 1369 ClearPagePrivate(page); 1370 return 1; 1371 } 1372 1373 /* 1374 * Structure of the f2fs node operations 1375 */ 1376 const struct address_space_operations f2fs_node_aops = { 1377 .writepage = f2fs_write_node_page, 1378 .writepages = f2fs_write_node_pages, 1379 .set_page_dirty = f2fs_set_node_page_dirty, 1380 .invalidatepage = f2fs_invalidate_node_page, 1381 .releasepage = f2fs_release_node_page, 1382 }; 1383 1384 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i, 1385 nid_t n) 1386 { 1387 return radix_tree_lookup(&nm_i->free_nid_root, n); 1388 } 1389 1390 static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i, 1391 struct free_nid *i) 1392 { 1393 list_del(&i->list); 1394 radix_tree_delete(&nm_i->free_nid_root, i->nid); 1395 } 1396 1397 static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build) 1398 { 1399 struct f2fs_nm_info *nm_i = NM_I(sbi); 1400 struct free_nid *i; 1401 struct nat_entry *ne; 1402 bool allocated = false; 1403 1404 if (!available_free_memory(sbi, FREE_NIDS)) 1405 return -1; 1406 1407 /* 0 nid should not be used */ 1408 if (unlikely(nid == 0)) 1409 return 0; 1410 1411 if (build) { 1412 /* do not add allocated nids */ 1413 read_lock(&nm_i->nat_tree_lock); 1414 ne = __lookup_nat_cache(nm_i, nid); 1415 if (ne && 1416 (!get_nat_flag(ne, IS_CHECKPOINTED) || 1417 nat_get_blkaddr(ne) != NULL_ADDR)) 1418 allocated = true; 1419 read_unlock(&nm_i->nat_tree_lock); 1420 if (allocated) 1421 return 0; 1422 } 1423 1424 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS); 1425 i->nid = nid; 1426 i->state = NID_NEW; 1427 1428 spin_lock(&nm_i->free_nid_list_lock); 1429 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) { 1430 spin_unlock(&nm_i->free_nid_list_lock); 1431 kmem_cache_free(free_nid_slab, i); 1432 return 0; 1433 } 1434 list_add_tail(&i->list, &nm_i->free_nid_list); 1435 nm_i->fcnt++; 1436 spin_unlock(&nm_i->free_nid_list_lock); 1437 return 1; 1438 } 1439 1440 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid) 1441 { 1442 struct free_nid *i; 1443 bool need_free = false; 1444 1445 spin_lock(&nm_i->free_nid_list_lock); 1446 i = __lookup_free_nid_list(nm_i, nid); 1447 if (i && i->state == NID_NEW) { 1448 __del_from_free_nid_list(nm_i, i); 1449 nm_i->fcnt--; 1450 need_free = true; 1451 } 1452 spin_unlock(&nm_i->free_nid_list_lock); 1453 1454 if (need_free) 1455 kmem_cache_free(free_nid_slab, i); 1456 } 1457 1458 static void scan_nat_page(struct f2fs_sb_info *sbi, 1459 struct page *nat_page, nid_t start_nid) 1460 { 1461 struct f2fs_nm_info *nm_i = NM_I(sbi); 1462 struct f2fs_nat_block *nat_blk = page_address(nat_page); 1463 block_t blk_addr; 1464 int i; 1465 1466 i = start_nid % NAT_ENTRY_PER_BLOCK; 1467 1468 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) { 1469 1470 if (unlikely(start_nid >= nm_i->max_nid)) 1471 break; 1472 1473 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr); 1474 f2fs_bug_on(sbi, blk_addr == NEW_ADDR); 1475 if (blk_addr == NULL_ADDR) { 1476 if (add_free_nid(sbi, start_nid, true) < 0) 1477 break; 1478 } 1479 } 1480 } 1481 1482 static void build_free_nids(struct f2fs_sb_info *sbi) 1483 { 1484 struct f2fs_nm_info *nm_i = NM_I(sbi); 1485 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1486 struct f2fs_summary_block *sum = curseg->sum_blk; 1487 int i = 0; 1488 nid_t nid = nm_i->next_scan_nid; 1489 1490 /* Enough entries */ 1491 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK) 1492 return; 1493 1494 /* readahead nat pages to be scanned */ 1495 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT); 1496 1497 while (1) { 1498 struct page *page = get_current_nat_page(sbi, nid); 1499 1500 scan_nat_page(sbi, page, nid); 1501 f2fs_put_page(page, 1); 1502 1503 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK)); 1504 if (unlikely(nid >= nm_i->max_nid)) 1505 nid = 0; 1506 1507 if (i++ == FREE_NID_PAGES) 1508 break; 1509 } 1510 1511 /* go to the next free nat pages to find free nids abundantly */ 1512 nm_i->next_scan_nid = nid; 1513 1514 /* find free nids from current sum_pages */ 1515 mutex_lock(&curseg->curseg_mutex); 1516 for (i = 0; i < nats_in_cursum(sum); i++) { 1517 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr); 1518 nid = le32_to_cpu(nid_in_journal(sum, i)); 1519 if (addr == NULL_ADDR) 1520 add_free_nid(sbi, nid, true); 1521 else 1522 remove_free_nid(nm_i, nid); 1523 } 1524 mutex_unlock(&curseg->curseg_mutex); 1525 } 1526 1527 /* 1528 * If this function returns success, caller can obtain a new nid 1529 * from second parameter of this function. 1530 * The returned nid could be used ino as well as nid when inode is created. 1531 */ 1532 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid) 1533 { 1534 struct f2fs_nm_info *nm_i = NM_I(sbi); 1535 struct free_nid *i = NULL; 1536 retry: 1537 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids)) 1538 return false; 1539 1540 spin_lock(&nm_i->free_nid_list_lock); 1541 1542 /* We should not use stale free nids created by build_free_nids */ 1543 if (nm_i->fcnt && !on_build_free_nids(nm_i)) { 1544 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list)); 1545 list_for_each_entry(i, &nm_i->free_nid_list, list) 1546 if (i->state == NID_NEW) 1547 break; 1548 1549 f2fs_bug_on(sbi, i->state != NID_NEW); 1550 *nid = i->nid; 1551 i->state = NID_ALLOC; 1552 nm_i->fcnt--; 1553 spin_unlock(&nm_i->free_nid_list_lock); 1554 return true; 1555 } 1556 spin_unlock(&nm_i->free_nid_list_lock); 1557 1558 /* Let's scan nat pages and its caches to get free nids */ 1559 mutex_lock(&nm_i->build_lock); 1560 build_free_nids(sbi); 1561 mutex_unlock(&nm_i->build_lock); 1562 goto retry; 1563 } 1564 1565 /* 1566 * alloc_nid() should be called prior to this function. 1567 */ 1568 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid) 1569 { 1570 struct f2fs_nm_info *nm_i = NM_I(sbi); 1571 struct free_nid *i; 1572 1573 spin_lock(&nm_i->free_nid_list_lock); 1574 i = __lookup_free_nid_list(nm_i, nid); 1575 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC); 1576 __del_from_free_nid_list(nm_i, i); 1577 spin_unlock(&nm_i->free_nid_list_lock); 1578 1579 kmem_cache_free(free_nid_slab, i); 1580 } 1581 1582 /* 1583 * alloc_nid() should be called prior to this function. 1584 */ 1585 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid) 1586 { 1587 struct f2fs_nm_info *nm_i = NM_I(sbi); 1588 struct free_nid *i; 1589 bool need_free = false; 1590 1591 if (!nid) 1592 return; 1593 1594 spin_lock(&nm_i->free_nid_list_lock); 1595 i = __lookup_free_nid_list(nm_i, nid); 1596 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC); 1597 if (!available_free_memory(sbi, FREE_NIDS)) { 1598 __del_from_free_nid_list(nm_i, i); 1599 need_free = true; 1600 } else { 1601 i->state = NID_NEW; 1602 nm_i->fcnt++; 1603 } 1604 spin_unlock(&nm_i->free_nid_list_lock); 1605 1606 if (need_free) 1607 kmem_cache_free(free_nid_slab, i); 1608 } 1609 1610 void recover_inline_xattr(struct inode *inode, struct page *page) 1611 { 1612 void *src_addr, *dst_addr; 1613 size_t inline_size; 1614 struct page *ipage; 1615 struct f2fs_inode *ri; 1616 1617 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 1618 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage)); 1619 1620 ri = F2FS_INODE(page); 1621 if (!(ri->i_inline & F2FS_INLINE_XATTR)) { 1622 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR); 1623 goto update_inode; 1624 } 1625 1626 dst_addr = inline_xattr_addr(ipage); 1627 src_addr = inline_xattr_addr(page); 1628 inline_size = inline_xattr_size(inode); 1629 1630 f2fs_wait_on_page_writeback(ipage, NODE); 1631 memcpy(dst_addr, src_addr, inline_size); 1632 update_inode: 1633 update_inode(inode, ipage); 1634 f2fs_put_page(ipage, 1); 1635 } 1636 1637 void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr) 1638 { 1639 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1640 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid; 1641 nid_t new_xnid = nid_of_node(page); 1642 struct node_info ni; 1643 1644 /* 1: invalidate the previous xattr nid */ 1645 if (!prev_xnid) 1646 goto recover_xnid; 1647 1648 /* Deallocate node address */ 1649 get_node_info(sbi, prev_xnid, &ni); 1650 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); 1651 invalidate_blocks(sbi, ni.blk_addr); 1652 dec_valid_node_count(sbi, inode); 1653 set_node_addr(sbi, &ni, NULL_ADDR, false); 1654 1655 recover_xnid: 1656 /* 2: allocate new xattr nid */ 1657 if (unlikely(!inc_valid_node_count(sbi, inode))) 1658 f2fs_bug_on(sbi, 1); 1659 1660 remove_free_nid(NM_I(sbi), new_xnid); 1661 get_node_info(sbi, new_xnid, &ni); 1662 ni.ino = inode->i_ino; 1663 set_node_addr(sbi, &ni, NEW_ADDR, false); 1664 F2FS_I(inode)->i_xattr_nid = new_xnid; 1665 1666 /* 3: update xattr blkaddr */ 1667 refresh_sit_entry(sbi, NEW_ADDR, blkaddr); 1668 set_node_addr(sbi, &ni, blkaddr, false); 1669 1670 update_inode_page(inode); 1671 } 1672 1673 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page) 1674 { 1675 struct f2fs_inode *src, *dst; 1676 nid_t ino = ino_of_node(page); 1677 struct node_info old_ni, new_ni; 1678 struct page *ipage; 1679 1680 get_node_info(sbi, ino, &old_ni); 1681 1682 if (unlikely(old_ni.blk_addr != NULL_ADDR)) 1683 return -EINVAL; 1684 1685 ipage = grab_cache_page(NODE_MAPPING(sbi), ino); 1686 if (!ipage) 1687 return -ENOMEM; 1688 1689 /* Should not use this inode from free nid list */ 1690 remove_free_nid(NM_I(sbi), ino); 1691 1692 SetPageUptodate(ipage); 1693 fill_node_footer(ipage, ino, ino, 0, true); 1694 1695 src = F2FS_INODE(page); 1696 dst = F2FS_INODE(ipage); 1697 1698 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src); 1699 dst->i_size = 0; 1700 dst->i_blocks = cpu_to_le64(1); 1701 dst->i_links = cpu_to_le32(1); 1702 dst->i_xattr_nid = 0; 1703 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR; 1704 1705 new_ni = old_ni; 1706 new_ni.ino = ino; 1707 1708 if (unlikely(!inc_valid_node_count(sbi, NULL))) 1709 WARN_ON(1); 1710 set_node_addr(sbi, &new_ni, NEW_ADDR, false); 1711 inc_valid_inode_count(sbi); 1712 set_page_dirty(ipage); 1713 f2fs_put_page(ipage, 1); 1714 return 0; 1715 } 1716 1717 /* 1718 * ra_sum_pages() merge contiguous pages into one bio and submit. 1719 * these pre-read pages are allocated in bd_inode's mapping tree. 1720 */ 1721 static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages, 1722 int start, int nrpages) 1723 { 1724 struct inode *inode = sbi->sb->s_bdev->bd_inode; 1725 struct address_space *mapping = inode->i_mapping; 1726 int i, page_idx = start; 1727 struct f2fs_io_info fio = { 1728 .type = META, 1729 .rw = READ_SYNC | REQ_META | REQ_PRIO 1730 }; 1731 1732 for (i = 0; page_idx < start + nrpages; page_idx++, i++) { 1733 /* alloc page in bd_inode for reading node summary info */ 1734 pages[i] = grab_cache_page(mapping, page_idx); 1735 if (!pages[i]) 1736 break; 1737 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio); 1738 } 1739 1740 f2fs_submit_merged_bio(sbi, META, READ); 1741 return i; 1742 } 1743 1744 int restore_node_summary(struct f2fs_sb_info *sbi, 1745 unsigned int segno, struct f2fs_summary_block *sum) 1746 { 1747 struct f2fs_node *rn; 1748 struct f2fs_summary *sum_entry; 1749 struct inode *inode = sbi->sb->s_bdev->bd_inode; 1750 block_t addr; 1751 int bio_blocks = MAX_BIO_BLOCKS(sbi); 1752 struct page *pages[bio_blocks]; 1753 int i, idx, last_offset, nrpages, err = 0; 1754 1755 /* scan the node segment */ 1756 last_offset = sbi->blocks_per_seg; 1757 addr = START_BLOCK(sbi, segno); 1758 sum_entry = &sum->entries[0]; 1759 1760 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) { 1761 nrpages = min(last_offset - i, bio_blocks); 1762 1763 /* readahead node pages */ 1764 nrpages = ra_sum_pages(sbi, pages, addr, nrpages); 1765 if (!nrpages) 1766 return -ENOMEM; 1767 1768 for (idx = 0; idx < nrpages; idx++) { 1769 if (err) 1770 goto skip; 1771 1772 lock_page(pages[idx]); 1773 if (unlikely(!PageUptodate(pages[idx]))) { 1774 err = -EIO; 1775 } else { 1776 rn = F2FS_NODE(pages[idx]); 1777 sum_entry->nid = rn->footer.nid; 1778 sum_entry->version = 0; 1779 sum_entry->ofs_in_node = 0; 1780 sum_entry++; 1781 } 1782 unlock_page(pages[idx]); 1783 skip: 1784 page_cache_release(pages[idx]); 1785 } 1786 1787 invalidate_mapping_pages(inode->i_mapping, addr, 1788 addr + nrpages); 1789 } 1790 return err; 1791 } 1792 1793 static void remove_nats_in_journal(struct f2fs_sb_info *sbi) 1794 { 1795 struct f2fs_nm_info *nm_i = NM_I(sbi); 1796 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1797 struct f2fs_summary_block *sum = curseg->sum_blk; 1798 int i; 1799 1800 mutex_lock(&curseg->curseg_mutex); 1801 for (i = 0; i < nats_in_cursum(sum); i++) { 1802 struct nat_entry *ne; 1803 struct f2fs_nat_entry raw_ne; 1804 nid_t nid = le32_to_cpu(nid_in_journal(sum, i)); 1805 1806 raw_ne = nat_in_journal(sum, i); 1807 retry: 1808 write_lock(&nm_i->nat_tree_lock); 1809 ne = __lookup_nat_cache(nm_i, nid); 1810 if (ne) 1811 goto found; 1812 1813 ne = grab_nat_entry(nm_i, nid); 1814 if (!ne) { 1815 write_unlock(&nm_i->nat_tree_lock); 1816 goto retry; 1817 } 1818 node_info_from_raw_nat(&ne->ni, &raw_ne); 1819 found: 1820 __set_nat_cache_dirty(nm_i, ne); 1821 write_unlock(&nm_i->nat_tree_lock); 1822 } 1823 update_nats_in_cursum(sum, -i); 1824 mutex_unlock(&curseg->curseg_mutex); 1825 } 1826 1827 static void __adjust_nat_entry_set(struct nat_entry_set *nes, 1828 struct list_head *head, int max) 1829 { 1830 struct nat_entry_set *cur; 1831 1832 if (nes->entry_cnt >= max) 1833 goto add_out; 1834 1835 list_for_each_entry(cur, head, set_list) { 1836 if (cur->entry_cnt >= nes->entry_cnt) { 1837 list_add(&nes->set_list, cur->set_list.prev); 1838 return; 1839 } 1840 } 1841 add_out: 1842 list_add_tail(&nes->set_list, head); 1843 } 1844 1845 static void __flush_nat_entry_set(struct f2fs_sb_info *sbi, 1846 struct nat_entry_set *set) 1847 { 1848 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1849 struct f2fs_summary_block *sum = curseg->sum_blk; 1850 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK; 1851 bool to_journal = true; 1852 struct f2fs_nat_block *nat_blk; 1853 struct nat_entry *ne, *cur; 1854 struct page *page = NULL; 1855 1856 /* 1857 * there are two steps to flush nat entries: 1858 * #1, flush nat entries to journal in current hot data summary block. 1859 * #2, flush nat entries to nat page. 1860 */ 1861 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL)) 1862 to_journal = false; 1863 1864 if (to_journal) { 1865 mutex_lock(&curseg->curseg_mutex); 1866 } else { 1867 page = get_next_nat_page(sbi, start_nid); 1868 nat_blk = page_address(page); 1869 f2fs_bug_on(sbi, !nat_blk); 1870 } 1871 1872 /* flush dirty nats in nat entry set */ 1873 list_for_each_entry_safe(ne, cur, &set->entry_list, list) { 1874 struct f2fs_nat_entry *raw_ne; 1875 nid_t nid = nat_get_nid(ne); 1876 int offset; 1877 1878 if (nat_get_blkaddr(ne) == NEW_ADDR) 1879 continue; 1880 1881 if (to_journal) { 1882 offset = lookup_journal_in_cursum(sum, 1883 NAT_JOURNAL, nid, 1); 1884 f2fs_bug_on(sbi, offset < 0); 1885 raw_ne = &nat_in_journal(sum, offset); 1886 nid_in_journal(sum, offset) = cpu_to_le32(nid); 1887 } else { 1888 raw_ne = &nat_blk->entries[nid - start_nid]; 1889 } 1890 raw_nat_from_node_info(raw_ne, &ne->ni); 1891 1892 write_lock(&NM_I(sbi)->nat_tree_lock); 1893 nat_reset_flag(ne); 1894 __clear_nat_cache_dirty(NM_I(sbi), ne); 1895 write_unlock(&NM_I(sbi)->nat_tree_lock); 1896 1897 if (nat_get_blkaddr(ne) == NULL_ADDR) 1898 add_free_nid(sbi, nid, false); 1899 } 1900 1901 if (to_journal) 1902 mutex_unlock(&curseg->curseg_mutex); 1903 else 1904 f2fs_put_page(page, 1); 1905 1906 if (!set->entry_cnt) { 1907 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set); 1908 kmem_cache_free(nat_entry_set_slab, set); 1909 } 1910 } 1911 1912 /* 1913 * This function is called during the checkpointing process. 1914 */ 1915 void flush_nat_entries(struct f2fs_sb_info *sbi) 1916 { 1917 struct f2fs_nm_info *nm_i = NM_I(sbi); 1918 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1919 struct f2fs_summary_block *sum = curseg->sum_blk; 1920 struct nat_entry_set *setvec[NATVEC_SIZE]; 1921 struct nat_entry_set *set, *tmp; 1922 unsigned int found; 1923 nid_t set_idx = 0; 1924 LIST_HEAD(sets); 1925 1926 /* 1927 * if there are no enough space in journal to store dirty nat 1928 * entries, remove all entries from journal and merge them 1929 * into nat entry set. 1930 */ 1931 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL)) 1932 remove_nats_in_journal(sbi); 1933 1934 if (!nm_i->dirty_nat_cnt) 1935 return; 1936 1937 while ((found = __gang_lookup_nat_set(nm_i, 1938 set_idx, NATVEC_SIZE, setvec))) { 1939 unsigned idx; 1940 set_idx = setvec[found - 1]->set + 1; 1941 for (idx = 0; idx < found; idx++) 1942 __adjust_nat_entry_set(setvec[idx], &sets, 1943 MAX_NAT_JENTRIES(sum)); 1944 } 1945 1946 /* flush dirty nats in nat entry set */ 1947 list_for_each_entry_safe(set, tmp, &sets, set_list) 1948 __flush_nat_entry_set(sbi, set); 1949 1950 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt); 1951 } 1952 1953 static int init_node_manager(struct f2fs_sb_info *sbi) 1954 { 1955 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); 1956 struct f2fs_nm_info *nm_i = NM_I(sbi); 1957 unsigned char *version_bitmap; 1958 unsigned int nat_segs, nat_blocks; 1959 1960 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); 1961 1962 /* segment_count_nat includes pair segment so divide to 2. */ 1963 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; 1964 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); 1965 1966 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks; 1967 1968 /* not used nids: 0, node, meta, (and root counted as valid node) */ 1969 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM; 1970 nm_i->fcnt = 0; 1971 nm_i->nat_cnt = 0; 1972 nm_i->ram_thresh = DEF_RAM_THRESHOLD; 1973 1974 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC); 1975 INIT_LIST_HEAD(&nm_i->free_nid_list); 1976 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC); 1977 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_ATOMIC); 1978 INIT_LIST_HEAD(&nm_i->nat_entries); 1979 1980 mutex_init(&nm_i->build_lock); 1981 spin_lock_init(&nm_i->free_nid_list_lock); 1982 rwlock_init(&nm_i->nat_tree_lock); 1983 1984 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 1985 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 1986 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 1987 if (!version_bitmap) 1988 return -EFAULT; 1989 1990 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size, 1991 GFP_KERNEL); 1992 if (!nm_i->nat_bitmap) 1993 return -ENOMEM; 1994 return 0; 1995 } 1996 1997 int build_node_manager(struct f2fs_sb_info *sbi) 1998 { 1999 int err; 2000 2001 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL); 2002 if (!sbi->nm_info) 2003 return -ENOMEM; 2004 2005 err = init_node_manager(sbi); 2006 if (err) 2007 return err; 2008 2009 build_free_nids(sbi); 2010 return 0; 2011 } 2012 2013 void destroy_node_manager(struct f2fs_sb_info *sbi) 2014 { 2015 struct f2fs_nm_info *nm_i = NM_I(sbi); 2016 struct free_nid *i, *next_i; 2017 struct nat_entry *natvec[NATVEC_SIZE]; 2018 nid_t nid = 0; 2019 unsigned int found; 2020 2021 if (!nm_i) 2022 return; 2023 2024 /* destroy free nid list */ 2025 spin_lock(&nm_i->free_nid_list_lock); 2026 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) { 2027 f2fs_bug_on(sbi, i->state == NID_ALLOC); 2028 __del_from_free_nid_list(nm_i, i); 2029 nm_i->fcnt--; 2030 spin_unlock(&nm_i->free_nid_list_lock); 2031 kmem_cache_free(free_nid_slab, i); 2032 spin_lock(&nm_i->free_nid_list_lock); 2033 } 2034 f2fs_bug_on(sbi, nm_i->fcnt); 2035 spin_unlock(&nm_i->free_nid_list_lock); 2036 2037 /* destroy nat cache */ 2038 write_lock(&nm_i->nat_tree_lock); 2039 while ((found = __gang_lookup_nat_cache(nm_i, 2040 nid, NATVEC_SIZE, natvec))) { 2041 unsigned idx; 2042 nid = nat_get_nid(natvec[found - 1]) + 1; 2043 for (idx = 0; idx < found; idx++) 2044 __del_from_nat_cache(nm_i, natvec[idx]); 2045 } 2046 f2fs_bug_on(sbi, nm_i->nat_cnt); 2047 write_unlock(&nm_i->nat_tree_lock); 2048 2049 kfree(nm_i->nat_bitmap); 2050 sbi->nm_info = NULL; 2051 kfree(nm_i); 2052 } 2053 2054 int __init create_node_manager_caches(void) 2055 { 2056 nat_entry_slab = f2fs_kmem_cache_create("nat_entry", 2057 sizeof(struct nat_entry)); 2058 if (!nat_entry_slab) 2059 goto fail; 2060 2061 free_nid_slab = f2fs_kmem_cache_create("free_nid", 2062 sizeof(struct free_nid)); 2063 if (!free_nid_slab) 2064 goto destory_nat_entry; 2065 2066 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set", 2067 sizeof(struct nat_entry_set)); 2068 if (!nat_entry_set_slab) 2069 goto destory_free_nid; 2070 return 0; 2071 2072 destory_free_nid: 2073 kmem_cache_destroy(free_nid_slab); 2074 destory_nat_entry: 2075 kmem_cache_destroy(nat_entry_slab); 2076 fail: 2077 return -ENOMEM; 2078 } 2079 2080 void destroy_node_manager_caches(void) 2081 { 2082 kmem_cache_destroy(nat_entry_set_slab); 2083 kmem_cache_destroy(free_nid_slab); 2084 kmem_cache_destroy(nat_entry_slab); 2085 } 2086