1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
9 #include "inode-item.h"
11 #include "transaction.h"
12 #include "space-info.h"
13 #include "accessors.h"
14 #include "extent-tree.h"
15 #include "file-item.h"
17 struct btrfs_inode_ref *btrfs_find_name_in_backref(const struct extent_buffer *leaf,
19 const struct fscrypt_str *name)
21 struct btrfs_inode_ref *ref;
23 unsigned long name_ptr;
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);
37 if (memcmp_extent_buffer(leaf, name->name, name_ptr,
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)
48 struct btrfs_inode_extref *extref;
50 unsigned long name_ptr;
55 item_size = btrfs_item_size(leaf, slot);
56 ptr = btrfs_item_ptr_offset(leaf, slot);
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.
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);
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,
75 cur_offset += ref_name_len + sizeof(*extref);
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,
92 key.objectid = inode_objectid;
93 key.type = BTRFS_INODE_EXTREF_KEY;
94 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
96 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
101 return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
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,
112 BTRFS_PATH_AUTO_FREE(path);
113 struct btrfs_key key;
114 struct btrfs_inode_extref *extref;
115 struct extent_buffer *leaf;
117 int del_len = name->len + sizeof(*extref);
119 unsigned long item_start;
122 key.objectid = inode_objectid;
123 key.type = BTRFS_INODE_EXTREF_KEY;
124 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
126 path = btrfs_alloc_path();
130 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
137 * Sanity check - did we find the right item for this name?
138 * This should always succeed so error here will make the FS
141 extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
144 btrfs_abort_transaction(trans, -ENOENT);
148 leaf = path->nodes[0];
149 item_size = btrfs_item_size(leaf, path->slots[0]);
151 *index = btrfs_inode_extref_index(leaf, extref);
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);
158 ptr = (unsigned long)extref;
159 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
161 memmove_extent_buffer(leaf, ptr, ptr + del_len,
162 item_size - (ptr + del_len - item_start));
164 btrfs_truncate_item(trans, path, item_size - del_len, 1);
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)
173 struct btrfs_path *path;
174 struct btrfs_key key;
175 struct btrfs_inode_ref *ref;
176 struct extent_buffer *leaf;
178 unsigned long item_start;
182 int search_ext_refs = 0;
183 int del_len = name->len + sizeof(*ref);
185 key.objectid = inode_objectid;
186 key.type = BTRFS_INODE_REF_KEY;
187 key.offset = ref_objectid;
189 path = btrfs_alloc_path();
193 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
198 } else if (ret < 0) {
202 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name);
208 leaf = path->nodes[0];
209 item_size = btrfs_item_size(leaf, path->slots[0]);
212 *index = btrfs_inode_ref_index(leaf, ref);
214 if (del_len == item_size) {
215 ret = btrfs_del_item(trans, root, path);
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);
225 btrfs_free_path(path);
227 if (search_ext_refs) {
229 * No refs were found, or we could not find the
230 * name in our ref array. Find and remove the extended
233 return btrfs_del_inode_extref(trans, root, name,
234 inode_objectid, ref_objectid, index);
241 * Insert an extended inode ref into a tree.
243 * The caller must have checked against BTRFS_LINK_MAX already.
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,
251 struct btrfs_inode_extref *extref;
253 int ins_len = name->len + sizeof(*extref);
255 BTRFS_PATH_AUTO_FREE(path);
256 struct btrfs_key key;
257 struct extent_buffer *leaf;
259 key.objectid = inode_objectid;
260 key.type = BTRFS_INODE_EXTREF_KEY;
261 key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
263 path = btrfs_alloc_path();
267 ret = btrfs_insert_empty_item(trans, root, path, &key,
269 if (ret == -EEXIST) {
270 if (btrfs_find_name_in_ext_backref(path->nodes[0],
276 btrfs_extend_item(trans, path, ins_len);
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;
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);
291 ptr = (unsigned long)&extref->name;
292 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
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)
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;
308 int ins_len = name->len + sizeof(*ref);
310 key.objectid = inode_objectid;
311 key.type = BTRFS_INODE_REF_KEY;
312 key.offset = ref_objectid;
314 path = btrfs_alloc_path();
318 path->skip_release_on_error = 1;
319 ret = btrfs_insert_empty_item(trans, root, path, &key,
321 if (ret == -EEXIST) {
323 ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
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);
337 } else if (ret < 0) {
338 if (ret == -EOVERFLOW) {
339 if (btrfs_find_name_in_backref(path->nodes[0],
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);
354 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
356 btrfs_free_path(path);
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,
366 ref_objectid, index);
372 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
373 struct btrfs_root *root,
374 struct btrfs_path *path, u64 objectid)
376 struct btrfs_key key;
378 key.objectid = objectid;
379 key.type = BTRFS_INODE_ITEM_KEY;
382 ret = btrfs_insert_empty_item(trans, root, path, &key,
383 sizeof(struct btrfs_inode_item));
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)
391 int ins_len = mod < 0 ? -1 : 0;
395 struct extent_buffer *leaf;
396 struct btrfs_key found_key;
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) {
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)
420 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
421 trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot,
424 trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset);
428 * Remove inode items from a given root.
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
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.
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.
445 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
446 struct btrfs_root *root,
447 struct btrfs_truncate_control *control)
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;
459 u32 found_type = (u8)-1;
461 int pending_del_nr = 0;
462 int pending_del_slot = 0;
463 int extent_type = -1;
465 u64 bytes_deleted = 0;
466 bool be_nice = false;
468 ASSERT(control->inode || !control->clear_extent_range);
469 ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY);
471 control->last_size = new_size;
472 control->sub_bytes = 0;
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.
478 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
481 path = btrfs_alloc_path();
484 path->reada = READA_BACK;
486 key.objectid = control->ino;
488 key.offset = (u64)-1;
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
496 if (be_nice && bytes_deleted > SZ_32M &&
497 btrfs_should_end_transaction(trans)) {
502 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
508 /* There are no items in the tree for us to truncate, we're done */
509 if (path->slots[0] == 0)
515 u64 clear_start = 0, clear_len = 0, extent_start = 0;
516 bool refill_delayed_refs_rsv = false;
519 leaf = path->nodes[0];
520 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
521 found_type = found_key.type;
523 if (found_key.objectid != control->ino)
526 if (found_type < control->min_type)
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)
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);
540 btrfs_trace_truncate(control->inode, leaf, fi,
541 found_key.offset, extent_type,
545 if (found_type > control->min_type) {
548 if (item_end < new_size)
550 if (found_key.offset >= new_size)
556 /* FIXME, shrink the extent if the ref count is only 1 */
557 if (found_type != BTRFS_EXTENT_DATA_KEY)
560 control->extents_found++;
562 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
565 clear_start = found_key.offset;
566 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
569 btrfs_file_extent_num_bytes(leaf, fi);
570 extent_num_bytes = ALIGN(new_size -
572 fs_info->sectorsize);
573 clear_start = ALIGN(new_size, fs_info->sectorsize);
575 btrfs_set_file_extent_num_bytes(leaf, fi,
577 num_dec = (orig_num_bytes - extent_num_bytes);
578 if (extent_start != 0)
579 control->sub_bytes += num_dec;
582 btrfs_file_extent_disk_num_bytes(leaf, fi);
583 extent_offset = found_key.offset -
584 btrfs_file_extent_offset(leaf, fi);
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;
592 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
594 * We can't truncate inline items that have had
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);
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) {
608 * We have to bail so the last_size is set to
609 * just before this extent.
611 ret = BTRFS_NEED_TRUNCATE_BLOCK;
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.
619 clear_len = fs_info->sectorsize;
622 control->sub_bytes += item_end + 1 - new_size;
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.
630 if (control->clear_extent_range) {
631 ret = btrfs_inode_clear_file_extent_range(control->inode,
632 clear_start, clear_len);
634 btrfs_abort_transaction(trans, ret);
640 ASSERT(!pending_del_nr ||
641 ((path->slots[0] + 1) == pending_del_slot));
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];
648 } else if (path->slots[0] + 1 == pending_del_slot) {
649 /* Hop on the pending chunk */
651 pending_del_slot = path->slots[0];
654 control->last_size = new_size;
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),
667 bytes_deleted += extent_num_bytes;
669 btrfs_init_data_ref(&ref, control->ino, extent_offset,
670 btrfs_root_id(root), false);
671 ret = btrfs_free_extent(trans, &ref);
673 btrfs_abort_transaction(trans, ret);
676 if (be_nice && btrfs_check_space_for_delayed_refs(fs_info))
677 refill_delayed_refs_rsv = true;
680 if (found_type == BTRFS_INODE_ITEM_KEY)
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,
691 btrfs_abort_transaction(trans, ret);
696 btrfs_release_path(path);
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.
708 if (refill_delayed_refs_rsv) {
709 ret = btrfs_delayed_refs_rsv_refill(fs_info,
710 BTRFS_RESERVE_NO_FLUSH);
722 if (ret >= 0 && pending_del_nr) {
725 err = btrfs_del_items(trans, root, path, pending_del_slot,
728 btrfs_abort_transaction(trans, err);
733 ASSERT(control->last_size >= new_size);
734 if (!ret && control->last_size > new_size)
735 control->last_size = new_size;
737 btrfs_free_path(path);