1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #include "messages.h" 7 #include "ctree.h" 8 #include "inode-item.h" 9 #include "disk-io.h" 10 #include "transaction.h" 11 #include "print-tree.h" 12 #include "space-info.h" 13 14 struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf, 15 int slot, const char *name, 16 int name_len) 17 { 18 struct btrfs_inode_ref *ref; 19 unsigned long ptr; 20 unsigned long name_ptr; 21 u32 item_size; 22 u32 cur_offset = 0; 23 int len; 24 25 item_size = btrfs_item_size(leaf, slot); 26 ptr = btrfs_item_ptr_offset(leaf, slot); 27 while (cur_offset < item_size) { 28 ref = (struct btrfs_inode_ref *)(ptr + cur_offset); 29 len = btrfs_inode_ref_name_len(leaf, ref); 30 name_ptr = (unsigned long)(ref + 1); 31 cur_offset += len + sizeof(*ref); 32 if (len != name_len) 33 continue; 34 if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) 35 return ref; 36 } 37 return NULL; 38 } 39 40 struct btrfs_inode_extref *btrfs_find_name_in_ext_backref( 41 struct extent_buffer *leaf, int slot, u64 ref_objectid, 42 const char *name, int name_len) 43 { 44 struct btrfs_inode_extref *extref; 45 unsigned long ptr; 46 unsigned long name_ptr; 47 u32 item_size; 48 u32 cur_offset = 0; 49 int ref_name_len; 50 51 item_size = btrfs_item_size(leaf, slot); 52 ptr = btrfs_item_ptr_offset(leaf, slot); 53 54 /* 55 * Search all extended backrefs in this item. We're only 56 * looking through any collisions so most of the time this is 57 * just going to compare against one buffer. If all is well, 58 * we'll return success and the inode ref object. 59 */ 60 while (cur_offset < item_size) { 61 extref = (struct btrfs_inode_extref *) (ptr + cur_offset); 62 name_ptr = (unsigned long)(&extref->name); 63 ref_name_len = btrfs_inode_extref_name_len(leaf, extref); 64 65 if (ref_name_len == name_len && 66 btrfs_inode_extref_parent(leaf, extref) == ref_objectid && 67 (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)) 68 return extref; 69 70 cur_offset += ref_name_len + sizeof(*extref); 71 } 72 return NULL; 73 } 74 75 /* Returns NULL if no extref found */ 76 struct btrfs_inode_extref * 77 btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans, 78 struct btrfs_root *root, 79 struct btrfs_path *path, 80 const char *name, int name_len, 81 u64 inode_objectid, u64 ref_objectid, int ins_len, 82 int cow) 83 { 84 int ret; 85 struct btrfs_key key; 86 87 key.objectid = inode_objectid; 88 key.type = BTRFS_INODE_EXTREF_KEY; 89 key.offset = btrfs_extref_hash(ref_objectid, name, name_len); 90 91 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); 92 if (ret < 0) 93 return ERR_PTR(ret); 94 if (ret > 0) 95 return NULL; 96 return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], 97 ref_objectid, name, name_len); 98 99 } 100 101 static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, 102 struct btrfs_root *root, 103 const char *name, int name_len, 104 u64 inode_objectid, u64 ref_objectid, 105 u64 *index) 106 { 107 struct btrfs_path *path; 108 struct btrfs_key key; 109 struct btrfs_inode_extref *extref; 110 struct extent_buffer *leaf; 111 int ret; 112 int del_len = name_len + sizeof(*extref); 113 unsigned long ptr; 114 unsigned long item_start; 115 u32 item_size; 116 117 key.objectid = inode_objectid; 118 key.type = BTRFS_INODE_EXTREF_KEY; 119 key.offset = btrfs_extref_hash(ref_objectid, name, name_len); 120 121 path = btrfs_alloc_path(); 122 if (!path) 123 return -ENOMEM; 124 125 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 126 if (ret > 0) 127 ret = -ENOENT; 128 if (ret < 0) 129 goto out; 130 131 /* 132 * Sanity check - did we find the right item for this name? 133 * This should always succeed so error here will make the FS 134 * readonly. 135 */ 136 extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], 137 ref_objectid, name, name_len); 138 if (!extref) { 139 btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL); 140 ret = -EROFS; 141 goto out; 142 } 143 144 leaf = path->nodes[0]; 145 item_size = btrfs_item_size(leaf, path->slots[0]); 146 if (index) 147 *index = btrfs_inode_extref_index(leaf, extref); 148 149 if (del_len == item_size) { 150 /* 151 * Common case only one ref in the item, remove the 152 * whole item. 153 */ 154 ret = btrfs_del_item(trans, root, path); 155 goto out; 156 } 157 158 ptr = (unsigned long)extref; 159 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); 160 161 memmove_extent_buffer(leaf, ptr, ptr + del_len, 162 item_size - (ptr + del_len - item_start)); 163 164 btrfs_truncate_item(path, item_size - del_len, 1); 165 166 out: 167 btrfs_free_path(path); 168 169 return ret; 170 } 171 172 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, 173 struct btrfs_root *root, 174 const char *name, int name_len, 175 u64 inode_objectid, u64 ref_objectid, u64 *index) 176 { 177 struct btrfs_path *path; 178 struct btrfs_key key; 179 struct btrfs_inode_ref *ref; 180 struct extent_buffer *leaf; 181 unsigned long ptr; 182 unsigned long item_start; 183 u32 item_size; 184 u32 sub_item_len; 185 int ret; 186 int search_ext_refs = 0; 187 int del_len = name_len + sizeof(*ref); 188 189 key.objectid = inode_objectid; 190 key.offset = ref_objectid; 191 key.type = BTRFS_INODE_REF_KEY; 192 193 path = btrfs_alloc_path(); 194 if (!path) 195 return -ENOMEM; 196 197 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 198 if (ret > 0) { 199 ret = -ENOENT; 200 search_ext_refs = 1; 201 goto out; 202 } else if (ret < 0) { 203 goto out; 204 } 205 206 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name, 207 name_len); 208 if (!ref) { 209 ret = -ENOENT; 210 search_ext_refs = 1; 211 goto out; 212 } 213 leaf = path->nodes[0]; 214 item_size = btrfs_item_size(leaf, path->slots[0]); 215 216 if (index) 217 *index = btrfs_inode_ref_index(leaf, ref); 218 219 if (del_len == item_size) { 220 ret = btrfs_del_item(trans, root, path); 221 goto out; 222 } 223 ptr = (unsigned long)ref; 224 sub_item_len = name_len + sizeof(*ref); 225 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); 226 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, 227 item_size - (ptr + sub_item_len - item_start)); 228 btrfs_truncate_item(path, item_size - sub_item_len, 1); 229 out: 230 btrfs_free_path(path); 231 232 if (search_ext_refs) { 233 /* 234 * No refs were found, or we could not find the 235 * name in our ref array. Find and remove the extended 236 * inode ref then. 237 */ 238 return btrfs_del_inode_extref(trans, root, name, name_len, 239 inode_objectid, ref_objectid, index); 240 } 241 242 return ret; 243 } 244 245 /* 246 * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree. 247 * 248 * The caller must have checked against BTRFS_LINK_MAX already. 249 */ 250 static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, 251 struct btrfs_root *root, 252 const char *name, int name_len, 253 u64 inode_objectid, u64 ref_objectid, u64 index) 254 { 255 struct btrfs_inode_extref *extref; 256 int ret; 257 int ins_len = name_len + sizeof(*extref); 258 unsigned long ptr; 259 struct btrfs_path *path; 260 struct btrfs_key key; 261 struct extent_buffer *leaf; 262 263 key.objectid = inode_objectid; 264 key.type = BTRFS_INODE_EXTREF_KEY; 265 key.offset = btrfs_extref_hash(ref_objectid, name, name_len); 266 267 path = btrfs_alloc_path(); 268 if (!path) 269 return -ENOMEM; 270 271 ret = btrfs_insert_empty_item(trans, root, path, &key, 272 ins_len); 273 if (ret == -EEXIST) { 274 if (btrfs_find_name_in_ext_backref(path->nodes[0], 275 path->slots[0], 276 ref_objectid, 277 name, name_len)) 278 goto out; 279 280 btrfs_extend_item(path, ins_len); 281 ret = 0; 282 } 283 if (ret < 0) 284 goto out; 285 286 leaf = path->nodes[0]; 287 ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char); 288 ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len; 289 extref = (struct btrfs_inode_extref *)ptr; 290 291 btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len); 292 btrfs_set_inode_extref_index(path->nodes[0], extref, index); 293 btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid); 294 295 ptr = (unsigned long)&extref->name; 296 write_extent_buffer(path->nodes[0], name, ptr, name_len); 297 btrfs_mark_buffer_dirty(path->nodes[0]); 298 299 out: 300 btrfs_free_path(path); 301 return ret; 302 } 303 304 /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */ 305 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, 306 struct btrfs_root *root, 307 const char *name, int name_len, 308 u64 inode_objectid, u64 ref_objectid, u64 index) 309 { 310 struct btrfs_fs_info *fs_info = root->fs_info; 311 struct btrfs_path *path; 312 struct btrfs_key key; 313 struct btrfs_inode_ref *ref; 314 unsigned long ptr; 315 int ret; 316 int ins_len = name_len + sizeof(*ref); 317 318 key.objectid = inode_objectid; 319 key.offset = ref_objectid; 320 key.type = BTRFS_INODE_REF_KEY; 321 322 path = btrfs_alloc_path(); 323 if (!path) 324 return -ENOMEM; 325 326 path->skip_release_on_error = 1; 327 ret = btrfs_insert_empty_item(trans, root, path, &key, 328 ins_len); 329 if (ret == -EEXIST) { 330 u32 old_size; 331 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], 332 name, name_len); 333 if (ref) 334 goto out; 335 336 old_size = btrfs_item_size(path->nodes[0], path->slots[0]); 337 btrfs_extend_item(path, ins_len); 338 ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 339 struct btrfs_inode_ref); 340 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size); 341 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); 342 btrfs_set_inode_ref_index(path->nodes[0], ref, index); 343 ptr = (unsigned long)(ref + 1); 344 ret = 0; 345 } else if (ret < 0) { 346 if (ret == -EOVERFLOW) { 347 if (btrfs_find_name_in_backref(path->nodes[0], 348 path->slots[0], 349 name, name_len)) 350 ret = -EEXIST; 351 else 352 ret = -EMLINK; 353 } 354 goto out; 355 } else { 356 ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 357 struct btrfs_inode_ref); 358 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); 359 btrfs_set_inode_ref_index(path->nodes[0], ref, index); 360 ptr = (unsigned long)(ref + 1); 361 } 362 write_extent_buffer(path->nodes[0], name, ptr, name_len); 363 btrfs_mark_buffer_dirty(path->nodes[0]); 364 365 out: 366 btrfs_free_path(path); 367 368 if (ret == -EMLINK) { 369 struct btrfs_super_block *disk_super = fs_info->super_copy; 370 /* We ran out of space in the ref array. Need to 371 * add an extended ref. */ 372 if (btrfs_super_incompat_flags(disk_super) 373 & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF) 374 ret = btrfs_insert_inode_extref(trans, root, name, 375 name_len, 376 inode_objectid, 377 ref_objectid, index); 378 } 379 380 return ret; 381 } 382 383 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, 384 struct btrfs_root *root, 385 struct btrfs_path *path, u64 objectid) 386 { 387 struct btrfs_key key; 388 int ret; 389 key.objectid = objectid; 390 key.type = BTRFS_INODE_ITEM_KEY; 391 key.offset = 0; 392 393 ret = btrfs_insert_empty_item(trans, root, path, &key, 394 sizeof(struct btrfs_inode_item)); 395 return ret; 396 } 397 398 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root 399 *root, struct btrfs_path *path, 400 struct btrfs_key *location, int mod) 401 { 402 int ins_len = mod < 0 ? -1 : 0; 403 int cow = mod != 0; 404 int ret; 405 int slot; 406 struct extent_buffer *leaf; 407 struct btrfs_key found_key; 408 409 ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); 410 if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY && 411 location->offset == (u64)-1 && path->slots[0] != 0) { 412 slot = path->slots[0] - 1; 413 leaf = path->nodes[0]; 414 btrfs_item_key_to_cpu(leaf, &found_key, slot); 415 if (found_key.objectid == location->objectid && 416 found_key.type == location->type) { 417 path->slots[0]--; 418 return 0; 419 } 420 } 421 return ret; 422 } 423 424 static inline void btrfs_trace_truncate(struct btrfs_inode *inode, 425 struct extent_buffer *leaf, 426 struct btrfs_file_extent_item *fi, 427 u64 offset, int extent_type, int slot) 428 { 429 if (!inode) 430 return; 431 if (extent_type == BTRFS_FILE_EXTENT_INLINE) 432 trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot, 433 offset); 434 else 435 trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset); 436 } 437 438 /* 439 * Remove inode items from a given root. 440 * 441 * @trans: A transaction handle. 442 * @root: The root from which to remove items. 443 * @inode: The inode whose items we want to remove. 444 * @control: The btrfs_truncate_control to control how and what we 445 * are truncating. 446 * 447 * Remove all keys associated with the inode from the given root that have a key 448 * with a type greater than or equals to @min_type. When @min_type has a value of 449 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value 450 * greater than or equals to @new_size. If a file extent item that starts before 451 * @new_size and ends after it is found, its length is adjusted. 452 * 453 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is 454 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block. 455 */ 456 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, 457 struct btrfs_root *root, 458 struct btrfs_truncate_control *control) 459 { 460 struct btrfs_fs_info *fs_info = root->fs_info; 461 struct btrfs_path *path; 462 struct extent_buffer *leaf; 463 struct btrfs_file_extent_item *fi; 464 struct btrfs_key key; 465 struct btrfs_key found_key; 466 u64 new_size = control->new_size; 467 u64 extent_num_bytes = 0; 468 u64 extent_offset = 0; 469 u64 item_end = 0; 470 u32 found_type = (u8)-1; 471 int del_item; 472 int pending_del_nr = 0; 473 int pending_del_slot = 0; 474 int extent_type = -1; 475 int ret; 476 u64 bytes_deleted = 0; 477 bool be_nice = false; 478 479 ASSERT(control->inode || !control->clear_extent_range); 480 ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY); 481 482 control->last_size = new_size; 483 control->sub_bytes = 0; 484 485 /* 486 * For shareable roots we want to back off from time to time, this turns 487 * out to be subvolume roots, reloc roots, and data reloc roots. 488 */ 489 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 490 be_nice = true; 491 492 path = btrfs_alloc_path(); 493 if (!path) 494 return -ENOMEM; 495 path->reada = READA_BACK; 496 497 key.objectid = control->ino; 498 key.offset = (u64)-1; 499 key.type = (u8)-1; 500 501 search_again: 502 /* 503 * With a 16K leaf size and 128MiB extents, you can actually queue up a 504 * huge file in a single leaf. Most of the time that bytes_deleted is 505 * > 0, it will be huge by the time we get here 506 */ 507 if (be_nice && bytes_deleted > SZ_32M && 508 btrfs_should_end_transaction(trans)) { 509 ret = -EAGAIN; 510 goto out; 511 } 512 513 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 514 if (ret < 0) 515 goto out; 516 517 if (ret > 0) { 518 ret = 0; 519 /* There are no items in the tree for us to truncate, we're done */ 520 if (path->slots[0] == 0) 521 goto out; 522 path->slots[0]--; 523 } 524 525 while (1) { 526 u64 clear_start = 0, clear_len = 0, extent_start = 0; 527 bool should_throttle = false; 528 529 fi = NULL; 530 leaf = path->nodes[0]; 531 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 532 found_type = found_key.type; 533 534 if (found_key.objectid != control->ino) 535 break; 536 537 if (found_type < control->min_type) 538 break; 539 540 item_end = found_key.offset; 541 if (found_type == BTRFS_EXTENT_DATA_KEY) { 542 fi = btrfs_item_ptr(leaf, path->slots[0], 543 struct btrfs_file_extent_item); 544 extent_type = btrfs_file_extent_type(leaf, fi); 545 if (extent_type != BTRFS_FILE_EXTENT_INLINE) 546 item_end += 547 btrfs_file_extent_num_bytes(leaf, fi); 548 else if (extent_type == BTRFS_FILE_EXTENT_INLINE) 549 item_end += btrfs_file_extent_ram_bytes(leaf, fi); 550 551 btrfs_trace_truncate(control->inode, leaf, fi, 552 found_key.offset, extent_type, 553 path->slots[0]); 554 item_end--; 555 } 556 if (found_type > control->min_type) { 557 del_item = 1; 558 } else { 559 if (item_end < new_size) 560 break; 561 if (found_key.offset >= new_size) 562 del_item = 1; 563 else 564 del_item = 0; 565 } 566 567 /* FIXME, shrink the extent if the ref count is only 1 */ 568 if (found_type != BTRFS_EXTENT_DATA_KEY) 569 goto delete; 570 571 control->extents_found++; 572 573 if (extent_type != BTRFS_FILE_EXTENT_INLINE) { 574 u64 num_dec; 575 576 clear_start = found_key.offset; 577 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); 578 if (!del_item) { 579 u64 orig_num_bytes = 580 btrfs_file_extent_num_bytes(leaf, fi); 581 extent_num_bytes = ALIGN(new_size - 582 found_key.offset, 583 fs_info->sectorsize); 584 clear_start = ALIGN(new_size, fs_info->sectorsize); 585 586 btrfs_set_file_extent_num_bytes(leaf, fi, 587 extent_num_bytes); 588 num_dec = (orig_num_bytes - extent_num_bytes); 589 if (extent_start != 0) 590 control->sub_bytes += num_dec; 591 btrfs_mark_buffer_dirty(leaf); 592 } else { 593 extent_num_bytes = 594 btrfs_file_extent_disk_num_bytes(leaf, fi); 595 extent_offset = found_key.offset - 596 btrfs_file_extent_offset(leaf, fi); 597 598 /* FIXME blocksize != 4096 */ 599 num_dec = btrfs_file_extent_num_bytes(leaf, fi); 600 if (extent_start != 0) 601 control->sub_bytes += num_dec; 602 } 603 clear_len = num_dec; 604 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 605 /* 606 * We can't truncate inline items that have had 607 * special encodings 608 */ 609 if (!del_item && 610 btrfs_file_extent_encryption(leaf, fi) == 0 && 611 btrfs_file_extent_other_encoding(leaf, fi) == 0 && 612 btrfs_file_extent_compression(leaf, fi) == 0) { 613 u32 size = (u32)(new_size - found_key.offset); 614 615 btrfs_set_file_extent_ram_bytes(leaf, fi, size); 616 size = btrfs_file_extent_calc_inline_size(size); 617 btrfs_truncate_item(path, size, 1); 618 } else if (!del_item) { 619 /* 620 * We have to bail so the last_size is set to 621 * just before this extent. 622 */ 623 ret = BTRFS_NEED_TRUNCATE_BLOCK; 624 break; 625 } else { 626 /* 627 * Inline extents are special, we just treat 628 * them as a full sector worth in the file 629 * extent tree just for simplicity sake. 630 */ 631 clear_len = fs_info->sectorsize; 632 } 633 634 control->sub_bytes += item_end + 1 - new_size; 635 } 636 delete: 637 /* 638 * We only want to clear the file extent range if we're 639 * modifying the actual inode's mapping, which is just the 640 * normal truncate path. 641 */ 642 if (control->clear_extent_range) { 643 ret = btrfs_inode_clear_file_extent_range(control->inode, 644 clear_start, clear_len); 645 if (ret) { 646 btrfs_abort_transaction(trans, ret); 647 break; 648 } 649 } 650 651 if (del_item) { 652 ASSERT(!pending_del_nr || 653 ((path->slots[0] + 1) == pending_del_slot)); 654 655 control->last_size = found_key.offset; 656 if (!pending_del_nr) { 657 /* No pending yet, add ourselves */ 658 pending_del_slot = path->slots[0]; 659 pending_del_nr = 1; 660 } else if (pending_del_nr && 661 path->slots[0] + 1 == pending_del_slot) { 662 /* Hop on the pending chunk */ 663 pending_del_nr++; 664 pending_del_slot = path->slots[0]; 665 } 666 } else { 667 control->last_size = new_size; 668 break; 669 } 670 671 if (del_item && extent_start != 0 && !control->skip_ref_updates) { 672 struct btrfs_ref ref = { 0 }; 673 674 bytes_deleted += extent_num_bytes; 675 676 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, 677 extent_start, extent_num_bytes, 0); 678 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), 679 control->ino, extent_offset, 680 root->root_key.objectid, false); 681 ret = btrfs_free_extent(trans, &ref); 682 if (ret) { 683 btrfs_abort_transaction(trans, ret); 684 break; 685 } 686 if (be_nice) { 687 if (btrfs_should_throttle_delayed_refs(trans)) 688 should_throttle = true; 689 } 690 } 691 692 if (found_type == BTRFS_INODE_ITEM_KEY) 693 break; 694 695 if (path->slots[0] == 0 || 696 path->slots[0] != pending_del_slot || 697 should_throttle) { 698 if (pending_del_nr) { 699 ret = btrfs_del_items(trans, root, path, 700 pending_del_slot, 701 pending_del_nr); 702 if (ret) { 703 btrfs_abort_transaction(trans, ret); 704 break; 705 } 706 pending_del_nr = 0; 707 } 708 btrfs_release_path(path); 709 710 /* 711 * We can generate a lot of delayed refs, so we need to 712 * throttle every once and a while and make sure we're 713 * adding enough space to keep up with the work we are 714 * generating. Since we hold a transaction here we 715 * can't flush, and we don't want to FLUSH_LIMIT because 716 * we could have generated too many delayed refs to 717 * actually allocate, so just bail if we're short and 718 * let the normal reservation dance happen higher up. 719 */ 720 if (should_throttle) { 721 ret = btrfs_delayed_refs_rsv_refill(fs_info, 722 BTRFS_RESERVE_NO_FLUSH); 723 if (ret) { 724 ret = -EAGAIN; 725 break; 726 } 727 } 728 goto search_again; 729 } else { 730 path->slots[0]--; 731 } 732 } 733 out: 734 if (ret >= 0 && pending_del_nr) { 735 int err; 736 737 err = btrfs_del_items(trans, root, path, pending_del_slot, 738 pending_del_nr); 739 if (err) { 740 btrfs_abort_transaction(trans, err); 741 ret = err; 742 } 743 } 744 745 ASSERT(control->last_size >= new_size); 746 if (!ret && control->last_size > new_size) 747 control->last_size = new_size; 748 749 btrfs_free_path(path); 750 return ret; 751 } 752