1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
7 #include "inode-item.h"
9 #include "transaction.h"
10 #include "print-tree.h"
12 struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf,
13 int slot, const char *name,
16 struct btrfs_inode_ref *ref;
18 unsigned long name_ptr;
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);
32 if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
38 struct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
39 struct extent_buffer *leaf, int slot, u64 ref_objectid,
40 const char *name, int name_len)
42 struct btrfs_inode_extref *extref;
44 unsigned long name_ptr;
49 item_size = btrfs_item_size(leaf, slot);
50 ptr = btrfs_item_ptr_offset(leaf, slot);
53 * Search all extended backrefs in this item. We're only
54 * looking through any collisions so most of the time this is
55 * just going to compare against one buffer. If all is well,
56 * we'll return success and the inode ref object.
58 while (cur_offset < item_size) {
59 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
60 name_ptr = (unsigned long)(&extref->name);
61 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
63 if (ref_name_len == name_len &&
64 btrfs_inode_extref_parent(leaf, extref) == ref_objectid &&
65 (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0))
68 cur_offset += ref_name_len + sizeof(*extref);
73 /* Returns NULL if no extref found */
74 struct btrfs_inode_extref *
75 btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root,
77 struct btrfs_path *path,
78 const char *name, int name_len,
79 u64 inode_objectid, u64 ref_objectid, int ins_len,
85 key.objectid = inode_objectid;
86 key.type = BTRFS_INODE_EXTREF_KEY;
87 key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
89 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
94 return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
95 ref_objectid, name, name_len);
99 static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
100 struct btrfs_root *root,
101 const char *name, int name_len,
102 u64 inode_objectid, u64 ref_objectid,
105 struct btrfs_path *path;
106 struct btrfs_key key;
107 struct btrfs_inode_extref *extref;
108 struct extent_buffer *leaf;
110 int del_len = name_len + sizeof(*extref);
112 unsigned long item_start;
115 key.objectid = inode_objectid;
116 key.type = BTRFS_INODE_EXTREF_KEY;
117 key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
119 path = btrfs_alloc_path();
123 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
130 * Sanity check - did we find the right item for this name?
131 * This should always succeed so error here will make the FS
134 extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
135 ref_objectid, name, name_len);
137 btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL);
142 leaf = path->nodes[0];
143 item_size = btrfs_item_size(leaf, path->slots[0]);
145 *index = btrfs_inode_extref_index(leaf, extref);
147 if (del_len == item_size) {
149 * Common case only one ref in the item, remove the
152 ret = btrfs_del_item(trans, root, path);
156 ptr = (unsigned long)extref;
157 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
159 memmove_extent_buffer(leaf, ptr, ptr + del_len,
160 item_size - (ptr + del_len - item_start));
162 btrfs_truncate_item(path, item_size - del_len, 1);
165 btrfs_free_path(path);
170 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root,
172 const char *name, int name_len,
173 u64 inode_objectid, u64 ref_objectid, u64 *index)
175 struct btrfs_path *path;
176 struct btrfs_key key;
177 struct btrfs_inode_ref *ref;
178 struct extent_buffer *leaf;
180 unsigned long item_start;
184 int search_ext_refs = 0;
185 int del_len = name_len + sizeof(*ref);
187 key.objectid = inode_objectid;
188 key.offset = ref_objectid;
189 key.type = BTRFS_INODE_REF_KEY;
191 path = btrfs_alloc_path();
195 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
200 } else if (ret < 0) {
204 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name,
211 leaf = path->nodes[0];
212 item_size = btrfs_item_size(leaf, path->slots[0]);
215 *index = btrfs_inode_ref_index(leaf, ref);
217 if (del_len == item_size) {
218 ret = btrfs_del_item(trans, root, path);
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);
228 btrfs_free_path(path);
230 if (search_ext_refs) {
232 * No refs were found, or we could not find the
233 * name in our ref array. Find and remove the extended
236 return btrfs_del_inode_extref(trans, root, name, name_len,
237 inode_objectid, ref_objectid, index);
244 * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
246 * The caller must have checked against BTRFS_LINK_MAX already.
248 static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
249 struct btrfs_root *root,
250 const char *name, int name_len,
251 u64 inode_objectid, u64 ref_objectid, u64 index)
253 struct btrfs_inode_extref *extref;
255 int ins_len = name_len + sizeof(*extref);
257 struct btrfs_path *path;
258 struct btrfs_key key;
259 struct extent_buffer *leaf;
261 key.objectid = inode_objectid;
262 key.type = BTRFS_INODE_EXTREF_KEY;
263 key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
265 path = btrfs_alloc_path();
269 ret = btrfs_insert_empty_item(trans, root, path, &key,
271 if (ret == -EEXIST) {
272 if (btrfs_find_name_in_ext_backref(path->nodes[0],
278 btrfs_extend_item(path, ins_len);
284 leaf = path->nodes[0];
285 ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
286 ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len;
287 extref = (struct btrfs_inode_extref *)ptr;
289 btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len);
290 btrfs_set_inode_extref_index(path->nodes[0], extref, index);
291 btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
293 ptr = (unsigned long)&extref->name;
294 write_extent_buffer(path->nodes[0], name, ptr, name_len);
295 btrfs_mark_buffer_dirty(path->nodes[0]);
298 btrfs_free_path(path);
302 /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
303 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
304 struct btrfs_root *root,
305 const char *name, int name_len,
306 u64 inode_objectid, u64 ref_objectid, u64 index)
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;
314 int ins_len = name_len + sizeof(*ref);
316 key.objectid = inode_objectid;
317 key.offset = ref_objectid;
318 key.type = BTRFS_INODE_REF_KEY;
320 path = btrfs_alloc_path();
324 path->skip_release_on_error = 1;
325 ret = btrfs_insert_empty_item(trans, root, path, &key,
327 if (ret == -EEXIST) {
329 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
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);
343 } else if (ret < 0) {
344 if (ret == -EOVERFLOW) {
345 if (btrfs_find_name_in_backref(path->nodes[0],
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);
360 write_extent_buffer(path->nodes[0], name, ptr, name_len);
361 btrfs_mark_buffer_dirty(path->nodes[0]);
364 btrfs_free_path(path);
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,
375 ref_objectid, index);
381 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
382 struct btrfs_root *root,
383 struct btrfs_path *path, u64 objectid)
385 struct btrfs_key key;
387 key.objectid = objectid;
388 key.type = BTRFS_INODE_ITEM_KEY;
391 ret = btrfs_insert_empty_item(trans, root, path, &key,
392 sizeof(struct btrfs_inode_item));
396 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
397 *root, struct btrfs_path *path,
398 struct btrfs_key *location, int mod)
400 int ins_len = mod < 0 ? -1 : 0;
404 struct extent_buffer *leaf;
405 struct btrfs_key found_key;
407 ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
408 if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
409 location->offset == (u64)-1 && path->slots[0] != 0) {
410 slot = path->slots[0] - 1;
411 leaf = path->nodes[0];
412 btrfs_item_key_to_cpu(leaf, &found_key, slot);
413 if (found_key.objectid == location->objectid &&
414 found_key.type == location->type) {
422 static inline void btrfs_trace_truncate(struct btrfs_inode *inode,
423 struct extent_buffer *leaf,
424 struct btrfs_file_extent_item *fi,
425 u64 offset, int extent_type, int slot)
429 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
430 trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot,
433 trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset);
437 * Remove inode items from a given root.
439 * @trans: A transaction handle.
440 * @root: The root from which to remove items.
441 * @inode: The inode whose items we want to remove.
442 * @control: The btrfs_truncate_control to control how and what we
445 * Remove all keys associated with the inode from the given root that have a key
446 * with a type greater than or equals to @min_type. When @min_type has a value of
447 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
448 * greater than or equals to @new_size. If a file extent item that starts before
449 * @new_size and ends after it is found, its length is adjusted.
451 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
452 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
454 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
455 struct btrfs_root *root,
456 struct btrfs_truncate_control *control)
458 struct btrfs_fs_info *fs_info = root->fs_info;
459 struct btrfs_path *path;
460 struct extent_buffer *leaf;
461 struct btrfs_file_extent_item *fi;
462 struct btrfs_key key;
463 struct btrfs_key found_key;
464 u64 new_size = control->new_size;
465 u64 extent_num_bytes = 0;
466 u64 extent_offset = 0;
468 u32 found_type = (u8)-1;
470 int pending_del_nr = 0;
471 int pending_del_slot = 0;
472 int extent_type = -1;
474 u64 bytes_deleted = 0;
475 bool be_nice = false;
477 ASSERT(control->inode || !control->clear_extent_range);
478 ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY);
480 control->last_size = new_size;
481 control->sub_bytes = 0;
484 * For shareable roots we want to back off from time to time, this turns
485 * out to be subvolume roots, reloc roots, and data reloc roots.
487 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
490 path = btrfs_alloc_path();
493 path->reada = READA_BACK;
495 key.objectid = control->ino;
496 key.offset = (u64)-1;
501 * With a 16K leaf size and 128MiB extents, you can actually queue up a
502 * huge file in a single leaf. Most of the time that bytes_deleted is
503 * > 0, it will be huge by the time we get here
505 if (be_nice && bytes_deleted > SZ_32M &&
506 btrfs_should_end_transaction(trans)) {
511 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
517 /* There are no items in the tree for us to truncate, we're done */
518 if (path->slots[0] == 0)
524 u64 clear_start = 0, clear_len = 0, extent_start = 0;
525 bool should_throttle = false;
528 leaf = path->nodes[0];
529 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
530 found_type = found_key.type;
532 if (found_key.objectid != control->ino)
535 if (found_type < control->min_type)
538 item_end = found_key.offset;
539 if (found_type == BTRFS_EXTENT_DATA_KEY) {
540 fi = btrfs_item_ptr(leaf, path->slots[0],
541 struct btrfs_file_extent_item);
542 extent_type = btrfs_file_extent_type(leaf, fi);
543 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
545 btrfs_file_extent_num_bytes(leaf, fi);
546 else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
547 item_end += btrfs_file_extent_ram_bytes(leaf, fi);
549 btrfs_trace_truncate(control->inode, leaf, fi,
550 found_key.offset, extent_type,
554 if (found_type > control->min_type) {
557 if (item_end < new_size)
559 if (found_key.offset >= new_size)
565 /* FIXME, shrink the extent if the ref count is only 1 */
566 if (found_type != BTRFS_EXTENT_DATA_KEY)
569 control->extents_found++;
571 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
574 clear_start = found_key.offset;
575 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
578 btrfs_file_extent_num_bytes(leaf, fi);
579 extent_num_bytes = ALIGN(new_size -
581 fs_info->sectorsize);
582 clear_start = ALIGN(new_size, fs_info->sectorsize);
584 btrfs_set_file_extent_num_bytes(leaf, fi,
586 num_dec = (orig_num_bytes - extent_num_bytes);
587 if (extent_start != 0)
588 control->sub_bytes += num_dec;
589 btrfs_mark_buffer_dirty(leaf);
592 btrfs_file_extent_disk_num_bytes(leaf, fi);
593 extent_offset = found_key.offset -
594 btrfs_file_extent_offset(leaf, fi);
596 /* FIXME blocksize != 4096 */
597 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
598 if (extent_start != 0)
599 control->sub_bytes += num_dec;
602 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
604 * We can't truncate inline items that have had
608 btrfs_file_extent_encryption(leaf, fi) == 0 &&
609 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
610 btrfs_file_extent_compression(leaf, fi) == 0) {
611 u32 size = (u32)(new_size - found_key.offset);
613 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
614 size = btrfs_file_extent_calc_inline_size(size);
615 btrfs_truncate_item(path, size, 1);
616 } else if (!del_item) {
618 * We have to bail so the last_size is set to
619 * just before this extent.
621 ret = BTRFS_NEED_TRUNCATE_BLOCK;
625 * Inline extents are special, we just treat
626 * them as a full sector worth in the file
627 * extent tree just for simplicity sake.
629 clear_len = fs_info->sectorsize;
632 control->sub_bytes += item_end + 1 - new_size;
636 * We only want to clear the file extent range if we're
637 * modifying the actual inode's mapping, which is just the
638 * normal truncate path.
640 if (control->clear_extent_range) {
641 ret = btrfs_inode_clear_file_extent_range(control->inode,
642 clear_start, clear_len);
644 btrfs_abort_transaction(trans, ret);
650 ASSERT(!pending_del_nr ||
651 ((path->slots[0] + 1) == pending_del_slot));
653 control->last_size = found_key.offset;
654 if (!pending_del_nr) {
655 /* No pending yet, add ourselves */
656 pending_del_slot = path->slots[0];
658 } else if (pending_del_nr &&
659 path->slots[0] + 1 == pending_del_slot) {
660 /* Hop on the pending chunk */
662 pending_del_slot = path->slots[0];
665 control->last_size = new_size;
669 if (del_item && extent_start != 0 && !control->skip_ref_updates) {
670 struct btrfs_ref ref = { 0 };
672 bytes_deleted += extent_num_bytes;
674 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
675 extent_start, extent_num_bytes, 0);
676 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
677 control->ino, extent_offset,
678 root->root_key.objectid, false);
679 ret = btrfs_free_extent(trans, &ref);
681 btrfs_abort_transaction(trans, ret);
685 if (btrfs_should_throttle_delayed_refs(trans))
686 should_throttle = true;
690 if (found_type == BTRFS_INODE_ITEM_KEY)
693 if (path->slots[0] == 0 ||
694 path->slots[0] != pending_del_slot ||
696 if (pending_del_nr) {
697 ret = btrfs_del_items(trans, root, path,
701 btrfs_abort_transaction(trans, ret);
706 btrfs_release_path(path);
709 * We can generate a lot of delayed refs, so we need to
710 * throttle every once and a while and make sure we're
711 * adding enough space to keep up with the work we are
712 * generating. Since we hold a transaction here we
713 * can't flush, and we don't want to FLUSH_LIMIT because
714 * we could have generated too many delayed refs to
715 * actually allocate, so just bail if we're short and
716 * let the normal reservation dance happen higher up.
718 if (should_throttle) {
719 ret = btrfs_delayed_refs_rsv_refill(fs_info,
720 BTRFS_RESERVE_NO_FLUSH);
732 if (ret >= 0 && pending_del_nr) {
735 err = btrfs_del_items(trans, root, path, pending_del_slot,
738 btrfs_abort_transaction(trans, err);
743 ASSERT(control->last_size >= new_size);
744 if (!ret && control->last_size > new_size)
745 control->last_size = new_size;
747 btrfs_free_path(path);