Skip to content

Commit cb93b52

Browse files
Qu Wenruomasoncl
authored andcommitted
btrfs: qgroup: Refactor btrfs_qgroup_insert_dirty_extent()
Refactor btrfs_qgroup_insert_dirty_extent() function, to two functions: 1. btrfs_qgroup_insert_dirty_extent_nolock() Almost the same with original code. For delayed_ref usage, which has delayed refs locked. Change the return value type to int, since caller never needs the pointer, but only needs to know if they need to free the allocated memory. 2. btrfs_qgroup_insert_dirty_extent() The more encapsulated version. Will do the delayed_refs lock, memory allocation, quota enabled check and other things. The original design is to keep exported functions to minimal, but since more btrfs hacks exposed, like replacing path in balance, we need to record dirty extents manually, so we have to add such functions. Also, add comment for both functions, to info developers how to keep qgroup correct when doing hacks. Cc: Mark Fasheh <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Reviewed-and-Tested-by: Goldwyn Rodrigues <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent d06f23d commit cb93b52

File tree

4 files changed

+71
-47
lines changed

4 files changed

+71
-47
lines changed

fs/btrfs/delayed-ref.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,6 @@ add_delayed_ref_head(struct btrfs_fs_info *fs_info,
541541
struct btrfs_delayed_ref_head *existing;
542542
struct btrfs_delayed_ref_head *head_ref = NULL;
543543
struct btrfs_delayed_ref_root *delayed_refs;
544-
struct btrfs_qgroup_extent_record *qexisting;
545544
int count_mod = 1;
546545
int must_insert_reserved = 0;
547546

@@ -606,10 +605,8 @@ add_delayed_ref_head(struct btrfs_fs_info *fs_info,
606605
qrecord->num_bytes = num_bytes;
607606
qrecord->old_roots = NULL;
608607

609-
qexisting = btrfs_qgroup_insert_dirty_extent(fs_info,
610-
delayed_refs,
611-
qrecord);
612-
if (qexisting)
608+
if(btrfs_qgroup_insert_dirty_extent_nolock(fs_info,
609+
delayed_refs, qrecord))
613610
kfree(qrecord);
614611
}
615612

fs/btrfs/extent-tree.c

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8521,35 +8521,6 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
85218521
wc->reada_slot = slot;
85228522
}
85238523

8524-
/*
8525-
* These may not be seen by the usual inc/dec ref code so we have to
8526-
* add them here.
8527-
*/
8528-
static int record_one_subtree_extent(struct btrfs_trans_handle *trans,
8529-
struct btrfs_root *root, u64 bytenr,
8530-
u64 num_bytes)
8531-
{
8532-
struct btrfs_qgroup_extent_record *qrecord;
8533-
struct btrfs_delayed_ref_root *delayed_refs;
8534-
8535-
qrecord = kmalloc(sizeof(*qrecord), GFP_NOFS);
8536-
if (!qrecord)
8537-
return -ENOMEM;
8538-
8539-
qrecord->bytenr = bytenr;
8540-
qrecord->num_bytes = num_bytes;
8541-
qrecord->old_roots = NULL;
8542-
8543-
delayed_refs = &trans->transaction->delayed_refs;
8544-
spin_lock(&delayed_refs->lock);
8545-
if (btrfs_qgroup_insert_dirty_extent(trans->fs_info,
8546-
delayed_refs, qrecord))
8547-
kfree(qrecord);
8548-
spin_unlock(&delayed_refs->lock);
8549-
8550-
return 0;
8551-
}
8552-
85538524
static int account_leaf_items(struct btrfs_trans_handle *trans,
85548525
struct btrfs_root *root,
85558526
struct extent_buffer *eb)
@@ -8583,7 +8554,8 @@ static int account_leaf_items(struct btrfs_trans_handle *trans,
85838554

85848555
num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
85858556

8586-
ret = record_one_subtree_extent(trans, root, bytenr, num_bytes);
8557+
ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
8558+
bytenr, num_bytes, GFP_NOFS);
85878559
if (ret)
85888560
return ret;
85898561
}
@@ -8732,8 +8704,9 @@ static int account_shared_subtree(struct btrfs_trans_handle *trans,
87328704
btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
87338705
path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
87348706

8735-
ret = record_one_subtree_extent(trans, root, child_bytenr,
8736-
root->nodesize);
8707+
ret = btrfs_qgroup_insert_dirty_extent(trans,
8708+
root->fs_info, child_bytenr,
8709+
root->nodesize, GFP_NOFS);
87378710
if (ret)
87388711
goto out;
87398712
}

fs/btrfs/qgroup.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,10 +1453,9 @@ int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
14531453
return ret;
14541454
}
14551455

1456-
struct btrfs_qgroup_extent_record *
1457-
btrfs_qgroup_insert_dirty_extent(struct btrfs_fs_info *fs_info,
1458-
struct btrfs_delayed_ref_root *delayed_refs,
1459-
struct btrfs_qgroup_extent_record *record)
1456+
int btrfs_qgroup_insert_dirty_extent_nolock(struct btrfs_fs_info *fs_info,
1457+
struct btrfs_delayed_ref_root *delayed_refs,
1458+
struct btrfs_qgroup_extent_record *record)
14601459
{
14611460
struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
14621461
struct rb_node *parent_node = NULL;
@@ -1475,12 +1474,42 @@ btrfs_qgroup_insert_dirty_extent(struct btrfs_fs_info *fs_info,
14751474
else if (bytenr > entry->bytenr)
14761475
p = &(*p)->rb_right;
14771476
else
1478-
return entry;
1477+
return 1;
14791478
}
14801479

14811480
rb_link_node(&record->node, parent_node, p);
14821481
rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1483-
return NULL;
1482+
return 0;
1483+
}
1484+
1485+
int btrfs_qgroup_insert_dirty_extent(struct btrfs_trans_handle *trans,
1486+
struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1487+
gfp_t gfp_flag)
1488+
{
1489+
struct btrfs_qgroup_extent_record *record;
1490+
struct btrfs_delayed_ref_root *delayed_refs;
1491+
int ret;
1492+
1493+
if (!fs_info->quota_enabled || bytenr == 0 || num_bytes == 0)
1494+
return 0;
1495+
if (WARN_ON(trans == NULL))
1496+
return -EINVAL;
1497+
record = kmalloc(sizeof(*record), gfp_flag);
1498+
if (!record)
1499+
return -ENOMEM;
1500+
1501+
delayed_refs = &trans->transaction->delayed_refs;
1502+
record->bytenr = bytenr;
1503+
record->num_bytes = num_bytes;
1504+
record->old_roots = NULL;
1505+
1506+
spin_lock(&delayed_refs->lock);
1507+
ret = btrfs_qgroup_insert_dirty_extent_nolock(fs_info, delayed_refs,
1508+
record);
1509+
spin_unlock(&delayed_refs->lock);
1510+
if (ret > 0)
1511+
kfree(record);
1512+
return 0;
14841513
}
14851514

14861515
#define UPDATE_NEW 0

fs/btrfs/qgroup.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,35 @@ void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
6464
struct btrfs_delayed_extent_op;
6565
int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
6666
struct btrfs_fs_info *fs_info);
67-
struct btrfs_qgroup_extent_record *
68-
btrfs_qgroup_insert_dirty_extent(struct btrfs_fs_info *fs_info,
69-
struct btrfs_delayed_ref_root *delayed_refs,
70-
struct btrfs_qgroup_extent_record *record);
67+
/*
68+
* Insert one dirty extent record into @delayed_refs, informing qgroup to
69+
* account that extent at commit trans time.
70+
*
71+
* No lock version, caller must acquire delayed ref lock and allocate memory.
72+
*
73+
* Return 0 for success insert
74+
* Return >0 for existing record, caller can free @record safely.
75+
* Error is not possible
76+
*/
77+
int btrfs_qgroup_insert_dirty_extent_nolock(
78+
struct btrfs_fs_info *fs_info,
79+
struct btrfs_delayed_ref_root *delayed_refs,
80+
struct btrfs_qgroup_extent_record *record);
81+
82+
/*
83+
* Insert one dirty extent record into @delayed_refs, informing qgroup to
84+
* account that extent at commit trans time.
85+
*
86+
* Better encapsulated version.
87+
*
88+
* Return 0 if the operation is done.
89+
* Return <0 for error, like memory allocation failure or invalid parameter
90+
* (NULL trans)
91+
*/
92+
int btrfs_qgroup_insert_dirty_extent(struct btrfs_trans_handle *trans,
93+
struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
94+
gfp_t gfp_flag);
95+
7196
int
7297
btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
7398
struct btrfs_fs_info *fs_info,

0 commit comments

Comments
 (0)