3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
10 * pending extents are blocks that we're trying to allocate in the extent
11 * map while trying to grow the map because of other allocations. To avoid
12 * recursing, they are tagged in the radix tree and cleaned up after
13 * other allocations are done. The pending tag is also used in the same
16 #define CTREE_EXTENT_PENDING 0
18 static int inc_block_ref(struct ctree_root
*root
, u64 blocknr
)
20 struct ctree_path path
;
24 struct extent_item
*item
;
26 key
.objectid
= blocknr
;
29 ret
= search_slot(root
->extent_root
, &key
, &path
, 0, 1);
31 l
= &path
.nodes
[0]->leaf
;
32 item
= (struct extent_item
*)(l
->data
+
33 l
->items
[path
.slots
[0]].offset
);
35 BUG_ON(list_empty(&path
.nodes
[0]->dirty
));
36 release_path(root
->extent_root
, &path
);
40 int btrfs_inc_ref(struct ctree_root
*root
, struct tree_buffer
*buf
)
44 for (i
= 0; i
< buf
->node
.header
.nritems
; i
++) {
45 blocknr
= buf
->node
.blockptrs
[i
];
46 inc_block_ref(root
, blocknr
);
52 * find all the blocks marked as pending in the radix tree and remove
53 * them from the extent map
55 static int del_pending_extents(struct ctree_root
*extent_root
)
59 struct tree_buffer
*gang
[4];
61 struct ctree_path path
;
64 ret
= radix_tree_gang_lookup_tag(&extent_root
->cache_radix
,
67 CTREE_EXTENT_PENDING
);
70 for (i
= 0; i
< ret
; i
++) {
71 key
.objectid
= gang
[i
]->blocknr
;
75 ret
= search_slot(extent_root
, &key
, &path
, -1, 1);
77 print_tree(extent_root
, extent_root
->node
);
78 printf("unable to find %Lu\n", key
.objectid
);
80 // FIXME undo it and return sane
83 ret
= del_item(extent_root
, &path
);
88 release_path(extent_root
, &path
);
89 radix_tree_tag_clear(&extent_root
->cache_radix
,
91 CTREE_EXTENT_PENDING
);
92 tree_block_release(extent_root
, gang
[i
]);
99 * remove an extent from the root, returns 0 on success
101 int free_extent(struct ctree_root
*root
, u64 blocknr
, u64 num_blocks
)
103 struct ctree_path path
;
105 struct ctree_root
*extent_root
= root
->extent_root
;
106 struct tree_buffer
*t
;
109 key
.objectid
= blocknr
;
111 key
.offset
= num_blocks
;
112 if (root
== extent_root
) {
113 t
= read_tree_block(root
, key
.objectid
);
114 radix_tree_tag_set(&root
->cache_radix
, key
.objectid
,
115 CTREE_EXTENT_PENDING
);
119 ret
= search_slot(extent_root
, &key
, &path
, -1, 1);
121 print_tree(extent_root
, extent_root
->node
);
122 printf("failed to find %Lu\n", key
.objectid
);
125 ret
= del_item(extent_root
, &path
);
128 release_path(extent_root
, &path
);
129 pending_ret
= del_pending_extents(root
->extent_root
);
130 return ret
? ret
: pending_ret
;
134 * walks the btree of allocated extents and find a hole of a given size.
135 * The key ins is changed to record the hole:
136 * ins->objectid == block start
138 * ins->offset == number of blocks
139 * Any available blocks before search_start are skipped.
141 static int find_free_extent(struct ctree_root
*orig_root
, u64 num_blocks
,
142 u64 search_start
, u64 search_end
, struct key
*ins
)
144 struct ctree_path path
;
152 struct ctree_root
* root
= orig_root
->extent_root
;
156 ins
->objectid
= search_start
;
160 ret
= search_slot(root
, ins
, &path
, 0, 0);
165 l
= &path
.nodes
[0]->leaf
;
166 slot
= path
.slots
[0];
167 if (slot
>= l
->header
.nritems
) {
168 ret
= next_leaf(root
, &path
);
174 ins
->objectid
= search_start
;
175 ins
->offset
= num_blocks
;
179 ins
->objectid
= last_block
> search_start
?
180 last_block
: search_start
;
181 ins
->offset
= num_blocks
;
184 key
= &l
->items
[slot
].key
;
185 if (key
->objectid
>= search_start
) {
187 hole_size
= key
->objectid
- last_block
;
188 if (hole_size
> num_blocks
) {
189 ins
->objectid
= last_block
;
190 ins
->offset
= num_blocks
;
195 last_block
= key
->objectid
+ key
->offset
;
201 /* we have to make sure we didn't find an extent that has already
202 * been allocated by the map tree or the original allocation
204 release_path(root
, &path
);
205 BUG_ON(ins
->objectid
< search_start
);
206 if (orig_root
->extent_root
== orig_root
) {
207 BUG_ON(num_blocks
!= 1);
208 if ((root
->current_insert
.objectid
<= ins
->objectid
&&
209 root
->current_insert
.objectid
+
210 root
->current_insert
.offset
> ins
->objectid
) ||
211 (root
->current_insert
.objectid
> ins
->objectid
&&
212 root
->current_insert
.objectid
<= ins
->objectid
+
214 radix_tree_tag_get(&root
->cache_radix
, ins
->objectid
,
215 CTREE_EXTENT_PENDING
)) {
216 search_start
= ins
->objectid
+ 1;
220 if (ins
->offset
!= 1)
224 release_path(root
, &path
);
229 * insert all of the pending extents reserved during the original
230 * allocation. (CTREE_EXTENT_PENDING). Returns zero if it all worked out
232 static int insert_pending_extents(struct ctree_root
*extent_root
)
236 struct extent_item item
;
237 struct tree_buffer
*gang
[4];
242 item
.owner
= extent_root
->node
->node
.header
.parentid
;
244 ret
= radix_tree_gang_lookup_tag(&extent_root
->cache_radix
,
247 CTREE_EXTENT_PENDING
);
250 for (i
= 0; i
< ret
; i
++) {
251 key
.objectid
= gang
[i
]->blocknr
;
254 ret
= insert_item(extent_root
, &key
, &item
,
257 printf("%Lu already in tree\n", key
.objectid
);
258 print_tree(extent_root
, extent_root
->node
);
260 // FIXME undo it and return sane
263 radix_tree_tag_clear(&extent_root
->cache_radix
,
265 CTREE_EXTENT_PENDING
);
266 printf("%Lu is not pending\n", gang
[i
]->blocknr
);
267 tree_block_release(extent_root
, gang
[i
]);
274 * finds a free extent and does all the dirty work required for allocation
275 * returns the key for the extent through ins, and a tree buffer for
276 * the first block of the extent through buf.
278 * returns 0 if everything worked, non-zero otherwise.
280 int alloc_extent(struct ctree_root
*root
, u64 num_blocks
, u64 search_start
,
281 u64 search_end
, u64 owner
, struct key
*ins
,
282 struct tree_buffer
**buf
)
286 struct extent_item extent_item
;
287 extent_item
.refs
= 1;
288 extent_item
.owner
= owner
;
290 ret
= find_free_extent(root
, num_blocks
, search_start
, search_end
, ins
);
293 if (root
!= root
->extent_root
) {
294 memcpy(&root
->extent_root
->current_insert
, ins
, sizeof(*ins
));
295 ret
= insert_item(root
->extent_root
, ins
, &extent_item
,
296 sizeof(extent_item
));
297 memset(&root
->extent_root
->current_insert
, 0,
299 pending_ret
= insert_pending_extents(root
->extent_root
);
304 *buf
= find_tree_block(root
, ins
->objectid
);
305 dirty_tree_block(root
, *buf
);
308 /* we're allocating an extent for the extent tree, don't recurse */
309 BUG_ON(ins
->offset
!= 1);
310 *buf
= find_tree_block(root
, ins
->objectid
);
312 printf("%Lu is pending\n", ins
->objectid
);
313 radix_tree_tag_set(&root
->cache_radix
, ins
->objectid
,
314 CTREE_EXTENT_PENDING
);
316 dirty_tree_block(root
, *buf
);
322 * helper function to allocate a block for a given tree
323 * returns the tree buffer or NULL.
325 struct tree_buffer
*alloc_free_block(struct ctree_root
*root
)
329 struct tree_buffer
*buf
= NULL
;
331 ret
= alloc_extent(root
, 1, 0, (unsigned long)-1,
332 root
->node
->node
.header
.parentid
,
339 if (root
!= root
->extent_root
)
340 BUG_ON(radix_tree_tag_get(&root
->extent_root
->cache_radix
,
341 buf
->blocknr
, CTREE_EXTENT_PENDING
));
This page took 0.117954 seconds and 5 git commands to generate.