Skip to content

Commit 6527cdb

Browse files
Zheng Yanchrismason-xx
authored andcommitted
Btrfs: Update find free objectid function for orphan cleanup code
Orphan items use BTRFS_ORPHAN_OBJECTID (-5UUL) as key objectid. This affects the find free objectid functions, inode objectid can easily overflow after orphan file cleanup. --- Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent b214107 commit 6527cdb

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ struct btrfs_ordered_sum;
8282
#define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
8383

8484
/*
85-
* All files have objectids higher than this.
85+
* All files have objectids in this range.
8686
*/
8787
#define BTRFS_FIRST_FREE_OBJECTID 256ULL
88+
#define BTRFS_LAST_FREE_OBJECTID -256ULL
8889
#define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
8990

9091

‎fs/btrfs/inode-map.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
3232
path = btrfs_alloc_path();
3333
BUG_ON(!path);
3434

35-
search_key.objectid = (u64)-1;
35+
search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
36+
search_key.type = -1;
3637
search_key.offset = (u64)-1;
3738
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
3839
if (ret < 0)
@@ -70,16 +71,17 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
7071
u64 search_start = dirid;
7172

7273
mutex_lock(&root->objectid_mutex);
73-
if (root->last_inode_alloc) {
74+
if (root->last_inode_alloc >= BTRFS_FIRST_FREE_OBJECTID &&
75+
root->last_inode_alloc < BTRFS_LAST_FREE_OBJECTID) {
7476
*objectid = ++root->last_inode_alloc;
7577
mutex_unlock(&root->objectid_mutex);
7678
return 0;
7779
}
7880
path = btrfs_alloc_path();
7981
BUG_ON(!path);
80-
search_start = root->last_inode_alloc;
8182
search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
8283
search_key.objectid = search_start;
84+
search_key.type = 0;
8385
search_key.offset = 0;
8486

8587
btrfs_init_path(path);
@@ -88,9 +90,6 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
8890
if (ret < 0)
8991
goto error;
9092

91-
if (path->slots[0] > 0)
92-
path->slots[0]--;
93-
9493
while (1) {
9594
l = path->nodes[0];
9695
slot = path->slots[0];
@@ -120,13 +119,15 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
120119
}
121120
}
122121
}
122+
if (key.objectid >= BTRFS_LAST_FREE_OBJECTID)
123+
break;
123124
start_found = 1;
124125
last_ino = key.objectid + 1;
125126
path->slots[0]++;
126127
}
127128
// FIXME -ENOSPC
129+
BUG_ON(1);
128130
found:
129-
root->last_inode_alloc = *objectid;
130131
btrfs_release_path(root, path);
131132
btrfs_free_path(path);
132133
BUG_ON(*objectid < search_start);

0 commit comments

Comments
 (0)