162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "ctree.h"
762306a36Sopenharmony_ci#include "fs.h"
862306a36Sopenharmony_ci#include "messages.h"
962306a36Sopenharmony_ci#include "inode-item.h"
1062306a36Sopenharmony_ci#include "disk-io.h"
1162306a36Sopenharmony_ci#include "transaction.h"
1262306a36Sopenharmony_ci#include "print-tree.h"
1362306a36Sopenharmony_ci#include "space-info.h"
1462306a36Sopenharmony_ci#include "accessors.h"
1562306a36Sopenharmony_ci#include "extent-tree.h"
1662306a36Sopenharmony_ci#include "file-item.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf,
1962306a36Sopenharmony_ci						   int slot,
2062306a36Sopenharmony_ci						   const struct fscrypt_str *name)
2162306a36Sopenharmony_ci{
2262306a36Sopenharmony_ci	struct btrfs_inode_ref *ref;
2362306a36Sopenharmony_ci	unsigned long ptr;
2462306a36Sopenharmony_ci	unsigned long name_ptr;
2562306a36Sopenharmony_ci	u32 item_size;
2662306a36Sopenharmony_ci	u32 cur_offset = 0;
2762306a36Sopenharmony_ci	int len;
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	item_size = btrfs_item_size(leaf, slot);
3062306a36Sopenharmony_ci	ptr = btrfs_item_ptr_offset(leaf, slot);
3162306a36Sopenharmony_ci	while (cur_offset < item_size) {
3262306a36Sopenharmony_ci		ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
3362306a36Sopenharmony_ci		len = btrfs_inode_ref_name_len(leaf, ref);
3462306a36Sopenharmony_ci		name_ptr = (unsigned long)(ref + 1);
3562306a36Sopenharmony_ci		cur_offset += len + sizeof(*ref);
3662306a36Sopenharmony_ci		if (len != name->len)
3762306a36Sopenharmony_ci			continue;
3862306a36Sopenharmony_ci		if (memcmp_extent_buffer(leaf, name->name, name_ptr,
3962306a36Sopenharmony_ci					 name->len) == 0)
4062306a36Sopenharmony_ci			return ref;
4162306a36Sopenharmony_ci	}
4262306a36Sopenharmony_ci	return NULL;
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistruct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
4662306a36Sopenharmony_ci		struct extent_buffer *leaf, int slot, u64 ref_objectid,
4762306a36Sopenharmony_ci		const struct fscrypt_str *name)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	struct btrfs_inode_extref *extref;
5062306a36Sopenharmony_ci	unsigned long ptr;
5162306a36Sopenharmony_ci	unsigned long name_ptr;
5262306a36Sopenharmony_ci	u32 item_size;
5362306a36Sopenharmony_ci	u32 cur_offset = 0;
5462306a36Sopenharmony_ci	int ref_name_len;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	item_size = btrfs_item_size(leaf, slot);
5762306a36Sopenharmony_ci	ptr = btrfs_item_ptr_offset(leaf, slot);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	/*
6062306a36Sopenharmony_ci	 * Search all extended backrefs in this item. We're only
6162306a36Sopenharmony_ci	 * looking through any collisions so most of the time this is
6262306a36Sopenharmony_ci	 * just going to compare against one buffer. If all is well,
6362306a36Sopenharmony_ci	 * we'll return success and the inode ref object.
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	while (cur_offset < item_size) {
6662306a36Sopenharmony_ci		extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
6762306a36Sopenharmony_ci		name_ptr = (unsigned long)(&extref->name);
6862306a36Sopenharmony_ci		ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci		if (ref_name_len == name->len &&
7162306a36Sopenharmony_ci		    btrfs_inode_extref_parent(leaf, extref) == ref_objectid &&
7262306a36Sopenharmony_ci		    (memcmp_extent_buffer(leaf, name->name, name_ptr,
7362306a36Sopenharmony_ci					  name->len) == 0))
7462306a36Sopenharmony_ci			return extref;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci		cur_offset += ref_name_len + sizeof(*extref);
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci	return NULL;
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* Returns NULL if no extref found */
8262306a36Sopenharmony_cistruct btrfs_inode_extref *
8362306a36Sopenharmony_cibtrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
8462306a36Sopenharmony_ci			  struct btrfs_root *root,
8562306a36Sopenharmony_ci			  struct btrfs_path *path,
8662306a36Sopenharmony_ci			  const struct fscrypt_str *name,
8762306a36Sopenharmony_ci			  u64 inode_objectid, u64 ref_objectid, int ins_len,
8862306a36Sopenharmony_ci			  int cow)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	int ret;
9162306a36Sopenharmony_ci	struct btrfs_key key;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	key.objectid = inode_objectid;
9462306a36Sopenharmony_ci	key.type = BTRFS_INODE_EXTREF_KEY;
9562306a36Sopenharmony_ci	key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
9862306a36Sopenharmony_ci	if (ret < 0)
9962306a36Sopenharmony_ci		return ERR_PTR(ret);
10062306a36Sopenharmony_ci	if (ret > 0)
10162306a36Sopenharmony_ci		return NULL;
10262306a36Sopenharmony_ci	return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
10362306a36Sopenharmony_ci					      ref_objectid, name);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
10862306a36Sopenharmony_ci				  struct btrfs_root *root,
10962306a36Sopenharmony_ci				  const struct fscrypt_str *name,
11062306a36Sopenharmony_ci				  u64 inode_objectid, u64 ref_objectid,
11162306a36Sopenharmony_ci				  u64 *index)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct btrfs_path *path;
11462306a36Sopenharmony_ci	struct btrfs_key key;
11562306a36Sopenharmony_ci	struct btrfs_inode_extref *extref;
11662306a36Sopenharmony_ci	struct extent_buffer *leaf;
11762306a36Sopenharmony_ci	int ret;
11862306a36Sopenharmony_ci	int del_len = name->len + sizeof(*extref);
11962306a36Sopenharmony_ci	unsigned long ptr;
12062306a36Sopenharmony_ci	unsigned long item_start;
12162306a36Sopenharmony_ci	u32 item_size;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	key.objectid = inode_objectid;
12462306a36Sopenharmony_ci	key.type = BTRFS_INODE_EXTREF_KEY;
12562306a36Sopenharmony_ci	key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	path = btrfs_alloc_path();
12862306a36Sopenharmony_ci	if (!path)
12962306a36Sopenharmony_ci		return -ENOMEM;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
13262306a36Sopenharmony_ci	if (ret > 0)
13362306a36Sopenharmony_ci		ret = -ENOENT;
13462306a36Sopenharmony_ci	if (ret < 0)
13562306a36Sopenharmony_ci		goto out;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	/*
13862306a36Sopenharmony_ci	 * Sanity check - did we find the right item for this name?
13962306a36Sopenharmony_ci	 * This should always succeed so error here will make the FS
14062306a36Sopenharmony_ci	 * readonly.
14162306a36Sopenharmony_ci	 */
14262306a36Sopenharmony_ci	extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
14362306a36Sopenharmony_ci						ref_objectid, name);
14462306a36Sopenharmony_ci	if (!extref) {
14562306a36Sopenharmony_ci		btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL);
14662306a36Sopenharmony_ci		ret = -EROFS;
14762306a36Sopenharmony_ci		goto out;
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	leaf = path->nodes[0];
15162306a36Sopenharmony_ci	item_size = btrfs_item_size(leaf, path->slots[0]);
15262306a36Sopenharmony_ci	if (index)
15362306a36Sopenharmony_ci		*index = btrfs_inode_extref_index(leaf, extref);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	if (del_len == item_size) {
15662306a36Sopenharmony_ci		/*
15762306a36Sopenharmony_ci		 * Common case only one ref in the item, remove the
15862306a36Sopenharmony_ci		 * whole item.
15962306a36Sopenharmony_ci		 */
16062306a36Sopenharmony_ci		ret = btrfs_del_item(trans, root, path);
16162306a36Sopenharmony_ci		goto out;
16262306a36Sopenharmony_ci	}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	ptr = (unsigned long)extref;
16562306a36Sopenharmony_ci	item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	memmove_extent_buffer(leaf, ptr, ptr + del_len,
16862306a36Sopenharmony_ci			      item_size - (ptr + del_len - item_start));
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	btrfs_truncate_item(trans, path, item_size - del_len, 1);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciout:
17362306a36Sopenharmony_ci	btrfs_free_path(path);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	return ret;
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciint btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
17962306a36Sopenharmony_ci			struct btrfs_root *root, const struct fscrypt_str *name,
18062306a36Sopenharmony_ci			u64 inode_objectid, u64 ref_objectid, u64 *index)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	struct btrfs_path *path;
18362306a36Sopenharmony_ci	struct btrfs_key key;
18462306a36Sopenharmony_ci	struct btrfs_inode_ref *ref;
18562306a36Sopenharmony_ci	struct extent_buffer *leaf;
18662306a36Sopenharmony_ci	unsigned long ptr;
18762306a36Sopenharmony_ci	unsigned long item_start;
18862306a36Sopenharmony_ci	u32 item_size;
18962306a36Sopenharmony_ci	u32 sub_item_len;
19062306a36Sopenharmony_ci	int ret;
19162306a36Sopenharmony_ci	int search_ext_refs = 0;
19262306a36Sopenharmony_ci	int del_len = name->len + sizeof(*ref);
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	key.objectid = inode_objectid;
19562306a36Sopenharmony_ci	key.offset = ref_objectid;
19662306a36Sopenharmony_ci	key.type = BTRFS_INODE_REF_KEY;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	path = btrfs_alloc_path();
19962306a36Sopenharmony_ci	if (!path)
20062306a36Sopenharmony_ci		return -ENOMEM;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
20362306a36Sopenharmony_ci	if (ret > 0) {
20462306a36Sopenharmony_ci		ret = -ENOENT;
20562306a36Sopenharmony_ci		search_ext_refs = 1;
20662306a36Sopenharmony_ci		goto out;
20762306a36Sopenharmony_ci	} else if (ret < 0) {
20862306a36Sopenharmony_ci		goto out;
20962306a36Sopenharmony_ci	}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name);
21262306a36Sopenharmony_ci	if (!ref) {
21362306a36Sopenharmony_ci		ret = -ENOENT;
21462306a36Sopenharmony_ci		search_ext_refs = 1;
21562306a36Sopenharmony_ci		goto out;
21662306a36Sopenharmony_ci	}
21762306a36Sopenharmony_ci	leaf = path->nodes[0];
21862306a36Sopenharmony_ci	item_size = btrfs_item_size(leaf, path->slots[0]);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	if (index)
22162306a36Sopenharmony_ci		*index = btrfs_inode_ref_index(leaf, ref);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	if (del_len == item_size) {
22462306a36Sopenharmony_ci		ret = btrfs_del_item(trans, root, path);
22562306a36Sopenharmony_ci		goto out;
22662306a36Sopenharmony_ci	}
22762306a36Sopenharmony_ci	ptr = (unsigned long)ref;
22862306a36Sopenharmony_ci	sub_item_len = name->len + sizeof(*ref);
22962306a36Sopenharmony_ci	item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
23062306a36Sopenharmony_ci	memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
23162306a36Sopenharmony_ci			      item_size - (ptr + sub_item_len - item_start));
23262306a36Sopenharmony_ci	btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
23362306a36Sopenharmony_ciout:
23462306a36Sopenharmony_ci	btrfs_free_path(path);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	if (search_ext_refs) {
23762306a36Sopenharmony_ci		/*
23862306a36Sopenharmony_ci		 * No refs were found, or we could not find the
23962306a36Sopenharmony_ci		 * name in our ref array. Find and remove the extended
24062306a36Sopenharmony_ci		 * inode ref then.
24162306a36Sopenharmony_ci		 */
24262306a36Sopenharmony_ci		return btrfs_del_inode_extref(trans, root, name,
24362306a36Sopenharmony_ci					      inode_objectid, ref_objectid, index);
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	return ret;
24762306a36Sopenharmony_ci}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/*
25062306a36Sopenharmony_ci * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
25162306a36Sopenharmony_ci *
25262306a36Sopenharmony_ci * The caller must have checked against BTRFS_LINK_MAX already.
25362306a36Sopenharmony_ci */
25462306a36Sopenharmony_cistatic int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
25562306a36Sopenharmony_ci				     struct btrfs_root *root,
25662306a36Sopenharmony_ci				     const struct fscrypt_str *name,
25762306a36Sopenharmony_ci				     u64 inode_objectid, u64 ref_objectid,
25862306a36Sopenharmony_ci				     u64 index)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	struct btrfs_inode_extref *extref;
26162306a36Sopenharmony_ci	int ret;
26262306a36Sopenharmony_ci	int ins_len = name->len + sizeof(*extref);
26362306a36Sopenharmony_ci	unsigned long ptr;
26462306a36Sopenharmony_ci	struct btrfs_path *path;
26562306a36Sopenharmony_ci	struct btrfs_key key;
26662306a36Sopenharmony_ci	struct extent_buffer *leaf;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	key.objectid = inode_objectid;
26962306a36Sopenharmony_ci	key.type = BTRFS_INODE_EXTREF_KEY;
27062306a36Sopenharmony_ci	key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	path = btrfs_alloc_path();
27362306a36Sopenharmony_ci	if (!path)
27462306a36Sopenharmony_ci		return -ENOMEM;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, &key,
27762306a36Sopenharmony_ci				      ins_len);
27862306a36Sopenharmony_ci	if (ret == -EEXIST) {
27962306a36Sopenharmony_ci		if (btrfs_find_name_in_ext_backref(path->nodes[0],
28062306a36Sopenharmony_ci						   path->slots[0],
28162306a36Sopenharmony_ci						   ref_objectid,
28262306a36Sopenharmony_ci						   name))
28362306a36Sopenharmony_ci			goto out;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci		btrfs_extend_item(trans, path, ins_len);
28662306a36Sopenharmony_ci		ret = 0;
28762306a36Sopenharmony_ci	}
28862306a36Sopenharmony_ci	if (ret < 0)
28962306a36Sopenharmony_ci		goto out;
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	leaf = path->nodes[0];
29262306a36Sopenharmony_ci	ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
29362306a36Sopenharmony_ci	ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len;
29462306a36Sopenharmony_ci	extref = (struct btrfs_inode_extref *)ptr;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	btrfs_set_inode_extref_name_len(path->nodes[0], extref, name->len);
29762306a36Sopenharmony_ci	btrfs_set_inode_extref_index(path->nodes[0], extref, index);
29862306a36Sopenharmony_ci	btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	ptr = (unsigned long)&extref->name;
30162306a36Sopenharmony_ci	write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
30262306a36Sopenharmony_ci	btrfs_mark_buffer_dirty(trans, path->nodes[0]);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ciout:
30562306a36Sopenharmony_ci	btrfs_free_path(path);
30662306a36Sopenharmony_ci	return ret;
30762306a36Sopenharmony_ci}
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci/* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
31062306a36Sopenharmony_ciint btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
31162306a36Sopenharmony_ci			   struct btrfs_root *root, const struct fscrypt_str *name,
31262306a36Sopenharmony_ci			   u64 inode_objectid, u64 ref_objectid, u64 index)
31362306a36Sopenharmony_ci{
31462306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
31562306a36Sopenharmony_ci	struct btrfs_path *path;
31662306a36Sopenharmony_ci	struct btrfs_key key;
31762306a36Sopenharmony_ci	struct btrfs_inode_ref *ref;
31862306a36Sopenharmony_ci	unsigned long ptr;
31962306a36Sopenharmony_ci	int ret;
32062306a36Sopenharmony_ci	int ins_len = name->len + sizeof(*ref);
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	key.objectid = inode_objectid;
32362306a36Sopenharmony_ci	key.offset = ref_objectid;
32462306a36Sopenharmony_ci	key.type = BTRFS_INODE_REF_KEY;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	path = btrfs_alloc_path();
32762306a36Sopenharmony_ci	if (!path)
32862306a36Sopenharmony_ci		return -ENOMEM;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	path->skip_release_on_error = 1;
33162306a36Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, &key,
33262306a36Sopenharmony_ci				      ins_len);
33362306a36Sopenharmony_ci	if (ret == -EEXIST) {
33462306a36Sopenharmony_ci		u32 old_size;
33562306a36Sopenharmony_ci		ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
33662306a36Sopenharmony_ci						 name);
33762306a36Sopenharmony_ci		if (ref)
33862306a36Sopenharmony_ci			goto out;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci		old_size = btrfs_item_size(path->nodes[0], path->slots[0]);
34162306a36Sopenharmony_ci		btrfs_extend_item(trans, path, ins_len);
34262306a36Sopenharmony_ci		ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
34362306a36Sopenharmony_ci				     struct btrfs_inode_ref);
34462306a36Sopenharmony_ci		ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
34562306a36Sopenharmony_ci		btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
34662306a36Sopenharmony_ci		btrfs_set_inode_ref_index(path->nodes[0], ref, index);
34762306a36Sopenharmony_ci		ptr = (unsigned long)(ref + 1);
34862306a36Sopenharmony_ci		ret = 0;
34962306a36Sopenharmony_ci	} else if (ret < 0) {
35062306a36Sopenharmony_ci		if (ret == -EOVERFLOW) {
35162306a36Sopenharmony_ci			if (btrfs_find_name_in_backref(path->nodes[0],
35262306a36Sopenharmony_ci						       path->slots[0],
35362306a36Sopenharmony_ci						       name))
35462306a36Sopenharmony_ci				ret = -EEXIST;
35562306a36Sopenharmony_ci			else
35662306a36Sopenharmony_ci				ret = -EMLINK;
35762306a36Sopenharmony_ci		}
35862306a36Sopenharmony_ci		goto out;
35962306a36Sopenharmony_ci	} else {
36062306a36Sopenharmony_ci		ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
36162306a36Sopenharmony_ci				     struct btrfs_inode_ref);
36262306a36Sopenharmony_ci		btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
36362306a36Sopenharmony_ci		btrfs_set_inode_ref_index(path->nodes[0], ref, index);
36462306a36Sopenharmony_ci		ptr = (unsigned long)(ref + 1);
36562306a36Sopenharmony_ci	}
36662306a36Sopenharmony_ci	write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
36762306a36Sopenharmony_ci	btrfs_mark_buffer_dirty(trans, path->nodes[0]);
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ciout:
37062306a36Sopenharmony_ci	btrfs_free_path(path);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	if (ret == -EMLINK) {
37362306a36Sopenharmony_ci		struct btrfs_super_block *disk_super = fs_info->super_copy;
37462306a36Sopenharmony_ci		/* We ran out of space in the ref array. Need to
37562306a36Sopenharmony_ci		 * add an extended ref. */
37662306a36Sopenharmony_ci		if (btrfs_super_incompat_flags(disk_super)
37762306a36Sopenharmony_ci		    & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
37862306a36Sopenharmony_ci			ret = btrfs_insert_inode_extref(trans, root, name,
37962306a36Sopenharmony_ci							inode_objectid,
38062306a36Sopenharmony_ci							ref_objectid, index);
38162306a36Sopenharmony_ci	}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	return ret;
38462306a36Sopenharmony_ci}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ciint btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
38762306a36Sopenharmony_ci			     struct btrfs_root *root,
38862306a36Sopenharmony_ci			     struct btrfs_path *path, u64 objectid)
38962306a36Sopenharmony_ci{
39062306a36Sopenharmony_ci	struct btrfs_key key;
39162306a36Sopenharmony_ci	int ret;
39262306a36Sopenharmony_ci	key.objectid = objectid;
39362306a36Sopenharmony_ci	key.type = BTRFS_INODE_ITEM_KEY;
39462306a36Sopenharmony_ci	key.offset = 0;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, &key,
39762306a36Sopenharmony_ci				      sizeof(struct btrfs_inode_item));
39862306a36Sopenharmony_ci	return ret;
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ciint btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
40262306a36Sopenharmony_ci		       *root, struct btrfs_path *path,
40362306a36Sopenharmony_ci		       struct btrfs_key *location, int mod)
40462306a36Sopenharmony_ci{
40562306a36Sopenharmony_ci	int ins_len = mod < 0 ? -1 : 0;
40662306a36Sopenharmony_ci	int cow = mod != 0;
40762306a36Sopenharmony_ci	int ret;
40862306a36Sopenharmony_ci	int slot;
40962306a36Sopenharmony_ci	struct extent_buffer *leaf;
41062306a36Sopenharmony_ci	struct btrfs_key found_key;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
41362306a36Sopenharmony_ci	if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
41462306a36Sopenharmony_ci	    location->offset == (u64)-1 && path->slots[0] != 0) {
41562306a36Sopenharmony_ci		slot = path->slots[0] - 1;
41662306a36Sopenharmony_ci		leaf = path->nodes[0];
41762306a36Sopenharmony_ci		btrfs_item_key_to_cpu(leaf, &found_key, slot);
41862306a36Sopenharmony_ci		if (found_key.objectid == location->objectid &&
41962306a36Sopenharmony_ci		    found_key.type == location->type) {
42062306a36Sopenharmony_ci			path->slots[0]--;
42162306a36Sopenharmony_ci			return 0;
42262306a36Sopenharmony_ci		}
42362306a36Sopenharmony_ci	}
42462306a36Sopenharmony_ci	return ret;
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_cistatic inline void btrfs_trace_truncate(struct btrfs_inode *inode,
42862306a36Sopenharmony_ci					struct extent_buffer *leaf,
42962306a36Sopenharmony_ci					struct btrfs_file_extent_item *fi,
43062306a36Sopenharmony_ci					u64 offset, int extent_type, int slot)
43162306a36Sopenharmony_ci{
43262306a36Sopenharmony_ci	if (!inode)
43362306a36Sopenharmony_ci		return;
43462306a36Sopenharmony_ci	if (extent_type == BTRFS_FILE_EXTENT_INLINE)
43562306a36Sopenharmony_ci		trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot,
43662306a36Sopenharmony_ci						    offset);
43762306a36Sopenharmony_ci	else
43862306a36Sopenharmony_ci		trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset);
43962306a36Sopenharmony_ci}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci/*
44262306a36Sopenharmony_ci * Remove inode items from a given root.
44362306a36Sopenharmony_ci *
44462306a36Sopenharmony_ci * @trans:		A transaction handle.
44562306a36Sopenharmony_ci * @root:		The root from which to remove items.
44662306a36Sopenharmony_ci * @inode:		The inode whose items we want to remove.
44762306a36Sopenharmony_ci * @control:		The btrfs_truncate_control to control how and what we
44862306a36Sopenharmony_ci *			are truncating.
44962306a36Sopenharmony_ci *
45062306a36Sopenharmony_ci * Remove all keys associated with the inode from the given root that have a key
45162306a36Sopenharmony_ci * with a type greater than or equals to @min_type. When @min_type has a value of
45262306a36Sopenharmony_ci * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
45362306a36Sopenharmony_ci * greater than or equals to @new_size. If a file extent item that starts before
45462306a36Sopenharmony_ci * @new_size and ends after it is found, its length is adjusted.
45562306a36Sopenharmony_ci *
45662306a36Sopenharmony_ci * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
45762306a36Sopenharmony_ci * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
45862306a36Sopenharmony_ci */
45962306a36Sopenharmony_ciint btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
46062306a36Sopenharmony_ci			       struct btrfs_root *root,
46162306a36Sopenharmony_ci			       struct btrfs_truncate_control *control)
46262306a36Sopenharmony_ci{
46362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = root->fs_info;
46462306a36Sopenharmony_ci	struct btrfs_path *path;
46562306a36Sopenharmony_ci	struct extent_buffer *leaf;
46662306a36Sopenharmony_ci	struct btrfs_file_extent_item *fi;
46762306a36Sopenharmony_ci	struct btrfs_key key;
46862306a36Sopenharmony_ci	struct btrfs_key found_key;
46962306a36Sopenharmony_ci	u64 new_size = control->new_size;
47062306a36Sopenharmony_ci	u64 extent_num_bytes = 0;
47162306a36Sopenharmony_ci	u64 extent_offset = 0;
47262306a36Sopenharmony_ci	u64 item_end = 0;
47362306a36Sopenharmony_ci	u32 found_type = (u8)-1;
47462306a36Sopenharmony_ci	int del_item;
47562306a36Sopenharmony_ci	int pending_del_nr = 0;
47662306a36Sopenharmony_ci	int pending_del_slot = 0;
47762306a36Sopenharmony_ci	int extent_type = -1;
47862306a36Sopenharmony_ci	int ret;
47962306a36Sopenharmony_ci	u64 bytes_deleted = 0;
48062306a36Sopenharmony_ci	bool be_nice = false;
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	ASSERT(control->inode || !control->clear_extent_range);
48362306a36Sopenharmony_ci	ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY);
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	control->last_size = new_size;
48662306a36Sopenharmony_ci	control->sub_bytes = 0;
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	/*
48962306a36Sopenharmony_ci	 * For shareable roots we want to back off from time to time, this turns
49062306a36Sopenharmony_ci	 * out to be subvolume roots, reloc roots, and data reloc roots.
49162306a36Sopenharmony_ci	 */
49262306a36Sopenharmony_ci	if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
49362306a36Sopenharmony_ci		be_nice = true;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	path = btrfs_alloc_path();
49662306a36Sopenharmony_ci	if (!path)
49762306a36Sopenharmony_ci		return -ENOMEM;
49862306a36Sopenharmony_ci	path->reada = READA_BACK;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	key.objectid = control->ino;
50162306a36Sopenharmony_ci	key.offset = (u64)-1;
50262306a36Sopenharmony_ci	key.type = (u8)-1;
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_cisearch_again:
50562306a36Sopenharmony_ci	/*
50662306a36Sopenharmony_ci	 * With a 16K leaf size and 128MiB extents, you can actually queue up a
50762306a36Sopenharmony_ci	 * huge file in a single leaf.  Most of the time that bytes_deleted is
50862306a36Sopenharmony_ci	 * > 0, it will be huge by the time we get here
50962306a36Sopenharmony_ci	 */
51062306a36Sopenharmony_ci	if (be_nice && bytes_deleted > SZ_32M &&
51162306a36Sopenharmony_ci	    btrfs_should_end_transaction(trans)) {
51262306a36Sopenharmony_ci		ret = -EAGAIN;
51362306a36Sopenharmony_ci		goto out;
51462306a36Sopenharmony_ci	}
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
51762306a36Sopenharmony_ci	if (ret < 0)
51862306a36Sopenharmony_ci		goto out;
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	if (ret > 0) {
52162306a36Sopenharmony_ci		ret = 0;
52262306a36Sopenharmony_ci		/* There are no items in the tree for us to truncate, we're done */
52362306a36Sopenharmony_ci		if (path->slots[0] == 0)
52462306a36Sopenharmony_ci			goto out;
52562306a36Sopenharmony_ci		path->slots[0]--;
52662306a36Sopenharmony_ci	}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	while (1) {
52962306a36Sopenharmony_ci		u64 clear_start = 0, clear_len = 0, extent_start = 0;
53062306a36Sopenharmony_ci		bool refill_delayed_refs_rsv = false;
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci		fi = NULL;
53362306a36Sopenharmony_ci		leaf = path->nodes[0];
53462306a36Sopenharmony_ci		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
53562306a36Sopenharmony_ci		found_type = found_key.type;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci		if (found_key.objectid != control->ino)
53862306a36Sopenharmony_ci			break;
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci		if (found_type < control->min_type)
54162306a36Sopenharmony_ci			break;
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci		item_end = found_key.offset;
54462306a36Sopenharmony_ci		if (found_type == BTRFS_EXTENT_DATA_KEY) {
54562306a36Sopenharmony_ci			fi = btrfs_item_ptr(leaf, path->slots[0],
54662306a36Sopenharmony_ci					    struct btrfs_file_extent_item);
54762306a36Sopenharmony_ci			extent_type = btrfs_file_extent_type(leaf, fi);
54862306a36Sopenharmony_ci			if (extent_type != BTRFS_FILE_EXTENT_INLINE)
54962306a36Sopenharmony_ci				item_end +=
55062306a36Sopenharmony_ci				    btrfs_file_extent_num_bytes(leaf, fi);
55162306a36Sopenharmony_ci			else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
55262306a36Sopenharmony_ci				item_end += btrfs_file_extent_ram_bytes(leaf, fi);
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci			btrfs_trace_truncate(control->inode, leaf, fi,
55562306a36Sopenharmony_ci					     found_key.offset, extent_type,
55662306a36Sopenharmony_ci					     path->slots[0]);
55762306a36Sopenharmony_ci			item_end--;
55862306a36Sopenharmony_ci		}
55962306a36Sopenharmony_ci		if (found_type > control->min_type) {
56062306a36Sopenharmony_ci			del_item = 1;
56162306a36Sopenharmony_ci		} else {
56262306a36Sopenharmony_ci			if (item_end < new_size)
56362306a36Sopenharmony_ci				break;
56462306a36Sopenharmony_ci			if (found_key.offset >= new_size)
56562306a36Sopenharmony_ci				del_item = 1;
56662306a36Sopenharmony_ci			else
56762306a36Sopenharmony_ci				del_item = 0;
56862306a36Sopenharmony_ci		}
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci		/* FIXME, shrink the extent if the ref count is only 1 */
57162306a36Sopenharmony_ci		if (found_type != BTRFS_EXTENT_DATA_KEY)
57262306a36Sopenharmony_ci			goto delete;
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci		control->extents_found++;
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
57762306a36Sopenharmony_ci			u64 num_dec;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci			clear_start = found_key.offset;
58062306a36Sopenharmony_ci			extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
58162306a36Sopenharmony_ci			if (!del_item) {
58262306a36Sopenharmony_ci				u64 orig_num_bytes =
58362306a36Sopenharmony_ci					btrfs_file_extent_num_bytes(leaf, fi);
58462306a36Sopenharmony_ci				extent_num_bytes = ALIGN(new_size -
58562306a36Sopenharmony_ci						found_key.offset,
58662306a36Sopenharmony_ci						fs_info->sectorsize);
58762306a36Sopenharmony_ci				clear_start = ALIGN(new_size, fs_info->sectorsize);
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci				btrfs_set_file_extent_num_bytes(leaf, fi,
59062306a36Sopenharmony_ci							 extent_num_bytes);
59162306a36Sopenharmony_ci				num_dec = (orig_num_bytes - extent_num_bytes);
59262306a36Sopenharmony_ci				if (extent_start != 0)
59362306a36Sopenharmony_ci					control->sub_bytes += num_dec;
59462306a36Sopenharmony_ci				btrfs_mark_buffer_dirty(trans, leaf);
59562306a36Sopenharmony_ci			} else {
59662306a36Sopenharmony_ci				extent_num_bytes =
59762306a36Sopenharmony_ci					btrfs_file_extent_disk_num_bytes(leaf, fi);
59862306a36Sopenharmony_ci				extent_offset = found_key.offset -
59962306a36Sopenharmony_ci					btrfs_file_extent_offset(leaf, fi);
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci				/* FIXME blocksize != 4096 */
60262306a36Sopenharmony_ci				num_dec = btrfs_file_extent_num_bytes(leaf, fi);
60362306a36Sopenharmony_ci				if (extent_start != 0)
60462306a36Sopenharmony_ci					control->sub_bytes += num_dec;
60562306a36Sopenharmony_ci			}
60662306a36Sopenharmony_ci			clear_len = num_dec;
60762306a36Sopenharmony_ci		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
60862306a36Sopenharmony_ci			/*
60962306a36Sopenharmony_ci			 * We can't truncate inline items that have had
61062306a36Sopenharmony_ci			 * special encodings
61162306a36Sopenharmony_ci			 */
61262306a36Sopenharmony_ci			if (!del_item &&
61362306a36Sopenharmony_ci			    btrfs_file_extent_encryption(leaf, fi) == 0 &&
61462306a36Sopenharmony_ci			    btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
61562306a36Sopenharmony_ci			    btrfs_file_extent_compression(leaf, fi) == 0) {
61662306a36Sopenharmony_ci				u32 size = (u32)(new_size - found_key.offset);
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci				btrfs_set_file_extent_ram_bytes(leaf, fi, size);
61962306a36Sopenharmony_ci				size = btrfs_file_extent_calc_inline_size(size);
62062306a36Sopenharmony_ci				btrfs_truncate_item(trans, path, size, 1);
62162306a36Sopenharmony_ci			} else if (!del_item) {
62262306a36Sopenharmony_ci				/*
62362306a36Sopenharmony_ci				 * We have to bail so the last_size is set to
62462306a36Sopenharmony_ci				 * just before this extent.
62562306a36Sopenharmony_ci				 */
62662306a36Sopenharmony_ci				ret = BTRFS_NEED_TRUNCATE_BLOCK;
62762306a36Sopenharmony_ci				break;
62862306a36Sopenharmony_ci			} else {
62962306a36Sopenharmony_ci				/*
63062306a36Sopenharmony_ci				 * Inline extents are special, we just treat
63162306a36Sopenharmony_ci				 * them as a full sector worth in the file
63262306a36Sopenharmony_ci				 * extent tree just for simplicity sake.
63362306a36Sopenharmony_ci				 */
63462306a36Sopenharmony_ci				clear_len = fs_info->sectorsize;
63562306a36Sopenharmony_ci			}
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci			control->sub_bytes += item_end + 1 - new_size;
63862306a36Sopenharmony_ci		}
63962306a36Sopenharmony_cidelete:
64062306a36Sopenharmony_ci		/*
64162306a36Sopenharmony_ci		 * We only want to clear the file extent range if we're
64262306a36Sopenharmony_ci		 * modifying the actual inode's mapping, which is just the
64362306a36Sopenharmony_ci		 * normal truncate path.
64462306a36Sopenharmony_ci		 */
64562306a36Sopenharmony_ci		if (control->clear_extent_range) {
64662306a36Sopenharmony_ci			ret = btrfs_inode_clear_file_extent_range(control->inode,
64762306a36Sopenharmony_ci						  clear_start, clear_len);
64862306a36Sopenharmony_ci			if (ret) {
64962306a36Sopenharmony_ci				btrfs_abort_transaction(trans, ret);
65062306a36Sopenharmony_ci				break;
65162306a36Sopenharmony_ci			}
65262306a36Sopenharmony_ci		}
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci		if (del_item) {
65562306a36Sopenharmony_ci			ASSERT(!pending_del_nr ||
65662306a36Sopenharmony_ci			       ((path->slots[0] + 1) == pending_del_slot));
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci			control->last_size = found_key.offset;
65962306a36Sopenharmony_ci			if (!pending_del_nr) {
66062306a36Sopenharmony_ci				/* No pending yet, add ourselves */
66162306a36Sopenharmony_ci				pending_del_slot = path->slots[0];
66262306a36Sopenharmony_ci				pending_del_nr = 1;
66362306a36Sopenharmony_ci			} else if (path->slots[0] + 1 == pending_del_slot) {
66462306a36Sopenharmony_ci				/* Hop on the pending chunk */
66562306a36Sopenharmony_ci				pending_del_nr++;
66662306a36Sopenharmony_ci				pending_del_slot = path->slots[0];
66762306a36Sopenharmony_ci			}
66862306a36Sopenharmony_ci		} else {
66962306a36Sopenharmony_ci			control->last_size = new_size;
67062306a36Sopenharmony_ci			break;
67162306a36Sopenharmony_ci		}
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci		if (del_item && extent_start != 0 && !control->skip_ref_updates) {
67462306a36Sopenharmony_ci			struct btrfs_ref ref = { 0 };
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci			bytes_deleted += extent_num_bytes;
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci			btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
67962306a36Sopenharmony_ci					extent_start, extent_num_bytes, 0);
68062306a36Sopenharmony_ci			btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
68162306a36Sopenharmony_ci					control->ino, extent_offset,
68262306a36Sopenharmony_ci					root->root_key.objectid, false);
68362306a36Sopenharmony_ci			ret = btrfs_free_extent(trans, &ref);
68462306a36Sopenharmony_ci			if (ret) {
68562306a36Sopenharmony_ci				btrfs_abort_transaction(trans, ret);
68662306a36Sopenharmony_ci				break;
68762306a36Sopenharmony_ci			}
68862306a36Sopenharmony_ci			if (be_nice && btrfs_check_space_for_delayed_refs(fs_info))
68962306a36Sopenharmony_ci				refill_delayed_refs_rsv = true;
69062306a36Sopenharmony_ci		}
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci		if (found_type == BTRFS_INODE_ITEM_KEY)
69362306a36Sopenharmony_ci			break;
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci		if (path->slots[0] == 0 ||
69662306a36Sopenharmony_ci		    path->slots[0] != pending_del_slot ||
69762306a36Sopenharmony_ci		    refill_delayed_refs_rsv) {
69862306a36Sopenharmony_ci			if (pending_del_nr) {
69962306a36Sopenharmony_ci				ret = btrfs_del_items(trans, root, path,
70062306a36Sopenharmony_ci						pending_del_slot,
70162306a36Sopenharmony_ci						pending_del_nr);
70262306a36Sopenharmony_ci				if (ret) {
70362306a36Sopenharmony_ci					btrfs_abort_transaction(trans, ret);
70462306a36Sopenharmony_ci					break;
70562306a36Sopenharmony_ci				}
70662306a36Sopenharmony_ci				pending_del_nr = 0;
70762306a36Sopenharmony_ci			}
70862306a36Sopenharmony_ci			btrfs_release_path(path);
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci			/*
71162306a36Sopenharmony_ci			 * We can generate a lot of delayed refs, so we need to
71262306a36Sopenharmony_ci			 * throttle every once and a while and make sure we're
71362306a36Sopenharmony_ci			 * adding enough space to keep up with the work we are
71462306a36Sopenharmony_ci			 * generating.  Since we hold a transaction here we
71562306a36Sopenharmony_ci			 * can't flush, and we don't want to FLUSH_LIMIT because
71662306a36Sopenharmony_ci			 * we could have generated too many delayed refs to
71762306a36Sopenharmony_ci			 * actually allocate, so just bail if we're short and
71862306a36Sopenharmony_ci			 * let the normal reservation dance happen higher up.
71962306a36Sopenharmony_ci			 */
72062306a36Sopenharmony_ci			if (refill_delayed_refs_rsv) {
72162306a36Sopenharmony_ci				ret = btrfs_delayed_refs_rsv_refill(fs_info,
72262306a36Sopenharmony_ci							BTRFS_RESERVE_NO_FLUSH);
72362306a36Sopenharmony_ci				if (ret) {
72462306a36Sopenharmony_ci					ret = -EAGAIN;
72562306a36Sopenharmony_ci					break;
72662306a36Sopenharmony_ci				}
72762306a36Sopenharmony_ci			}
72862306a36Sopenharmony_ci			goto search_again;
72962306a36Sopenharmony_ci		} else {
73062306a36Sopenharmony_ci			path->slots[0]--;
73162306a36Sopenharmony_ci		}
73262306a36Sopenharmony_ci	}
73362306a36Sopenharmony_ciout:
73462306a36Sopenharmony_ci	if (ret >= 0 && pending_del_nr) {
73562306a36Sopenharmony_ci		int err;
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci		err = btrfs_del_items(trans, root, path, pending_del_slot,
73862306a36Sopenharmony_ci				      pending_del_nr);
73962306a36Sopenharmony_ci		if (err) {
74062306a36Sopenharmony_ci			btrfs_abort_transaction(trans, err);
74162306a36Sopenharmony_ci			ret = err;
74262306a36Sopenharmony_ci		}
74362306a36Sopenharmony_ci	}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	ASSERT(control->last_size >= new_size);
74662306a36Sopenharmony_ci	if (!ret && control->last_size > new_size)
74762306a36Sopenharmony_ci		control->last_size = new_size;
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci	btrfs_free_path(path);
75062306a36Sopenharmony_ci	return ret;
75162306a36Sopenharmony_ci}
752