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