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