Commit d9a620f7 authored by Boris Burkov's avatar Boris Burkov Committed by David Sterba

btrfs: new inline ref storing owning subvol of data extents

In order to implement simple quota groups, we need to be able to
associate a data extent with the subvolume that created it. Once you
account for reflink, this information cannot be recovered without
explicitly storing it. Options for storing it are:

- a new key/item
- a new extent inline ref item

The former is backwards compatible, but wastes space, the latter is
incompat, but is efficient in space and reuses the existing inline ref
machinery, while only abusing it a tiny amount -- specifically, the new
item is not a ref, per-se.
Signed-off-by: default avatarBoris Burkov <boris@bur.io>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent cf79ac47
...@@ -358,6 +358,9 @@ BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref, count, 3 ...@@ -358,6 +358,9 @@ BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref, count, 3
BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32); BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);
BTRFS_SETGET_FUNCS(extent_owner_ref_root_id, struct btrfs_extent_owner_ref,
root_id, 64);
BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref, BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
type, 8); type, 8);
BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref, BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
...@@ -374,6 +377,8 @@ static inline u32 btrfs_extent_inline_ref_size(int type) ...@@ -374,6 +377,8 @@ static inline u32 btrfs_extent_inline_ref_size(int type)
if (type == BTRFS_EXTENT_DATA_REF_KEY) if (type == BTRFS_EXTENT_DATA_REF_KEY)
return sizeof(struct btrfs_extent_data_ref) + return sizeof(struct btrfs_extent_data_ref) +
offsetof(struct btrfs_extent_inline_ref, offset); offsetof(struct btrfs_extent_inline_ref, offset);
if (type == BTRFS_EXTENT_OWNER_REF_KEY)
return sizeof(struct btrfs_extent_inline_ref);
return 0; return 0;
} }
......
...@@ -1129,6 +1129,9 @@ static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx, ...@@ -1129,6 +1129,9 @@ static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx,
count, sc, GFP_NOFS); count, sc, GFP_NOFS);
break; break;
} }
case BTRFS_EXTENT_OWNER_REF_KEY:
ASSERT(btrfs_fs_incompat(ctx->fs_info, SIMPLE_QUOTA));
break;
default: default:
WARN_ON(1); WARN_ON(1);
} }
......
...@@ -345,9 +345,15 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, ...@@ -345,9 +345,15 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
struct btrfs_extent_inline_ref *iref, struct btrfs_extent_inline_ref *iref,
enum btrfs_inline_ref_type is_data) enum btrfs_inline_ref_type is_data)
{ {
struct btrfs_fs_info *fs_info = eb->fs_info;
int type = btrfs_extent_inline_ref_type(eb, iref); int type = btrfs_extent_inline_ref_type(eb, iref);
u64 offset = btrfs_extent_inline_ref_offset(eb, iref); u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
return type;
}
if (type == BTRFS_TREE_BLOCK_REF_KEY || if (type == BTRFS_TREE_BLOCK_REF_KEY ||
type == BTRFS_SHARED_BLOCK_REF_KEY || type == BTRFS_SHARED_BLOCK_REF_KEY ||
type == BTRFS_SHARED_DATA_REF_KEY || type == BTRFS_SHARED_DATA_REF_KEY ||
...@@ -356,26 +362,25 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, ...@@ -356,26 +362,25 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
if (type == BTRFS_TREE_BLOCK_REF_KEY) if (type == BTRFS_TREE_BLOCK_REF_KEY)
return type; return type;
if (type == BTRFS_SHARED_BLOCK_REF_KEY) { if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
ASSERT(eb->fs_info); ASSERT(fs_info);
/* /*
* Every shared one has parent tree block, * Every shared one has parent tree block,
* which must be aligned to sector size. * which must be aligned to sector size.
*/ */
if (offset && if (offset && IS_ALIGNED(offset, fs_info->sectorsize))
IS_ALIGNED(offset, eb->fs_info->sectorsize))
return type; return type;
} }
} else if (is_data == BTRFS_REF_TYPE_DATA) { } else if (is_data == BTRFS_REF_TYPE_DATA) {
if (type == BTRFS_EXTENT_DATA_REF_KEY) if (type == BTRFS_EXTENT_DATA_REF_KEY)
return type; return type;
if (type == BTRFS_SHARED_DATA_REF_KEY) { if (type == BTRFS_SHARED_DATA_REF_KEY) {
ASSERT(eb->fs_info); ASSERT(fs_info);
/* /*
* Every shared one has parent tree block, * Every shared one has parent tree block,
* which must be aligned to sector size. * which must be aligned to sector size.
*/ */
if (offset && if (offset &&
IS_ALIGNED(offset, eb->fs_info->sectorsize)) IS_ALIGNED(offset, fs_info->sectorsize))
return type; return type;
} }
} else { } else {
...@@ -386,7 +391,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, ...@@ -386,7 +391,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
WARN_ON(1); WARN_ON(1);
btrfs_print_leaf(eb); btrfs_print_leaf(eb);
btrfs_err(eb->fs_info, btrfs_err(fs_info,
"eb %llu iref 0x%lx invalid extent inline ref type %d", "eb %llu iref 0x%lx invalid extent inline ref type %d",
eb->start, (unsigned long)iref, type); eb->start, (unsigned long)iref, type);
...@@ -887,6 +892,11 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, ...@@ -887,6 +892,11 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
while (ptr < end) { while (ptr < end) {
iref = (struct btrfs_extent_inline_ref *)ptr; iref = (struct btrfs_extent_inline_ref *)ptr;
type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
ptr += btrfs_extent_inline_ref_size(type);
continue;
}
if (type == BTRFS_REF_TYPE_INVALID) { if (type == BTRFS_REF_TYPE_INVALID) {
ret = -EUCLEAN; ret = -EUCLEAN;
goto out; goto out;
...@@ -1742,6 +1752,8 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans, ...@@ -1742,6 +1752,8 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
node->type == BTRFS_SHARED_DATA_REF_KEY) node->type == BTRFS_SHARED_DATA_REF_KEY)
ret = run_delayed_data_ref(trans, node, extent_op, ret = run_delayed_data_ref(trans, node, extent_op,
insert_reserved); insert_reserved);
else if (node->type == BTRFS_EXTENT_OWNER_REF_KEY)
ret = 0;
else else
BUG(); BUG();
if (ret && insert_reserved) if (ret && insert_reserved)
...@@ -2313,6 +2325,7 @@ static noinline int check_committed_ref(struct btrfs_root *root, ...@@ -2313,6 +2325,7 @@ static noinline int check_committed_ref(struct btrfs_root *root,
struct btrfs_extent_item *ei; struct btrfs_extent_item *ei;
struct btrfs_key key; struct btrfs_key key;
u32 item_size; u32 item_size;
u32 expected_size;
int type; int type;
int ret; int ret;
...@@ -2339,10 +2352,22 @@ static noinline int check_committed_ref(struct btrfs_root *root, ...@@ -2339,10 +2352,22 @@ static noinline int check_committed_ref(struct btrfs_root *root,
ret = 1; ret = 1;
item_size = btrfs_item_size(leaf, path->slots[0]); item_size = btrfs_item_size(leaf, path->slots[0]);
ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
expected_size = sizeof(*ei) + btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY);
/* No inline refs; we need to bail before checking for owner ref. */
if (item_size == sizeof(*ei))
goto out;
/* Check for an owner ref; skip over it to the real inline refs. */
iref = (struct btrfs_extent_inline_ref *)(ei + 1);
type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
if (btrfs_fs_incompat(fs_info, SIMPLE_QUOTA) && type == BTRFS_EXTENT_OWNER_REF_KEY) {
expected_size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
iref = (struct btrfs_extent_inline_ref *)(iref + 1);
}
/* If extent item has more than 1 inline ref then it's shared */ /* If extent item has more than 1 inline ref then it's shared */
if (item_size != sizeof(*ei) + if (item_size != expected_size)
btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
goto out; goto out;
/* /*
...@@ -2354,8 +2379,6 @@ static noinline int check_committed_ref(struct btrfs_root *root, ...@@ -2354,8 +2379,6 @@ static noinline int check_committed_ref(struct btrfs_root *root,
btrfs_root_last_snapshot(&root->root_item))) btrfs_root_last_snapshot(&root->root_item)))
goto out; goto out;
iref = (struct btrfs_extent_inline_ref *)(ei + 1);
/* If this extent has SHARED_DATA_REF then it's shared */ /* If this extent has SHARED_DATA_REF then it's shared */
type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
if (type != BTRFS_EXTENT_DATA_REF_KEY) if (type != BTRFS_EXTENT_DATA_REF_KEY)
...@@ -4617,18 +4640,23 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, ...@@ -4617,18 +4640,23 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *extent_root; struct btrfs_root *extent_root;
int ret; int ret;
struct btrfs_extent_item *extent_item; struct btrfs_extent_item *extent_item;
struct btrfs_extent_owner_ref *oref;
struct btrfs_extent_inline_ref *iref; struct btrfs_extent_inline_ref *iref;
struct btrfs_path *path; struct btrfs_path *path;
struct extent_buffer *leaf; struct extent_buffer *leaf;
int type; int type;
u32 size; u32 size;
const bool simple_quota = (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
if (parent > 0) if (parent > 0)
type = BTRFS_SHARED_DATA_REF_KEY; type = BTRFS_SHARED_DATA_REF_KEY;
else else
type = BTRFS_EXTENT_DATA_REF_KEY; type = BTRFS_EXTENT_DATA_REF_KEY;
size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); size = sizeof(*extent_item);
if (simple_quota)
size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
size += btrfs_extent_inline_ref_size(type);
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) if (!path)
...@@ -4650,7 +4678,14 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, ...@@ -4650,7 +4678,14 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
flags | BTRFS_EXTENT_FLAG_DATA); flags | BTRFS_EXTENT_FLAG_DATA);
iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
if (simple_quota) {
btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_EXTENT_OWNER_REF_KEY);
oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
btrfs_set_extent_owner_ref_root_id(leaf, oref, root_objectid);
iref = (struct btrfs_extent_inline_ref *)(oref + 1);
}
btrfs_set_extent_inline_ref_type(leaf, iref, type); btrfs_set_extent_inline_ref_type(leaf, iref, type);
if (parent > 0) { if (parent > 0) {
struct btrfs_shared_data_ref *ref; struct btrfs_shared_data_ref *ref;
ref = (struct btrfs_shared_data_ref *)(iref + 1); ref = (struct btrfs_shared_data_ref *)(iref + 1);
......
...@@ -83,12 +83,20 @@ static void print_extent_data_ref(const struct extent_buffer *eb, ...@@ -83,12 +83,20 @@ static void print_extent_data_ref(const struct extent_buffer *eb,
btrfs_extent_data_ref_count(eb, ref)); btrfs_extent_data_ref_count(eb, ref));
} }
static void print_extent_owner_ref(const struct extent_buffer *eb,
const struct btrfs_extent_owner_ref *ref)
{
ASSERT(btrfs_fs_incompat(eb->fs_info, SIMPLE_QUOTA));
pr_cont("extent data owner root %llu\n", btrfs_extent_owner_ref_root_id(eb, ref));
}
static void print_extent_item(const struct extent_buffer *eb, int slot, int type) static void print_extent_item(const struct extent_buffer *eb, int slot, int type)
{ {
struct btrfs_extent_item *ei; struct btrfs_extent_item *ei;
struct btrfs_extent_inline_ref *iref; struct btrfs_extent_inline_ref *iref;
struct btrfs_extent_data_ref *dref; struct btrfs_extent_data_ref *dref;
struct btrfs_shared_data_ref *sref; struct btrfs_shared_data_ref *sref;
struct btrfs_extent_owner_ref *oref;
struct btrfs_disk_key key; struct btrfs_disk_key key;
unsigned long end; unsigned long end;
unsigned long ptr; unsigned long ptr;
...@@ -164,6 +172,10 @@ static void print_extent_item(const struct extent_buffer *eb, int slot, int type ...@@ -164,6 +172,10 @@ static void print_extent_item(const struct extent_buffer *eb, int slot, int type
"\t\t\t(parent %llu not aligned to sectorsize %u)\n", "\t\t\t(parent %llu not aligned to sectorsize %u)\n",
offset, eb->fs_info->sectorsize); offset, eb->fs_info->sectorsize);
break; break;
case BTRFS_EXTENT_OWNER_REF_KEY:
oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
print_extent_owner_ref(eb, oref);
break;
default: default:
pr_cont("(extent %llu has INVALID ref type %d)\n", pr_cont("(extent %llu has INVALID ref type %d)\n",
eb->start, type); eb->start, type);
......
...@@ -485,6 +485,9 @@ static int process_extent_item(struct btrfs_fs_info *fs_info, ...@@ -485,6 +485,9 @@ static int process_extent_item(struct btrfs_fs_info *fs_info,
ret = add_shared_data_ref(fs_info, offset, count, ret = add_shared_data_ref(fs_info, offset, count,
key->objectid, key->offset); key->objectid, key->offset);
break; break;
case BTRFS_EXTENT_OWNER_REF_KEY:
WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
break;
default: default:
btrfs_err(fs_info, "invalid key type in iref"); btrfs_err(fs_info, "invalid key type in iref");
ret = -EINVAL; ret = -EINVAL;
......
...@@ -1467,6 +1467,9 @@ static int check_extent_item(struct extent_buffer *leaf, ...@@ -1467,6 +1467,9 @@ static int check_extent_item(struct extent_buffer *leaf,
} }
inline_refs += btrfs_shared_data_ref_count(leaf, sref); inline_refs += btrfs_shared_data_ref_count(leaf, sref);
break; break;
case BTRFS_EXTENT_OWNER_REF_KEY:
WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
break;
default: default:
extent_err(leaf, slot, "unknown inline ref type: %u", extent_err(leaf, slot, "unknown inline ref type: %u",
inline_type); inline_type);
......
...@@ -233,6 +233,14 @@ ...@@ -233,6 +233,14 @@
#define BTRFS_SHARED_DATA_REF_KEY 184 #define BTRFS_SHARED_DATA_REF_KEY 184
/*
* Special inline ref key which stores the id of the subvolume which originally
* created the extent. This subvolume owns the extent permanently from the
* perspective of simple quotas. Needed to know which subvolume to free quota
* usage from when the extent is deleted.
*/
#define BTRFS_EXTENT_OWNER_REF_KEY 188
/* /*
* block groups give us hints into the extent allocation trees. Which * block groups give us hints into the extent allocation trees. Which
* blocks are free etc etc * blocks are free etc etc
...@@ -816,6 +824,10 @@ struct btrfs_shared_data_ref { ...@@ -816,6 +824,10 @@ struct btrfs_shared_data_ref {
__le32 count; __le32 count;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
struct btrfs_extent_owner_ref {
__le64 root_id;
} __attribute__ ((__packed__));
struct btrfs_extent_inline_ref { struct btrfs_extent_inline_ref {
__u8 type; __u8 type;
__le64 offset; __le64 offset;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment