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