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