+
+int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root, const char *name, int name_len,
+ u64 ino, u64 parent_ino, u64 *index)
+{
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ struct btrfs_inode_ref *ref;
+ struct extent_buffer *leaf;
+ unsigned long ptr;
+ unsigned long item_start;
+ u32 item_size;
+ u32 sub_item_len;
+ int ret;
+ int search_ext_refs = 0;
+ int del_len = name_len + sizeof(*ref);
+
+ key.objectid = ino;
+ key.offset = parent_ino;
+ key.type = BTRFS_INODE_REF_KEY;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
+ if (ret > 0) {
+ ret = -ENOENT;
+ search_ext_refs = 1;
+ goto out;
+ } else if (ret < 0) {
+ goto out;
+ }
+ if (!find_name_in_backref(path, name, name_len, &ref)) {
+ ret = -ENOENT;
+ search_ext_refs = 1;
+ goto out;
+ }
+ leaf = path->nodes[0];
+ item_size = btrfs_item_size_nr(leaf, path->slots[0]);
+
+ if (index)
+ *index = btrfs_inode_ref_index(leaf, ref);
+
+ if (del_len == item_size) {
+ ret = btrfs_del_item(trans, root, path);
+ goto out;
+ }
+ ptr = (unsigned long)ref;
+ sub_item_len = name_len + sizeof(*ref);
+ item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
+ memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
+ item_size - (ptr + sub_item_len - item_start));
+ btrfs_truncate_item(root, path, item_size - sub_item_len, 1);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+out:
+ btrfs_free_path(path);
+
+ if (search_ext_refs &&
+ btrfs_fs_incompat(root->fs_info, EXTENDED_IREF)) {
+ /*
+ * No refs were found, or we could not find the name in our ref
+ * array. Find and remove the extended inode ref then.
+ */
+ return btrfs_del_inode_extref(trans, root, name, name_len,
+ ino, parent_ino, index);
+ }
+
+ return ret;
+}