Skip to content

Commit 27cdeb7

Browse files
Miao Xiemasoncl
authored andcommitted
Btrfs: use bitfield instead of integer data type for the some variants in btrfs_root
Signed-off-by: Miao Xie <[email protected]> Signed-off-by: Wang Shilong <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent f959492 commit 27cdeb7

File tree

12 files changed

+109
-94
lines changed

12 files changed

+109
-94
lines changed

fs/btrfs/ctree.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
224224
static void add_root_to_dirty_list(struct btrfs_root *root)
225225
{
226226
spin_lock(&root->fs_info->trans_lock);
227-
if (root->track_dirty && list_empty(&root->dirty_list)) {
227+
if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
228+
list_empty(&root->dirty_list)) {
228229
list_add(&root->dirty_list,
229230
&root->fs_info->dirty_cowonly_roots);
230231
}
@@ -246,9 +247,10 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
246247
int level;
247248
struct btrfs_disk_key disk_key;
248249

249-
WARN_ON(root->ref_cows && trans->transid !=
250-
root->fs_info->running_transaction->transid);
251-
WARN_ON(root->ref_cows && trans->transid != root->last_trans);
250+
WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
251+
trans->transid != root->fs_info->running_transaction->transid);
252+
WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
253+
trans->transid != root->last_trans);
252254

253255
level = btrfs_header_level(buf);
254256
if (level == 0)
@@ -997,14 +999,14 @@ int btrfs_block_can_be_shared(struct btrfs_root *root,
997999
* snapshot and the block was not allocated by tree relocation,
9981000
* we know the block is not shared.
9991001
*/
1000-
if (root->ref_cows &&
1002+
if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
10011003
buf != root->node && buf != root->commit_root &&
10021004
(btrfs_header_generation(buf) <=
10031005
btrfs_root_last_snapshot(&root->root_item) ||
10041006
btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
10051007
return 1;
10061008
#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1007-
if (root->ref_cows &&
1009+
if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
10081010
btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
10091011
return 1;
10101012
#endif
@@ -1146,9 +1148,10 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
11461148

11471149
btrfs_assert_tree_locked(buf);
11481150

1149-
WARN_ON(root->ref_cows && trans->transid !=
1150-
root->fs_info->running_transaction->transid);
1151-
WARN_ON(root->ref_cows && trans->transid != root->last_trans);
1151+
WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1152+
trans->transid != root->fs_info->running_transaction->transid);
1153+
WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1154+
trans->transid != root->last_trans);
11521155

11531156
level = btrfs_header_level(buf);
11541157

@@ -1193,7 +1196,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
11931196
return ret;
11941197
}
11951198

1196-
if (root->ref_cows) {
1199+
if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
11971200
ret = btrfs_reloc_cow_block(trans, root, buf, cow);
11981201
if (ret)
11991202
return ret;
@@ -1556,7 +1559,7 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans,
15561559
!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
15571560
!(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
15581561
btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1559-
!root->force_cow)
1562+
!test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
15601563
return 0;
15611564
return 1;
15621565
}

fs/btrfs/ctree.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,26 @@ struct btrfs_subvolume_writers {
17101710
wait_queue_head_t wait;
17111711
};
17121712

1713+
/*
1714+
* The state of btrfs root
1715+
*/
1716+
/*
1717+
* btrfs_record_root_in_trans is a multi-step process,
1718+
* and it can race with the balancing code. But the
1719+
* race is very small, and only the first time the root
1720+
* is added to each transaction. So IN_TRANS_SETUP
1721+
* is used to tell us when more checks are required
1722+
*/
1723+
#define BTRFS_ROOT_IN_TRANS_SETUP 0
1724+
#define BTRFS_ROOT_REF_COWS 1
1725+
#define BTRFS_ROOT_TRACK_DIRTY 2
1726+
#define BTRFS_ROOT_IN_RADIX 3
1727+
#define BTRFS_ROOT_DUMMY_ROOT 4
1728+
#define BTRFS_ROOT_ORPHAN_ITEM_INSERTED 5
1729+
#define BTRFS_ROOT_DEFRAG_RUNNING 6
1730+
#define BTRFS_ROOT_FORCE_COW 7
1731+
#define BTRFS_ROOT_MULTI_LOG_TASKS 8
1732+
17131733
/*
17141734
* in ram representation of the tree. extent_root is used for all allocations
17151735
* and for the extent tree extent_root root.
@@ -1721,6 +1741,7 @@ struct btrfs_root {
17211741
struct btrfs_root *log_root;
17221742
struct btrfs_root *reloc_root;
17231743

1744+
unsigned long state;
17241745
struct btrfs_root_item root_item;
17251746
struct btrfs_key root_key;
17261747
struct btrfs_fs_info *fs_info;
@@ -1755,7 +1776,6 @@ struct btrfs_root {
17551776
/* Just be updated when the commit succeeds. */
17561777
int last_log_commit;
17571778
pid_t log_start_pid;
1758-
bool log_multiple_pids;
17591779

17601780
u64 objectid;
17611781
u64 last_trans;
@@ -1775,23 +1795,9 @@ struct btrfs_root {
17751795

17761796
u64 highest_objectid;
17771797

1778-
/* btrfs_record_root_in_trans is a multi-step process,
1779-
* and it can race with the balancing code. But the
1780-
* race is very small, and only the first time the root
1781-
* is added to each transaction. So in_trans_setup
1782-
* is used to tell us when more checks are required
1783-
*/
1784-
unsigned long in_trans_setup;
1785-
int ref_cows;
1786-
int track_dirty;
1787-
int in_radix;
1788-
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1789-
int dummy_root;
1790-
#endif
17911798
u64 defrag_trans_start;
17921799
struct btrfs_key defrag_progress;
17931800
struct btrfs_key defrag_max;
1794-
int defrag_running;
17951801
char *name;
17961802

17971803
/* the dirty list is only used by non-reference counted roots */
@@ -1805,7 +1811,6 @@ struct btrfs_root {
18051811
spinlock_t orphan_lock;
18061812
atomic_t orphan_inodes;
18071813
struct btrfs_block_rsv *orphan_block_rsv;
1808-
int orphan_item_inserted;
18091814
int orphan_cleanup_state;
18101815

18111816
spinlock_t inode_lock;
@@ -1823,8 +1828,6 @@ struct btrfs_root {
18231828
*/
18241829
dev_t anon_dev;
18251830

1826-
int force_cow;
1827-
18281831
spinlock_t root_item_lock;
18291832
atomic_t refs;
18301833

fs/btrfs/disk-io.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,10 +1201,7 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
12011201
root->nodesize = nodesize;
12021202
root->leafsize = leafsize;
12031203
root->stripesize = stripesize;
1204-
root->ref_cows = 0;
1205-
root->track_dirty = 0;
1206-
root->in_radix = 0;
1207-
root->orphan_item_inserted = 0;
1204+
root->state = 0;
12081205
root->orphan_cleanup_state = 0;
12091206

12101207
root->objectid = objectid;
@@ -1265,7 +1262,6 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
12651262
else
12661263
root->defrag_trans_start = 0;
12671264
init_completion(&root->kobj_unregister);
1268-
root->defrag_running = 0;
12691265
root->root_key.objectid = objectid;
12701266
root->anon_dev = 0;
12711267

@@ -1290,7 +1286,7 @@ struct btrfs_root *btrfs_alloc_dummy_root(void)
12901286
if (!root)
12911287
return ERR_PTR(-ENOMEM);
12921288
__setup_root(4096, 4096, 4096, 4096, root, NULL, 1);
1293-
root->dummy_root = 1;
1289+
set_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state);
12941290

12951291
return root;
12961292
}
@@ -1341,8 +1337,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
13411337
btrfs_mark_buffer_dirty(leaf);
13421338

13431339
root->commit_root = btrfs_root_node(root);
1344-
root->track_dirty = 1;
1345-
1340+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
13461341

13471342
root->root_item.flags = 0;
13481343
root->root_item.byte_limit = 0;
@@ -1396,13 +1391,15 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
13961391
root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
13971392
root->root_key.type = BTRFS_ROOT_ITEM_KEY;
13981393
root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
1394+
13991395
/*
1396+
* DON'T set REF_COWS for log trees
1397+
*
14001398
* log trees do not get reference counted because they go away
14011399
* before a real commit is actually done. They do store pointers
14021400
* to file data extents, and those reference counts still get
14031401
* updated (along with back refs to the log tree).
14041402
*/
1405-
root->ref_cows = 0;
14061403

14071404
leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
14081405
BTRFS_TREE_LOG_OBJECTID, NULL,
@@ -1536,7 +1533,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
15361533
return root;
15371534

15381535
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
1539-
root->ref_cows = 1;
1536+
set_bit(BTRFS_ROOT_REF_COWS, &root->state);
15401537
btrfs_check_and_init_root_item(&root->root_item);
15411538
}
15421539

@@ -1606,7 +1603,7 @@ int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
16061603
(unsigned long)root->root_key.objectid,
16071604
root);
16081605
if (ret == 0)
1609-
root->in_radix = 1;
1606+
set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
16101607
spin_unlock(&fs_info->fs_roots_radix_lock);
16111608
radix_tree_preload_end();
16121609

@@ -1662,7 +1659,7 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
16621659
if (ret < 0)
16631660
goto fail;
16641661
if (ret == 0)
1665-
root->orphan_item_inserted = 1;
1662+
set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
16661663

16671664
ret = btrfs_insert_fs_root(fs_info, root);
16681665
if (ret) {
@@ -2101,7 +2098,7 @@ static void del_fs_roots(struct btrfs_fs_info *fs_info)
21012098
struct btrfs_root, root_list);
21022099
list_del(&gang[0]->root_list);
21032100

2104-
if (gang[0]->in_radix) {
2101+
if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
21052102
btrfs_drop_and_free_fs_root(fs_info, gang[0]);
21062103
} else {
21072104
free_extent_buffer(gang[0]->node);
@@ -2694,7 +2691,7 @@ int open_ctree(struct super_block *sb,
26942691
ret = PTR_ERR(extent_root);
26952692
goto recovery_tree_root;
26962693
}
2697-
extent_root->track_dirty = 1;
2694+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &extent_root->state);
26982695
fs_info->extent_root = extent_root;
26992696

27002697
location.objectid = BTRFS_DEV_TREE_OBJECTID;
@@ -2703,7 +2700,7 @@ int open_ctree(struct super_block *sb,
27032700
ret = PTR_ERR(dev_root);
27042701
goto recovery_tree_root;
27052702
}
2706-
dev_root->track_dirty = 1;
2703+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &dev_root->state);
27072704
fs_info->dev_root = dev_root;
27082705
btrfs_init_devices_late(fs_info);
27092706

@@ -2713,13 +2710,13 @@ int open_ctree(struct super_block *sb,
27132710
ret = PTR_ERR(csum_root);
27142711
goto recovery_tree_root;
27152712
}
2716-
csum_root->track_dirty = 1;
2713+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &csum_root->state);
27172714
fs_info->csum_root = csum_root;
27182715

27192716
location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
27202717
quota_root = btrfs_read_tree_root(tree_root, &location);
27212718
if (!IS_ERR(quota_root)) {
2722-
quota_root->track_dirty = 1;
2719+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &quota_root->state);
27232720
fs_info->quota_enabled = 1;
27242721
fs_info->pending_quota_state = 1;
27252722
fs_info->quota_root = quota_root;
@@ -2734,7 +2731,7 @@ int open_ctree(struct super_block *sb,
27342731
create_uuid_tree = true;
27352732
check_uuid_tree = false;
27362733
} else {
2737-
uuid_root->track_dirty = 1;
2734+
set_bit(BTRFS_ROOT_TRACK_DIRTY, &uuid_root->state);
27382735
fs_info->uuid_root = uuid_root;
27392736
create_uuid_tree = false;
27402737
check_uuid_tree =

fs/btrfs/extent-tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,7 +2983,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
29832983
nritems = btrfs_header_nritems(buf);
29842984
level = btrfs_header_level(buf);
29852985

2986-
if (!root->ref_cows && level == 0)
2986+
if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
29872987
return 0;
29882988

29892989
if (inc)
@@ -4472,7 +4472,7 @@ static struct btrfs_block_rsv *get_block_rsv(
44724472
{
44734473
struct btrfs_block_rsv *block_rsv = NULL;
44744474

4475-
if (root->ref_cows)
4475+
if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
44764476
block_rsv = trans->block_rsv;
44774477

44784478
if (root == root->fs_info->csum_root && trans->adding_csums)
@@ -7838,7 +7838,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
78387838
}
78397839
}
78407840

7841-
if (root->in_radix) {
7841+
if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
78427842
btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
78437843
} else {
78447844
free_extent_buffer(root->node);

fs/btrfs/file.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
714714
int recow;
715715
int ret;
716716
int modify_tree = -1;
717-
int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
717+
int update_refs;
718718
int found = 0;
719719
int leafs_visited = 0;
720720

@@ -724,6 +724,8 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
724724
if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
725725
modify_tree = 0;
726726

727+
update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
728+
root == root->fs_info->tree_root);
727729
while (1) {
728730
recow = 0;
729731
ret = btrfs_lookup_file_extent(trans, root, path, ino,

0 commit comments

Comments
 (0)