Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / fs / btrfs / inode-item.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
1e1d2701 6#include "ctree.h"
ec8eb376
JB
7#include "fs.h"
8#include "messages.h"
26c2c454 9#include "inode-item.h"
1e1d2701 10#include "disk-io.h"
e089f05c 11#include "transaction.h"
f1e5c618 12#include "space-info.h"
07e81dc9 13#include "accessors.h"
a0231804 14#include "extent-tree.h"
7c8ede16 15#include "file-item.h"
1e1d2701 16
ca283ea9 17struct btrfs_inode_ref *btrfs_find_name_in_backref(const struct extent_buffer *leaf,
e43eec81 18 int slot,
6db75318 19 const struct fscrypt_str *name)
3954401f 20{
3954401f
CM
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
3212fa14 28 item_size = btrfs_item_size(leaf, slot);
1f250e92 29 ptr = btrfs_item_ptr_offset(leaf, slot);
3954401f
CM
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);
e43eec81 35 if (len != name->len)
3954401f 36 continue;
e43eec81
STD
37 if (memcmp_extent_buffer(leaf, name->name, name_ptr,
38 name->len) == 0)
9bb8407f 39 return ref;
3954401f 40 }
9bb8407f 41 return NULL;
3954401f
CM
42}
43
6ff49c6a 44struct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
ca283ea9 45 const struct extent_buffer *leaf, int slot, u64 ref_objectid,
6db75318 46 const struct fscrypt_str *name)
f186373f 47{
f186373f
MF
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
3212fa14 55 item_size = btrfs_item_size(leaf, slot);
1f250e92 56 ptr = btrfs_item_ptr_offset(leaf, slot);
f186373f
MF
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
e43eec81 69 if (ref_name_len == name->len &&
f186373f 70 btrfs_inode_extref_parent(leaf, extref) == ref_objectid &&
e43eec81
STD
71 (memcmp_extent_buffer(leaf, name->name, name_ptr,
72 name->len) == 0))
6ff49c6a 73 return extref;
f186373f
MF
74
75 cur_offset += ref_name_len + sizeof(*extref);
76 }
6ff49c6a 77 return NULL;
f186373f
MF
78}
79
f186373f
MF
80/* Returns NULL if no extref found */
81struct btrfs_inode_extref *
82btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 struct btrfs_path *path,
6db75318 85 const struct fscrypt_str *name,
f186373f
MF
86 u64 inode_objectid, u64 ref_objectid, int ins_len,
87 int cow)
88{
89 int ret;
90 struct btrfs_key key;
f186373f
MF
91
92 key.objectid = inode_objectid;
93 key.type = BTRFS_INODE_EXTREF_KEY;
e43eec81 94 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
f186373f
MF
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;
6ff49c6a 101 return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
e43eec81 102 ref_objectid, name);
6ff49c6a 103
f186373f
MF
104}
105
48a3b636
ES
106static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
6db75318 108 const struct fscrypt_str *name,
48a3b636
ES
109 u64 inode_objectid, u64 ref_objectid,
110 u64 *index)
f186373f 111{
f6a359e3 112 BTRFS_PATH_AUTO_FREE(path);
f186373f
MF
113 struct btrfs_key key;
114 struct btrfs_inode_extref *extref;
115 struct extent_buffer *leaf;
116 int ret;
e43eec81 117 int del_len = name->len + sizeof(*extref);
f186373f
MF
118 unsigned long ptr;
119 unsigned long item_start;
120 u32 item_size;
121
122 key.objectid = inode_objectid;
962a298f 123 key.type = BTRFS_INODE_EXTREF_KEY;
e43eec81 124 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
f186373f
MF
125
126 path = btrfs_alloc_path();
127 if (!path)
128 return -ENOMEM;
129
f186373f
MF
130 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
131 if (ret > 0)
f6a359e3 132 return -ENOENT;
f186373f 133 if (ret < 0)
f6a359e3 134 return ret;
f186373f
MF
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 */
6ff49c6a 141 extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
e43eec81 142 ref_objectid, name);
6ff49c6a 143 if (!extref) {
f4f89477 144 btrfs_abort_transaction(trans, -ENOENT);
f6a359e3 145 return -ENOENT;
f186373f
MF
146 }
147
148 leaf = path->nodes[0];
3212fa14 149 item_size = btrfs_item_size(leaf, path->slots[0]);
f186373f
MF
150 if (index)
151 *index = btrfs_inode_extref_index(leaf, extref);
152
153 if (del_len == item_size) {
f6a359e3
DS
154 /* Common case only one ref in the item, remove the whole item. */
155 return btrfs_del_item(trans, root, path);
f186373f
MF
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
50564b65 164 btrfs_truncate_item(trans, path, item_size - del_len, 1);
f186373f 165
f186373f
MF
166 return ret;
167}
168
169int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
6db75318 170 struct btrfs_root *root, const struct fscrypt_str *name,
f186373f 171 u64 inode_objectid, u64 ref_objectid, u64 *index)
3954401f
CM
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;
f186373f 182 int search_ext_refs = 0;
e43eec81 183 int del_len = name->len + sizeof(*ref);
3954401f
CM
184
185 key.objectid = inode_objectid;
962a298f 186 key.type = BTRFS_INODE_REF_KEY;
dba6ae0b 187 key.offset = ref_objectid;
3954401f
CM
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;
f186373f 196 search_ext_refs = 1;
3954401f
CM
197 goto out;
198 } else if (ret < 0) {
199 goto out;
200 }
9bb8407f 201
e43eec81 202 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name);
9bb8407f 203 if (!ref) {
3954401f 204 ret = -ENOENT;
f186373f 205 search_ext_refs = 1;
3954401f
CM
206 goto out;
207 }
208 leaf = path->nodes[0];
3212fa14 209 item_size = btrfs_item_size(leaf, path->slots[0]);
aec7477b
JB
210
211 if (index)
212 *index = btrfs_inode_ref_index(leaf, ref);
213
3954401f
CM
214 if (del_len == item_size) {
215 ret = btrfs_del_item(trans, root, path);
216 goto out;
217 }
218 ptr = (unsigned long)ref;
e43eec81 219 sub_item_len = name->len + sizeof(*ref);
3954401f
CM
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));
50564b65 223 btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
f186373f
MF
224out:
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 */
e43eec81 233 return btrfs_del_inode_extref(trans, root, name,
f186373f
MF
234 inode_objectid, ref_objectid, index);
235 }
236
237 return ret;
238}
239
240/*
9580503b 241 * Insert an extended inode ref into a tree.
f186373f
MF
242 *
243 * The caller must have checked against BTRFS_LINK_MAX already.
244 */
245static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
246 struct btrfs_root *root,
6db75318 247 const struct fscrypt_str *name,
e43eec81
STD
248 u64 inode_objectid, u64 ref_objectid,
249 u64 index)
f186373f
MF
250{
251 struct btrfs_inode_extref *extref;
252 int ret;
e43eec81 253 int ins_len = name->len + sizeof(*extref);
f186373f 254 unsigned long ptr;
dcb5bccc 255 BTRFS_PATH_AUTO_FREE(path);
f186373f
MF
256 struct btrfs_key key;
257 struct extent_buffer *leaf;
f186373f
MF
258
259 key.objectid = inode_objectid;
260 key.type = BTRFS_INODE_EXTREF_KEY;
e43eec81 261 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
f186373f
MF
262
263 path = btrfs_alloc_path();
264 if (!path)
265 return -ENOMEM;
266
f186373f
MF
267 ret = btrfs_insert_empty_item(trans, root, path, &key,
268 ins_len);
269 if (ret == -EEXIST) {
1f250e92
FM
270 if (btrfs_find_name_in_ext_backref(path->nodes[0],
271 path->slots[0],
272 ref_objectid,
e43eec81 273 name))
dcb5bccc 274 return ret;
f186373f 275
50564b65 276 btrfs_extend_item(trans, path, ins_len);
f186373f
MF
277 ret = 0;
278 }
279 if (ret < 0)
dcb5bccc 280 return ret;
f186373f
MF
281
282 leaf = path->nodes[0];
f186373f 283 ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
3212fa14 284 ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len;
f186373f
MF
285 extref = (struct btrfs_inode_extref *)ptr;
286
e43eec81 287 btrfs_set_inode_extref_name_len(path->nodes[0], extref, name->len);
f186373f
MF
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;
e43eec81 292 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
dcb5bccc
DS
293
294 return 0;
3954401f
CM
295}
296
79787eaa 297/* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
3954401f 298int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
6db75318 299 struct btrfs_root *root, const struct fscrypt_str *name,
aec7477b 300 u64 inode_objectid, u64 ref_objectid, u64 index)
3954401f 301{
0b246afa 302 struct btrfs_fs_info *fs_info = root->fs_info;
3954401f
CM
303 struct btrfs_path *path;
304 struct btrfs_key key;
305 struct btrfs_inode_ref *ref;
306 unsigned long ptr;
307 int ret;
e43eec81 308 int ins_len = name->len + sizeof(*ref);
3954401f
CM
309
310 key.objectid = inode_objectid;
962a298f 311 key.type = BTRFS_INODE_REF_KEY;
dba6ae0b 312 key.offset = ref_objectid;
3954401f
CM
313
314 path = btrfs_alloc_path();
315 if (!path)
316 return -ENOMEM;
317
df8d116f 318 path->skip_release_on_error = 1;
3954401f
CM
319 ret = btrfs_insert_empty_item(trans, root, path, &key,
320 ins_len);
321 if (ret == -EEXIST) {
322 u32 old_size;
9bb8407f 323 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
e43eec81 324 name);
9bb8407f 325 if (ref)
3954401f
CM
326 goto out;
327
3212fa14 328 old_size = btrfs_item_size(path->nodes[0], path->slots[0]);
50564b65 329 btrfs_extend_item(trans, path, ins_len);
3954401f
CM
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);
e43eec81 333 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
aec7477b 334 btrfs_set_inode_ref_index(path->nodes[0], ref, index);
3954401f
CM
335 ptr = (unsigned long)(ref + 1);
336 ret = 0;
337 } else if (ret < 0) {
df8d116f 338 if (ret == -EOVERFLOW) {
1f250e92
FM
339 if (btrfs_find_name_in_backref(path->nodes[0],
340 path->slots[0],
e43eec81 341 name))
df8d116f
FM
342 ret = -EEXIST;
343 else
344 ret = -EMLINK;
345 }
3954401f
CM
346 goto out;
347 } else {
348 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
349 struct btrfs_inode_ref);
e43eec81 350 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
aec7477b 351 btrfs_set_inode_ref_index(path->nodes[0], ref, index);
3954401f
CM
352 ptr = (unsigned long)(ref + 1);
353 }
e43eec81 354 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
3954401f
CM
355out:
356 btrfs_free_path(path);
f186373f
MF
357
358 if (ret == -EMLINK) {
0b246afa 359 struct btrfs_super_block *disk_super = fs_info->super_copy;
f186373f
MF
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,
f186373f
MF
365 inode_objectid,
366 ref_objectid, index);
367 }
368
3954401f
CM
369 return ret;
370}
371
5f39d397
CM
372int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
373 struct btrfs_root *root,
374 struct btrfs_path *path, u64 objectid)
1e1d2701 375{
1e1d2701
CM
376 struct btrfs_key key;
377 int ret;
378 key.objectid = objectid;
962a298f 379 key.type = BTRFS_INODE_ITEM_KEY;
1e1d2701
CM
380 key.offset = 0;
381
5f39d397
CM
382 ret = btrfs_insert_empty_item(trans, root, path, &key,
383 sizeof(struct btrfs_inode_item));
1e1d2701
CM
384 return ret;
385}
386
e089f05c 387int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
d6e4a428
CM
388 *root, struct btrfs_path *path,
389 struct btrfs_key *location, int mod)
1e1d2701 390{
1e1d2701
CM
391 int ins_len = mod < 0 ? -1 : 0;
392 int cow = mod != 0;
d6e4a428
CM
393 int ret;
394 int slot;
5f39d397 395 struct extent_buffer *leaf;
d6e4a428 396 struct btrfs_key found_key;
1e1d2701 397
d6e4a428 398 ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
962a298f 399 if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
d6e4a428
CM
400 location->offset == (u64)-1 && path->slots[0] != 0) {
401 slot = path->slots[0] - 1;
5f39d397
CM
402 leaf = path->nodes[0];
403 btrfs_item_key_to_cpu(leaf, &found_key, slot);
d6e4a428 404 if (found_key.objectid == location->objectid &&
962a298f 405 found_key.type == location->type) {
d6e4a428
CM
406 path->slots[0]--;
407 return 0;
408 }
409 }
410 return ret;
1e1d2701 411}
54f03ab1 412
ca283ea9
DS
413static inline void btrfs_trace_truncate(const struct btrfs_inode *inode,
414 const struct extent_buffer *leaf,
415 const struct btrfs_file_extent_item *fi,
71d18b53
JB
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
54f03ab1
JB
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.
d9ac19c3
JB
433 * @control: The btrfs_truncate_control to control how and what we
434 * are truncating.
54f03ab1
JB
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 */
445int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
446 struct btrfs_root *root,
d9ac19c3 447 struct btrfs_truncate_control *control)
54f03ab1
JB
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;
d9ac19c3 455 u64 new_size = control->new_size;
54f03ab1
JB
456 u64 extent_num_bytes = 0;
457 u64 extent_offset = 0;
458 u64 item_end = 0;
54f03ab1 459 u32 found_type = (u8)-1;
54f03ab1
JB
460 int del_item;
461 int pending_del_nr = 0;
462 int pending_del_slot = 0;
463 int extent_type = -1;
464 int ret;
54f03ab1
JB
465 u64 bytes_deleted = 0;
466 bool be_nice = false;
54f03ab1 467
71d18b53 468 ASSERT(control->inode || !control->clear_extent_range);
56e1edb0 469 ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY);
54f03ab1 470
c2ddb612 471 control->last_size = new_size;
462b728e 472 control->sub_bytes = 0;
c2ddb612 473
54f03ab1 474 /*
275312a0
JB
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.
54f03ab1 477 */
275312a0 478 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
54f03ab1
JB
479 be_nice = true;
480
481 path = btrfs_alloc_path();
482 if (!path)
483 return -ENOMEM;
484 path->reada = READA_BACK;
485
487e81d2 486 key.objectid = control->ino;
54f03ab1 487 key.type = (u8)-1;
dba6ae0b 488 key.offset = (u64)-1;
54f03ab1
JB
489
490search_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) {
7097a941 515 u64 clear_start = 0, clear_len = 0, extent_start = 0;
a8fdc051 516 bool refill_delayed_refs_rsv = false;
54f03ab1
JB
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
487e81d2 523 if (found_key.objectid != control->ino)
54f03ab1
JB
524 break;
525
d9ac19c3 526 if (found_type < control->min_type)
54f03ab1
JB
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);
71d18b53 534 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
54f03ab1
JB
535 item_end +=
536 btrfs_file_extent_num_bytes(leaf, fi);
71d18b53 537 else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
54f03ab1
JB
538 item_end += btrfs_file_extent_ram_bytes(leaf, fi);
539
71d18b53
JB
540 btrfs_trace_truncate(control->inode, leaf, fi,
541 found_key.offset, extent_type,
542 path->slots[0]);
54f03ab1
JB
543 item_end--;
544 }
d9ac19c3 545 if (found_type > control->min_type) {
54f03ab1
JB
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 }
7097a941 555
54f03ab1
JB
556 /* FIXME, shrink the extent if the ref count is only 1 */
557 if (found_type != BTRFS_EXTENT_DATA_KEY)
558 goto delete;
559
d9ac19c3 560 control->extents_found++;
54f03ab1
JB
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);
462b728e
JB
578 if (extent_start != 0)
579 control->sub_bytes += num_dec;
54f03ab1
JB
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);
462b728e
JB
588 if (extent_start != 0)
589 control->sub_bytes += num_dec;
54f03ab1
JB
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);
50564b65 605 btrfs_truncate_item(trans, path, size, 1);
54f03ab1
JB
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
462b728e 622 control->sub_bytes += item_end + 1 - new_size;
54f03ab1
JB
623 }
624delete:
625 /*
655807b8
JB
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.
54f03ab1 629 */
655807b8 630 if (control->clear_extent_range) {
71d18b53 631 ret = btrfs_inode_clear_file_extent_range(control->inode,
54f03ab1
JB
632 clear_start, clear_len);
633 if (ret) {
634 btrfs_abort_transaction(trans, ret);
635 break;
636 }
637 }
638
54f03ab1 639 if (del_item) {
376b91d5
JB
640 ASSERT(!pending_del_nr ||
641 ((path->slots[0] + 1) == pending_del_slot));
642
0adbc619 643 control->last_size = found_key.offset;
54f03ab1
JB
644 if (!pending_del_nr) {
645 /* No pending yet, add ourselves */
646 pending_del_slot = path->slots[0];
647 pending_del_nr = 1;
4a6f5cca 648 } else if (path->slots[0] + 1 == pending_del_slot) {
54f03ab1
JB
649 /* Hop on the pending chunk */
650 pending_del_nr++;
651 pending_del_slot = path->slots[0];
54f03ab1
JB
652 }
653 } else {
0adbc619 654 control->last_size = new_size;
54f03ab1
JB
655 break;
656 }
54f03ab1 657
5caa490e 658 if (del_item && extent_start != 0 && !control->skip_ref_updates) {
4d09b4e9
JB
659 struct btrfs_ref ref = {
660 .action = BTRFS_DROP_DELAYED_REF,
661 .bytenr = extent_start,
12390e42 662 .num_bytes = extent_num_bytes,
e094f480 663 .owning_root = btrfs_root_id(root),
f2e69a77 664 .ref_root = btrfs_header_owner(leaf),
4d09b4e9 665 };
54f03ab1
JB
666
667 bytes_deleted += extent_num_bytes;
668
f2e69a77 669 btrfs_init_data_ref(&ref, control->ino, extent_offset,
e094f480 670 btrfs_root_id(root), false);
54f03ab1
JB
671 ret = btrfs_free_extent(trans, &ref);
672 if (ret) {
673 btrfs_abort_transaction(trans, ret);
674 break;
675 }
a8fdc051
FM
676 if (be_nice && btrfs_check_space_for_delayed_refs(fs_info))
677 refill_delayed_refs_rsv = true;
54f03ab1
JB
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 ||
a8fdc051 685 refill_delayed_refs_rsv) {
54f03ab1
JB
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 */
a8fdc051 708 if (refill_delayed_refs_rsv) {
54f03ab1
JB
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 }
721out:
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 }
c2ddb612
JB
732
733 ASSERT(control->last_size >= new_size);
734 if (!ret && control->last_size > new_size)
735 control->last_size = new_size;
54f03ab1
JB
736
737 btrfs_free_path(path);
738 return ret;
739}