1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2011 Fujitsu. All rights reserved. 4 * Written by Miao Xie <miaox@cn.fujitsu.com> 5 */ 6 7 #include <linux/slab.h> 8 #include <linux/iversion.h> 9 #include "delayed-inode.h" 10 #include "disk-io.h" 11 #include "transaction.h" 12 #include "ctree.h" 13 #include "qgroup.h" 14 15 #define BTRFS_DELAYED_WRITEBACK 512 16 #define BTRFS_DELAYED_BACKGROUND 128 17 #define BTRFS_DELAYED_BATCH 16 18 19 static struct kmem_cache *delayed_node_cache; 20 21 int __init btrfs_delayed_inode_init(void) 22 { 23 delayed_node_cache = kmem_cache_create("btrfs_delayed_node", 24 sizeof(struct btrfs_delayed_node), 25 0, 26 SLAB_MEM_SPREAD, 27 NULL); 28 if (!delayed_node_cache) 29 return -ENOMEM; 30 return 0; 31 } 32 33 void __cold btrfs_delayed_inode_exit(void) 34 { 35 kmem_cache_destroy(delayed_node_cache); 36 } 37 38 static inline void btrfs_init_delayed_node( 39 struct btrfs_delayed_node *delayed_node, 40 struct btrfs_root *root, u64 inode_id) 41 { 42 delayed_node->root = root; 43 delayed_node->inode_id = inode_id; 44 refcount_set(&delayed_node->refs, 0); 45 delayed_node->ins_root = RB_ROOT; 46 delayed_node->del_root = RB_ROOT; 47 mutex_init(&delayed_node->mutex); 48 INIT_LIST_HEAD(&delayed_node->n_list); 49 INIT_LIST_HEAD(&delayed_node->p_list); 50 } 51 52 static inline int btrfs_is_continuous_delayed_item( 53 struct btrfs_delayed_item *item1, 54 struct btrfs_delayed_item *item2) 55 { 56 if (item1->key.type == BTRFS_DIR_INDEX_KEY && 57 item1->key.objectid == item2->key.objectid && 58 item1->key.type == item2->key.type && 59 item1->key.offset + 1 == item2->key.offset) 60 return 1; 61 return 0; 62 } 63 64 static struct btrfs_delayed_node *btrfs_get_delayed_node( 65 struct btrfs_inode *btrfs_inode) 66 { 67 struct btrfs_root *root = btrfs_inode->root; 68 u64 ino = btrfs_ino(btrfs_inode); 69 struct btrfs_delayed_node *node; 70 71 node = READ_ONCE(btrfs_inode->delayed_node); 72 if (node) { 73 refcount_inc(&node->refs); 74 return node; 75 } 76 77 spin_lock(&root->inode_lock); 78 node = radix_tree_lookup(&root->delayed_nodes_tree, ino); 79 80 if (node) { 81 if (btrfs_inode->delayed_node) { 82 refcount_inc(&node->refs); /* can be accessed */ 83 BUG_ON(btrfs_inode->delayed_node != node); 84 spin_unlock(&root->inode_lock); 85 return node; 86 } 87 88 /* 89 * It's possible that we're racing into the middle of removing 90 * this node from the radix tree. In this case, the refcount 91 * was zero and it should never go back to one. Just return 92 * NULL like it was never in the radix at all; our release 93 * function is in the process of removing it. 94 * 95 * Some implementations of refcount_inc refuse to bump the 96 * refcount once it has hit zero. If we don't do this dance 97 * here, refcount_inc() may decide to just WARN_ONCE() instead 98 * of actually bumping the refcount. 99 * 100 * If this node is properly in the radix, we want to bump the 101 * refcount twice, once for the inode and once for this get 102 * operation. 103 */ 104 if (refcount_inc_not_zero(&node->refs)) { 105 refcount_inc(&node->refs); 106 btrfs_inode->delayed_node = node; 107 } else { 108 node = NULL; 109 } 110 111 spin_unlock(&root->inode_lock); 112 return node; 113 } 114 spin_unlock(&root->inode_lock); 115 116 return NULL; 117 } 118 119 /* Will return either the node or PTR_ERR(-ENOMEM) */ 120 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( 121 struct btrfs_inode *btrfs_inode) 122 { 123 struct btrfs_delayed_node *node; 124 struct btrfs_root *root = btrfs_inode->root; 125 u64 ino = btrfs_ino(btrfs_inode); 126 int ret; 127 128 again: 129 node = btrfs_get_delayed_node(btrfs_inode); 130 if (node) 131 return node; 132 133 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS); 134 if (!node) 135 return ERR_PTR(-ENOMEM); 136 btrfs_init_delayed_node(node, root, ino); 137 138 /* cached in the btrfs inode and can be accessed */ 139 refcount_set(&node->refs, 2); 140 141 ret = radix_tree_preload(GFP_NOFS); 142 if (ret) { 143 kmem_cache_free(delayed_node_cache, node); 144 return ERR_PTR(ret); 145 } 146 147 spin_lock(&root->inode_lock); 148 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node); 149 if (ret == -EEXIST) { 150 spin_unlock(&root->inode_lock); 151 kmem_cache_free(delayed_node_cache, node); 152 radix_tree_preload_end(); 153 goto again; 154 } 155 btrfs_inode->delayed_node = node; 156 spin_unlock(&root->inode_lock); 157 radix_tree_preload_end(); 158 159 return node; 160 } 161 162 /* 163 * Call it when holding delayed_node->mutex 164 * 165 * If mod = 1, add this node into the prepared list. 166 */ 167 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root, 168 struct btrfs_delayed_node *node, 169 int mod) 170 { 171 spin_lock(&root->lock); 172 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { 173 if (!list_empty(&node->p_list)) 174 list_move_tail(&node->p_list, &root->prepare_list); 175 else if (mod) 176 list_add_tail(&node->p_list, &root->prepare_list); 177 } else { 178 list_add_tail(&node->n_list, &root->node_list); 179 list_add_tail(&node->p_list, &root->prepare_list); 180 refcount_inc(&node->refs); /* inserted into list */ 181 root->nodes++; 182 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); 183 } 184 spin_unlock(&root->lock); 185 } 186 187 /* Call it when holding delayed_node->mutex */ 188 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root, 189 struct btrfs_delayed_node *node) 190 { 191 spin_lock(&root->lock); 192 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { 193 root->nodes--; 194 refcount_dec(&node->refs); /* not in the list */ 195 list_del_init(&node->n_list); 196 if (!list_empty(&node->p_list)) 197 list_del_init(&node->p_list); 198 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); 199 } 200 spin_unlock(&root->lock); 201 } 202 203 static struct btrfs_delayed_node *btrfs_first_delayed_node( 204 struct btrfs_delayed_root *delayed_root) 205 { 206 struct list_head *p; 207 struct btrfs_delayed_node *node = NULL; 208 209 spin_lock(&delayed_root->lock); 210 if (list_empty(&delayed_root->node_list)) 211 goto out; 212 213 p = delayed_root->node_list.next; 214 node = list_entry(p, struct btrfs_delayed_node, n_list); 215 refcount_inc(&node->refs); 216 out: 217 spin_unlock(&delayed_root->lock); 218 219 return node; 220 } 221 222 static struct btrfs_delayed_node *btrfs_next_delayed_node( 223 struct btrfs_delayed_node *node) 224 { 225 struct btrfs_delayed_root *delayed_root; 226 struct list_head *p; 227 struct btrfs_delayed_node *next = NULL; 228 229 delayed_root = node->root->fs_info->delayed_root; 230 spin_lock(&delayed_root->lock); 231 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { 232 /* not in the list */ 233 if (list_empty(&delayed_root->node_list)) 234 goto out; 235 p = delayed_root->node_list.next; 236 } else if (list_is_last(&node->n_list, &delayed_root->node_list)) 237 goto out; 238 else 239 p = node->n_list.next; 240 241 next = list_entry(p, struct btrfs_delayed_node, n_list); 242 refcount_inc(&next->refs); 243 out: 244 spin_unlock(&delayed_root->lock); 245 246 return next; 247 } 248 249 static void __btrfs_release_delayed_node( 250 struct btrfs_delayed_node *delayed_node, 251 int mod) 252 { 253 struct btrfs_delayed_root *delayed_root; 254 255 if (!delayed_node) 256 return; 257 258 delayed_root = delayed_node->root->fs_info->delayed_root; 259 260 mutex_lock(&delayed_node->mutex); 261 if (delayed_node->count) 262 btrfs_queue_delayed_node(delayed_root, delayed_node, mod); 263 else 264 btrfs_dequeue_delayed_node(delayed_root, delayed_node); 265 mutex_unlock(&delayed_node->mutex); 266 267 if (refcount_dec_and_test(&delayed_node->refs)) { 268 struct btrfs_root *root = delayed_node->root; 269 270 spin_lock(&root->inode_lock); 271 /* 272 * Once our refcount goes to zero, nobody is allowed to bump it 273 * back up. We can delete it now. 274 */ 275 ASSERT(refcount_read(&delayed_node->refs) == 0); 276 radix_tree_delete(&root->delayed_nodes_tree, 277 delayed_node->inode_id); 278 spin_unlock(&root->inode_lock); 279 kmem_cache_free(delayed_node_cache, delayed_node); 280 } 281 } 282 283 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node) 284 { 285 __btrfs_release_delayed_node(node, 0); 286 } 287 288 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node( 289 struct btrfs_delayed_root *delayed_root) 290 { 291 struct list_head *p; 292 struct btrfs_delayed_node *node = NULL; 293 294 spin_lock(&delayed_root->lock); 295 if (list_empty(&delayed_root->prepare_list)) 296 goto out; 297 298 p = delayed_root->prepare_list.next; 299 list_del_init(p); 300 node = list_entry(p, struct btrfs_delayed_node, p_list); 301 refcount_inc(&node->refs); 302 out: 303 spin_unlock(&delayed_root->lock); 304 305 return node; 306 } 307 308 static inline void btrfs_release_prepared_delayed_node( 309 struct btrfs_delayed_node *node) 310 { 311 __btrfs_release_delayed_node(node, 1); 312 } 313 314 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len) 315 { 316 struct btrfs_delayed_item *item; 317 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS); 318 if (item) { 319 item->data_len = data_len; 320 item->ins_or_del = 0; 321 item->bytes_reserved = 0; 322 item->delayed_node = NULL; 323 refcount_set(&item->refs, 1); 324 } 325 return item; 326 } 327 328 /* 329 * __btrfs_lookup_delayed_item - look up the delayed item by key 330 * @delayed_node: pointer to the delayed node 331 * @key: the key to look up 332 * @prev: used to store the prev item if the right item isn't found 333 * @next: used to store the next item if the right item isn't found 334 * 335 * Note: if we don't find the right item, we will return the prev item and 336 * the next item. 337 */ 338 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item( 339 struct rb_root *root, 340 struct btrfs_key *key, 341 struct btrfs_delayed_item **prev, 342 struct btrfs_delayed_item **next) 343 { 344 struct rb_node *node, *prev_node = NULL; 345 struct btrfs_delayed_item *delayed_item = NULL; 346 int ret = 0; 347 348 node = root->rb_node; 349 350 while (node) { 351 delayed_item = rb_entry(node, struct btrfs_delayed_item, 352 rb_node); 353 prev_node = node; 354 ret = btrfs_comp_cpu_keys(&delayed_item->key, key); 355 if (ret < 0) 356 node = node->rb_right; 357 else if (ret > 0) 358 node = node->rb_left; 359 else 360 return delayed_item; 361 } 362 363 if (prev) { 364 if (!prev_node) 365 *prev = NULL; 366 else if (ret < 0) 367 *prev = delayed_item; 368 else if ((node = rb_prev(prev_node)) != NULL) { 369 *prev = rb_entry(node, struct btrfs_delayed_item, 370 rb_node); 371 } else 372 *prev = NULL; 373 } 374 375 if (next) { 376 if (!prev_node) 377 *next = NULL; 378 else if (ret > 0) 379 *next = delayed_item; 380 else if ((node = rb_next(prev_node)) != NULL) { 381 *next = rb_entry(node, struct btrfs_delayed_item, 382 rb_node); 383 } else 384 *next = NULL; 385 } 386 return NULL; 387 } 388 389 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item( 390 struct btrfs_delayed_node *delayed_node, 391 struct btrfs_key *key) 392 { 393 return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key, 394 NULL, NULL); 395 } 396 397 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node, 398 struct btrfs_delayed_item *ins, 399 int action) 400 { 401 struct rb_node **p, *node; 402 struct rb_node *parent_node = NULL; 403 struct rb_root *root; 404 struct btrfs_delayed_item *item; 405 int cmp; 406 407 if (action == BTRFS_DELAYED_INSERTION_ITEM) 408 root = &delayed_node->ins_root; 409 else if (action == BTRFS_DELAYED_DELETION_ITEM) 410 root = &delayed_node->del_root; 411 else 412 BUG(); 413 p = &root->rb_node; 414 node = &ins->rb_node; 415 416 while (*p) { 417 parent_node = *p; 418 item = rb_entry(parent_node, struct btrfs_delayed_item, 419 rb_node); 420 421 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key); 422 if (cmp < 0) 423 p = &(*p)->rb_right; 424 else if (cmp > 0) 425 p = &(*p)->rb_left; 426 else 427 return -EEXIST; 428 } 429 430 rb_link_node(node, parent_node, p); 431 rb_insert_color(node, root); 432 ins->delayed_node = delayed_node; 433 ins->ins_or_del = action; 434 435 if (ins->key.type == BTRFS_DIR_INDEX_KEY && 436 action == BTRFS_DELAYED_INSERTION_ITEM && 437 ins->key.offset >= delayed_node->index_cnt) 438 delayed_node->index_cnt = ins->key.offset + 1; 439 440 delayed_node->count++; 441 atomic_inc(&delayed_node->root->fs_info->delayed_root->items); 442 return 0; 443 } 444 445 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node, 446 struct btrfs_delayed_item *item) 447 { 448 return __btrfs_add_delayed_item(node, item, 449 BTRFS_DELAYED_INSERTION_ITEM); 450 } 451 452 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node, 453 struct btrfs_delayed_item *item) 454 { 455 return __btrfs_add_delayed_item(node, item, 456 BTRFS_DELAYED_DELETION_ITEM); 457 } 458 459 static void finish_one_item(struct btrfs_delayed_root *delayed_root) 460 { 461 int seq = atomic_inc_return(&delayed_root->items_seq); 462 463 /* 464 * atomic_dec_return implies a barrier for waitqueue_active 465 */ 466 if ((atomic_dec_return(&delayed_root->items) < 467 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) && 468 waitqueue_active(&delayed_root->wait)) 469 wake_up(&delayed_root->wait); 470 } 471 472 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item) 473 { 474 struct rb_root *root; 475 struct btrfs_delayed_root *delayed_root; 476 477 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root; 478 479 BUG_ON(!delayed_root); 480 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM && 481 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM); 482 483 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM) 484 root = &delayed_item->delayed_node->ins_root; 485 else 486 root = &delayed_item->delayed_node->del_root; 487 488 rb_erase(&delayed_item->rb_node, root); 489 delayed_item->delayed_node->count--; 490 491 finish_one_item(delayed_root); 492 } 493 494 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item) 495 { 496 if (item) { 497 __btrfs_remove_delayed_item(item); 498 if (refcount_dec_and_test(&item->refs)) 499 kfree(item); 500 } 501 } 502 503 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item( 504 struct btrfs_delayed_node *delayed_node) 505 { 506 struct rb_node *p; 507 struct btrfs_delayed_item *item = NULL; 508 509 p = rb_first(&delayed_node->ins_root); 510 if (p) 511 item = rb_entry(p, struct btrfs_delayed_item, rb_node); 512 513 return item; 514 } 515 516 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item( 517 struct btrfs_delayed_node *delayed_node) 518 { 519 struct rb_node *p; 520 struct btrfs_delayed_item *item = NULL; 521 522 p = rb_first(&delayed_node->del_root); 523 if (p) 524 item = rb_entry(p, struct btrfs_delayed_item, rb_node); 525 526 return item; 527 } 528 529 static struct btrfs_delayed_item *__btrfs_next_delayed_item( 530 struct btrfs_delayed_item *item) 531 { 532 struct rb_node *p; 533 struct btrfs_delayed_item *next = NULL; 534 535 p = rb_next(&item->rb_node); 536 if (p) 537 next = rb_entry(p, struct btrfs_delayed_item, rb_node); 538 539 return next; 540 } 541 542 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, 543 struct btrfs_root *root, 544 struct btrfs_delayed_item *item) 545 { 546 struct btrfs_block_rsv *src_rsv; 547 struct btrfs_block_rsv *dst_rsv; 548 struct btrfs_fs_info *fs_info = root->fs_info; 549 u64 num_bytes; 550 int ret; 551 552 if (!trans->bytes_reserved) 553 return 0; 554 555 src_rsv = trans->block_rsv; 556 dst_rsv = &fs_info->delayed_block_rsv; 557 558 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); 559 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); 560 if (!ret) { 561 trace_btrfs_space_reservation(fs_info, "delayed_item", 562 item->key.objectid, 563 num_bytes, 1); 564 item->bytes_reserved = num_bytes; 565 } 566 567 return ret; 568 } 569 570 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, 571 struct btrfs_delayed_item *item) 572 { 573 struct btrfs_block_rsv *rsv; 574 struct btrfs_fs_info *fs_info = root->fs_info; 575 576 if (!item->bytes_reserved) 577 return; 578 579 rsv = &fs_info->delayed_block_rsv; 580 btrfs_qgroup_convert_reserved_meta(root, item->bytes_reserved); 581 trace_btrfs_space_reservation(fs_info, "delayed_item", 582 item->key.objectid, item->bytes_reserved, 583 0); 584 btrfs_block_rsv_release(fs_info, rsv, 585 item->bytes_reserved); 586 } 587 588 static int btrfs_delayed_inode_reserve_metadata( 589 struct btrfs_trans_handle *trans, 590 struct btrfs_root *root, 591 struct btrfs_inode *inode, 592 struct btrfs_delayed_node *node) 593 { 594 struct btrfs_fs_info *fs_info = root->fs_info; 595 struct btrfs_block_rsv *src_rsv; 596 struct btrfs_block_rsv *dst_rsv; 597 u64 num_bytes; 598 int ret; 599 600 src_rsv = trans->block_rsv; 601 dst_rsv = &fs_info->delayed_block_rsv; 602 603 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); 604 605 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true); 606 if (ret < 0) 607 return ret; 608 /* 609 * btrfs_dirty_inode will update the inode under btrfs_join_transaction 610 * which doesn't reserve space for speed. This is a problem since we 611 * still need to reserve space for this update, so try to reserve the 612 * space. 613 * 614 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since 615 * we always reserve enough to update the inode item. 616 */ 617 if (!src_rsv || (!trans->bytes_reserved && 618 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) { 619 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes, 620 BTRFS_RESERVE_NO_FLUSH); 621 /* 622 * Since we're under a transaction reserve_metadata_bytes could 623 * try to commit the transaction which will make it return 624 * EAGAIN to make us stop the transaction we have, so return 625 * ENOSPC instead so that btrfs_dirty_inode knows what to do. 626 */ 627 if (ret == -EAGAIN) { 628 ret = -ENOSPC; 629 btrfs_qgroup_free_meta_prealloc(root, num_bytes); 630 } 631 if (!ret) { 632 node->bytes_reserved = num_bytes; 633 trace_btrfs_space_reservation(fs_info, 634 "delayed_inode", 635 btrfs_ino(inode), 636 num_bytes, 1); 637 } 638 return ret; 639 } 640 641 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); 642 if (!ret) { 643 trace_btrfs_space_reservation(fs_info, "delayed_inode", 644 btrfs_ino(inode), num_bytes, 1); 645 node->bytes_reserved = num_bytes; 646 } 647 648 return ret; 649 } 650 651 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info, 652 struct btrfs_delayed_node *node, 653 bool qgroup_free) 654 { 655 struct btrfs_block_rsv *rsv; 656 657 if (!node->bytes_reserved) 658 return; 659 660 rsv = &fs_info->delayed_block_rsv; 661 trace_btrfs_space_reservation(fs_info, "delayed_inode", 662 node->inode_id, node->bytes_reserved, 0); 663 btrfs_block_rsv_release(fs_info, rsv, 664 node->bytes_reserved); 665 if (qgroup_free) 666 btrfs_qgroup_free_meta_prealloc(node->root, 667 node->bytes_reserved); 668 else 669 btrfs_qgroup_convert_reserved_meta(node->root, 670 node->bytes_reserved); 671 node->bytes_reserved = 0; 672 } 673 674 /* 675 * This helper will insert some continuous items into the same leaf according 676 * to the free space of the leaf. 677 */ 678 static int btrfs_batch_insert_items(struct btrfs_root *root, 679 struct btrfs_path *path, 680 struct btrfs_delayed_item *item) 681 { 682 struct btrfs_fs_info *fs_info = root->fs_info; 683 struct btrfs_delayed_item *curr, *next; 684 int free_space; 685 int total_data_size = 0, total_size = 0; 686 struct extent_buffer *leaf; 687 char *data_ptr; 688 struct btrfs_key *keys; 689 u32 *data_size; 690 struct list_head head; 691 int slot; 692 int nitems; 693 int i; 694 int ret = 0; 695 696 BUG_ON(!path->nodes[0]); 697 698 leaf = path->nodes[0]; 699 free_space = btrfs_leaf_free_space(fs_info, leaf); 700 INIT_LIST_HEAD(&head); 701 702 next = item; 703 nitems = 0; 704 705 /* 706 * count the number of the continuous items that we can insert in batch 707 */ 708 while (total_size + next->data_len + sizeof(struct btrfs_item) <= 709 free_space) { 710 total_data_size += next->data_len; 711 total_size += next->data_len + sizeof(struct btrfs_item); 712 list_add_tail(&next->tree_list, &head); 713 nitems++; 714 715 curr = next; 716 next = __btrfs_next_delayed_item(curr); 717 if (!next) 718 break; 719 720 if (!btrfs_is_continuous_delayed_item(curr, next)) 721 break; 722 } 723 724 if (!nitems) { 725 ret = 0; 726 goto out; 727 } 728 729 /* 730 * we need allocate some memory space, but it might cause the task 731 * to sleep, so we set all locked nodes in the path to blocking locks 732 * first. 733 */ 734 btrfs_set_path_blocking(path); 735 736 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS); 737 if (!keys) { 738 ret = -ENOMEM; 739 goto out; 740 } 741 742 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS); 743 if (!data_size) { 744 ret = -ENOMEM; 745 goto error; 746 } 747 748 /* get keys of all the delayed items */ 749 i = 0; 750 list_for_each_entry(next, &head, tree_list) { 751 keys[i] = next->key; 752 data_size[i] = next->data_len; 753 i++; 754 } 755 756 /* reset all the locked nodes in the patch to spinning locks. */ 757 btrfs_clear_path_blocking(path, NULL, 0); 758 759 /* insert the keys of the items */ 760 setup_items_for_insert(root, path, keys, data_size, 761 total_data_size, total_size, nitems); 762 763 /* insert the dir index items */ 764 slot = path->slots[0]; 765 list_for_each_entry_safe(curr, next, &head, tree_list) { 766 data_ptr = btrfs_item_ptr(leaf, slot, char); 767 write_extent_buffer(leaf, &curr->data, 768 (unsigned long)data_ptr, 769 curr->data_len); 770 slot++; 771 772 btrfs_delayed_item_release_metadata(root, curr); 773 774 list_del(&curr->tree_list); 775 btrfs_release_delayed_item(curr); 776 } 777 778 error: 779 kfree(data_size); 780 kfree(keys); 781 out: 782 return ret; 783 } 784 785 /* 786 * This helper can just do simple insertion that needn't extend item for new 787 * data, such as directory name index insertion, inode insertion. 788 */ 789 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans, 790 struct btrfs_root *root, 791 struct btrfs_path *path, 792 struct btrfs_delayed_item *delayed_item) 793 { 794 struct extent_buffer *leaf; 795 char *ptr; 796 int ret; 797 798 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key, 799 delayed_item->data_len); 800 if (ret < 0 && ret != -EEXIST) 801 return ret; 802 803 leaf = path->nodes[0]; 804 805 ptr = btrfs_item_ptr(leaf, path->slots[0], char); 806 807 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr, 808 delayed_item->data_len); 809 btrfs_mark_buffer_dirty(leaf); 810 811 btrfs_delayed_item_release_metadata(root, delayed_item); 812 return 0; 813 } 814 815 /* 816 * we insert an item first, then if there are some continuous items, we try 817 * to insert those items into the same leaf. 818 */ 819 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans, 820 struct btrfs_path *path, 821 struct btrfs_root *root, 822 struct btrfs_delayed_node *node) 823 { 824 struct btrfs_delayed_item *curr, *prev; 825 int ret = 0; 826 827 do_again: 828 mutex_lock(&node->mutex); 829 curr = __btrfs_first_delayed_insertion_item(node); 830 if (!curr) 831 goto insert_end; 832 833 ret = btrfs_insert_delayed_item(trans, root, path, curr); 834 if (ret < 0) { 835 btrfs_release_path(path); 836 goto insert_end; 837 } 838 839 prev = curr; 840 curr = __btrfs_next_delayed_item(prev); 841 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) { 842 /* insert the continuous items into the same leaf */ 843 path->slots[0]++; 844 btrfs_batch_insert_items(root, path, curr); 845 } 846 btrfs_release_delayed_item(prev); 847 btrfs_mark_buffer_dirty(path->nodes[0]); 848 849 btrfs_release_path(path); 850 mutex_unlock(&node->mutex); 851 goto do_again; 852 853 insert_end: 854 mutex_unlock(&node->mutex); 855 return ret; 856 } 857 858 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans, 859 struct btrfs_root *root, 860 struct btrfs_path *path, 861 struct btrfs_delayed_item *item) 862 { 863 struct btrfs_delayed_item *curr, *next; 864 struct extent_buffer *leaf; 865 struct btrfs_key key; 866 struct list_head head; 867 int nitems, i, last_item; 868 int ret = 0; 869 870 BUG_ON(!path->nodes[0]); 871 872 leaf = path->nodes[0]; 873 874 i = path->slots[0]; 875 last_item = btrfs_header_nritems(leaf) - 1; 876 if (i > last_item) 877 return -ENOENT; /* FIXME: Is errno suitable? */ 878 879 next = item; 880 INIT_LIST_HEAD(&head); 881 btrfs_item_key_to_cpu(leaf, &key, i); 882 nitems = 0; 883 /* 884 * count the number of the dir index items that we can delete in batch 885 */ 886 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) { 887 list_add_tail(&next->tree_list, &head); 888 nitems++; 889 890 curr = next; 891 next = __btrfs_next_delayed_item(curr); 892 if (!next) 893 break; 894 895 if (!btrfs_is_continuous_delayed_item(curr, next)) 896 break; 897 898 i++; 899 if (i > last_item) 900 break; 901 btrfs_item_key_to_cpu(leaf, &key, i); 902 } 903 904 if (!nitems) 905 return 0; 906 907 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems); 908 if (ret) 909 goto out; 910 911 list_for_each_entry_safe(curr, next, &head, tree_list) { 912 btrfs_delayed_item_release_metadata(root, curr); 913 list_del(&curr->tree_list); 914 btrfs_release_delayed_item(curr); 915 } 916 917 out: 918 return ret; 919 } 920 921 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans, 922 struct btrfs_path *path, 923 struct btrfs_root *root, 924 struct btrfs_delayed_node *node) 925 { 926 struct btrfs_delayed_item *curr, *prev; 927 int ret = 0; 928 929 do_again: 930 mutex_lock(&node->mutex); 931 curr = __btrfs_first_delayed_deletion_item(node); 932 if (!curr) 933 goto delete_fail; 934 935 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1); 936 if (ret < 0) 937 goto delete_fail; 938 else if (ret > 0) { 939 /* 940 * can't find the item which the node points to, so this node 941 * is invalid, just drop it. 942 */ 943 prev = curr; 944 curr = __btrfs_next_delayed_item(prev); 945 btrfs_release_delayed_item(prev); 946 ret = 0; 947 btrfs_release_path(path); 948 if (curr) { 949 mutex_unlock(&node->mutex); 950 goto do_again; 951 } else 952 goto delete_fail; 953 } 954 955 btrfs_batch_delete_items(trans, root, path, curr); 956 btrfs_release_path(path); 957 mutex_unlock(&node->mutex); 958 goto do_again; 959 960 delete_fail: 961 btrfs_release_path(path); 962 mutex_unlock(&node->mutex); 963 return ret; 964 } 965 966 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node) 967 { 968 struct btrfs_delayed_root *delayed_root; 969 970 if (delayed_node && 971 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 972 BUG_ON(!delayed_node->root); 973 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); 974 delayed_node->count--; 975 976 delayed_root = delayed_node->root->fs_info->delayed_root; 977 finish_one_item(delayed_root); 978 } 979 } 980 981 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node) 982 { 983 struct btrfs_delayed_root *delayed_root; 984 985 ASSERT(delayed_node->root); 986 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); 987 delayed_node->count--; 988 989 delayed_root = delayed_node->root->fs_info->delayed_root; 990 finish_one_item(delayed_root); 991 } 992 993 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, 994 struct btrfs_root *root, 995 struct btrfs_path *path, 996 struct btrfs_delayed_node *node) 997 { 998 struct btrfs_fs_info *fs_info = root->fs_info; 999 struct btrfs_key key; 1000 struct btrfs_inode_item *inode_item; 1001 struct extent_buffer *leaf; 1002 int mod; 1003 int ret; 1004 1005 key.objectid = node->inode_id; 1006 key.type = BTRFS_INODE_ITEM_KEY; 1007 key.offset = 0; 1008 1009 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) 1010 mod = -1; 1011 else 1012 mod = 1; 1013 1014 ret = btrfs_lookup_inode(trans, root, path, &key, mod); 1015 if (ret > 0) { 1016 btrfs_release_path(path); 1017 return -ENOENT; 1018 } else if (ret < 0) { 1019 return ret; 1020 } 1021 1022 leaf = path->nodes[0]; 1023 inode_item = btrfs_item_ptr(leaf, path->slots[0], 1024 struct btrfs_inode_item); 1025 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item, 1026 sizeof(struct btrfs_inode_item)); 1027 btrfs_mark_buffer_dirty(leaf); 1028 1029 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) 1030 goto no_iref; 1031 1032 path->slots[0]++; 1033 if (path->slots[0] >= btrfs_header_nritems(leaf)) 1034 goto search; 1035 again: 1036 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 1037 if (key.objectid != node->inode_id) 1038 goto out; 1039 1040 if (key.type != BTRFS_INODE_REF_KEY && 1041 key.type != BTRFS_INODE_EXTREF_KEY) 1042 goto out; 1043 1044 /* 1045 * Delayed iref deletion is for the inode who has only one link, 1046 * so there is only one iref. The case that several irefs are 1047 * in the same item doesn't exist. 1048 */ 1049 btrfs_del_item(trans, root, path); 1050 out: 1051 btrfs_release_delayed_iref(node); 1052 no_iref: 1053 btrfs_release_path(path); 1054 err_out: 1055 btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0)); 1056 btrfs_release_delayed_inode(node); 1057 1058 return ret; 1059 1060 search: 1061 btrfs_release_path(path); 1062 1063 key.type = BTRFS_INODE_EXTREF_KEY; 1064 key.offset = -1; 1065 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 1066 if (ret < 0) 1067 goto err_out; 1068 ASSERT(ret); 1069 1070 ret = 0; 1071 leaf = path->nodes[0]; 1072 path->slots[0]--; 1073 goto again; 1074 } 1075 1076 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, 1077 struct btrfs_root *root, 1078 struct btrfs_path *path, 1079 struct btrfs_delayed_node *node) 1080 { 1081 int ret; 1082 1083 mutex_lock(&node->mutex); 1084 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) { 1085 mutex_unlock(&node->mutex); 1086 return 0; 1087 } 1088 1089 ret = __btrfs_update_delayed_inode(trans, root, path, node); 1090 mutex_unlock(&node->mutex); 1091 return ret; 1092 } 1093 1094 static inline int 1095 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, 1096 struct btrfs_path *path, 1097 struct btrfs_delayed_node *node) 1098 { 1099 int ret; 1100 1101 ret = btrfs_insert_delayed_items(trans, path, node->root, node); 1102 if (ret) 1103 return ret; 1104 1105 ret = btrfs_delete_delayed_items(trans, path, node->root, node); 1106 if (ret) 1107 return ret; 1108 1109 ret = btrfs_update_delayed_inode(trans, node->root, path, node); 1110 return ret; 1111 } 1112 1113 /* 1114 * Called when committing the transaction. 1115 * Returns 0 on success. 1116 * Returns < 0 on error and returns with an aborted transaction with any 1117 * outstanding delayed items cleaned up. 1118 */ 1119 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr) 1120 { 1121 struct btrfs_fs_info *fs_info = trans->fs_info; 1122 struct btrfs_delayed_root *delayed_root; 1123 struct btrfs_delayed_node *curr_node, *prev_node; 1124 struct btrfs_path *path; 1125 struct btrfs_block_rsv *block_rsv; 1126 int ret = 0; 1127 bool count = (nr > 0); 1128 1129 if (trans->aborted) 1130 return -EIO; 1131 1132 path = btrfs_alloc_path(); 1133 if (!path) 1134 return -ENOMEM; 1135 path->leave_spinning = 1; 1136 1137 block_rsv = trans->block_rsv; 1138 trans->block_rsv = &fs_info->delayed_block_rsv; 1139 1140 delayed_root = fs_info->delayed_root; 1141 1142 curr_node = btrfs_first_delayed_node(delayed_root); 1143 while (curr_node && (!count || (count && nr--))) { 1144 ret = __btrfs_commit_inode_delayed_items(trans, path, 1145 curr_node); 1146 if (ret) { 1147 btrfs_release_delayed_node(curr_node); 1148 curr_node = NULL; 1149 btrfs_abort_transaction(trans, ret); 1150 break; 1151 } 1152 1153 prev_node = curr_node; 1154 curr_node = btrfs_next_delayed_node(curr_node); 1155 btrfs_release_delayed_node(prev_node); 1156 } 1157 1158 if (curr_node) 1159 btrfs_release_delayed_node(curr_node); 1160 btrfs_free_path(path); 1161 trans->block_rsv = block_rsv; 1162 1163 return ret; 1164 } 1165 1166 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans) 1167 { 1168 return __btrfs_run_delayed_items(trans, -1); 1169 } 1170 1171 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr) 1172 { 1173 return __btrfs_run_delayed_items(trans, nr); 1174 } 1175 1176 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, 1177 struct btrfs_inode *inode) 1178 { 1179 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); 1180 struct btrfs_path *path; 1181 struct btrfs_block_rsv *block_rsv; 1182 int ret; 1183 1184 if (!delayed_node) 1185 return 0; 1186 1187 mutex_lock(&delayed_node->mutex); 1188 if (!delayed_node->count) { 1189 mutex_unlock(&delayed_node->mutex); 1190 btrfs_release_delayed_node(delayed_node); 1191 return 0; 1192 } 1193 mutex_unlock(&delayed_node->mutex); 1194 1195 path = btrfs_alloc_path(); 1196 if (!path) { 1197 btrfs_release_delayed_node(delayed_node); 1198 return -ENOMEM; 1199 } 1200 path->leave_spinning = 1; 1201 1202 block_rsv = trans->block_rsv; 1203 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; 1204 1205 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node); 1206 1207 btrfs_release_delayed_node(delayed_node); 1208 btrfs_free_path(path); 1209 trans->block_rsv = block_rsv; 1210 1211 return ret; 1212 } 1213 1214 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode) 1215 { 1216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 1217 struct btrfs_trans_handle *trans; 1218 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); 1219 struct btrfs_path *path; 1220 struct btrfs_block_rsv *block_rsv; 1221 int ret; 1222 1223 if (!delayed_node) 1224 return 0; 1225 1226 mutex_lock(&delayed_node->mutex); 1227 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 1228 mutex_unlock(&delayed_node->mutex); 1229 btrfs_release_delayed_node(delayed_node); 1230 return 0; 1231 } 1232 mutex_unlock(&delayed_node->mutex); 1233 1234 trans = btrfs_join_transaction(delayed_node->root); 1235 if (IS_ERR(trans)) { 1236 ret = PTR_ERR(trans); 1237 goto out; 1238 } 1239 1240 path = btrfs_alloc_path(); 1241 if (!path) { 1242 ret = -ENOMEM; 1243 goto trans_out; 1244 } 1245 path->leave_spinning = 1; 1246 1247 block_rsv = trans->block_rsv; 1248 trans->block_rsv = &fs_info->delayed_block_rsv; 1249 1250 mutex_lock(&delayed_node->mutex); 1251 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) 1252 ret = __btrfs_update_delayed_inode(trans, delayed_node->root, 1253 path, delayed_node); 1254 else 1255 ret = 0; 1256 mutex_unlock(&delayed_node->mutex); 1257 1258 btrfs_free_path(path); 1259 trans->block_rsv = block_rsv; 1260 trans_out: 1261 btrfs_end_transaction(trans); 1262 btrfs_btree_balance_dirty(fs_info); 1263 out: 1264 btrfs_release_delayed_node(delayed_node); 1265 1266 return ret; 1267 } 1268 1269 void btrfs_remove_delayed_node(struct btrfs_inode *inode) 1270 { 1271 struct btrfs_delayed_node *delayed_node; 1272 1273 delayed_node = READ_ONCE(inode->delayed_node); 1274 if (!delayed_node) 1275 return; 1276 1277 inode->delayed_node = NULL; 1278 btrfs_release_delayed_node(delayed_node); 1279 } 1280 1281 struct btrfs_async_delayed_work { 1282 struct btrfs_delayed_root *delayed_root; 1283 int nr; 1284 struct btrfs_work work; 1285 }; 1286 1287 static void btrfs_async_run_delayed_root(struct btrfs_work *work) 1288 { 1289 struct btrfs_async_delayed_work *async_work; 1290 struct btrfs_delayed_root *delayed_root; 1291 struct btrfs_trans_handle *trans; 1292 struct btrfs_path *path; 1293 struct btrfs_delayed_node *delayed_node = NULL; 1294 struct btrfs_root *root; 1295 struct btrfs_block_rsv *block_rsv; 1296 int total_done = 0; 1297 1298 async_work = container_of(work, struct btrfs_async_delayed_work, work); 1299 delayed_root = async_work->delayed_root; 1300 1301 path = btrfs_alloc_path(); 1302 if (!path) 1303 goto out; 1304 1305 do { 1306 if (atomic_read(&delayed_root->items) < 1307 BTRFS_DELAYED_BACKGROUND / 2) 1308 break; 1309 1310 delayed_node = btrfs_first_prepared_delayed_node(delayed_root); 1311 if (!delayed_node) 1312 break; 1313 1314 path->leave_spinning = 1; 1315 root = delayed_node->root; 1316 1317 trans = btrfs_join_transaction(root); 1318 if (IS_ERR(trans)) { 1319 btrfs_release_path(path); 1320 btrfs_release_prepared_delayed_node(delayed_node); 1321 total_done++; 1322 continue; 1323 } 1324 1325 block_rsv = trans->block_rsv; 1326 trans->block_rsv = &root->fs_info->delayed_block_rsv; 1327 1328 __btrfs_commit_inode_delayed_items(trans, path, delayed_node); 1329 1330 trans->block_rsv = block_rsv; 1331 btrfs_end_transaction(trans); 1332 btrfs_btree_balance_dirty_nodelay(root->fs_info); 1333 1334 btrfs_release_path(path); 1335 btrfs_release_prepared_delayed_node(delayed_node); 1336 total_done++; 1337 1338 } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK) 1339 || total_done < async_work->nr); 1340 1341 btrfs_free_path(path); 1342 out: 1343 wake_up(&delayed_root->wait); 1344 kfree(async_work); 1345 } 1346 1347 1348 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, 1349 struct btrfs_fs_info *fs_info, int nr) 1350 { 1351 struct btrfs_async_delayed_work *async_work; 1352 1353 async_work = kmalloc(sizeof(*async_work), GFP_NOFS); 1354 if (!async_work) 1355 return -ENOMEM; 1356 1357 async_work->delayed_root = delayed_root; 1358 btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper, 1359 btrfs_async_run_delayed_root, NULL, NULL); 1360 async_work->nr = nr; 1361 1362 btrfs_queue_work(fs_info->delayed_workers, &async_work->work); 1363 return 0; 1364 } 1365 1366 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info) 1367 { 1368 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root)); 1369 } 1370 1371 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq) 1372 { 1373 int val = atomic_read(&delayed_root->items_seq); 1374 1375 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH) 1376 return 1; 1377 1378 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) 1379 return 1; 1380 1381 return 0; 1382 } 1383 1384 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info) 1385 { 1386 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root; 1387 1388 if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) || 1389 btrfs_workqueue_normal_congested(fs_info->delayed_workers)) 1390 return; 1391 1392 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) { 1393 int seq; 1394 int ret; 1395 1396 seq = atomic_read(&delayed_root->items_seq); 1397 1398 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0); 1399 if (ret) 1400 return; 1401 1402 wait_event_interruptible(delayed_root->wait, 1403 could_end_wait(delayed_root, seq)); 1404 return; 1405 } 1406 1407 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH); 1408 } 1409 1410 /* Will return 0 or -ENOMEM */ 1411 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, 1412 struct btrfs_fs_info *fs_info, 1413 const char *name, int name_len, 1414 struct btrfs_inode *dir, 1415 struct btrfs_disk_key *disk_key, u8 type, 1416 u64 index) 1417 { 1418 struct btrfs_delayed_node *delayed_node; 1419 struct btrfs_delayed_item *delayed_item; 1420 struct btrfs_dir_item *dir_item; 1421 int ret; 1422 1423 delayed_node = btrfs_get_or_create_delayed_node(dir); 1424 if (IS_ERR(delayed_node)) 1425 return PTR_ERR(delayed_node); 1426 1427 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len); 1428 if (!delayed_item) { 1429 ret = -ENOMEM; 1430 goto release_node; 1431 } 1432 1433 delayed_item->key.objectid = btrfs_ino(dir); 1434 delayed_item->key.type = BTRFS_DIR_INDEX_KEY; 1435 delayed_item->key.offset = index; 1436 1437 dir_item = (struct btrfs_dir_item *)delayed_item->data; 1438 dir_item->location = *disk_key; 1439 btrfs_set_stack_dir_transid(dir_item, trans->transid); 1440 btrfs_set_stack_dir_data_len(dir_item, 0); 1441 btrfs_set_stack_dir_name_len(dir_item, name_len); 1442 btrfs_set_stack_dir_type(dir_item, type); 1443 memcpy((char *)(dir_item + 1), name, name_len); 1444 1445 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item); 1446 /* 1447 * we have reserved enough space when we start a new transaction, 1448 * so reserving metadata failure is impossible 1449 */ 1450 BUG_ON(ret); 1451 1452 1453 mutex_lock(&delayed_node->mutex); 1454 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item); 1455 if (unlikely(ret)) { 1456 btrfs_err(fs_info, 1457 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", 1458 name_len, name, delayed_node->root->objectid, 1459 delayed_node->inode_id, ret); 1460 BUG(); 1461 } 1462 mutex_unlock(&delayed_node->mutex); 1463 1464 release_node: 1465 btrfs_release_delayed_node(delayed_node); 1466 return ret; 1467 } 1468 1469 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info, 1470 struct btrfs_delayed_node *node, 1471 struct btrfs_key *key) 1472 { 1473 struct btrfs_delayed_item *item; 1474 1475 mutex_lock(&node->mutex); 1476 item = __btrfs_lookup_delayed_insertion_item(node, key); 1477 if (!item) { 1478 mutex_unlock(&node->mutex); 1479 return 1; 1480 } 1481 1482 btrfs_delayed_item_release_metadata(node->root, item); 1483 btrfs_release_delayed_item(item); 1484 mutex_unlock(&node->mutex); 1485 return 0; 1486 } 1487 1488 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, 1489 struct btrfs_fs_info *fs_info, 1490 struct btrfs_inode *dir, u64 index) 1491 { 1492 struct btrfs_delayed_node *node; 1493 struct btrfs_delayed_item *item; 1494 struct btrfs_key item_key; 1495 int ret; 1496 1497 node = btrfs_get_or_create_delayed_node(dir); 1498 if (IS_ERR(node)) 1499 return PTR_ERR(node); 1500 1501 item_key.objectid = btrfs_ino(dir); 1502 item_key.type = BTRFS_DIR_INDEX_KEY; 1503 item_key.offset = index; 1504 1505 ret = btrfs_delete_delayed_insertion_item(fs_info, node, &item_key); 1506 if (!ret) 1507 goto end; 1508 1509 item = btrfs_alloc_delayed_item(0); 1510 if (!item) { 1511 ret = -ENOMEM; 1512 goto end; 1513 } 1514 1515 item->key = item_key; 1516 1517 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item); 1518 /* 1519 * we have reserved enough space when we start a new transaction, 1520 * so reserving metadata failure is impossible. 1521 */ 1522 BUG_ON(ret); 1523 1524 mutex_lock(&node->mutex); 1525 ret = __btrfs_add_delayed_deletion_item(node, item); 1526 if (unlikely(ret)) { 1527 btrfs_err(fs_info, 1528 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", 1529 index, node->root->objectid, node->inode_id, ret); 1530 BUG(); 1531 } 1532 mutex_unlock(&node->mutex); 1533 end: 1534 btrfs_release_delayed_node(node); 1535 return ret; 1536 } 1537 1538 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode) 1539 { 1540 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); 1541 1542 if (!delayed_node) 1543 return -ENOENT; 1544 1545 /* 1546 * Since we have held i_mutex of this directory, it is impossible that 1547 * a new directory index is added into the delayed node and index_cnt 1548 * is updated now. So we needn't lock the delayed node. 1549 */ 1550 if (!delayed_node->index_cnt) { 1551 btrfs_release_delayed_node(delayed_node); 1552 return -EINVAL; 1553 } 1554 1555 inode->index_cnt = delayed_node->index_cnt; 1556 btrfs_release_delayed_node(delayed_node); 1557 return 0; 1558 } 1559 1560 bool btrfs_readdir_get_delayed_items(struct inode *inode, 1561 struct list_head *ins_list, 1562 struct list_head *del_list) 1563 { 1564 struct btrfs_delayed_node *delayed_node; 1565 struct btrfs_delayed_item *item; 1566 1567 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); 1568 if (!delayed_node) 1569 return false; 1570 1571 /* 1572 * We can only do one readdir with delayed items at a time because of 1573 * item->readdir_list. 1574 */ 1575 inode_unlock_shared(inode); 1576 inode_lock(inode); 1577 1578 mutex_lock(&delayed_node->mutex); 1579 item = __btrfs_first_delayed_insertion_item(delayed_node); 1580 while (item) { 1581 refcount_inc(&item->refs); 1582 list_add_tail(&item->readdir_list, ins_list); 1583 item = __btrfs_next_delayed_item(item); 1584 } 1585 1586 item = __btrfs_first_delayed_deletion_item(delayed_node); 1587 while (item) { 1588 refcount_inc(&item->refs); 1589 list_add_tail(&item->readdir_list, del_list); 1590 item = __btrfs_next_delayed_item(item); 1591 } 1592 mutex_unlock(&delayed_node->mutex); 1593 /* 1594 * This delayed node is still cached in the btrfs inode, so refs 1595 * must be > 1 now, and we needn't check it is going to be freed 1596 * or not. 1597 * 1598 * Besides that, this function is used to read dir, we do not 1599 * insert/delete delayed items in this period. So we also needn't 1600 * requeue or dequeue this delayed node. 1601 */ 1602 refcount_dec(&delayed_node->refs); 1603 1604 return true; 1605 } 1606 1607 void btrfs_readdir_put_delayed_items(struct inode *inode, 1608 struct list_head *ins_list, 1609 struct list_head *del_list) 1610 { 1611 struct btrfs_delayed_item *curr, *next; 1612 1613 list_for_each_entry_safe(curr, next, ins_list, readdir_list) { 1614 list_del(&curr->readdir_list); 1615 if (refcount_dec_and_test(&curr->refs)) 1616 kfree(curr); 1617 } 1618 1619 list_for_each_entry_safe(curr, next, del_list, readdir_list) { 1620 list_del(&curr->readdir_list); 1621 if (refcount_dec_and_test(&curr->refs)) 1622 kfree(curr); 1623 } 1624 1625 /* 1626 * The VFS is going to do up_read(), so we need to downgrade back to a 1627 * read lock. 1628 */ 1629 downgrade_write(&inode->i_rwsem); 1630 } 1631 1632 int btrfs_should_delete_dir_index(struct list_head *del_list, 1633 u64 index) 1634 { 1635 struct btrfs_delayed_item *curr; 1636 int ret = 0; 1637 1638 list_for_each_entry(curr, del_list, readdir_list) { 1639 if (curr->key.offset > index) 1640 break; 1641 if (curr->key.offset == index) { 1642 ret = 1; 1643 break; 1644 } 1645 } 1646 return ret; 1647 } 1648 1649 /* 1650 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree 1651 * 1652 */ 1653 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, 1654 struct list_head *ins_list) 1655 { 1656 struct btrfs_dir_item *di; 1657 struct btrfs_delayed_item *curr, *next; 1658 struct btrfs_key location; 1659 char *name; 1660 int name_len; 1661 int over = 0; 1662 unsigned char d_type; 1663 1664 if (list_empty(ins_list)) 1665 return 0; 1666 1667 /* 1668 * Changing the data of the delayed item is impossible. So 1669 * we needn't lock them. And we have held i_mutex of the 1670 * directory, nobody can delete any directory indexes now. 1671 */ 1672 list_for_each_entry_safe(curr, next, ins_list, readdir_list) { 1673 list_del(&curr->readdir_list); 1674 1675 if (curr->key.offset < ctx->pos) { 1676 if (refcount_dec_and_test(&curr->refs)) 1677 kfree(curr); 1678 continue; 1679 } 1680 1681 ctx->pos = curr->key.offset; 1682 1683 di = (struct btrfs_dir_item *)curr->data; 1684 name = (char *)(di + 1); 1685 name_len = btrfs_stack_dir_name_len(di); 1686 1687 d_type = btrfs_filetype_table[di->type]; 1688 btrfs_disk_key_to_cpu(&location, &di->location); 1689 1690 over = !dir_emit(ctx, name, name_len, 1691 location.objectid, d_type); 1692 1693 if (refcount_dec_and_test(&curr->refs)) 1694 kfree(curr); 1695 1696 if (over) 1697 return 1; 1698 ctx->pos++; 1699 } 1700 return 0; 1701 } 1702 1703 static void fill_stack_inode_item(struct btrfs_trans_handle *trans, 1704 struct btrfs_inode_item *inode_item, 1705 struct inode *inode) 1706 { 1707 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode)); 1708 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode)); 1709 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size); 1710 btrfs_set_stack_inode_mode(inode_item, inode->i_mode); 1711 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink); 1712 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode)); 1713 btrfs_set_stack_inode_generation(inode_item, 1714 BTRFS_I(inode)->generation); 1715 btrfs_set_stack_inode_sequence(inode_item, 1716 inode_peek_iversion(inode)); 1717 btrfs_set_stack_inode_transid(inode_item, trans->transid); 1718 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev); 1719 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags); 1720 btrfs_set_stack_inode_block_group(inode_item, 0); 1721 1722 btrfs_set_stack_timespec_sec(&inode_item->atime, 1723 inode->i_atime.tv_sec); 1724 btrfs_set_stack_timespec_nsec(&inode_item->atime, 1725 inode->i_atime.tv_nsec); 1726 1727 btrfs_set_stack_timespec_sec(&inode_item->mtime, 1728 inode->i_mtime.tv_sec); 1729 btrfs_set_stack_timespec_nsec(&inode_item->mtime, 1730 inode->i_mtime.tv_nsec); 1731 1732 btrfs_set_stack_timespec_sec(&inode_item->ctime, 1733 inode->i_ctime.tv_sec); 1734 btrfs_set_stack_timespec_nsec(&inode_item->ctime, 1735 inode->i_ctime.tv_nsec); 1736 1737 btrfs_set_stack_timespec_sec(&inode_item->otime, 1738 BTRFS_I(inode)->i_otime.tv_sec); 1739 btrfs_set_stack_timespec_nsec(&inode_item->otime, 1740 BTRFS_I(inode)->i_otime.tv_nsec); 1741 } 1742 1743 int btrfs_fill_inode(struct inode *inode, u32 *rdev) 1744 { 1745 struct btrfs_delayed_node *delayed_node; 1746 struct btrfs_inode_item *inode_item; 1747 1748 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); 1749 if (!delayed_node) 1750 return -ENOENT; 1751 1752 mutex_lock(&delayed_node->mutex); 1753 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 1754 mutex_unlock(&delayed_node->mutex); 1755 btrfs_release_delayed_node(delayed_node); 1756 return -ENOENT; 1757 } 1758 1759 inode_item = &delayed_node->inode_item; 1760 1761 i_uid_write(inode, btrfs_stack_inode_uid(inode_item)); 1762 i_gid_write(inode, btrfs_stack_inode_gid(inode_item)); 1763 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item)); 1764 inode->i_mode = btrfs_stack_inode_mode(inode_item); 1765 set_nlink(inode, btrfs_stack_inode_nlink(inode_item)); 1766 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item)); 1767 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item); 1768 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item); 1769 1770 inode_set_iversion_queried(inode, 1771 btrfs_stack_inode_sequence(inode_item)); 1772 inode->i_rdev = 0; 1773 *rdev = btrfs_stack_inode_rdev(inode_item); 1774 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item); 1775 1776 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime); 1777 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime); 1778 1779 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime); 1780 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime); 1781 1782 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime); 1783 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime); 1784 1785 BTRFS_I(inode)->i_otime.tv_sec = 1786 btrfs_stack_timespec_sec(&inode_item->otime); 1787 BTRFS_I(inode)->i_otime.tv_nsec = 1788 btrfs_stack_timespec_nsec(&inode_item->otime); 1789 1790 inode->i_generation = BTRFS_I(inode)->generation; 1791 BTRFS_I(inode)->index_cnt = (u64)-1; 1792 1793 mutex_unlock(&delayed_node->mutex); 1794 btrfs_release_delayed_node(delayed_node); 1795 return 0; 1796 } 1797 1798 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans, 1799 struct btrfs_root *root, struct inode *inode) 1800 { 1801 struct btrfs_delayed_node *delayed_node; 1802 int ret = 0; 1803 1804 delayed_node = btrfs_get_or_create_delayed_node(BTRFS_I(inode)); 1805 if (IS_ERR(delayed_node)) 1806 return PTR_ERR(delayed_node); 1807 1808 mutex_lock(&delayed_node->mutex); 1809 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 1810 fill_stack_inode_item(trans, &delayed_node->inode_item, inode); 1811 goto release_node; 1812 } 1813 1814 ret = btrfs_delayed_inode_reserve_metadata(trans, root, BTRFS_I(inode), 1815 delayed_node); 1816 if (ret) 1817 goto release_node; 1818 1819 fill_stack_inode_item(trans, &delayed_node->inode_item, inode); 1820 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); 1821 delayed_node->count++; 1822 atomic_inc(&root->fs_info->delayed_root->items); 1823 release_node: 1824 mutex_unlock(&delayed_node->mutex); 1825 btrfs_release_delayed_node(delayed_node); 1826 return ret; 1827 } 1828 1829 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode) 1830 { 1831 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 1832 struct btrfs_delayed_node *delayed_node; 1833 1834 /* 1835 * we don't do delayed inode updates during log recovery because it 1836 * leads to enospc problems. This means we also can't do 1837 * delayed inode refs 1838 */ 1839 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 1840 return -EAGAIN; 1841 1842 delayed_node = btrfs_get_or_create_delayed_node(inode); 1843 if (IS_ERR(delayed_node)) 1844 return PTR_ERR(delayed_node); 1845 1846 /* 1847 * We don't reserve space for inode ref deletion is because: 1848 * - We ONLY do async inode ref deletion for the inode who has only 1849 * one link(i_nlink == 1), it means there is only one inode ref. 1850 * And in most case, the inode ref and the inode item are in the 1851 * same leaf, and we will deal with them at the same time. 1852 * Since we are sure we will reserve the space for the inode item, 1853 * it is unnecessary to reserve space for inode ref deletion. 1854 * - If the inode ref and the inode item are not in the same leaf, 1855 * We also needn't worry about enospc problem, because we reserve 1856 * much more space for the inode update than it needs. 1857 * - At the worst, we can steal some space from the global reservation. 1858 * It is very rare. 1859 */ 1860 mutex_lock(&delayed_node->mutex); 1861 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) 1862 goto release_node; 1863 1864 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); 1865 delayed_node->count++; 1866 atomic_inc(&fs_info->delayed_root->items); 1867 release_node: 1868 mutex_unlock(&delayed_node->mutex); 1869 btrfs_release_delayed_node(delayed_node); 1870 return 0; 1871 } 1872 1873 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node) 1874 { 1875 struct btrfs_root *root = delayed_node->root; 1876 struct btrfs_fs_info *fs_info = root->fs_info; 1877 struct btrfs_delayed_item *curr_item, *prev_item; 1878 1879 mutex_lock(&delayed_node->mutex); 1880 curr_item = __btrfs_first_delayed_insertion_item(delayed_node); 1881 while (curr_item) { 1882 btrfs_delayed_item_release_metadata(root, curr_item); 1883 prev_item = curr_item; 1884 curr_item = __btrfs_next_delayed_item(prev_item); 1885 btrfs_release_delayed_item(prev_item); 1886 } 1887 1888 curr_item = __btrfs_first_delayed_deletion_item(delayed_node); 1889 while (curr_item) { 1890 btrfs_delayed_item_release_metadata(root, curr_item); 1891 prev_item = curr_item; 1892 curr_item = __btrfs_next_delayed_item(prev_item); 1893 btrfs_release_delayed_item(prev_item); 1894 } 1895 1896 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) 1897 btrfs_release_delayed_iref(delayed_node); 1898 1899 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 1900 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false); 1901 btrfs_release_delayed_inode(delayed_node); 1902 } 1903 mutex_unlock(&delayed_node->mutex); 1904 } 1905 1906 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode) 1907 { 1908 struct btrfs_delayed_node *delayed_node; 1909 1910 delayed_node = btrfs_get_delayed_node(inode); 1911 if (!delayed_node) 1912 return; 1913 1914 __btrfs_kill_delayed_node(delayed_node); 1915 btrfs_release_delayed_node(delayed_node); 1916 } 1917 1918 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root) 1919 { 1920 u64 inode_id = 0; 1921 struct btrfs_delayed_node *delayed_nodes[8]; 1922 int i, n; 1923 1924 while (1) { 1925 spin_lock(&root->inode_lock); 1926 n = radix_tree_gang_lookup(&root->delayed_nodes_tree, 1927 (void **)delayed_nodes, inode_id, 1928 ARRAY_SIZE(delayed_nodes)); 1929 if (!n) { 1930 spin_unlock(&root->inode_lock); 1931 break; 1932 } 1933 1934 inode_id = delayed_nodes[n - 1]->inode_id + 1; 1935 1936 for (i = 0; i < n; i++) 1937 refcount_inc(&delayed_nodes[i]->refs); 1938 spin_unlock(&root->inode_lock); 1939 1940 for (i = 0; i < n; i++) { 1941 __btrfs_kill_delayed_node(delayed_nodes[i]); 1942 btrfs_release_delayed_node(delayed_nodes[i]); 1943 } 1944 } 1945 } 1946 1947 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info) 1948 { 1949 struct btrfs_delayed_node *curr_node, *prev_node; 1950 1951 curr_node = btrfs_first_delayed_node(fs_info->delayed_root); 1952 while (curr_node) { 1953 __btrfs_kill_delayed_node(curr_node); 1954 1955 prev_node = curr_node; 1956 curr_node = btrfs_next_delayed_node(curr_node); 1957 btrfs_release_delayed_node(prev_node); 1958 } 1959 } 1960 1961