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 23 static struct kmem_cache *nat_entry_slab; 24 static struct kmem_cache *free_nid_slab; 25 26 static void clear_node_page_dirty(struct page *page) 27 { 28 struct address_space *mapping = page->mapping; 29 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 30 unsigned int long flags; 31 32 if (PageDirty(page)) { 33 spin_lock_irqsave(&mapping->tree_lock, flags); 34 radix_tree_tag_clear(&mapping->page_tree, 35 page_index(page), 36 PAGECACHE_TAG_DIRTY); 37 spin_unlock_irqrestore(&mapping->tree_lock, flags); 38 39 clear_page_dirty_for_io(page); 40 dec_page_count(sbi, F2FS_DIRTY_NODES); 41 } 42 ClearPageUptodate(page); 43 } 44 45 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 46 { 47 pgoff_t index = current_nat_addr(sbi, nid); 48 return get_meta_page(sbi, index); 49 } 50 51 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 52 { 53 struct page *src_page; 54 struct page *dst_page; 55 pgoff_t src_off; 56 pgoff_t dst_off; 57 void *src_addr; 58 void *dst_addr; 59 struct f2fs_nm_info *nm_i = NM_I(sbi); 60 61 src_off = current_nat_addr(sbi, nid); 62 dst_off = next_nat_addr(sbi, src_off); 63 64 /* get current nat block page with lock */ 65 src_page = get_meta_page(sbi, src_off); 66 67 /* Dirty src_page means that it is already the new target NAT page. */ 68 if (PageDirty(src_page)) 69 return src_page; 70 71 dst_page = grab_meta_page(sbi, dst_off); 72 73 src_addr = page_address(src_page); 74 dst_addr = page_address(dst_page); 75 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); 76 set_page_dirty(dst_page); 77 f2fs_put_page(src_page, 1); 78 79 set_to_next_nat(nm_i, nid); 80 81 return dst_page; 82 } 83 84 /* 85 * Readahead NAT pages 86 */ 87 static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid) 88 { 89 struct address_space *mapping = sbi->meta_inode->i_mapping; 90 struct f2fs_nm_info *nm_i = NM_I(sbi); 91 struct page *page; 92 pgoff_t index; 93 int i; 94 95 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) { 96 if (nid >= nm_i->max_nid) 97 nid = 0; 98 index = current_nat_addr(sbi, nid); 99 100 page = grab_cache_page(mapping, index); 101 if (!page) 102 continue; 103 if (f2fs_readpage(sbi, page, index, READ)) { 104 f2fs_put_page(page, 1); 105 continue; 106 } 107 page_cache_release(page); 108 } 109 } 110 111 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n) 112 { 113 return radix_tree_lookup(&nm_i->nat_root, n); 114 } 115 116 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i, 117 nid_t start, unsigned int nr, struct nat_entry **ep) 118 { 119 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr); 120 } 121 122 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) 123 { 124 list_del(&e->list); 125 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); 126 nm_i->nat_cnt--; 127 kmem_cache_free(nat_entry_slab, e); 128 } 129 130 int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid) 131 { 132 struct f2fs_nm_info *nm_i = NM_I(sbi); 133 struct nat_entry *e; 134 int is_cp = 1; 135 136 read_lock(&nm_i->nat_tree_lock); 137 e = __lookup_nat_cache(nm_i, nid); 138 if (e && !e->checkpointed) 139 is_cp = 0; 140 read_unlock(&nm_i->nat_tree_lock); 141 return is_cp; 142 } 143 144 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid) 145 { 146 struct nat_entry *new; 147 148 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC); 149 if (!new) 150 return NULL; 151 if (radix_tree_insert(&nm_i->nat_root, nid, new)) { 152 kmem_cache_free(nat_entry_slab, new); 153 return NULL; 154 } 155 memset(new, 0, sizeof(struct nat_entry)); 156 nat_set_nid(new, nid); 157 list_add_tail(&new->list, &nm_i->nat_entries); 158 nm_i->nat_cnt++; 159 return new; 160 } 161 162 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid, 163 struct f2fs_nat_entry *ne) 164 { 165 struct nat_entry *e; 166 retry: 167 write_lock(&nm_i->nat_tree_lock); 168 e = __lookup_nat_cache(nm_i, nid); 169 if (!e) { 170 e = grab_nat_entry(nm_i, nid); 171 if (!e) { 172 write_unlock(&nm_i->nat_tree_lock); 173 goto retry; 174 } 175 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr)); 176 nat_set_ino(e, le32_to_cpu(ne->ino)); 177 nat_set_version(e, ne->version); 178 e->checkpointed = true; 179 } 180 write_unlock(&nm_i->nat_tree_lock); 181 } 182 183 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni, 184 block_t new_blkaddr) 185 { 186 struct f2fs_nm_info *nm_i = NM_I(sbi); 187 struct nat_entry *e; 188 retry: 189 write_lock(&nm_i->nat_tree_lock); 190 e = __lookup_nat_cache(nm_i, ni->nid); 191 if (!e) { 192 e = grab_nat_entry(nm_i, ni->nid); 193 if (!e) { 194 write_unlock(&nm_i->nat_tree_lock); 195 goto retry; 196 } 197 e->ni = *ni; 198 e->checkpointed = true; 199 BUG_ON(ni->blk_addr == NEW_ADDR); 200 } else if (new_blkaddr == NEW_ADDR) { 201 /* 202 * when nid is reallocated, 203 * previous nat entry can be remained in nat cache. 204 * So, reinitialize it with new information. 205 */ 206 e->ni = *ni; 207 BUG_ON(ni->blk_addr != NULL_ADDR); 208 } 209 210 if (new_blkaddr == NEW_ADDR) 211 e->checkpointed = false; 212 213 /* sanity check */ 214 BUG_ON(nat_get_blkaddr(e) != ni->blk_addr); 215 BUG_ON(nat_get_blkaddr(e) == NULL_ADDR && 216 new_blkaddr == NULL_ADDR); 217 BUG_ON(nat_get_blkaddr(e) == NEW_ADDR && 218 new_blkaddr == NEW_ADDR); 219 BUG_ON(nat_get_blkaddr(e) != NEW_ADDR && 220 nat_get_blkaddr(e) != NULL_ADDR && 221 new_blkaddr == NEW_ADDR); 222 223 /* increament version no as node is removed */ 224 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) { 225 unsigned char version = nat_get_version(e); 226 nat_set_version(e, inc_node_version(version)); 227 } 228 229 /* change address */ 230 nat_set_blkaddr(e, new_blkaddr); 231 __set_nat_cache_dirty(nm_i, e); 232 write_unlock(&nm_i->nat_tree_lock); 233 } 234 235 static int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink) 236 { 237 struct f2fs_nm_info *nm_i = NM_I(sbi); 238 239 if (nm_i->nat_cnt < 2 * NM_WOUT_THRESHOLD) 240 return 0; 241 242 write_lock(&nm_i->nat_tree_lock); 243 while (nr_shrink && !list_empty(&nm_i->nat_entries)) { 244 struct nat_entry *ne; 245 ne = list_first_entry(&nm_i->nat_entries, 246 struct nat_entry, list); 247 __del_from_nat_cache(nm_i, ne); 248 nr_shrink--; 249 } 250 write_unlock(&nm_i->nat_tree_lock); 251 return nr_shrink; 252 } 253 254 /* 255 * This function returns always success 256 */ 257 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) 258 { 259 struct f2fs_nm_info *nm_i = NM_I(sbi); 260 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 261 struct f2fs_summary_block *sum = curseg->sum_blk; 262 nid_t start_nid = START_NID(nid); 263 struct f2fs_nat_block *nat_blk; 264 struct page *page = NULL; 265 struct f2fs_nat_entry ne; 266 struct nat_entry *e; 267 int i; 268 269 memset(&ne, 0, sizeof(struct f2fs_nat_entry)); 270 ni->nid = nid; 271 272 /* Check nat cache */ 273 read_lock(&nm_i->nat_tree_lock); 274 e = __lookup_nat_cache(nm_i, nid); 275 if (e) { 276 ni->ino = nat_get_ino(e); 277 ni->blk_addr = nat_get_blkaddr(e); 278 ni->version = nat_get_version(e); 279 } 280 read_unlock(&nm_i->nat_tree_lock); 281 if (e) 282 return; 283 284 /* Check current segment summary */ 285 mutex_lock(&curseg->curseg_mutex); 286 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0); 287 if (i >= 0) { 288 ne = nat_in_journal(sum, i); 289 node_info_from_raw_nat(ni, &ne); 290 } 291 mutex_unlock(&curseg->curseg_mutex); 292 if (i >= 0) 293 goto cache; 294 295 /* Fill node_info from nat page */ 296 page = get_current_nat_page(sbi, start_nid); 297 nat_blk = (struct f2fs_nat_block *)page_address(page); 298 ne = nat_blk->entries[nid - start_nid]; 299 node_info_from_raw_nat(ni, &ne); 300 f2fs_put_page(page, 1); 301 cache: 302 /* cache nat entry */ 303 cache_nat_entry(NM_I(sbi), nid, &ne); 304 } 305 306 /* 307 * The maximum depth is four. 308 * Offset[0] will have raw inode offset. 309 */ 310 static int get_node_path(long block, int offset[4], unsigned int noffset[4]) 311 { 312 const long direct_index = ADDRS_PER_INODE; 313 const long direct_blks = ADDRS_PER_BLOCK; 314 const long dptrs_per_blk = NIDS_PER_BLOCK; 315 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK; 316 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK; 317 int n = 0; 318 int level = 0; 319 320 noffset[0] = 0; 321 322 if (block < direct_index) { 323 offset[n++] = block; 324 level = 0; 325 goto got; 326 } 327 block -= direct_index; 328 if (block < direct_blks) { 329 offset[n++] = NODE_DIR1_BLOCK; 330 noffset[n] = 1; 331 offset[n++] = block; 332 level = 1; 333 goto got; 334 } 335 block -= direct_blks; 336 if (block < direct_blks) { 337 offset[n++] = NODE_DIR2_BLOCK; 338 noffset[n] = 2; 339 offset[n++] = block; 340 level = 1; 341 goto got; 342 } 343 block -= direct_blks; 344 if (block < indirect_blks) { 345 offset[n++] = NODE_IND1_BLOCK; 346 noffset[n] = 3; 347 offset[n++] = block / direct_blks; 348 noffset[n] = 4 + offset[n - 1]; 349 offset[n++] = block % direct_blks; 350 level = 2; 351 goto got; 352 } 353 block -= indirect_blks; 354 if (block < indirect_blks) { 355 offset[n++] = NODE_IND2_BLOCK; 356 noffset[n] = 4 + dptrs_per_blk; 357 offset[n++] = block / direct_blks; 358 noffset[n] = 5 + dptrs_per_blk + offset[n - 1]; 359 offset[n++] = block % direct_blks; 360 level = 2; 361 goto got; 362 } 363 block -= indirect_blks; 364 if (block < dindirect_blks) { 365 offset[n++] = NODE_DIND_BLOCK; 366 noffset[n] = 5 + (dptrs_per_blk * 2); 367 offset[n++] = block / indirect_blks; 368 noffset[n] = 6 + (dptrs_per_blk * 2) + 369 offset[n - 1] * (dptrs_per_blk + 1); 370 offset[n++] = (block / direct_blks) % dptrs_per_blk; 371 noffset[n] = 7 + (dptrs_per_blk * 2) + 372 offset[n - 2] * (dptrs_per_blk + 1) + 373 offset[n - 1]; 374 offset[n++] = block % direct_blks; 375 level = 3; 376 goto got; 377 } else { 378 BUG(); 379 } 380 got: 381 return level; 382 } 383 384 /* 385 * Caller should call f2fs_put_dnode(dn). 386 */ 387 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int ro) 388 { 389 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 390 struct page *npage[4]; 391 struct page *parent; 392 int offset[4]; 393 unsigned int noffset[4]; 394 nid_t nids[4]; 395 int level, i; 396 int err = 0; 397 398 level = get_node_path(index, offset, noffset); 399 400 nids[0] = dn->inode->i_ino; 401 npage[0] = get_node_page(sbi, nids[0]); 402 if (IS_ERR(npage[0])) 403 return PTR_ERR(npage[0]); 404 405 parent = npage[0]; 406 nids[1] = get_nid(parent, offset[0], true); 407 dn->inode_page = npage[0]; 408 dn->inode_page_locked = true; 409 410 /* get indirect or direct nodes */ 411 for (i = 1; i <= level; i++) { 412 bool done = false; 413 414 if (!nids[i] && !ro) { 415 mutex_lock_op(sbi, NODE_NEW); 416 417 /* alloc new node */ 418 if (!alloc_nid(sbi, &(nids[i]))) { 419 mutex_unlock_op(sbi, NODE_NEW); 420 err = -ENOSPC; 421 goto release_pages; 422 } 423 424 dn->nid = nids[i]; 425 npage[i] = new_node_page(dn, noffset[i]); 426 if (IS_ERR(npage[i])) { 427 alloc_nid_failed(sbi, nids[i]); 428 mutex_unlock_op(sbi, NODE_NEW); 429 err = PTR_ERR(npage[i]); 430 goto release_pages; 431 } 432 433 set_nid(parent, offset[i - 1], nids[i], i == 1); 434 alloc_nid_done(sbi, nids[i]); 435 mutex_unlock_op(sbi, NODE_NEW); 436 done = true; 437 } else if (ro && i == level && level > 1) { 438 npage[i] = get_node_page_ra(parent, offset[i - 1]); 439 if (IS_ERR(npage[i])) { 440 err = PTR_ERR(npage[i]); 441 goto release_pages; 442 } 443 done = true; 444 } 445 if (i == 1) { 446 dn->inode_page_locked = false; 447 unlock_page(parent); 448 } else { 449 f2fs_put_page(parent, 1); 450 } 451 452 if (!done) { 453 npage[i] = get_node_page(sbi, nids[i]); 454 if (IS_ERR(npage[i])) { 455 err = PTR_ERR(npage[i]); 456 f2fs_put_page(npage[0], 0); 457 goto release_out; 458 } 459 } 460 if (i < level) { 461 parent = npage[i]; 462 nids[i + 1] = get_nid(parent, offset[i], false); 463 } 464 } 465 dn->nid = nids[level]; 466 dn->ofs_in_node = offset[level]; 467 dn->node_page = npage[level]; 468 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); 469 return 0; 470 471 release_pages: 472 f2fs_put_page(parent, 1); 473 if (i > 1) 474 f2fs_put_page(npage[0], 0); 475 release_out: 476 dn->inode_page = NULL; 477 dn->node_page = NULL; 478 return err; 479 } 480 481 static void truncate_node(struct dnode_of_data *dn) 482 { 483 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 484 struct node_info ni; 485 486 get_node_info(sbi, dn->nid, &ni); 487 if (dn->inode->i_blocks == 0) { 488 BUG_ON(ni.blk_addr != NULL_ADDR); 489 goto invalidate; 490 } 491 BUG_ON(ni.blk_addr == NULL_ADDR); 492 493 /* Deallocate node address */ 494 invalidate_blocks(sbi, ni.blk_addr); 495 dec_valid_node_count(sbi, dn->inode, 1); 496 set_node_addr(sbi, &ni, NULL_ADDR); 497 498 if (dn->nid == dn->inode->i_ino) { 499 remove_orphan_inode(sbi, dn->nid); 500 dec_valid_inode_count(sbi); 501 } else { 502 sync_inode_page(dn); 503 } 504 invalidate: 505 clear_node_page_dirty(dn->node_page); 506 F2FS_SET_SB_DIRT(sbi); 507 508 f2fs_put_page(dn->node_page, 1); 509 dn->node_page = NULL; 510 } 511 512 static int truncate_dnode(struct dnode_of_data *dn) 513 { 514 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 515 struct page *page; 516 517 if (dn->nid == 0) 518 return 1; 519 520 /* get direct node */ 521 page = get_node_page(sbi, dn->nid); 522 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT) 523 return 1; 524 else if (IS_ERR(page)) 525 return PTR_ERR(page); 526 527 /* Make dnode_of_data for parameter */ 528 dn->node_page = page; 529 dn->ofs_in_node = 0; 530 truncate_data_blocks(dn); 531 truncate_node(dn); 532 return 1; 533 } 534 535 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs, 536 int ofs, int depth) 537 { 538 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 539 struct dnode_of_data rdn = *dn; 540 struct page *page; 541 struct f2fs_node *rn; 542 nid_t child_nid; 543 unsigned int child_nofs; 544 int freed = 0; 545 int i, ret; 546 547 if (dn->nid == 0) 548 return NIDS_PER_BLOCK + 1; 549 550 page = get_node_page(sbi, dn->nid); 551 if (IS_ERR(page)) 552 return PTR_ERR(page); 553 554 rn = (struct f2fs_node *)page_address(page); 555 if (depth < 3) { 556 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) { 557 child_nid = le32_to_cpu(rn->in.nid[i]); 558 if (child_nid == 0) 559 continue; 560 rdn.nid = child_nid; 561 ret = truncate_dnode(&rdn); 562 if (ret < 0) 563 goto out_err; 564 set_nid(page, i, 0, false); 565 } 566 } else { 567 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1; 568 for (i = ofs; i < NIDS_PER_BLOCK; i++) { 569 child_nid = le32_to_cpu(rn->in.nid[i]); 570 if (child_nid == 0) { 571 child_nofs += NIDS_PER_BLOCK + 1; 572 continue; 573 } 574 rdn.nid = child_nid; 575 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1); 576 if (ret == (NIDS_PER_BLOCK + 1)) { 577 set_nid(page, i, 0, false); 578 child_nofs += ret; 579 } else if (ret < 0 && ret != -ENOENT) { 580 goto out_err; 581 } 582 } 583 freed = child_nofs; 584 } 585 586 if (!ofs) { 587 /* remove current indirect node */ 588 dn->node_page = page; 589 truncate_node(dn); 590 freed++; 591 } else { 592 f2fs_put_page(page, 1); 593 } 594 return freed; 595 596 out_err: 597 f2fs_put_page(page, 1); 598 return ret; 599 } 600 601 static int truncate_partial_nodes(struct dnode_of_data *dn, 602 struct f2fs_inode *ri, int *offset, int depth) 603 { 604 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 605 struct page *pages[2]; 606 nid_t nid[3]; 607 nid_t child_nid; 608 int err = 0; 609 int i; 610 int idx = depth - 2; 611 612 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); 613 if (!nid[0]) 614 return 0; 615 616 /* get indirect nodes in the path */ 617 for (i = 0; i < depth - 1; i++) { 618 /* refernece count'll be increased */ 619 pages[i] = get_node_page(sbi, nid[i]); 620 if (IS_ERR(pages[i])) { 621 depth = i + 1; 622 err = PTR_ERR(pages[i]); 623 goto fail; 624 } 625 nid[i + 1] = get_nid(pages[i], offset[i + 1], false); 626 } 627 628 /* free direct nodes linked to a partial indirect node */ 629 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) { 630 child_nid = get_nid(pages[idx], i, false); 631 if (!child_nid) 632 continue; 633 dn->nid = child_nid; 634 err = truncate_dnode(dn); 635 if (err < 0) 636 goto fail; 637 set_nid(pages[idx], i, 0, false); 638 } 639 640 if (offset[depth - 1] == 0) { 641 dn->node_page = pages[idx]; 642 dn->nid = nid[idx]; 643 truncate_node(dn); 644 } else { 645 f2fs_put_page(pages[idx], 1); 646 } 647 offset[idx]++; 648 offset[depth - 1] = 0; 649 fail: 650 for (i = depth - 3; i >= 0; i--) 651 f2fs_put_page(pages[i], 1); 652 return err; 653 } 654 655 /* 656 * All the block addresses of data and nodes should be nullified. 657 */ 658 int truncate_inode_blocks(struct inode *inode, pgoff_t from) 659 { 660 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 661 int err = 0, cont = 1; 662 int level, offset[4], noffset[4]; 663 unsigned int nofs; 664 struct f2fs_node *rn; 665 struct dnode_of_data dn; 666 struct page *page; 667 668 level = get_node_path(from, offset, noffset); 669 670 page = get_node_page(sbi, inode->i_ino); 671 if (IS_ERR(page)) 672 return PTR_ERR(page); 673 674 set_new_dnode(&dn, inode, page, NULL, 0); 675 unlock_page(page); 676 677 rn = page_address(page); 678 switch (level) { 679 case 0: 680 case 1: 681 nofs = noffset[1]; 682 break; 683 case 2: 684 nofs = noffset[1]; 685 if (!offset[level - 1]) 686 goto skip_partial; 687 err = truncate_partial_nodes(&dn, &rn->i, offset, level); 688 if (err < 0 && err != -ENOENT) 689 goto fail; 690 nofs += 1 + NIDS_PER_BLOCK; 691 break; 692 case 3: 693 nofs = 5 + 2 * NIDS_PER_BLOCK; 694 if (!offset[level - 1]) 695 goto skip_partial; 696 err = truncate_partial_nodes(&dn, &rn->i, offset, level); 697 if (err < 0 && err != -ENOENT) 698 goto fail; 699 break; 700 default: 701 BUG(); 702 } 703 704 skip_partial: 705 while (cont) { 706 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]); 707 switch (offset[0]) { 708 case NODE_DIR1_BLOCK: 709 case NODE_DIR2_BLOCK: 710 err = truncate_dnode(&dn); 711 break; 712 713 case NODE_IND1_BLOCK: 714 case NODE_IND2_BLOCK: 715 err = truncate_nodes(&dn, nofs, offset[1], 2); 716 break; 717 718 case NODE_DIND_BLOCK: 719 err = truncate_nodes(&dn, nofs, offset[1], 3); 720 cont = 0; 721 break; 722 723 default: 724 BUG(); 725 } 726 if (err < 0 && err != -ENOENT) 727 goto fail; 728 if (offset[1] == 0 && 729 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) { 730 lock_page(page); 731 wait_on_page_writeback(page); 732 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0; 733 set_page_dirty(page); 734 unlock_page(page); 735 } 736 offset[1] = 0; 737 offset[0]++; 738 nofs += err; 739 } 740 fail: 741 f2fs_put_page(page, 0); 742 return err > 0 ? 0 : err; 743 } 744 745 int remove_inode_page(struct inode *inode) 746 { 747 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 748 struct page *page; 749 nid_t ino = inode->i_ino; 750 struct dnode_of_data dn; 751 752 mutex_lock_op(sbi, NODE_TRUNC); 753 page = get_node_page(sbi, ino); 754 if (IS_ERR(page)) { 755 mutex_unlock_op(sbi, NODE_TRUNC); 756 return PTR_ERR(page); 757 } 758 759 if (F2FS_I(inode)->i_xattr_nid) { 760 nid_t nid = F2FS_I(inode)->i_xattr_nid; 761 struct page *npage = get_node_page(sbi, nid); 762 763 if (IS_ERR(npage)) { 764 mutex_unlock_op(sbi, NODE_TRUNC); 765 return PTR_ERR(npage); 766 } 767 768 F2FS_I(inode)->i_xattr_nid = 0; 769 set_new_dnode(&dn, inode, page, npage, nid); 770 dn.inode_page_locked = 1; 771 truncate_node(&dn); 772 } 773 774 /* 0 is possible, after f2fs_new_inode() is failed */ 775 BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1); 776 set_new_dnode(&dn, inode, page, page, ino); 777 truncate_node(&dn); 778 779 mutex_unlock_op(sbi, NODE_TRUNC); 780 return 0; 781 } 782 783 int new_inode_page(struct inode *inode, struct dentry *dentry) 784 { 785 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 786 struct page *page; 787 struct dnode_of_data dn; 788 789 /* allocate inode page for new inode */ 790 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 791 mutex_lock_op(sbi, NODE_NEW); 792 page = new_node_page(&dn, 0); 793 init_dent_inode(dentry, page); 794 mutex_unlock_op(sbi, NODE_NEW); 795 if (IS_ERR(page)) 796 return PTR_ERR(page); 797 f2fs_put_page(page, 1); 798 return 0; 799 } 800 801 struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs) 802 { 803 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 804 struct address_space *mapping = sbi->node_inode->i_mapping; 805 struct node_info old_ni, new_ni; 806 struct page *page; 807 int err; 808 809 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)) 810 return ERR_PTR(-EPERM); 811 812 page = grab_cache_page(mapping, dn->nid); 813 if (!page) 814 return ERR_PTR(-ENOMEM); 815 816 get_node_info(sbi, dn->nid, &old_ni); 817 818 SetPageUptodate(page); 819 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true); 820 821 /* Reinitialize old_ni with new node page */ 822 BUG_ON(old_ni.blk_addr != NULL_ADDR); 823 new_ni = old_ni; 824 new_ni.ino = dn->inode->i_ino; 825 826 if (!inc_valid_node_count(sbi, dn->inode, 1)) { 827 err = -ENOSPC; 828 goto fail; 829 } 830 set_node_addr(sbi, &new_ni, NEW_ADDR); 831 set_cold_node(dn->inode, page); 832 833 dn->node_page = page; 834 sync_inode_page(dn); 835 set_page_dirty(page); 836 if (ofs == 0) 837 inc_valid_inode_count(sbi); 838 839 return page; 840 841 fail: 842 clear_node_page_dirty(page); 843 f2fs_put_page(page, 1); 844 return ERR_PTR(err); 845 } 846 847 static int read_node_page(struct page *page, int type) 848 { 849 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); 850 struct node_info ni; 851 852 get_node_info(sbi, page->index, &ni); 853 854 if (ni.blk_addr == NULL_ADDR) 855 return -ENOENT; 856 return f2fs_readpage(sbi, page, ni.blk_addr, type); 857 } 858 859 /* 860 * Readahead a node page 861 */ 862 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid) 863 { 864 struct address_space *mapping = sbi->node_inode->i_mapping; 865 struct page *apage; 866 867 apage = find_get_page(mapping, nid); 868 if (apage && PageUptodate(apage)) 869 goto release_out; 870 f2fs_put_page(apage, 0); 871 872 apage = grab_cache_page(mapping, nid); 873 if (!apage) 874 return; 875 876 if (read_node_page(apage, READA)) 877 goto unlock_out; 878 879 page_cache_release(apage); 880 return; 881 882 unlock_out: 883 unlock_page(apage); 884 release_out: 885 page_cache_release(apage); 886 } 887 888 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid) 889 { 890 int err; 891 struct page *page; 892 struct address_space *mapping = sbi->node_inode->i_mapping; 893 894 page = grab_cache_page(mapping, nid); 895 if (!page) 896 return ERR_PTR(-ENOMEM); 897 898 err = read_node_page(page, READ_SYNC); 899 if (err) { 900 f2fs_put_page(page, 1); 901 return ERR_PTR(err); 902 } 903 904 BUG_ON(nid != nid_of_node(page)); 905 mark_page_accessed(page); 906 return page; 907 } 908 909 /* 910 * Return a locked page for the desired node page. 911 * And, readahead MAX_RA_NODE number of node pages. 912 */ 913 struct page *get_node_page_ra(struct page *parent, int start) 914 { 915 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb); 916 struct address_space *mapping = sbi->node_inode->i_mapping; 917 int i, end; 918 int err = 0; 919 nid_t nid; 920 struct page *page; 921 922 /* First, try getting the desired direct node. */ 923 nid = get_nid(parent, start, false); 924 if (!nid) 925 return ERR_PTR(-ENOENT); 926 927 page = find_get_page(mapping, nid); 928 if (page && PageUptodate(page)) 929 goto page_hit; 930 f2fs_put_page(page, 0); 931 932 repeat: 933 page = grab_cache_page(mapping, nid); 934 if (!page) 935 return ERR_PTR(-ENOMEM); 936 937 err = read_node_page(page, READA); 938 if (err) { 939 f2fs_put_page(page, 1); 940 return ERR_PTR(err); 941 } 942 943 /* Then, try readahead for siblings of the desired node */ 944 end = start + MAX_RA_NODE; 945 end = min(end, NIDS_PER_BLOCK); 946 for (i = start + 1; i < end; i++) { 947 nid = get_nid(parent, i, false); 948 if (!nid) 949 continue; 950 ra_node_page(sbi, nid); 951 } 952 953 page_hit: 954 lock_page(page); 955 if (PageError(page)) { 956 f2fs_put_page(page, 1); 957 return ERR_PTR(-EIO); 958 } 959 960 /* Has the page been truncated? */ 961 if (page->mapping != mapping) { 962 f2fs_put_page(page, 1); 963 goto repeat; 964 } 965 return page; 966 } 967 968 void sync_inode_page(struct dnode_of_data *dn) 969 { 970 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) { 971 update_inode(dn->inode, dn->node_page); 972 } else if (dn->inode_page) { 973 if (!dn->inode_page_locked) 974 lock_page(dn->inode_page); 975 update_inode(dn->inode, dn->inode_page); 976 if (!dn->inode_page_locked) 977 unlock_page(dn->inode_page); 978 } else { 979 f2fs_write_inode(dn->inode, NULL); 980 } 981 } 982 983 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino, 984 struct writeback_control *wbc) 985 { 986 struct address_space *mapping = sbi->node_inode->i_mapping; 987 pgoff_t index, end; 988 struct pagevec pvec; 989 int step = ino ? 2 : 0; 990 int nwritten = 0, wrote = 0; 991 992 pagevec_init(&pvec, 0); 993 994 next_step: 995 index = 0; 996 end = LONG_MAX; 997 998 while (index <= end) { 999 int i, nr_pages; 1000 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 1001 PAGECACHE_TAG_DIRTY, 1002 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 1003 if (nr_pages == 0) 1004 break; 1005 1006 for (i = 0; i < nr_pages; i++) { 1007 struct page *page = pvec.pages[i]; 1008 1009 /* 1010 * flushing sequence with step: 1011 * 0. indirect nodes 1012 * 1. dentry dnodes 1013 * 2. file dnodes 1014 */ 1015 if (step == 0 && IS_DNODE(page)) 1016 continue; 1017 if (step == 1 && (!IS_DNODE(page) || 1018 is_cold_node(page))) 1019 continue; 1020 if (step == 2 && (!IS_DNODE(page) || 1021 !is_cold_node(page))) 1022 continue; 1023 1024 /* 1025 * If an fsync mode, 1026 * we should not skip writing node pages. 1027 */ 1028 if (ino && ino_of_node(page) == ino) 1029 lock_page(page); 1030 else if (!trylock_page(page)) 1031 continue; 1032 1033 if (unlikely(page->mapping != mapping)) { 1034 continue_unlock: 1035 unlock_page(page); 1036 continue; 1037 } 1038 if (ino && ino_of_node(page) != ino) 1039 goto continue_unlock; 1040 1041 if (!PageDirty(page)) { 1042 /* someone wrote it for us */ 1043 goto continue_unlock; 1044 } 1045 1046 if (!clear_page_dirty_for_io(page)) 1047 goto continue_unlock; 1048 1049 /* called by fsync() */ 1050 if (ino && IS_DNODE(page)) { 1051 int mark = !is_checkpointed_node(sbi, ino); 1052 set_fsync_mark(page, 1); 1053 if (IS_INODE(page)) 1054 set_dentry_mark(page, mark); 1055 nwritten++; 1056 } else { 1057 set_fsync_mark(page, 0); 1058 set_dentry_mark(page, 0); 1059 } 1060 mapping->a_ops->writepage(page, wbc); 1061 wrote++; 1062 1063 if (--wbc->nr_to_write == 0) 1064 break; 1065 } 1066 pagevec_release(&pvec); 1067 cond_resched(); 1068 1069 if (wbc->nr_to_write == 0) { 1070 step = 2; 1071 break; 1072 } 1073 } 1074 1075 if (step < 2) { 1076 step++; 1077 goto next_step; 1078 } 1079 1080 if (wrote) 1081 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL); 1082 1083 return nwritten; 1084 } 1085 1086 static int f2fs_write_node_page(struct page *page, 1087 struct writeback_control *wbc) 1088 { 1089 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); 1090 nid_t nid; 1091 block_t new_addr; 1092 struct node_info ni; 1093 1094 if (wbc->for_reclaim) { 1095 dec_page_count(sbi, F2FS_DIRTY_NODES); 1096 wbc->pages_skipped++; 1097 set_page_dirty(page); 1098 return AOP_WRITEPAGE_ACTIVATE; 1099 } 1100 1101 wait_on_page_writeback(page); 1102 1103 mutex_lock_op(sbi, NODE_WRITE); 1104 1105 /* get old block addr of this node page */ 1106 nid = nid_of_node(page); 1107 BUG_ON(page->index != nid); 1108 1109 get_node_info(sbi, nid, &ni); 1110 1111 /* This page is already truncated */ 1112 if (ni.blk_addr == NULL_ADDR) 1113 return 0; 1114 1115 set_page_writeback(page); 1116 1117 /* insert node offset */ 1118 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr); 1119 set_node_addr(sbi, &ni, new_addr); 1120 dec_page_count(sbi, F2FS_DIRTY_NODES); 1121 1122 mutex_unlock_op(sbi, NODE_WRITE); 1123 unlock_page(page); 1124 return 0; 1125 } 1126 1127 /* 1128 * It is very important to gather dirty pages and write at once, so that we can 1129 * submit a big bio without interfering other data writes. 1130 * Be default, 512 pages (2MB), a segment size, is quite reasonable. 1131 */ 1132 #define COLLECT_DIRTY_NODES 512 1133 static int f2fs_write_node_pages(struct address_space *mapping, 1134 struct writeback_control *wbc) 1135 { 1136 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 1137 struct block_device *bdev = sbi->sb->s_bdev; 1138 long nr_to_write = wbc->nr_to_write; 1139 1140 /* First check balancing cached NAT entries */ 1141 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK)) { 1142 write_checkpoint(sbi, false, false); 1143 return 0; 1144 } 1145 1146 /* collect a number of dirty node pages and write together */ 1147 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES) 1148 return 0; 1149 1150 /* if mounting is failed, skip writing node pages */ 1151 wbc->nr_to_write = bio_get_nr_vecs(bdev); 1152 sync_node_pages(sbi, 0, wbc); 1153 wbc->nr_to_write = nr_to_write - 1154 (bio_get_nr_vecs(bdev) - wbc->nr_to_write); 1155 return 0; 1156 } 1157 1158 static int f2fs_set_node_page_dirty(struct page *page) 1159 { 1160 struct address_space *mapping = page->mapping; 1161 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb); 1162 1163 SetPageUptodate(page); 1164 if (!PageDirty(page)) { 1165 __set_page_dirty_nobuffers(page); 1166 inc_page_count(sbi, F2FS_DIRTY_NODES); 1167 SetPagePrivate(page); 1168 return 1; 1169 } 1170 return 0; 1171 } 1172 1173 static void f2fs_invalidate_node_page(struct page *page, unsigned long offset) 1174 { 1175 struct inode *inode = page->mapping->host; 1176 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 1177 if (PageDirty(page)) 1178 dec_page_count(sbi, F2FS_DIRTY_NODES); 1179 ClearPagePrivate(page); 1180 } 1181 1182 static int f2fs_release_node_page(struct page *page, gfp_t wait) 1183 { 1184 ClearPagePrivate(page); 1185 return 0; 1186 } 1187 1188 /* 1189 * Structure of the f2fs node operations 1190 */ 1191 const struct address_space_operations f2fs_node_aops = { 1192 .writepage = f2fs_write_node_page, 1193 .writepages = f2fs_write_node_pages, 1194 .set_page_dirty = f2fs_set_node_page_dirty, 1195 .invalidatepage = f2fs_invalidate_node_page, 1196 .releasepage = f2fs_release_node_page, 1197 }; 1198 1199 static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head) 1200 { 1201 struct list_head *this; 1202 struct free_nid *i = NULL; 1203 list_for_each(this, head) { 1204 i = list_entry(this, struct free_nid, list); 1205 if (i->nid == n) 1206 break; 1207 i = NULL; 1208 } 1209 return i; 1210 } 1211 1212 static void __del_from_free_nid_list(struct free_nid *i) 1213 { 1214 list_del(&i->list); 1215 kmem_cache_free(free_nid_slab, i); 1216 } 1217 1218 static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid) 1219 { 1220 struct free_nid *i; 1221 1222 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) 1223 return 0; 1224 retry: 1225 i = kmem_cache_alloc(free_nid_slab, GFP_NOFS); 1226 if (!i) { 1227 cond_resched(); 1228 goto retry; 1229 } 1230 i->nid = nid; 1231 i->state = NID_NEW; 1232 1233 spin_lock(&nm_i->free_nid_list_lock); 1234 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) { 1235 spin_unlock(&nm_i->free_nid_list_lock); 1236 kmem_cache_free(free_nid_slab, i); 1237 return 0; 1238 } 1239 list_add_tail(&i->list, &nm_i->free_nid_list); 1240 nm_i->fcnt++; 1241 spin_unlock(&nm_i->free_nid_list_lock); 1242 return 1; 1243 } 1244 1245 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid) 1246 { 1247 struct free_nid *i; 1248 spin_lock(&nm_i->free_nid_list_lock); 1249 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list); 1250 if (i && i->state == NID_NEW) { 1251 __del_from_free_nid_list(i); 1252 nm_i->fcnt--; 1253 } 1254 spin_unlock(&nm_i->free_nid_list_lock); 1255 } 1256 1257 static int scan_nat_page(struct f2fs_nm_info *nm_i, 1258 struct page *nat_page, nid_t start_nid) 1259 { 1260 struct f2fs_nat_block *nat_blk = page_address(nat_page); 1261 block_t blk_addr; 1262 int fcnt = 0; 1263 int i; 1264 1265 /* 0 nid should not be used */ 1266 if (start_nid == 0) 1267 ++start_nid; 1268 1269 i = start_nid % NAT_ENTRY_PER_BLOCK; 1270 1271 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) { 1272 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr); 1273 BUG_ON(blk_addr == NEW_ADDR); 1274 if (blk_addr == NULL_ADDR) 1275 fcnt += add_free_nid(nm_i, start_nid); 1276 } 1277 return fcnt; 1278 } 1279 1280 static void build_free_nids(struct f2fs_sb_info *sbi) 1281 { 1282 struct free_nid *fnid, *next_fnid; 1283 struct f2fs_nm_info *nm_i = NM_I(sbi); 1284 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1285 struct f2fs_summary_block *sum = curseg->sum_blk; 1286 nid_t nid = 0; 1287 bool is_cycled = false; 1288 int fcnt = 0; 1289 int i; 1290 1291 nid = nm_i->next_scan_nid; 1292 nm_i->init_scan_nid = nid; 1293 1294 ra_nat_pages(sbi, nid); 1295 1296 while (1) { 1297 struct page *page = get_current_nat_page(sbi, nid); 1298 1299 fcnt += scan_nat_page(nm_i, page, nid); 1300 f2fs_put_page(page, 1); 1301 1302 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK)); 1303 1304 if (nid >= nm_i->max_nid) { 1305 nid = 0; 1306 is_cycled = true; 1307 } 1308 if (fcnt > MAX_FREE_NIDS) 1309 break; 1310 if (is_cycled && nm_i->init_scan_nid <= nid) 1311 break; 1312 } 1313 1314 nm_i->next_scan_nid = nid; 1315 1316 /* find free nids from current sum_pages */ 1317 mutex_lock(&curseg->curseg_mutex); 1318 for (i = 0; i < nats_in_cursum(sum); i++) { 1319 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr); 1320 nid = le32_to_cpu(nid_in_journal(sum, i)); 1321 if (addr == NULL_ADDR) 1322 add_free_nid(nm_i, nid); 1323 else 1324 remove_free_nid(nm_i, nid); 1325 } 1326 mutex_unlock(&curseg->curseg_mutex); 1327 1328 /* remove the free nids from current allocated nids */ 1329 list_for_each_entry_safe(fnid, next_fnid, &nm_i->free_nid_list, list) { 1330 struct nat_entry *ne; 1331 1332 read_lock(&nm_i->nat_tree_lock); 1333 ne = __lookup_nat_cache(nm_i, fnid->nid); 1334 if (ne && nat_get_blkaddr(ne) != NULL_ADDR) 1335 remove_free_nid(nm_i, fnid->nid); 1336 read_unlock(&nm_i->nat_tree_lock); 1337 } 1338 } 1339 1340 /* 1341 * If this function returns success, caller can obtain a new nid 1342 * from second parameter of this function. 1343 * The returned nid could be used ino as well as nid when inode is created. 1344 */ 1345 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid) 1346 { 1347 struct f2fs_nm_info *nm_i = NM_I(sbi); 1348 struct free_nid *i = NULL; 1349 struct list_head *this; 1350 retry: 1351 mutex_lock(&nm_i->build_lock); 1352 if (!nm_i->fcnt) { 1353 /* scan NAT in order to build free nid list */ 1354 build_free_nids(sbi); 1355 if (!nm_i->fcnt) { 1356 mutex_unlock(&nm_i->build_lock); 1357 return false; 1358 } 1359 } 1360 mutex_unlock(&nm_i->build_lock); 1361 1362 /* 1363 * We check fcnt again since previous check is racy as 1364 * we didn't hold free_nid_list_lock. So other thread 1365 * could consume all of free nids. 1366 */ 1367 spin_lock(&nm_i->free_nid_list_lock); 1368 if (!nm_i->fcnt) { 1369 spin_unlock(&nm_i->free_nid_list_lock); 1370 goto retry; 1371 } 1372 1373 BUG_ON(list_empty(&nm_i->free_nid_list)); 1374 list_for_each(this, &nm_i->free_nid_list) { 1375 i = list_entry(this, struct free_nid, list); 1376 if (i->state == NID_NEW) 1377 break; 1378 } 1379 1380 BUG_ON(i->state != NID_NEW); 1381 *nid = i->nid; 1382 i->state = NID_ALLOC; 1383 nm_i->fcnt--; 1384 spin_unlock(&nm_i->free_nid_list_lock); 1385 return true; 1386 } 1387 1388 /* 1389 * alloc_nid() should be called prior to this function. 1390 */ 1391 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid) 1392 { 1393 struct f2fs_nm_info *nm_i = NM_I(sbi); 1394 struct free_nid *i; 1395 1396 spin_lock(&nm_i->free_nid_list_lock); 1397 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list); 1398 if (i) { 1399 BUG_ON(i->state != NID_ALLOC); 1400 __del_from_free_nid_list(i); 1401 } 1402 spin_unlock(&nm_i->free_nid_list_lock); 1403 } 1404 1405 /* 1406 * alloc_nid() should be called prior to this function. 1407 */ 1408 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid) 1409 { 1410 alloc_nid_done(sbi, nid); 1411 add_free_nid(NM_I(sbi), nid); 1412 } 1413 1414 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page, 1415 struct f2fs_summary *sum, struct node_info *ni, 1416 block_t new_blkaddr) 1417 { 1418 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr); 1419 set_node_addr(sbi, ni, new_blkaddr); 1420 clear_node_page_dirty(page); 1421 } 1422 1423 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page) 1424 { 1425 struct address_space *mapping = sbi->node_inode->i_mapping; 1426 struct f2fs_node *src, *dst; 1427 nid_t ino = ino_of_node(page); 1428 struct node_info old_ni, new_ni; 1429 struct page *ipage; 1430 1431 ipage = grab_cache_page(mapping, ino); 1432 if (!ipage) 1433 return -ENOMEM; 1434 1435 /* Should not use this inode from free nid list */ 1436 remove_free_nid(NM_I(sbi), ino); 1437 1438 get_node_info(sbi, ino, &old_ni); 1439 SetPageUptodate(ipage); 1440 fill_node_footer(ipage, ino, ino, 0, true); 1441 1442 src = (struct f2fs_node *)page_address(page); 1443 dst = (struct f2fs_node *)page_address(ipage); 1444 1445 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i); 1446 dst->i.i_size = 0; 1447 dst->i.i_blocks = cpu_to_le64(1); 1448 dst->i.i_links = cpu_to_le32(1); 1449 dst->i.i_xattr_nid = 0; 1450 1451 new_ni = old_ni; 1452 new_ni.ino = ino; 1453 1454 set_node_addr(sbi, &new_ni, NEW_ADDR); 1455 inc_valid_inode_count(sbi); 1456 1457 f2fs_put_page(ipage, 1); 1458 return 0; 1459 } 1460 1461 int restore_node_summary(struct f2fs_sb_info *sbi, 1462 unsigned int segno, struct f2fs_summary_block *sum) 1463 { 1464 struct f2fs_node *rn; 1465 struct f2fs_summary *sum_entry; 1466 struct page *page; 1467 block_t addr; 1468 int i, last_offset; 1469 1470 /* alloc temporal page for read node */ 1471 page = alloc_page(GFP_NOFS | __GFP_ZERO); 1472 if (IS_ERR(page)) 1473 return PTR_ERR(page); 1474 lock_page(page); 1475 1476 /* scan the node segment */ 1477 last_offset = sbi->blocks_per_seg; 1478 addr = START_BLOCK(sbi, segno); 1479 sum_entry = &sum->entries[0]; 1480 1481 for (i = 0; i < last_offset; i++, sum_entry++) { 1482 if (f2fs_readpage(sbi, page, addr, READ_SYNC)) 1483 goto out; 1484 1485 rn = (struct f2fs_node *)page_address(page); 1486 sum_entry->nid = rn->footer.nid; 1487 sum_entry->version = 0; 1488 sum_entry->ofs_in_node = 0; 1489 addr++; 1490 1491 /* 1492 * In order to read next node page, 1493 * we must clear PageUptodate flag. 1494 */ 1495 ClearPageUptodate(page); 1496 } 1497 out: 1498 unlock_page(page); 1499 __free_pages(page, 0); 1500 return 0; 1501 } 1502 1503 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi) 1504 { 1505 struct f2fs_nm_info *nm_i = NM_I(sbi); 1506 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1507 struct f2fs_summary_block *sum = curseg->sum_blk; 1508 int i; 1509 1510 mutex_lock(&curseg->curseg_mutex); 1511 1512 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) { 1513 mutex_unlock(&curseg->curseg_mutex); 1514 return false; 1515 } 1516 1517 for (i = 0; i < nats_in_cursum(sum); i++) { 1518 struct nat_entry *ne; 1519 struct f2fs_nat_entry raw_ne; 1520 nid_t nid = le32_to_cpu(nid_in_journal(sum, i)); 1521 1522 raw_ne = nat_in_journal(sum, i); 1523 retry: 1524 write_lock(&nm_i->nat_tree_lock); 1525 ne = __lookup_nat_cache(nm_i, nid); 1526 if (ne) { 1527 __set_nat_cache_dirty(nm_i, ne); 1528 write_unlock(&nm_i->nat_tree_lock); 1529 continue; 1530 } 1531 ne = grab_nat_entry(nm_i, nid); 1532 if (!ne) { 1533 write_unlock(&nm_i->nat_tree_lock); 1534 goto retry; 1535 } 1536 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr)); 1537 nat_set_ino(ne, le32_to_cpu(raw_ne.ino)); 1538 nat_set_version(ne, raw_ne.version); 1539 __set_nat_cache_dirty(nm_i, ne); 1540 write_unlock(&nm_i->nat_tree_lock); 1541 } 1542 update_nats_in_cursum(sum, -i); 1543 mutex_unlock(&curseg->curseg_mutex); 1544 return true; 1545 } 1546 1547 /* 1548 * This function is called during the checkpointing process. 1549 */ 1550 void flush_nat_entries(struct f2fs_sb_info *sbi) 1551 { 1552 struct f2fs_nm_info *nm_i = NM_I(sbi); 1553 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1554 struct f2fs_summary_block *sum = curseg->sum_blk; 1555 struct list_head *cur, *n; 1556 struct page *page = NULL; 1557 struct f2fs_nat_block *nat_blk = NULL; 1558 nid_t start_nid = 0, end_nid = 0; 1559 bool flushed; 1560 1561 flushed = flush_nats_in_journal(sbi); 1562 1563 if (!flushed) 1564 mutex_lock(&curseg->curseg_mutex); 1565 1566 /* 1) flush dirty nat caches */ 1567 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) { 1568 struct nat_entry *ne; 1569 nid_t nid; 1570 struct f2fs_nat_entry raw_ne; 1571 int offset = -1; 1572 block_t new_blkaddr; 1573 1574 ne = list_entry(cur, struct nat_entry, list); 1575 nid = nat_get_nid(ne); 1576 1577 if (nat_get_blkaddr(ne) == NEW_ADDR) 1578 continue; 1579 if (flushed) 1580 goto to_nat_page; 1581 1582 /* if there is room for nat enries in curseg->sumpage */ 1583 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1); 1584 if (offset >= 0) { 1585 raw_ne = nat_in_journal(sum, offset); 1586 goto flush_now; 1587 } 1588 to_nat_page: 1589 if (!page || (start_nid > nid || nid > end_nid)) { 1590 if (page) { 1591 f2fs_put_page(page, 1); 1592 page = NULL; 1593 } 1594 start_nid = START_NID(nid); 1595 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1; 1596 1597 /* 1598 * get nat block with dirty flag, increased reference 1599 * count, mapped and lock 1600 */ 1601 page = get_next_nat_page(sbi, start_nid); 1602 nat_blk = page_address(page); 1603 } 1604 1605 BUG_ON(!nat_blk); 1606 raw_ne = nat_blk->entries[nid - start_nid]; 1607 flush_now: 1608 new_blkaddr = nat_get_blkaddr(ne); 1609 1610 raw_ne.ino = cpu_to_le32(nat_get_ino(ne)); 1611 raw_ne.block_addr = cpu_to_le32(new_blkaddr); 1612 raw_ne.version = nat_get_version(ne); 1613 1614 if (offset < 0) { 1615 nat_blk->entries[nid - start_nid] = raw_ne; 1616 } else { 1617 nat_in_journal(sum, offset) = raw_ne; 1618 nid_in_journal(sum, offset) = cpu_to_le32(nid); 1619 } 1620 1621 if (nat_get_blkaddr(ne) == NULL_ADDR) { 1622 write_lock(&nm_i->nat_tree_lock); 1623 __del_from_nat_cache(nm_i, ne); 1624 write_unlock(&nm_i->nat_tree_lock); 1625 1626 /* We can reuse this freed nid at this point */ 1627 add_free_nid(NM_I(sbi), nid); 1628 } else { 1629 write_lock(&nm_i->nat_tree_lock); 1630 __clear_nat_cache_dirty(nm_i, ne); 1631 ne->checkpointed = true; 1632 write_unlock(&nm_i->nat_tree_lock); 1633 } 1634 } 1635 if (!flushed) 1636 mutex_unlock(&curseg->curseg_mutex); 1637 f2fs_put_page(page, 1); 1638 1639 /* 2) shrink nat caches if necessary */ 1640 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD); 1641 } 1642 1643 static int init_node_manager(struct f2fs_sb_info *sbi) 1644 { 1645 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); 1646 struct f2fs_nm_info *nm_i = NM_I(sbi); 1647 unsigned char *version_bitmap; 1648 unsigned int nat_segs, nat_blocks; 1649 1650 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); 1651 1652 /* segment_count_nat includes pair segment so divide to 2. */ 1653 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; 1654 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); 1655 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks; 1656 nm_i->fcnt = 0; 1657 nm_i->nat_cnt = 0; 1658 1659 INIT_LIST_HEAD(&nm_i->free_nid_list); 1660 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC); 1661 INIT_LIST_HEAD(&nm_i->nat_entries); 1662 INIT_LIST_HEAD(&nm_i->dirty_nat_entries); 1663 1664 mutex_init(&nm_i->build_lock); 1665 spin_lock_init(&nm_i->free_nid_list_lock); 1666 rwlock_init(&nm_i->nat_tree_lock); 1667 1668 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 1669 nm_i->init_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 1670 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 1671 1672 nm_i->nat_bitmap = kzalloc(nm_i->bitmap_size, GFP_KERNEL); 1673 if (!nm_i->nat_bitmap) 1674 return -ENOMEM; 1675 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 1676 if (!version_bitmap) 1677 return -EFAULT; 1678 1679 /* copy version bitmap */ 1680 memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size); 1681 return 0; 1682 } 1683 1684 int build_node_manager(struct f2fs_sb_info *sbi) 1685 { 1686 int err; 1687 1688 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL); 1689 if (!sbi->nm_info) 1690 return -ENOMEM; 1691 1692 err = init_node_manager(sbi); 1693 if (err) 1694 return err; 1695 1696 build_free_nids(sbi); 1697 return 0; 1698 } 1699 1700 void destroy_node_manager(struct f2fs_sb_info *sbi) 1701 { 1702 struct f2fs_nm_info *nm_i = NM_I(sbi); 1703 struct free_nid *i, *next_i; 1704 struct nat_entry *natvec[NATVEC_SIZE]; 1705 nid_t nid = 0; 1706 unsigned int found; 1707 1708 if (!nm_i) 1709 return; 1710 1711 /* destroy free nid list */ 1712 spin_lock(&nm_i->free_nid_list_lock); 1713 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) { 1714 BUG_ON(i->state == NID_ALLOC); 1715 __del_from_free_nid_list(i); 1716 nm_i->fcnt--; 1717 } 1718 BUG_ON(nm_i->fcnt); 1719 spin_unlock(&nm_i->free_nid_list_lock); 1720 1721 /* destroy nat cache */ 1722 write_lock(&nm_i->nat_tree_lock); 1723 while ((found = __gang_lookup_nat_cache(nm_i, 1724 nid, NATVEC_SIZE, natvec))) { 1725 unsigned idx; 1726 for (idx = 0; idx < found; idx++) { 1727 struct nat_entry *e = natvec[idx]; 1728 nid = nat_get_nid(e) + 1; 1729 __del_from_nat_cache(nm_i, e); 1730 } 1731 } 1732 BUG_ON(nm_i->nat_cnt); 1733 write_unlock(&nm_i->nat_tree_lock); 1734 1735 kfree(nm_i->nat_bitmap); 1736 sbi->nm_info = NULL; 1737 kfree(nm_i); 1738 } 1739 1740 int __init create_node_manager_caches(void) 1741 { 1742 nat_entry_slab = f2fs_kmem_cache_create("nat_entry", 1743 sizeof(struct nat_entry), NULL); 1744 if (!nat_entry_slab) 1745 return -ENOMEM; 1746 1747 free_nid_slab = f2fs_kmem_cache_create("free_nid", 1748 sizeof(struct free_nid), NULL); 1749 if (!free_nid_slab) { 1750 kmem_cache_destroy(nat_entry_slab); 1751 return -ENOMEM; 1752 } 1753 return 0; 1754 } 1755 1756 void destroy_node_manager_caches(void) 1757 { 1758 kmem_cache_destroy(free_nid_slab); 1759 kmem_cache_destroy(nat_entry_slab); 1760 } 1761