]>
Commit | Line | Data |
---|---|---|
0f9dd46c JB |
1 | /* |
2 | * Copyright (C) 2008 Red Hat. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
96303081 | 19 | #include <linux/pagemap.h> |
0f9dd46c | 20 | #include <linux/sched.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
96303081 | 22 | #include <linux/math64.h> |
6ab60601 | 23 | #include <linux/ratelimit.h> |
0f9dd46c | 24 | #include "ctree.h" |
fa9c0d79 CM |
25 | #include "free-space-cache.h" |
26 | #include "transaction.h" | |
0af3d00b | 27 | #include "disk-io.h" |
43be2146 | 28 | #include "extent_io.h" |
581bb050 | 29 | #include "inode-map.h" |
fa9c0d79 | 30 | |
96303081 JB |
31 | #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8) |
32 | #define MAX_CACHE_BYTES_PER_GIG (32 * 1024) | |
0f9dd46c | 33 | |
34d52cb6 | 34 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
0cb59c99 JB |
35 | struct btrfs_free_space *info); |
36 | ||
0414efae LZ |
37 | static struct inode *__lookup_free_space_inode(struct btrfs_root *root, |
38 | struct btrfs_path *path, | |
39 | u64 offset) | |
0af3d00b JB |
40 | { |
41 | struct btrfs_key key; | |
42 | struct btrfs_key location; | |
43 | struct btrfs_disk_key disk_key; | |
44 | struct btrfs_free_space_header *header; | |
45 | struct extent_buffer *leaf; | |
46 | struct inode *inode = NULL; | |
47 | int ret; | |
48 | ||
0af3d00b | 49 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
0414efae | 50 | key.offset = offset; |
0af3d00b JB |
51 | key.type = 0; |
52 | ||
53 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
54 | if (ret < 0) | |
55 | return ERR_PTR(ret); | |
56 | if (ret > 0) { | |
b3b4aa74 | 57 | btrfs_release_path(path); |
0af3d00b JB |
58 | return ERR_PTR(-ENOENT); |
59 | } | |
60 | ||
61 | leaf = path->nodes[0]; | |
62 | header = btrfs_item_ptr(leaf, path->slots[0], | |
63 | struct btrfs_free_space_header); | |
64 | btrfs_free_space_key(leaf, header, &disk_key); | |
65 | btrfs_disk_key_to_cpu(&location, &disk_key); | |
b3b4aa74 | 66 | btrfs_release_path(path); |
0af3d00b JB |
67 | |
68 | inode = btrfs_iget(root->fs_info->sb, &location, root, NULL); | |
69 | if (!inode) | |
70 | return ERR_PTR(-ENOENT); | |
71 | if (IS_ERR(inode)) | |
72 | return inode; | |
73 | if (is_bad_inode(inode)) { | |
74 | iput(inode); | |
75 | return ERR_PTR(-ENOENT); | |
76 | } | |
77 | ||
adae52b9 MX |
78 | inode->i_mapping->flags &= ~__GFP_FS; |
79 | ||
0414efae LZ |
80 | return inode; |
81 | } | |
82 | ||
83 | struct inode *lookup_free_space_inode(struct btrfs_root *root, | |
84 | struct btrfs_block_group_cache | |
85 | *block_group, struct btrfs_path *path) | |
86 | { | |
87 | struct inode *inode = NULL; | |
88 | ||
89 | spin_lock(&block_group->lock); | |
90 | if (block_group->inode) | |
91 | inode = igrab(block_group->inode); | |
92 | spin_unlock(&block_group->lock); | |
93 | if (inode) | |
94 | return inode; | |
95 | ||
96 | inode = __lookup_free_space_inode(root, path, | |
97 | block_group->key.objectid); | |
98 | if (IS_ERR(inode)) | |
99 | return inode; | |
100 | ||
0af3d00b | 101 | spin_lock(&block_group->lock); |
2f356126 JB |
102 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) { |
103 | printk(KERN_INFO "Old style space inode found, converting.\n"); | |
104 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NODATASUM; | |
105 | block_group->disk_cache_state = BTRFS_DC_CLEAR; | |
106 | } | |
107 | ||
300e4f8a | 108 | if (!block_group->iref) { |
0af3d00b JB |
109 | block_group->inode = igrab(inode); |
110 | block_group->iref = 1; | |
111 | } | |
112 | spin_unlock(&block_group->lock); | |
113 | ||
114 | return inode; | |
115 | } | |
116 | ||
0414efae LZ |
117 | int __create_free_space_inode(struct btrfs_root *root, |
118 | struct btrfs_trans_handle *trans, | |
119 | struct btrfs_path *path, u64 ino, u64 offset) | |
0af3d00b JB |
120 | { |
121 | struct btrfs_key key; | |
122 | struct btrfs_disk_key disk_key; | |
123 | struct btrfs_free_space_header *header; | |
124 | struct btrfs_inode_item *inode_item; | |
125 | struct extent_buffer *leaf; | |
0af3d00b JB |
126 | int ret; |
127 | ||
0414efae | 128 | ret = btrfs_insert_empty_inode(trans, root, path, ino); |
0af3d00b JB |
129 | if (ret) |
130 | return ret; | |
131 | ||
132 | leaf = path->nodes[0]; | |
133 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
134 | struct btrfs_inode_item); | |
135 | btrfs_item_key(leaf, &disk_key, path->slots[0]); | |
136 | memset_extent_buffer(leaf, 0, (unsigned long)inode_item, | |
137 | sizeof(*inode_item)); | |
138 | btrfs_set_inode_generation(leaf, inode_item, trans->transid); | |
139 | btrfs_set_inode_size(leaf, inode_item, 0); | |
140 | btrfs_set_inode_nbytes(leaf, inode_item, 0); | |
141 | btrfs_set_inode_uid(leaf, inode_item, 0); | |
142 | btrfs_set_inode_gid(leaf, inode_item, 0); | |
143 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); | |
144 | btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS | | |
2f356126 | 145 | BTRFS_INODE_PREALLOC); |
0af3d00b JB |
146 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
147 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); | |
0414efae | 148 | btrfs_set_inode_block_group(leaf, inode_item, offset); |
0af3d00b | 149 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 150 | btrfs_release_path(path); |
0af3d00b JB |
151 | |
152 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
0414efae | 153 | key.offset = offset; |
0af3d00b JB |
154 | key.type = 0; |
155 | ||
156 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
157 | sizeof(struct btrfs_free_space_header)); | |
158 | if (ret < 0) { | |
b3b4aa74 | 159 | btrfs_release_path(path); |
0af3d00b JB |
160 | return ret; |
161 | } | |
162 | leaf = path->nodes[0]; | |
163 | header = btrfs_item_ptr(leaf, path->slots[0], | |
164 | struct btrfs_free_space_header); | |
165 | memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header)); | |
166 | btrfs_set_free_space_key(leaf, header, &disk_key); | |
167 | btrfs_mark_buffer_dirty(leaf); | |
b3b4aa74 | 168 | btrfs_release_path(path); |
0af3d00b JB |
169 | |
170 | return 0; | |
171 | } | |
172 | ||
0414efae LZ |
173 | int create_free_space_inode(struct btrfs_root *root, |
174 | struct btrfs_trans_handle *trans, | |
175 | struct btrfs_block_group_cache *block_group, | |
176 | struct btrfs_path *path) | |
177 | { | |
178 | int ret; | |
179 | u64 ino; | |
180 | ||
181 | ret = btrfs_find_free_objectid(root, &ino); | |
182 | if (ret < 0) | |
183 | return ret; | |
184 | ||
185 | return __create_free_space_inode(root, trans, path, ino, | |
186 | block_group->key.objectid); | |
187 | } | |
188 | ||
0af3d00b JB |
189 | int btrfs_truncate_free_space_cache(struct btrfs_root *root, |
190 | struct btrfs_trans_handle *trans, | |
191 | struct btrfs_path *path, | |
192 | struct inode *inode) | |
193 | { | |
65450aa6 | 194 | struct btrfs_block_rsv *rsv; |
0af3d00b JB |
195 | loff_t oldsize; |
196 | int ret = 0; | |
197 | ||
65450aa6 | 198 | rsv = trans->block_rsv; |
0af3d00b | 199 | trans->block_rsv = root->orphan_block_rsv; |
4a92b1b8 | 200 | ret = btrfs_block_rsv_check(root, root->orphan_block_rsv, 0, 5, 0); |
0af3d00b JB |
201 | if (ret) |
202 | return ret; | |
203 | ||
204 | oldsize = i_size_read(inode); | |
205 | btrfs_i_size_write(inode, 0); | |
206 | truncate_pagecache(inode, oldsize, 0); | |
207 | ||
208 | /* | |
209 | * We don't need an orphan item because truncating the free space cache | |
210 | * will never be split across transactions. | |
211 | */ | |
212 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
213 | 0, BTRFS_EXTENT_DATA_KEY); | |
65450aa6 LB |
214 | |
215 | trans->block_rsv = rsv; | |
0af3d00b JB |
216 | if (ret) { |
217 | WARN_ON(1); | |
218 | return ret; | |
219 | } | |
220 | ||
82d5902d LZ |
221 | ret = btrfs_update_inode(trans, root, inode); |
222 | return ret; | |
0af3d00b JB |
223 | } |
224 | ||
9d66e233 JB |
225 | static int readahead_cache(struct inode *inode) |
226 | { | |
227 | struct file_ra_state *ra; | |
228 | unsigned long last_index; | |
229 | ||
230 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
231 | if (!ra) | |
232 | return -ENOMEM; | |
233 | ||
234 | file_ra_state_init(ra, inode->i_mapping); | |
235 | last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT; | |
236 | ||
237 | page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index); | |
238 | ||
239 | kfree(ra); | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
a67509c3 JB |
244 | struct io_ctl { |
245 | void *cur, *orig; | |
246 | struct page *page; | |
247 | struct page **pages; | |
248 | struct btrfs_root *root; | |
249 | unsigned long size; | |
250 | int index; | |
251 | int num_pages; | |
252 | }; | |
253 | ||
254 | static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, | |
255 | struct btrfs_root *root) | |
256 | { | |
257 | memset(io_ctl, 0, sizeof(struct io_ctl)); | |
258 | io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | |
259 | PAGE_CACHE_SHIFT; | |
260 | io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages, | |
261 | GFP_NOFS); | |
262 | if (!io_ctl->pages) | |
263 | return -ENOMEM; | |
264 | io_ctl->root = root; | |
265 | return 0; | |
266 | } | |
267 | ||
268 | static void io_ctl_free(struct io_ctl *io_ctl) | |
269 | { | |
270 | kfree(io_ctl->pages); | |
271 | } | |
272 | ||
273 | static void io_ctl_unmap_page(struct io_ctl *io_ctl) | |
274 | { | |
275 | if (io_ctl->cur) { | |
276 | kunmap(io_ctl->page); | |
277 | io_ctl->cur = NULL; | |
278 | io_ctl->orig = NULL; | |
279 | } | |
280 | } | |
281 | ||
282 | static void io_ctl_map_page(struct io_ctl *io_ctl, int clear) | |
283 | { | |
284 | WARN_ON(io_ctl->cur); | |
285 | BUG_ON(io_ctl->index >= io_ctl->num_pages); | |
286 | io_ctl->page = io_ctl->pages[io_ctl->index++]; | |
287 | io_ctl->cur = kmap(io_ctl->page); | |
288 | io_ctl->orig = io_ctl->cur; | |
289 | io_ctl->size = PAGE_CACHE_SIZE; | |
290 | if (clear) | |
291 | memset(io_ctl->cur, 0, PAGE_CACHE_SIZE); | |
292 | } | |
293 | ||
294 | static void io_ctl_drop_pages(struct io_ctl *io_ctl) | |
295 | { | |
296 | int i; | |
297 | ||
298 | io_ctl_unmap_page(io_ctl); | |
299 | ||
300 | for (i = 0; i < io_ctl->num_pages; i++) { | |
301 | ClearPageChecked(io_ctl->pages[i]); | |
302 | unlock_page(io_ctl->pages[i]); | |
303 | page_cache_release(io_ctl->pages[i]); | |
304 | } | |
305 | } | |
306 | ||
307 | static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode, | |
308 | int uptodate) | |
309 | { | |
310 | struct page *page; | |
311 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); | |
312 | int i; | |
313 | ||
314 | for (i = 0; i < io_ctl->num_pages; i++) { | |
315 | page = find_or_create_page(inode->i_mapping, i, mask); | |
316 | if (!page) { | |
317 | io_ctl_drop_pages(io_ctl); | |
318 | return -ENOMEM; | |
319 | } | |
320 | io_ctl->pages[i] = page; | |
321 | if (uptodate && !PageUptodate(page)) { | |
322 | btrfs_readpage(NULL, page); | |
323 | lock_page(page); | |
324 | if (!PageUptodate(page)) { | |
325 | printk(KERN_ERR "btrfs: error reading free " | |
326 | "space cache\n"); | |
327 | io_ctl_drop_pages(io_ctl); | |
328 | return -EIO; | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
336 | static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation) | |
337 | { | |
338 | u64 *val; | |
339 | ||
340 | io_ctl_map_page(io_ctl, 1); | |
341 | ||
342 | /* | |
343 | * Skip the first 64bits to make sure theres a bogus crc for old | |
344 | * kernels | |
345 | */ | |
346 | io_ctl->cur += sizeof(u64); | |
347 | ||
348 | val = io_ctl->cur; | |
349 | *val = cpu_to_le64(generation); | |
350 | io_ctl->cur += sizeof(u64); | |
351 | io_ctl->size -= sizeof(u64) * 2; | |
352 | } | |
353 | ||
354 | static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation) | |
355 | { | |
356 | u64 *gen; | |
357 | ||
358 | io_ctl_map_page(io_ctl, 0); | |
359 | ||
360 | /* Skip the bogus crc area */ | |
361 | io_ctl->cur += sizeof(u64); | |
362 | gen = io_ctl->cur; | |
363 | if (le64_to_cpu(*gen) != generation) { | |
364 | printk_ratelimited(KERN_ERR "btrfs: space cache generation " | |
365 | "(%Lu) does not match inode (%Lu)\n", *gen, | |
366 | generation); | |
367 | io_ctl_unmap_page(io_ctl); | |
368 | return -EIO; | |
369 | } | |
370 | io_ctl->cur += sizeof(u64); | |
371 | io_ctl->size -= sizeof(u64) * 2; | |
372 | return 0; | |
373 | } | |
374 | ||
375 | static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes, | |
376 | void *bitmap) | |
377 | { | |
378 | struct btrfs_free_space_entry *entry; | |
379 | ||
380 | if (!io_ctl->cur) | |
381 | return -ENOSPC; | |
382 | ||
383 | entry = io_ctl->cur; | |
384 | entry->offset = cpu_to_le64(offset); | |
385 | entry->bytes = cpu_to_le64(bytes); | |
386 | entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP : | |
387 | BTRFS_FREE_SPACE_EXTENT; | |
388 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); | |
389 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); | |
390 | ||
391 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) | |
392 | return 0; | |
393 | ||
394 | /* | |
395 | * index == 1 means the current page is 0, we need to generate a bogus | |
396 | * crc for older kernels. | |
397 | */ | |
398 | if (io_ctl->index == 1) { | |
399 | u32 *tmp; | |
400 | u32 crc = ~(u32)0; | |
401 | ||
402 | crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + sizeof(u64), | |
403 | crc, PAGE_CACHE_SIZE - sizeof(u64)); | |
404 | btrfs_csum_final(crc, (char *)&crc); | |
405 | crc++; | |
406 | tmp = io_ctl->orig; | |
407 | *tmp = crc; | |
408 | } | |
409 | io_ctl_unmap_page(io_ctl); | |
410 | ||
411 | /* No more pages to map */ | |
412 | if (io_ctl->index >= io_ctl->num_pages) | |
413 | return 0; | |
414 | ||
415 | /* map the next page */ | |
416 | io_ctl_map_page(io_ctl, 1); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap) | |
421 | { | |
422 | if (!io_ctl->cur) | |
423 | return -ENOSPC; | |
424 | ||
425 | /* | |
426 | * If we aren't at the start of the current page, unmap this one and | |
427 | * map the next one if there is any left. | |
428 | */ | |
429 | if (io_ctl->cur != io_ctl->orig) { | |
430 | io_ctl_unmap_page(io_ctl); | |
431 | if (io_ctl->index >= io_ctl->num_pages) | |
432 | return -ENOSPC; | |
433 | io_ctl_map_page(io_ctl, 0); | |
434 | } | |
435 | ||
436 | memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE); | |
437 | io_ctl_unmap_page(io_ctl); | |
438 | if (io_ctl->index < io_ctl->num_pages) | |
439 | io_ctl_map_page(io_ctl, 0); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl) | |
444 | { | |
445 | io_ctl_unmap_page(io_ctl); | |
446 | ||
447 | while (io_ctl->index < io_ctl->num_pages) { | |
448 | io_ctl_map_page(io_ctl, 1); | |
449 | io_ctl_unmap_page(io_ctl); | |
450 | } | |
451 | } | |
452 | ||
453 | static u8 io_ctl_read_entry(struct io_ctl *io_ctl, | |
454 | struct btrfs_free_space *entry) | |
455 | { | |
456 | struct btrfs_free_space_entry *e; | |
457 | u8 type; | |
458 | ||
459 | e = io_ctl->cur; | |
460 | entry->offset = le64_to_cpu(e->offset); | |
461 | entry->bytes = le64_to_cpu(e->bytes); | |
462 | type = e->type; | |
463 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); | |
464 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); | |
465 | ||
466 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) | |
467 | return type; | |
468 | ||
469 | io_ctl_unmap_page(io_ctl); | |
470 | ||
471 | if (io_ctl->index >= io_ctl->num_pages) | |
472 | return type; | |
473 | ||
474 | io_ctl_map_page(io_ctl, 0); | |
475 | return type; | |
476 | } | |
477 | ||
478 | static void io_ctl_read_bitmap(struct io_ctl *io_ctl, | |
479 | struct btrfs_free_space *entry) | |
480 | { | |
481 | BUG_ON(!io_ctl->cur); | |
482 | if (io_ctl->cur != io_ctl->orig) { | |
483 | io_ctl_unmap_page(io_ctl); | |
484 | io_ctl_map_page(io_ctl, 0); | |
485 | } | |
486 | memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE); | |
487 | io_ctl_unmap_page(io_ctl); | |
488 | if (io_ctl->index < io_ctl->num_pages) | |
489 | io_ctl_map_page(io_ctl, 0); | |
490 | } | |
491 | ||
0414efae LZ |
492 | int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, |
493 | struct btrfs_free_space_ctl *ctl, | |
494 | struct btrfs_path *path, u64 offset) | |
9d66e233 | 495 | { |
9d66e233 JB |
496 | struct btrfs_free_space_header *header; |
497 | struct extent_buffer *leaf; | |
a67509c3 | 498 | struct io_ctl io_ctl; |
9d66e233 | 499 | struct btrfs_key key; |
a67509c3 | 500 | struct btrfs_free_space *e, *n; |
9d66e233 JB |
501 | struct list_head bitmaps; |
502 | u64 num_entries; | |
503 | u64 num_bitmaps; | |
504 | u64 generation; | |
a67509c3 | 505 | u8 type; |
f6a39829 | 506 | int ret = 0; |
9d66e233 JB |
507 | |
508 | INIT_LIST_HEAD(&bitmaps); | |
509 | ||
9d66e233 | 510 | /* Nothing in the space cache, goodbye */ |
0414efae | 511 | if (!i_size_read(inode)) |
a67509c3 | 512 | return 0; |
9d66e233 JB |
513 | |
514 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
0414efae | 515 | key.offset = offset; |
9d66e233 JB |
516 | key.type = 0; |
517 | ||
518 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
0414efae | 519 | if (ret < 0) |
a67509c3 | 520 | return 0; |
0414efae | 521 | else if (ret > 0) { |
945d8962 | 522 | btrfs_release_path(path); |
a67509c3 | 523 | return 0; |
9d66e233 JB |
524 | } |
525 | ||
0414efae LZ |
526 | ret = -1; |
527 | ||
9d66e233 JB |
528 | leaf = path->nodes[0]; |
529 | header = btrfs_item_ptr(leaf, path->slots[0], | |
530 | struct btrfs_free_space_header); | |
531 | num_entries = btrfs_free_space_entries(leaf, header); | |
532 | num_bitmaps = btrfs_free_space_bitmaps(leaf, header); | |
533 | generation = btrfs_free_space_generation(leaf, header); | |
945d8962 | 534 | btrfs_release_path(path); |
9d66e233 JB |
535 | |
536 | if (BTRFS_I(inode)->generation != generation) { | |
537 | printk(KERN_ERR "btrfs: free space inode generation (%llu) did" | |
0414efae | 538 | " not match free space cache generation (%llu)\n", |
9d66e233 | 539 | (unsigned long long)BTRFS_I(inode)->generation, |
0414efae | 540 | (unsigned long long)generation); |
a67509c3 | 541 | return 0; |
9d66e233 JB |
542 | } |
543 | ||
544 | if (!num_entries) | |
a67509c3 | 545 | return 0; |
9d66e233 | 546 | |
a67509c3 | 547 | io_ctl_init(&io_ctl, inode, root); |
9d66e233 | 548 | ret = readahead_cache(inode); |
0414efae | 549 | if (ret) |
9d66e233 | 550 | goto out; |
9d66e233 | 551 | |
a67509c3 JB |
552 | ret = io_ctl_prepare_pages(&io_ctl, inode, 1); |
553 | if (ret) | |
554 | goto out; | |
9d66e233 | 555 | |
a67509c3 JB |
556 | ret = io_ctl_check_generation(&io_ctl, generation); |
557 | if (ret) | |
558 | goto free_cache; | |
9d66e233 | 559 | |
a67509c3 JB |
560 | while (num_entries) { |
561 | e = kmem_cache_zalloc(btrfs_free_space_cachep, | |
562 | GFP_NOFS); | |
563 | if (!e) | |
9d66e233 | 564 | goto free_cache; |
9d66e233 | 565 | |
a67509c3 JB |
566 | type = io_ctl_read_entry(&io_ctl, e); |
567 | if (!e->bytes) { | |
568 | kmem_cache_free(btrfs_free_space_cachep, e); | |
569 | goto free_cache; | |
9d66e233 | 570 | } |
a67509c3 JB |
571 | |
572 | if (type == BTRFS_FREE_SPACE_EXTENT) { | |
573 | spin_lock(&ctl->tree_lock); | |
574 | ret = link_free_space(ctl, e); | |
575 | spin_unlock(&ctl->tree_lock); | |
576 | if (ret) { | |
577 | printk(KERN_ERR "Duplicate entries in " | |
578 | "free space cache, dumping\n"); | |
579 | kmem_cache_free(btrfs_free_space_cachep, e); | |
9d66e233 JB |
580 | goto free_cache; |
581 | } | |
a67509c3 JB |
582 | } else { |
583 | BUG_ON(!num_bitmaps); | |
584 | num_bitmaps--; | |
585 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); | |
586 | if (!e->bitmap) { | |
587 | kmem_cache_free( | |
588 | btrfs_free_space_cachep, e); | |
9d66e233 JB |
589 | goto free_cache; |
590 | } | |
a67509c3 JB |
591 | spin_lock(&ctl->tree_lock); |
592 | ret = link_free_space(ctl, e); | |
593 | ctl->total_bitmaps++; | |
594 | ctl->op->recalc_thresholds(ctl); | |
595 | spin_unlock(&ctl->tree_lock); | |
596 | if (ret) { | |
597 | printk(KERN_ERR "Duplicate entries in " | |
598 | "free space cache, dumping\n"); | |
dc89e982 | 599 | kmem_cache_free(btrfs_free_space_cachep, e); |
9d66e233 JB |
600 | goto free_cache; |
601 | } | |
a67509c3 | 602 | list_add_tail(&e->list, &bitmaps); |
9d66e233 JB |
603 | } |
604 | ||
a67509c3 JB |
605 | num_entries--; |
606 | } | |
9d66e233 | 607 | |
a67509c3 JB |
608 | /* |
609 | * We add the bitmaps at the end of the entries in order that | |
610 | * the bitmap entries are added to the cache. | |
611 | */ | |
612 | list_for_each_entry_safe(e, n, &bitmaps, list) { | |
9d66e233 | 613 | list_del_init(&e->list); |
a67509c3 | 614 | io_ctl_read_bitmap(&io_ctl, e); |
9d66e233 JB |
615 | } |
616 | ||
a67509c3 | 617 | io_ctl_drop_pages(&io_ctl); |
9d66e233 JB |
618 | ret = 1; |
619 | out: | |
a67509c3 | 620 | io_ctl_free(&io_ctl); |
9d66e233 | 621 | return ret; |
9d66e233 | 622 | free_cache: |
a67509c3 | 623 | io_ctl_drop_pages(&io_ctl); |
0414efae | 624 | __btrfs_remove_free_space_cache(ctl); |
9d66e233 JB |
625 | goto out; |
626 | } | |
627 | ||
0414efae LZ |
628 | int load_free_space_cache(struct btrfs_fs_info *fs_info, |
629 | struct btrfs_block_group_cache *block_group) | |
0cb59c99 | 630 | { |
34d52cb6 | 631 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
0414efae LZ |
632 | struct btrfs_root *root = fs_info->tree_root; |
633 | struct inode *inode; | |
634 | struct btrfs_path *path; | |
635 | int ret; | |
636 | bool matched; | |
637 | u64 used = btrfs_block_group_used(&block_group->item); | |
638 | ||
639 | /* | |
640 | * If we're unmounting then just return, since this does a search on the | |
641 | * normal root and not the commit root and we could deadlock. | |
642 | */ | |
7841cb28 | 643 | if (btrfs_fs_closing(fs_info)) |
0414efae LZ |
644 | return 0; |
645 | ||
646 | /* | |
647 | * If this block group has been marked to be cleared for one reason or | |
648 | * another then we can't trust the on disk cache, so just return. | |
649 | */ | |
9d66e233 | 650 | spin_lock(&block_group->lock); |
0414efae LZ |
651 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
652 | spin_unlock(&block_group->lock); | |
653 | return 0; | |
654 | } | |
9d66e233 | 655 | spin_unlock(&block_group->lock); |
0414efae LZ |
656 | |
657 | path = btrfs_alloc_path(); | |
658 | if (!path) | |
659 | return 0; | |
660 | ||
661 | inode = lookup_free_space_inode(root, block_group, path); | |
662 | if (IS_ERR(inode)) { | |
663 | btrfs_free_path(path); | |
664 | return 0; | |
665 | } | |
666 | ||
667 | ret = __load_free_space_cache(fs_info->tree_root, inode, ctl, | |
668 | path, block_group->key.objectid); | |
669 | btrfs_free_path(path); | |
670 | if (ret <= 0) | |
671 | goto out; | |
672 | ||
673 | spin_lock(&ctl->tree_lock); | |
674 | matched = (ctl->free_space == (block_group->key.offset - used - | |
675 | block_group->bytes_super)); | |
676 | spin_unlock(&ctl->tree_lock); | |
677 | ||
678 | if (!matched) { | |
679 | __btrfs_remove_free_space_cache(ctl); | |
680 | printk(KERN_ERR "block group %llu has an wrong amount of free " | |
681 | "space\n", block_group->key.objectid); | |
682 | ret = -1; | |
683 | } | |
684 | out: | |
685 | if (ret < 0) { | |
686 | /* This cache is bogus, make sure it gets cleared */ | |
687 | spin_lock(&block_group->lock); | |
688 | block_group->disk_cache_state = BTRFS_DC_CLEAR; | |
689 | spin_unlock(&block_group->lock); | |
82d5902d | 690 | ret = 0; |
0414efae LZ |
691 | |
692 | printk(KERN_ERR "btrfs: failed to load free space cache " | |
693 | "for block group %llu\n", block_group->key.objectid); | |
694 | } | |
695 | ||
696 | iput(inode); | |
697 | return ret; | |
9d66e233 JB |
698 | } |
699 | ||
c09544e0 JB |
700 | /** |
701 | * __btrfs_write_out_cache - write out cached info to an inode | |
702 | * @root - the root the inode belongs to | |
703 | * @ctl - the free space cache we are going to write out | |
704 | * @block_group - the block_group for this cache if it belongs to a block_group | |
705 | * @trans - the trans handle | |
706 | * @path - the path to use | |
707 | * @offset - the offset for the key we'll insert | |
708 | * | |
709 | * This function writes out a free space cache struct to disk for quick recovery | |
710 | * on mount. This will return 0 if it was successfull in writing the cache out, | |
711 | * and -1 if it was not. | |
712 | */ | |
0414efae LZ |
713 | int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, |
714 | struct btrfs_free_space_ctl *ctl, | |
715 | struct btrfs_block_group_cache *block_group, | |
716 | struct btrfs_trans_handle *trans, | |
717 | struct btrfs_path *path, u64 offset) | |
0cb59c99 JB |
718 | { |
719 | struct btrfs_free_space_header *header; | |
720 | struct extent_buffer *leaf; | |
0cb59c99 JB |
721 | struct rb_node *node; |
722 | struct list_head *pos, *n; | |
0cb59c99 | 723 | struct extent_state *cached_state = NULL; |
43be2146 JB |
724 | struct btrfs_free_cluster *cluster = NULL; |
725 | struct extent_io_tree *unpin = NULL; | |
a67509c3 | 726 | struct io_ctl io_ctl; |
0cb59c99 JB |
727 | struct list_head bitmap_list; |
728 | struct btrfs_key key; | |
43be2146 | 729 | u64 start, end, len; |
0cb59c99 JB |
730 | int entries = 0; |
731 | int bitmaps = 0; | |
c09544e0 JB |
732 | int ret; |
733 | int err = -1; | |
0cb59c99 | 734 | |
0cb59c99 JB |
735 | INIT_LIST_HEAD(&bitmap_list); |
736 | ||
0414efae LZ |
737 | if (!i_size_read(inode)) |
738 | return -1; | |
2b20982e | 739 | |
0cb59c99 JB |
740 | filemap_write_and_wait(inode->i_mapping); |
741 | btrfs_wait_ordered_range(inode, inode->i_size & | |
742 | ~(root->sectorsize - 1), (u64)-1); | |
743 | ||
a67509c3 | 744 | io_ctl_init(&io_ctl, inode, root); |
be1a12a0 | 745 | |
43be2146 | 746 | /* Get the cluster for this block_group if it exists */ |
0414efae | 747 | if (block_group && !list_empty(&block_group->cluster_list)) |
43be2146 JB |
748 | cluster = list_entry(block_group->cluster_list.next, |
749 | struct btrfs_free_cluster, | |
750 | block_group_list); | |
751 | ||
752 | /* | |
753 | * We shouldn't have switched the pinned extents yet so this is the | |
754 | * right one | |
755 | */ | |
756 | unpin = root->fs_info->pinned_extents; | |
757 | ||
a67509c3 JB |
758 | /* Lock all pages first so we can lock the extent safely. */ |
759 | io_ctl_prepare_pages(&io_ctl, inode, 0); | |
0cb59c99 | 760 | |
0cb59c99 JB |
761 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
762 | 0, &cached_state, GFP_NOFS); | |
763 | ||
43be2146 JB |
764 | /* |
765 | * When searching for pinned extents, we need to start at our start | |
766 | * offset. | |
767 | */ | |
0414efae LZ |
768 | if (block_group) |
769 | start = block_group->key.objectid; | |
43be2146 | 770 | |
f75b130e JB |
771 | node = rb_first(&ctl->free_space_offset); |
772 | if (!node && cluster) { | |
773 | node = rb_first(&cluster->root); | |
774 | cluster = NULL; | |
775 | } | |
776 | ||
a67509c3 | 777 | io_ctl_set_generation(&io_ctl, trans->transid); |
43be2146 | 778 | |
a67509c3 JB |
779 | /* Write out the extent entries */ |
780 | while (node) { | |
781 | struct btrfs_free_space *e; | |
0cb59c99 | 782 | |
a67509c3 JB |
783 | e = rb_entry(node, struct btrfs_free_space, offset_index); |
784 | entries++; | |
0cb59c99 | 785 | |
a67509c3 JB |
786 | ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes, |
787 | e->bitmap); | |
788 | if (ret) | |
789 | goto out_nospc; | |
2f356126 | 790 | |
a67509c3 JB |
791 | if (e->bitmap) { |
792 | list_add_tail(&e->list, &bitmap_list); | |
793 | bitmaps++; | |
2f356126 | 794 | } |
a67509c3 JB |
795 | node = rb_next(node); |
796 | if (!node && cluster) { | |
797 | node = rb_first(&cluster->root); | |
798 | cluster = NULL; | |
43be2146 | 799 | } |
a67509c3 | 800 | } |
43be2146 | 801 | |
a67509c3 JB |
802 | /* |
803 | * We want to add any pinned extents to our free space cache | |
804 | * so we don't leak the space | |
805 | */ | |
806 | while (block_group && (start < block_group->key.objectid + | |
807 | block_group->key.offset)) { | |
808 | ret = find_first_extent_bit(unpin, start, &start, &end, | |
809 | EXTENT_DIRTY); | |
810 | if (ret) { | |
811 | ret = 0; | |
812 | break; | |
0cb59c99 | 813 | } |
0cb59c99 | 814 | |
a67509c3 JB |
815 | /* This pinned extent is out of our range */ |
816 | if (start >= block_group->key.objectid + | |
817 | block_group->key.offset) | |
818 | break; | |
2f356126 | 819 | |
a67509c3 JB |
820 | len = block_group->key.objectid + |
821 | block_group->key.offset - start; | |
822 | len = min(len, end + 1 - start); | |
0cb59c99 | 823 | |
a67509c3 JB |
824 | entries++; |
825 | ret = io_ctl_add_entry(&io_ctl, start, len, NULL); | |
826 | if (ret) | |
827 | goto out_nospc; | |
0cb59c99 | 828 | |
a67509c3 JB |
829 | start = end + 1; |
830 | } | |
0cb59c99 JB |
831 | |
832 | /* Write out the bitmaps */ | |
833 | list_for_each_safe(pos, n, &bitmap_list) { | |
0cb59c99 JB |
834 | struct btrfs_free_space *entry = |
835 | list_entry(pos, struct btrfs_free_space, list); | |
836 | ||
a67509c3 JB |
837 | ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap); |
838 | if (ret) | |
839 | goto out_nospc; | |
0cb59c99 | 840 | list_del_init(&entry->list); |
be1a12a0 JB |
841 | } |
842 | ||
0cb59c99 | 843 | /* Zero out the rest of the pages just to make sure */ |
a67509c3 | 844 | io_ctl_zero_remaining_pages(&io_ctl); |
0cb59c99 | 845 | |
a67509c3 JB |
846 | ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages, |
847 | 0, i_size_read(inode), &cached_state); | |
848 | io_ctl_drop_pages(&io_ctl); | |
0cb59c99 JB |
849 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
850 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); | |
851 | ||
c09544e0 | 852 | if (ret) |
2f356126 | 853 | goto out; |
be1a12a0 | 854 | |
be1a12a0 | 855 | |
549b4fdb JB |
856 | ret = filemap_write_and_wait(inode->i_mapping); |
857 | if (ret) | |
858 | goto out; | |
0cb59c99 JB |
859 | |
860 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
0414efae | 861 | key.offset = offset; |
0cb59c99 JB |
862 | key.type = 0; |
863 | ||
a9b5fcdd | 864 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
0cb59c99 | 865 | if (ret < 0) { |
a67509c3 | 866 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
0cb59c99 JB |
867 | EXTENT_DIRTY | EXTENT_DELALLOC | |
868 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS); | |
2f356126 | 869 | goto out; |
0cb59c99 JB |
870 | } |
871 | leaf = path->nodes[0]; | |
872 | if (ret > 0) { | |
873 | struct btrfs_key found_key; | |
874 | BUG_ON(!path->slots[0]); | |
875 | path->slots[0]--; | |
876 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
877 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || | |
0414efae | 878 | found_key.offset != offset) { |
a67509c3 JB |
879 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, |
880 | inode->i_size - 1, | |
0cb59c99 JB |
881 | EXTENT_DIRTY | EXTENT_DELALLOC | |
882 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, | |
883 | GFP_NOFS); | |
b3b4aa74 | 884 | btrfs_release_path(path); |
2f356126 | 885 | goto out; |
0cb59c99 JB |
886 | } |
887 | } | |
549b4fdb JB |
888 | |
889 | BTRFS_I(inode)->generation = trans->transid; | |
0cb59c99 JB |
890 | header = btrfs_item_ptr(leaf, path->slots[0], |
891 | struct btrfs_free_space_header); | |
892 | btrfs_set_free_space_entries(leaf, header, entries); | |
893 | btrfs_set_free_space_bitmaps(leaf, header, bitmaps); | |
894 | btrfs_set_free_space_generation(leaf, header, trans->transid); | |
895 | btrfs_mark_buffer_dirty(leaf); | |
b3b4aa74 | 896 | btrfs_release_path(path); |
0cb59c99 | 897 | |
c09544e0 | 898 | err = 0; |
2f356126 | 899 | out: |
a67509c3 | 900 | io_ctl_free(&io_ctl); |
c09544e0 | 901 | if (err) { |
a67509c3 | 902 | invalidate_inode_pages2(inode->i_mapping); |
0cb59c99 JB |
903 | BTRFS_I(inode)->generation = 0; |
904 | } | |
0cb59c99 | 905 | btrfs_update_inode(trans, root, inode); |
c09544e0 | 906 | return err; |
a67509c3 JB |
907 | |
908 | out_nospc: | |
909 | list_for_each_safe(pos, n, &bitmap_list) { | |
910 | struct btrfs_free_space *entry = | |
911 | list_entry(pos, struct btrfs_free_space, list); | |
912 | list_del_init(&entry->list); | |
913 | } | |
914 | io_ctl_drop_pages(&io_ctl); | |
915 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | |
916 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); | |
917 | goto out; | |
0414efae LZ |
918 | } |
919 | ||
920 | int btrfs_write_out_cache(struct btrfs_root *root, | |
921 | struct btrfs_trans_handle *trans, | |
922 | struct btrfs_block_group_cache *block_group, | |
923 | struct btrfs_path *path) | |
924 | { | |
925 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; | |
926 | struct inode *inode; | |
927 | int ret = 0; | |
928 | ||
929 | root = root->fs_info->tree_root; | |
930 | ||
931 | spin_lock(&block_group->lock); | |
932 | if (block_group->disk_cache_state < BTRFS_DC_SETUP) { | |
933 | spin_unlock(&block_group->lock); | |
934 | return 0; | |
935 | } | |
936 | spin_unlock(&block_group->lock); | |
937 | ||
938 | inode = lookup_free_space_inode(root, block_group, path); | |
939 | if (IS_ERR(inode)) | |
940 | return 0; | |
941 | ||
942 | ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans, | |
943 | path, block_group->key.objectid); | |
c09544e0 JB |
944 | if (ret) { |
945 | btrfs_delalloc_release_metadata(inode, inode->i_size); | |
0414efae LZ |
946 | spin_lock(&block_group->lock); |
947 | block_group->disk_cache_state = BTRFS_DC_ERROR; | |
948 | spin_unlock(&block_group->lock); | |
82d5902d | 949 | ret = 0; |
c09544e0 | 950 | #ifdef DEBUG |
0414efae LZ |
951 | printk(KERN_ERR "btrfs: failed to write free space cace " |
952 | "for block group %llu\n", block_group->key.objectid); | |
c09544e0 | 953 | #endif |
0414efae LZ |
954 | } |
955 | ||
0cb59c99 JB |
956 | iput(inode); |
957 | return ret; | |
958 | } | |
959 | ||
34d52cb6 | 960 | static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit, |
96303081 | 961 | u64 offset) |
0f9dd46c | 962 | { |
96303081 JB |
963 | BUG_ON(offset < bitmap_start); |
964 | offset -= bitmap_start; | |
34d52cb6 | 965 | return (unsigned long)(div_u64(offset, unit)); |
96303081 | 966 | } |
0f9dd46c | 967 | |
34d52cb6 | 968 | static inline unsigned long bytes_to_bits(u64 bytes, u32 unit) |
96303081 | 969 | { |
34d52cb6 | 970 | return (unsigned long)(div_u64(bytes, unit)); |
96303081 | 971 | } |
0f9dd46c | 972 | |
34d52cb6 | 973 | static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, |
96303081 JB |
974 | u64 offset) |
975 | { | |
976 | u64 bitmap_start; | |
977 | u64 bytes_per_bitmap; | |
0f9dd46c | 978 | |
34d52cb6 LZ |
979 | bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; |
980 | bitmap_start = offset - ctl->start; | |
96303081 JB |
981 | bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap); |
982 | bitmap_start *= bytes_per_bitmap; | |
34d52cb6 | 983 | bitmap_start += ctl->start; |
0f9dd46c | 984 | |
96303081 | 985 | return bitmap_start; |
0f9dd46c JB |
986 | } |
987 | ||
96303081 JB |
988 | static int tree_insert_offset(struct rb_root *root, u64 offset, |
989 | struct rb_node *node, int bitmap) | |
0f9dd46c JB |
990 | { |
991 | struct rb_node **p = &root->rb_node; | |
992 | struct rb_node *parent = NULL; | |
993 | struct btrfs_free_space *info; | |
994 | ||
995 | while (*p) { | |
996 | parent = *p; | |
96303081 | 997 | info = rb_entry(parent, struct btrfs_free_space, offset_index); |
0f9dd46c | 998 | |
96303081 | 999 | if (offset < info->offset) { |
0f9dd46c | 1000 | p = &(*p)->rb_left; |
96303081 | 1001 | } else if (offset > info->offset) { |
0f9dd46c | 1002 | p = &(*p)->rb_right; |
96303081 JB |
1003 | } else { |
1004 | /* | |
1005 | * we could have a bitmap entry and an extent entry | |
1006 | * share the same offset. If this is the case, we want | |
1007 | * the extent entry to always be found first if we do a | |
1008 | * linear search through the tree, since we want to have | |
1009 | * the quickest allocation time, and allocating from an | |
1010 | * extent is faster than allocating from a bitmap. So | |
1011 | * if we're inserting a bitmap and we find an entry at | |
1012 | * this offset, we want to go right, or after this entry | |
1013 | * logically. If we are inserting an extent and we've | |
1014 | * found a bitmap, we want to go left, or before | |
1015 | * logically. | |
1016 | */ | |
1017 | if (bitmap) { | |
207dde82 JB |
1018 | if (info->bitmap) { |
1019 | WARN_ON_ONCE(1); | |
1020 | return -EEXIST; | |
1021 | } | |
96303081 JB |
1022 | p = &(*p)->rb_right; |
1023 | } else { | |
207dde82 JB |
1024 | if (!info->bitmap) { |
1025 | WARN_ON_ONCE(1); | |
1026 | return -EEXIST; | |
1027 | } | |
96303081 JB |
1028 | p = &(*p)->rb_left; |
1029 | } | |
1030 | } | |
0f9dd46c JB |
1031 | } |
1032 | ||
1033 | rb_link_node(node, parent, p); | |
1034 | rb_insert_color(node, root); | |
1035 | ||
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | /* | |
70cb0743 JB |
1040 | * searches the tree for the given offset. |
1041 | * | |
96303081 JB |
1042 | * fuzzy - If this is set, then we are trying to make an allocation, and we just |
1043 | * want a section that has at least bytes size and comes at or after the given | |
1044 | * offset. | |
0f9dd46c | 1045 | */ |
96303081 | 1046 | static struct btrfs_free_space * |
34d52cb6 | 1047 | tree_search_offset(struct btrfs_free_space_ctl *ctl, |
96303081 | 1048 | u64 offset, int bitmap_only, int fuzzy) |
0f9dd46c | 1049 | { |
34d52cb6 | 1050 | struct rb_node *n = ctl->free_space_offset.rb_node; |
96303081 JB |
1051 | struct btrfs_free_space *entry, *prev = NULL; |
1052 | ||
1053 | /* find entry that is closest to the 'offset' */ | |
1054 | while (1) { | |
1055 | if (!n) { | |
1056 | entry = NULL; | |
1057 | break; | |
1058 | } | |
0f9dd46c | 1059 | |
0f9dd46c | 1060 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
96303081 | 1061 | prev = entry; |
0f9dd46c | 1062 | |
96303081 | 1063 | if (offset < entry->offset) |
0f9dd46c | 1064 | n = n->rb_left; |
96303081 | 1065 | else if (offset > entry->offset) |
0f9dd46c | 1066 | n = n->rb_right; |
96303081 | 1067 | else |
0f9dd46c | 1068 | break; |
0f9dd46c JB |
1069 | } |
1070 | ||
96303081 JB |
1071 | if (bitmap_only) { |
1072 | if (!entry) | |
1073 | return NULL; | |
1074 | if (entry->bitmap) | |
1075 | return entry; | |
0f9dd46c | 1076 | |
96303081 JB |
1077 | /* |
1078 | * bitmap entry and extent entry may share same offset, | |
1079 | * in that case, bitmap entry comes after extent entry. | |
1080 | */ | |
1081 | n = rb_next(n); | |
1082 | if (!n) | |
1083 | return NULL; | |
1084 | entry = rb_entry(n, struct btrfs_free_space, offset_index); | |
1085 | if (entry->offset != offset) | |
1086 | return NULL; | |
0f9dd46c | 1087 | |
96303081 JB |
1088 | WARN_ON(!entry->bitmap); |
1089 | return entry; | |
1090 | } else if (entry) { | |
1091 | if (entry->bitmap) { | |
0f9dd46c | 1092 | /* |
96303081 JB |
1093 | * if previous extent entry covers the offset, |
1094 | * we should return it instead of the bitmap entry | |
0f9dd46c | 1095 | */ |
96303081 JB |
1096 | n = &entry->offset_index; |
1097 | while (1) { | |
1098 | n = rb_prev(n); | |
1099 | if (!n) | |
1100 | break; | |
1101 | prev = rb_entry(n, struct btrfs_free_space, | |
1102 | offset_index); | |
1103 | if (!prev->bitmap) { | |
1104 | if (prev->offset + prev->bytes > offset) | |
1105 | entry = prev; | |
1106 | break; | |
1107 | } | |
0f9dd46c | 1108 | } |
96303081 JB |
1109 | } |
1110 | return entry; | |
1111 | } | |
1112 | ||
1113 | if (!prev) | |
1114 | return NULL; | |
1115 | ||
1116 | /* find last entry before the 'offset' */ | |
1117 | entry = prev; | |
1118 | if (entry->offset > offset) { | |
1119 | n = rb_prev(&entry->offset_index); | |
1120 | if (n) { | |
1121 | entry = rb_entry(n, struct btrfs_free_space, | |
1122 | offset_index); | |
1123 | BUG_ON(entry->offset > offset); | |
0f9dd46c | 1124 | } else { |
96303081 JB |
1125 | if (fuzzy) |
1126 | return entry; | |
1127 | else | |
1128 | return NULL; | |
0f9dd46c JB |
1129 | } |
1130 | } | |
1131 | ||
96303081 JB |
1132 | if (entry->bitmap) { |
1133 | n = &entry->offset_index; | |
1134 | while (1) { | |
1135 | n = rb_prev(n); | |
1136 | if (!n) | |
1137 | break; | |
1138 | prev = rb_entry(n, struct btrfs_free_space, | |
1139 | offset_index); | |
1140 | if (!prev->bitmap) { | |
1141 | if (prev->offset + prev->bytes > offset) | |
1142 | return prev; | |
1143 | break; | |
1144 | } | |
1145 | } | |
34d52cb6 | 1146 | if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) |
96303081 JB |
1147 | return entry; |
1148 | } else if (entry->offset + entry->bytes > offset) | |
1149 | return entry; | |
1150 | ||
1151 | if (!fuzzy) | |
1152 | return NULL; | |
1153 | ||
1154 | while (1) { | |
1155 | if (entry->bitmap) { | |
1156 | if (entry->offset + BITS_PER_BITMAP * | |
34d52cb6 | 1157 | ctl->unit > offset) |
96303081 JB |
1158 | break; |
1159 | } else { | |
1160 | if (entry->offset + entry->bytes > offset) | |
1161 | break; | |
1162 | } | |
1163 | ||
1164 | n = rb_next(&entry->offset_index); | |
1165 | if (!n) | |
1166 | return NULL; | |
1167 | entry = rb_entry(n, struct btrfs_free_space, offset_index); | |
1168 | } | |
1169 | return entry; | |
0f9dd46c JB |
1170 | } |
1171 | ||
f333adb5 | 1172 | static inline void |
34d52cb6 | 1173 | __unlink_free_space(struct btrfs_free_space_ctl *ctl, |
f333adb5 | 1174 | struct btrfs_free_space *info) |
0f9dd46c | 1175 | { |
34d52cb6 LZ |
1176 | rb_erase(&info->offset_index, &ctl->free_space_offset); |
1177 | ctl->free_extents--; | |
f333adb5 LZ |
1178 | } |
1179 | ||
34d52cb6 | 1180 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
f333adb5 LZ |
1181 | struct btrfs_free_space *info) |
1182 | { | |
34d52cb6 LZ |
1183 | __unlink_free_space(ctl, info); |
1184 | ctl->free_space -= info->bytes; | |
0f9dd46c JB |
1185 | } |
1186 | ||
34d52cb6 | 1187 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
0f9dd46c JB |
1188 | struct btrfs_free_space *info) |
1189 | { | |
1190 | int ret = 0; | |
1191 | ||
96303081 | 1192 | BUG_ON(!info->bitmap && !info->bytes); |
34d52cb6 | 1193 | ret = tree_insert_offset(&ctl->free_space_offset, info->offset, |
96303081 | 1194 | &info->offset_index, (info->bitmap != NULL)); |
0f9dd46c JB |
1195 | if (ret) |
1196 | return ret; | |
1197 | ||
34d52cb6 LZ |
1198 | ctl->free_space += info->bytes; |
1199 | ctl->free_extents++; | |
96303081 JB |
1200 | return ret; |
1201 | } | |
1202 | ||
34d52cb6 | 1203 | static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) |
96303081 | 1204 | { |
34d52cb6 | 1205 | struct btrfs_block_group_cache *block_group = ctl->private; |
25891f79 JB |
1206 | u64 max_bytes; |
1207 | u64 bitmap_bytes; | |
1208 | u64 extent_bytes; | |
8eb2d829 | 1209 | u64 size = block_group->key.offset; |
34d52cb6 LZ |
1210 | u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize; |
1211 | int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg); | |
1212 | ||
1213 | BUG_ON(ctl->total_bitmaps > max_bitmaps); | |
96303081 JB |
1214 | |
1215 | /* | |
1216 | * The goal is to keep the total amount of memory used per 1gb of space | |
1217 | * at or below 32k, so we need to adjust how much memory we allow to be | |
1218 | * used by extent based free space tracking | |
1219 | */ | |
8eb2d829 LZ |
1220 | if (size < 1024 * 1024 * 1024) |
1221 | max_bytes = MAX_CACHE_BYTES_PER_GIG; | |
1222 | else | |
1223 | max_bytes = MAX_CACHE_BYTES_PER_GIG * | |
1224 | div64_u64(size, 1024 * 1024 * 1024); | |
96303081 | 1225 | |
25891f79 JB |
1226 | /* |
1227 | * we want to account for 1 more bitmap than what we have so we can make | |
1228 | * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as | |
1229 | * we add more bitmaps. | |
1230 | */ | |
34d52cb6 | 1231 | bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE; |
96303081 | 1232 | |
25891f79 | 1233 | if (bitmap_bytes >= max_bytes) { |
34d52cb6 | 1234 | ctl->extents_thresh = 0; |
25891f79 JB |
1235 | return; |
1236 | } | |
96303081 | 1237 | |
25891f79 JB |
1238 | /* |
1239 | * we want the extent entry threshold to always be at most 1/2 the maxw | |
1240 | * bytes we can have, or whatever is less than that. | |
1241 | */ | |
1242 | extent_bytes = max_bytes - bitmap_bytes; | |
1243 | extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2)); | |
96303081 | 1244 | |
34d52cb6 | 1245 | ctl->extents_thresh = |
25891f79 | 1246 | div64_u64(extent_bytes, (sizeof(struct btrfs_free_space))); |
96303081 JB |
1247 | } |
1248 | ||
bb3ac5a4 MX |
1249 | static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
1250 | struct btrfs_free_space *info, | |
1251 | u64 offset, u64 bytes) | |
96303081 | 1252 | { |
f38b6e75 | 1253 | unsigned long start, count; |
96303081 | 1254 | |
34d52cb6 LZ |
1255 | start = offset_to_bit(info->offset, ctl->unit, offset); |
1256 | count = bytes_to_bits(bytes, ctl->unit); | |
f38b6e75 | 1257 | BUG_ON(start + count > BITS_PER_BITMAP); |
96303081 | 1258 | |
f38b6e75 | 1259 | bitmap_clear(info->bitmap, start, count); |
96303081 JB |
1260 | |
1261 | info->bytes -= bytes; | |
bb3ac5a4 MX |
1262 | } |
1263 | ||
1264 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, | |
1265 | struct btrfs_free_space *info, u64 offset, | |
1266 | u64 bytes) | |
1267 | { | |
1268 | __bitmap_clear_bits(ctl, info, offset, bytes); | |
34d52cb6 | 1269 | ctl->free_space -= bytes; |
96303081 JB |
1270 | } |
1271 | ||
34d52cb6 | 1272 | static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, |
817d52f8 JB |
1273 | struct btrfs_free_space *info, u64 offset, |
1274 | u64 bytes) | |
96303081 | 1275 | { |
f38b6e75 | 1276 | unsigned long start, count; |
96303081 | 1277 | |
34d52cb6 LZ |
1278 | start = offset_to_bit(info->offset, ctl->unit, offset); |
1279 | count = bytes_to_bits(bytes, ctl->unit); | |
f38b6e75 | 1280 | BUG_ON(start + count > BITS_PER_BITMAP); |
96303081 | 1281 | |
f38b6e75 | 1282 | bitmap_set(info->bitmap, start, count); |
96303081 JB |
1283 | |
1284 | info->bytes += bytes; | |
34d52cb6 | 1285 | ctl->free_space += bytes; |
96303081 JB |
1286 | } |
1287 | ||
34d52cb6 | 1288 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
96303081 JB |
1289 | struct btrfs_free_space *bitmap_info, u64 *offset, |
1290 | u64 *bytes) | |
1291 | { | |
1292 | unsigned long found_bits = 0; | |
1293 | unsigned long bits, i; | |
1294 | unsigned long next_zero; | |
1295 | ||
34d52cb6 | 1296 | i = offset_to_bit(bitmap_info->offset, ctl->unit, |
96303081 | 1297 | max_t(u64, *offset, bitmap_info->offset)); |
34d52cb6 | 1298 | bits = bytes_to_bits(*bytes, ctl->unit); |
96303081 JB |
1299 | |
1300 | for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i); | |
1301 | i < BITS_PER_BITMAP; | |
1302 | i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) { | |
1303 | next_zero = find_next_zero_bit(bitmap_info->bitmap, | |
1304 | BITS_PER_BITMAP, i); | |
1305 | if ((next_zero - i) >= bits) { | |
1306 | found_bits = next_zero - i; | |
1307 | break; | |
1308 | } | |
1309 | i = next_zero; | |
1310 | } | |
1311 | ||
1312 | if (found_bits) { | |
34d52cb6 LZ |
1313 | *offset = (u64)(i * ctl->unit) + bitmap_info->offset; |
1314 | *bytes = (u64)(found_bits) * ctl->unit; | |
96303081 JB |
1315 | return 0; |
1316 | } | |
1317 | ||
1318 | return -1; | |
1319 | } | |
1320 | ||
34d52cb6 LZ |
1321 | static struct btrfs_free_space * |
1322 | find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes) | |
96303081 JB |
1323 | { |
1324 | struct btrfs_free_space *entry; | |
1325 | struct rb_node *node; | |
1326 | int ret; | |
1327 | ||
34d52cb6 | 1328 | if (!ctl->free_space_offset.rb_node) |
96303081 JB |
1329 | return NULL; |
1330 | ||
34d52cb6 | 1331 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1); |
96303081 JB |
1332 | if (!entry) |
1333 | return NULL; | |
1334 | ||
1335 | for (node = &entry->offset_index; node; node = rb_next(node)) { | |
1336 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
1337 | if (entry->bytes < *bytes) | |
1338 | continue; | |
1339 | ||
1340 | if (entry->bitmap) { | |
34d52cb6 | 1341 | ret = search_bitmap(ctl, entry, offset, bytes); |
96303081 JB |
1342 | if (!ret) |
1343 | return entry; | |
1344 | continue; | |
1345 | } | |
1346 | ||
1347 | *offset = entry->offset; | |
1348 | *bytes = entry->bytes; | |
1349 | return entry; | |
1350 | } | |
1351 | ||
1352 | return NULL; | |
1353 | } | |
1354 | ||
34d52cb6 | 1355 | static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, |
96303081 JB |
1356 | struct btrfs_free_space *info, u64 offset) |
1357 | { | |
34d52cb6 | 1358 | info->offset = offset_to_bitmap(ctl, offset); |
f019f426 | 1359 | info->bytes = 0; |
34d52cb6 LZ |
1360 | link_free_space(ctl, info); |
1361 | ctl->total_bitmaps++; | |
96303081 | 1362 | |
34d52cb6 | 1363 | ctl->op->recalc_thresholds(ctl); |
96303081 JB |
1364 | } |
1365 | ||
34d52cb6 | 1366 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
edf6e2d1 LZ |
1367 | struct btrfs_free_space *bitmap_info) |
1368 | { | |
34d52cb6 | 1369 | unlink_free_space(ctl, bitmap_info); |
edf6e2d1 | 1370 | kfree(bitmap_info->bitmap); |
dc89e982 | 1371 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
34d52cb6 LZ |
1372 | ctl->total_bitmaps--; |
1373 | ctl->op->recalc_thresholds(ctl); | |
edf6e2d1 LZ |
1374 | } |
1375 | ||
34d52cb6 | 1376 | static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, |
96303081 JB |
1377 | struct btrfs_free_space *bitmap_info, |
1378 | u64 *offset, u64 *bytes) | |
1379 | { | |
1380 | u64 end; | |
6606bb97 JB |
1381 | u64 search_start, search_bytes; |
1382 | int ret; | |
96303081 JB |
1383 | |
1384 | again: | |
34d52cb6 | 1385 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; |
96303081 | 1386 | |
6606bb97 JB |
1387 | /* |
1388 | * XXX - this can go away after a few releases. | |
1389 | * | |
1390 | * since the only user of btrfs_remove_free_space is the tree logging | |
1391 | * stuff, and the only way to test that is under crash conditions, we | |
1392 | * want to have this debug stuff here just in case somethings not | |
1393 | * working. Search the bitmap for the space we are trying to use to | |
1394 | * make sure its actually there. If its not there then we need to stop | |
1395 | * because something has gone wrong. | |
1396 | */ | |
1397 | search_start = *offset; | |
1398 | search_bytes = *bytes; | |
13dbc089 | 1399 | search_bytes = min(search_bytes, end - search_start + 1); |
34d52cb6 | 1400 | ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes); |
6606bb97 JB |
1401 | BUG_ON(ret < 0 || search_start != *offset); |
1402 | ||
96303081 | 1403 | if (*offset > bitmap_info->offset && *offset + *bytes > end) { |
34d52cb6 | 1404 | bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1); |
96303081 JB |
1405 | *bytes -= end - *offset + 1; |
1406 | *offset = end + 1; | |
1407 | } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) { | |
34d52cb6 | 1408 | bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes); |
96303081 JB |
1409 | *bytes = 0; |
1410 | } | |
1411 | ||
1412 | if (*bytes) { | |
6606bb97 | 1413 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
edf6e2d1 | 1414 | if (!bitmap_info->bytes) |
34d52cb6 | 1415 | free_bitmap(ctl, bitmap_info); |
96303081 | 1416 | |
6606bb97 JB |
1417 | /* |
1418 | * no entry after this bitmap, but we still have bytes to | |
1419 | * remove, so something has gone wrong. | |
1420 | */ | |
1421 | if (!next) | |
96303081 JB |
1422 | return -EINVAL; |
1423 | ||
6606bb97 JB |
1424 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
1425 | offset_index); | |
1426 | ||
1427 | /* | |
1428 | * if the next entry isn't a bitmap we need to return to let the | |
1429 | * extent stuff do its work. | |
1430 | */ | |
96303081 JB |
1431 | if (!bitmap_info->bitmap) |
1432 | return -EAGAIN; | |
1433 | ||
6606bb97 JB |
1434 | /* |
1435 | * Ok the next item is a bitmap, but it may not actually hold | |
1436 | * the information for the rest of this free space stuff, so | |
1437 | * look for it, and if we don't find it return so we can try | |
1438 | * everything over again. | |
1439 | */ | |
1440 | search_start = *offset; | |
1441 | search_bytes = *bytes; | |
34d52cb6 | 1442 | ret = search_bitmap(ctl, bitmap_info, &search_start, |
6606bb97 JB |
1443 | &search_bytes); |
1444 | if (ret < 0 || search_start != *offset) | |
1445 | return -EAGAIN; | |
1446 | ||
96303081 | 1447 | goto again; |
edf6e2d1 | 1448 | } else if (!bitmap_info->bytes) |
34d52cb6 | 1449 | free_bitmap(ctl, bitmap_info); |
96303081 JB |
1450 | |
1451 | return 0; | |
1452 | } | |
1453 | ||
2cdc342c JB |
1454 | static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, |
1455 | struct btrfs_free_space *info, u64 offset, | |
1456 | u64 bytes) | |
1457 | { | |
1458 | u64 bytes_to_set = 0; | |
1459 | u64 end; | |
1460 | ||
1461 | end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); | |
1462 | ||
1463 | bytes_to_set = min(end - offset, bytes); | |
1464 | ||
1465 | bitmap_set_bits(ctl, info, offset, bytes_to_set); | |
1466 | ||
1467 | return bytes_to_set; | |
1468 | ||
1469 | } | |
1470 | ||
34d52cb6 LZ |
1471 | static bool use_bitmap(struct btrfs_free_space_ctl *ctl, |
1472 | struct btrfs_free_space *info) | |
96303081 | 1473 | { |
34d52cb6 | 1474 | struct btrfs_block_group_cache *block_group = ctl->private; |
96303081 JB |
1475 | |
1476 | /* | |
1477 | * If we are below the extents threshold then we can add this as an | |
1478 | * extent, and don't have to deal with the bitmap | |
1479 | */ | |
34d52cb6 | 1480 | if (ctl->free_extents < ctl->extents_thresh) { |
32cb0840 JB |
1481 | /* |
1482 | * If this block group has some small extents we don't want to | |
1483 | * use up all of our free slots in the cache with them, we want | |
1484 | * to reserve them to larger extents, however if we have plent | |
1485 | * of cache left then go ahead an dadd them, no sense in adding | |
1486 | * the overhead of a bitmap if we don't have to. | |
1487 | */ | |
1488 | if (info->bytes <= block_group->sectorsize * 4) { | |
34d52cb6 LZ |
1489 | if (ctl->free_extents * 2 <= ctl->extents_thresh) |
1490 | return false; | |
32cb0840 | 1491 | } else { |
34d52cb6 | 1492 | return false; |
32cb0840 JB |
1493 | } |
1494 | } | |
96303081 JB |
1495 | |
1496 | /* | |
1497 | * some block groups are so tiny they can't be enveloped by a bitmap, so | |
1498 | * don't even bother to create a bitmap for this | |
1499 | */ | |
1500 | if (BITS_PER_BITMAP * block_group->sectorsize > | |
1501 | block_group->key.offset) | |
34d52cb6 LZ |
1502 | return false; |
1503 | ||
1504 | return true; | |
1505 | } | |
1506 | ||
2cdc342c JB |
1507 | static struct btrfs_free_space_op free_space_op = { |
1508 | .recalc_thresholds = recalculate_thresholds, | |
1509 | .use_bitmap = use_bitmap, | |
1510 | }; | |
1511 | ||
34d52cb6 LZ |
1512 | static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, |
1513 | struct btrfs_free_space *info) | |
1514 | { | |
1515 | struct btrfs_free_space *bitmap_info; | |
2cdc342c | 1516 | struct btrfs_block_group_cache *block_group = NULL; |
34d52cb6 | 1517 | int added = 0; |
2cdc342c | 1518 | u64 bytes, offset, bytes_added; |
34d52cb6 | 1519 | int ret; |
96303081 JB |
1520 | |
1521 | bytes = info->bytes; | |
1522 | offset = info->offset; | |
1523 | ||
34d52cb6 LZ |
1524 | if (!ctl->op->use_bitmap(ctl, info)) |
1525 | return 0; | |
1526 | ||
2cdc342c JB |
1527 | if (ctl->op == &free_space_op) |
1528 | block_group = ctl->private; | |
38e87880 | 1529 | again: |
2cdc342c JB |
1530 | /* |
1531 | * Since we link bitmaps right into the cluster we need to see if we | |
1532 | * have a cluster here, and if so and it has our bitmap we need to add | |
1533 | * the free space to that bitmap. | |
1534 | */ | |
1535 | if (block_group && !list_empty(&block_group->cluster_list)) { | |
1536 | struct btrfs_free_cluster *cluster; | |
1537 | struct rb_node *node; | |
1538 | struct btrfs_free_space *entry; | |
1539 | ||
1540 | cluster = list_entry(block_group->cluster_list.next, | |
1541 | struct btrfs_free_cluster, | |
1542 | block_group_list); | |
1543 | spin_lock(&cluster->lock); | |
1544 | node = rb_first(&cluster->root); | |
1545 | if (!node) { | |
1546 | spin_unlock(&cluster->lock); | |
38e87880 | 1547 | goto no_cluster_bitmap; |
2cdc342c JB |
1548 | } |
1549 | ||
1550 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
1551 | if (!entry->bitmap) { | |
1552 | spin_unlock(&cluster->lock); | |
38e87880 | 1553 | goto no_cluster_bitmap; |
2cdc342c JB |
1554 | } |
1555 | ||
1556 | if (entry->offset == offset_to_bitmap(ctl, offset)) { | |
1557 | bytes_added = add_bytes_to_bitmap(ctl, entry, | |
1558 | offset, bytes); | |
1559 | bytes -= bytes_added; | |
1560 | offset += bytes_added; | |
1561 | } | |
1562 | spin_unlock(&cluster->lock); | |
1563 | if (!bytes) { | |
1564 | ret = 1; | |
1565 | goto out; | |
1566 | } | |
1567 | } | |
38e87880 CM |
1568 | |
1569 | no_cluster_bitmap: | |
34d52cb6 | 1570 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
96303081 JB |
1571 | 1, 0); |
1572 | if (!bitmap_info) { | |
1573 | BUG_ON(added); | |
1574 | goto new_bitmap; | |
1575 | } | |
1576 | ||
2cdc342c JB |
1577 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes); |
1578 | bytes -= bytes_added; | |
1579 | offset += bytes_added; | |
1580 | added = 0; | |
96303081 JB |
1581 | |
1582 | if (!bytes) { | |
1583 | ret = 1; | |
1584 | goto out; | |
1585 | } else | |
1586 | goto again; | |
1587 | ||
1588 | new_bitmap: | |
1589 | if (info && info->bitmap) { | |
34d52cb6 | 1590 | add_new_bitmap(ctl, info, offset); |
96303081 JB |
1591 | added = 1; |
1592 | info = NULL; | |
1593 | goto again; | |
1594 | } else { | |
34d52cb6 | 1595 | spin_unlock(&ctl->tree_lock); |
96303081 JB |
1596 | |
1597 | /* no pre-allocated info, allocate a new one */ | |
1598 | if (!info) { | |
dc89e982 JB |
1599 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
1600 | GFP_NOFS); | |
96303081 | 1601 | if (!info) { |
34d52cb6 | 1602 | spin_lock(&ctl->tree_lock); |
96303081 JB |
1603 | ret = -ENOMEM; |
1604 | goto out; | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | /* allocate the bitmap */ | |
1609 | info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); | |
34d52cb6 | 1610 | spin_lock(&ctl->tree_lock); |
96303081 JB |
1611 | if (!info->bitmap) { |
1612 | ret = -ENOMEM; | |
1613 | goto out; | |
1614 | } | |
1615 | goto again; | |
1616 | } | |
1617 | ||
1618 | out: | |
1619 | if (info) { | |
1620 | if (info->bitmap) | |
1621 | kfree(info->bitmap); | |
dc89e982 | 1622 | kmem_cache_free(btrfs_free_space_cachep, info); |
96303081 | 1623 | } |
0f9dd46c JB |
1624 | |
1625 | return ret; | |
1626 | } | |
1627 | ||
945d8962 | 1628 | static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, |
f333adb5 | 1629 | struct btrfs_free_space *info, bool update_stat) |
0f9dd46c | 1630 | { |
120d66ee LZ |
1631 | struct btrfs_free_space *left_info; |
1632 | struct btrfs_free_space *right_info; | |
1633 | bool merged = false; | |
1634 | u64 offset = info->offset; | |
1635 | u64 bytes = info->bytes; | |
6226cb0a | 1636 | |
0f9dd46c JB |
1637 | /* |
1638 | * first we want to see if there is free space adjacent to the range we | |
1639 | * are adding, if there is remove that struct and add a new one to | |
1640 | * cover the entire range | |
1641 | */ | |
34d52cb6 | 1642 | right_info = tree_search_offset(ctl, offset + bytes, 0, 0); |
96303081 JB |
1643 | if (right_info && rb_prev(&right_info->offset_index)) |
1644 | left_info = rb_entry(rb_prev(&right_info->offset_index), | |
1645 | struct btrfs_free_space, offset_index); | |
1646 | else | |
34d52cb6 | 1647 | left_info = tree_search_offset(ctl, offset - 1, 0, 0); |
0f9dd46c | 1648 | |
96303081 | 1649 | if (right_info && !right_info->bitmap) { |
f333adb5 | 1650 | if (update_stat) |
34d52cb6 | 1651 | unlink_free_space(ctl, right_info); |
f333adb5 | 1652 | else |
34d52cb6 | 1653 | __unlink_free_space(ctl, right_info); |
6226cb0a | 1654 | info->bytes += right_info->bytes; |
dc89e982 | 1655 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
120d66ee | 1656 | merged = true; |
0f9dd46c JB |
1657 | } |
1658 | ||
96303081 JB |
1659 | if (left_info && !left_info->bitmap && |
1660 | left_info->offset + left_info->bytes == offset) { | |
f333adb5 | 1661 | if (update_stat) |
34d52cb6 | 1662 | unlink_free_space(ctl, left_info); |
f333adb5 | 1663 | else |
34d52cb6 | 1664 | __unlink_free_space(ctl, left_info); |
6226cb0a JB |
1665 | info->offset = left_info->offset; |
1666 | info->bytes += left_info->bytes; | |
dc89e982 | 1667 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
120d66ee | 1668 | merged = true; |
0f9dd46c JB |
1669 | } |
1670 | ||
120d66ee LZ |
1671 | return merged; |
1672 | } | |
1673 | ||
581bb050 LZ |
1674 | int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl, |
1675 | u64 offset, u64 bytes) | |
120d66ee LZ |
1676 | { |
1677 | struct btrfs_free_space *info; | |
1678 | int ret = 0; | |
1679 | ||
dc89e982 | 1680 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
120d66ee LZ |
1681 | if (!info) |
1682 | return -ENOMEM; | |
1683 | ||
1684 | info->offset = offset; | |
1685 | info->bytes = bytes; | |
1686 | ||
34d52cb6 | 1687 | spin_lock(&ctl->tree_lock); |
120d66ee | 1688 | |
34d52cb6 | 1689 | if (try_merge_free_space(ctl, info, true)) |
120d66ee LZ |
1690 | goto link; |
1691 | ||
1692 | /* | |
1693 | * There was no extent directly to the left or right of this new | |
1694 | * extent then we know we're going to have to allocate a new extent, so | |
1695 | * before we do that see if we need to drop this into a bitmap | |
1696 | */ | |
34d52cb6 | 1697 | ret = insert_into_bitmap(ctl, info); |
120d66ee LZ |
1698 | if (ret < 0) { |
1699 | goto out; | |
1700 | } else if (ret) { | |
1701 | ret = 0; | |
1702 | goto out; | |
1703 | } | |
1704 | link: | |
34d52cb6 | 1705 | ret = link_free_space(ctl, info); |
0f9dd46c | 1706 | if (ret) |
dc89e982 | 1707 | kmem_cache_free(btrfs_free_space_cachep, info); |
96303081 | 1708 | out: |
34d52cb6 | 1709 | spin_unlock(&ctl->tree_lock); |
6226cb0a | 1710 | |
0f9dd46c | 1711 | if (ret) { |
96303081 | 1712 | printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret); |
c293498b | 1713 | BUG_ON(ret == -EEXIST); |
0f9dd46c JB |
1714 | } |
1715 | ||
0f9dd46c JB |
1716 | return ret; |
1717 | } | |
1718 | ||
6226cb0a JB |
1719 | int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group, |
1720 | u64 offset, u64 bytes) | |
0f9dd46c | 1721 | { |
34d52cb6 | 1722 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
0f9dd46c | 1723 | struct btrfs_free_space *info; |
96303081 | 1724 | struct btrfs_free_space *next_info = NULL; |
0f9dd46c JB |
1725 | int ret = 0; |
1726 | ||
34d52cb6 | 1727 | spin_lock(&ctl->tree_lock); |
6226cb0a | 1728 | |
96303081 | 1729 | again: |
34d52cb6 | 1730 | info = tree_search_offset(ctl, offset, 0, 0); |
96303081 | 1731 | if (!info) { |
6606bb97 JB |
1732 | /* |
1733 | * oops didn't find an extent that matched the space we wanted | |
1734 | * to remove, look for a bitmap instead | |
1735 | */ | |
34d52cb6 | 1736 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
6606bb97 JB |
1737 | 1, 0); |
1738 | if (!info) { | |
1739 | WARN_ON(1); | |
1740 | goto out_lock; | |
1741 | } | |
96303081 JB |
1742 | } |
1743 | ||
1744 | if (info->bytes < bytes && rb_next(&info->offset_index)) { | |
1745 | u64 end; | |
1746 | next_info = rb_entry(rb_next(&info->offset_index), | |
1747 | struct btrfs_free_space, | |
1748 | offset_index); | |
1749 | ||
1750 | if (next_info->bitmap) | |
34d52cb6 LZ |
1751 | end = next_info->offset + |
1752 | BITS_PER_BITMAP * ctl->unit - 1; | |
96303081 JB |
1753 | else |
1754 | end = next_info->offset + next_info->bytes; | |
1755 | ||
1756 | if (next_info->bytes < bytes || | |
1757 | next_info->offset > offset || offset > end) { | |
1758 | printk(KERN_CRIT "Found free space at %llu, size %llu," | |
1759 | " trying to use %llu\n", | |
1760 | (unsigned long long)info->offset, | |
1761 | (unsigned long long)info->bytes, | |
1762 | (unsigned long long)bytes); | |
0f9dd46c JB |
1763 | WARN_ON(1); |
1764 | ret = -EINVAL; | |
96303081 | 1765 | goto out_lock; |
0f9dd46c | 1766 | } |
0f9dd46c | 1767 | |
96303081 JB |
1768 | info = next_info; |
1769 | } | |
1770 | ||
1771 | if (info->bytes == bytes) { | |
34d52cb6 | 1772 | unlink_free_space(ctl, info); |
96303081 JB |
1773 | if (info->bitmap) { |
1774 | kfree(info->bitmap); | |
34d52cb6 | 1775 | ctl->total_bitmaps--; |
0f9dd46c | 1776 | } |
dc89e982 | 1777 | kmem_cache_free(btrfs_free_space_cachep, info); |
96303081 JB |
1778 | goto out_lock; |
1779 | } | |
0f9dd46c | 1780 | |
96303081 | 1781 | if (!info->bitmap && info->offset == offset) { |
34d52cb6 | 1782 | unlink_free_space(ctl, info); |
0f9dd46c JB |
1783 | info->offset += bytes; |
1784 | info->bytes -= bytes; | |
34d52cb6 | 1785 | link_free_space(ctl, info); |
96303081 JB |
1786 | goto out_lock; |
1787 | } | |
0f9dd46c | 1788 | |
96303081 JB |
1789 | if (!info->bitmap && info->offset <= offset && |
1790 | info->offset + info->bytes >= offset + bytes) { | |
9b49c9b9 CM |
1791 | u64 old_start = info->offset; |
1792 | /* | |
1793 | * we're freeing space in the middle of the info, | |
1794 | * this can happen during tree log replay | |
1795 | * | |
1796 | * first unlink the old info and then | |
1797 | * insert it again after the hole we're creating | |
1798 | */ | |
34d52cb6 | 1799 | unlink_free_space(ctl, info); |
9b49c9b9 CM |
1800 | if (offset + bytes < info->offset + info->bytes) { |
1801 | u64 old_end = info->offset + info->bytes; | |
1802 | ||
1803 | info->offset = offset + bytes; | |
1804 | info->bytes = old_end - info->offset; | |
34d52cb6 | 1805 | ret = link_free_space(ctl, info); |
96303081 JB |
1806 | WARN_ON(ret); |
1807 | if (ret) | |
1808 | goto out_lock; | |
9b49c9b9 CM |
1809 | } else { |
1810 | /* the hole we're creating ends at the end | |
1811 | * of the info struct, just free the info | |
1812 | */ | |
dc89e982 | 1813 | kmem_cache_free(btrfs_free_space_cachep, info); |
9b49c9b9 | 1814 | } |
34d52cb6 | 1815 | spin_unlock(&ctl->tree_lock); |
96303081 JB |
1816 | |
1817 | /* step two, insert a new info struct to cover | |
1818 | * anything before the hole | |
9b49c9b9 | 1819 | */ |
6226cb0a JB |
1820 | ret = btrfs_add_free_space(block_group, old_start, |
1821 | offset - old_start); | |
96303081 JB |
1822 | WARN_ON(ret); |
1823 | goto out; | |
0f9dd46c | 1824 | } |
96303081 | 1825 | |
34d52cb6 | 1826 | ret = remove_from_bitmap(ctl, info, &offset, &bytes); |
96303081 JB |
1827 | if (ret == -EAGAIN) |
1828 | goto again; | |
1829 | BUG_ON(ret); | |
1830 | out_lock: | |
34d52cb6 | 1831 | spin_unlock(&ctl->tree_lock); |
0f9dd46c | 1832 | out: |
25179201 JB |
1833 | return ret; |
1834 | } | |
1835 | ||
0f9dd46c JB |
1836 | void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group, |
1837 | u64 bytes) | |
1838 | { | |
34d52cb6 | 1839 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
0f9dd46c JB |
1840 | struct btrfs_free_space *info; |
1841 | struct rb_node *n; | |
1842 | int count = 0; | |
1843 | ||
34d52cb6 | 1844 | for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { |
0f9dd46c JB |
1845 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
1846 | if (info->bytes >= bytes) | |
1847 | count++; | |
96303081 | 1848 | printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n", |
21380931 | 1849 | (unsigned long long)info->offset, |
96303081 JB |
1850 | (unsigned long long)info->bytes, |
1851 | (info->bitmap) ? "yes" : "no"); | |
0f9dd46c | 1852 | } |
96303081 JB |
1853 | printk(KERN_INFO "block group has cluster?: %s\n", |
1854 | list_empty(&block_group->cluster_list) ? "no" : "yes"); | |
0f9dd46c JB |
1855 | printk(KERN_INFO "%d blocks of free space at or bigger than bytes is" |
1856 | "\n", count); | |
1857 | } | |
1858 | ||
34d52cb6 | 1859 | void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group) |
0f9dd46c | 1860 | { |
34d52cb6 | 1861 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
0f9dd46c | 1862 | |
34d52cb6 LZ |
1863 | spin_lock_init(&ctl->tree_lock); |
1864 | ctl->unit = block_group->sectorsize; | |
1865 | ctl->start = block_group->key.objectid; | |
1866 | ctl->private = block_group; | |
1867 | ctl->op = &free_space_op; | |
0f9dd46c | 1868 | |
34d52cb6 LZ |
1869 | /* |
1870 | * we only want to have 32k of ram per block group for keeping | |
1871 | * track of free space, and if we pass 1/2 of that we want to | |
1872 | * start converting things over to using bitmaps | |
1873 | */ | |
1874 | ctl->extents_thresh = ((1024 * 32) / 2) / | |
1875 | sizeof(struct btrfs_free_space); | |
0f9dd46c JB |
1876 | } |
1877 | ||
fa9c0d79 CM |
1878 | /* |
1879 | * for a given cluster, put all of its extents back into the free | |
1880 | * space cache. If the block group passed doesn't match the block group | |
1881 | * pointed to by the cluster, someone else raced in and freed the | |
1882 | * cluster already. In that case, we just return without changing anything | |
1883 | */ | |
1884 | static int | |
1885 | __btrfs_return_cluster_to_free_space( | |
1886 | struct btrfs_block_group_cache *block_group, | |
1887 | struct btrfs_free_cluster *cluster) | |
1888 | { | |
34d52cb6 | 1889 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
fa9c0d79 CM |
1890 | struct btrfs_free_space *entry; |
1891 | struct rb_node *node; | |
1892 | ||
1893 | spin_lock(&cluster->lock); | |
1894 | if (cluster->block_group != block_group) | |
1895 | goto out; | |
1896 | ||
96303081 | 1897 | cluster->block_group = NULL; |
fa9c0d79 | 1898 | cluster->window_start = 0; |
96303081 | 1899 | list_del_init(&cluster->block_group_list); |
96303081 | 1900 | |
fa9c0d79 | 1901 | node = rb_first(&cluster->root); |
96303081 | 1902 | while (node) { |
4e69b598 JB |
1903 | bool bitmap; |
1904 | ||
fa9c0d79 CM |
1905 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
1906 | node = rb_next(&entry->offset_index); | |
1907 | rb_erase(&entry->offset_index, &cluster->root); | |
4e69b598 JB |
1908 | |
1909 | bitmap = (entry->bitmap != NULL); | |
1910 | if (!bitmap) | |
34d52cb6 LZ |
1911 | try_merge_free_space(ctl, entry, false); |
1912 | tree_insert_offset(&ctl->free_space_offset, | |
4e69b598 | 1913 | entry->offset, &entry->offset_index, bitmap); |
fa9c0d79 | 1914 | } |
6bef4d31 | 1915 | cluster->root = RB_ROOT; |
96303081 | 1916 | |
fa9c0d79 CM |
1917 | out: |
1918 | spin_unlock(&cluster->lock); | |
96303081 | 1919 | btrfs_put_block_group(block_group); |
fa9c0d79 CM |
1920 | return 0; |
1921 | } | |
1922 | ||
09655373 | 1923 | void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl) |
0f9dd46c JB |
1924 | { |
1925 | struct btrfs_free_space *info; | |
1926 | struct rb_node *node; | |
581bb050 | 1927 | |
581bb050 LZ |
1928 | while ((node = rb_last(&ctl->free_space_offset)) != NULL) { |
1929 | info = rb_entry(node, struct btrfs_free_space, offset_index); | |
9b90f513 JB |
1930 | if (!info->bitmap) { |
1931 | unlink_free_space(ctl, info); | |
1932 | kmem_cache_free(btrfs_free_space_cachep, info); | |
1933 | } else { | |
1934 | free_bitmap(ctl, info); | |
1935 | } | |
581bb050 LZ |
1936 | if (need_resched()) { |
1937 | spin_unlock(&ctl->tree_lock); | |
1938 | cond_resched(); | |
1939 | spin_lock(&ctl->tree_lock); | |
1940 | } | |
1941 | } | |
09655373 CM |
1942 | } |
1943 | ||
1944 | void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) | |
1945 | { | |
1946 | spin_lock(&ctl->tree_lock); | |
1947 | __btrfs_remove_free_space_cache_locked(ctl); | |
581bb050 LZ |
1948 | spin_unlock(&ctl->tree_lock); |
1949 | } | |
1950 | ||
1951 | void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group) | |
1952 | { | |
1953 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; | |
fa9c0d79 | 1954 | struct btrfs_free_cluster *cluster; |
96303081 | 1955 | struct list_head *head; |
0f9dd46c | 1956 | |
34d52cb6 | 1957 | spin_lock(&ctl->tree_lock); |
96303081 JB |
1958 | while ((head = block_group->cluster_list.next) != |
1959 | &block_group->cluster_list) { | |
1960 | cluster = list_entry(head, struct btrfs_free_cluster, | |
1961 | block_group_list); | |
fa9c0d79 CM |
1962 | |
1963 | WARN_ON(cluster->block_group != block_group); | |
1964 | __btrfs_return_cluster_to_free_space(block_group, cluster); | |
96303081 | 1965 | if (need_resched()) { |
34d52cb6 | 1966 | spin_unlock(&ctl->tree_lock); |
96303081 | 1967 | cond_resched(); |
34d52cb6 | 1968 | spin_lock(&ctl->tree_lock); |
96303081 | 1969 | } |
fa9c0d79 | 1970 | } |
09655373 | 1971 | __btrfs_remove_free_space_cache_locked(ctl); |
34d52cb6 | 1972 | spin_unlock(&ctl->tree_lock); |
fa9c0d79 | 1973 | |
0f9dd46c JB |
1974 | } |
1975 | ||
6226cb0a JB |
1976 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group, |
1977 | u64 offset, u64 bytes, u64 empty_size) | |
0f9dd46c | 1978 | { |
34d52cb6 | 1979 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
6226cb0a | 1980 | struct btrfs_free_space *entry = NULL; |
96303081 | 1981 | u64 bytes_search = bytes + empty_size; |
6226cb0a | 1982 | u64 ret = 0; |
0f9dd46c | 1983 | |
34d52cb6 LZ |
1984 | spin_lock(&ctl->tree_lock); |
1985 | entry = find_free_space(ctl, &offset, &bytes_search); | |
6226cb0a | 1986 | if (!entry) |
96303081 JB |
1987 | goto out; |
1988 | ||
1989 | ret = offset; | |
1990 | if (entry->bitmap) { | |
34d52cb6 | 1991 | bitmap_clear_bits(ctl, entry, offset, bytes); |
edf6e2d1 | 1992 | if (!entry->bytes) |
34d52cb6 | 1993 | free_bitmap(ctl, entry); |
96303081 | 1994 | } else { |
34d52cb6 | 1995 | unlink_free_space(ctl, entry); |
6226cb0a JB |
1996 | entry->offset += bytes; |
1997 | entry->bytes -= bytes; | |
6226cb0a | 1998 | if (!entry->bytes) |
dc89e982 | 1999 | kmem_cache_free(btrfs_free_space_cachep, entry); |
6226cb0a | 2000 | else |
34d52cb6 | 2001 | link_free_space(ctl, entry); |
6226cb0a | 2002 | } |
0f9dd46c | 2003 | |
96303081 | 2004 | out: |
34d52cb6 | 2005 | spin_unlock(&ctl->tree_lock); |
817d52f8 | 2006 | |
0f9dd46c JB |
2007 | return ret; |
2008 | } | |
fa9c0d79 CM |
2009 | |
2010 | /* | |
2011 | * given a cluster, put all of its extents back into the free space | |
2012 | * cache. If a block group is passed, this function will only free | |
2013 | * a cluster that belongs to the passed block group. | |
2014 | * | |
2015 | * Otherwise, it'll get a reference on the block group pointed to by the | |
2016 | * cluster and remove the cluster from it. | |
2017 | */ | |
2018 | int btrfs_return_cluster_to_free_space( | |
2019 | struct btrfs_block_group_cache *block_group, | |
2020 | struct btrfs_free_cluster *cluster) | |
2021 | { | |
34d52cb6 | 2022 | struct btrfs_free_space_ctl *ctl; |
fa9c0d79 CM |
2023 | int ret; |
2024 | ||
2025 | /* first, get a safe pointer to the block group */ | |
2026 | spin_lock(&cluster->lock); | |
2027 | if (!block_group) { | |
2028 | block_group = cluster->block_group; | |
2029 | if (!block_group) { | |
2030 | spin_unlock(&cluster->lock); | |
2031 | return 0; | |
2032 | } | |
2033 | } else if (cluster->block_group != block_group) { | |
2034 | /* someone else has already freed it don't redo their work */ | |
2035 | spin_unlock(&cluster->lock); | |
2036 | return 0; | |
2037 | } | |
2038 | atomic_inc(&block_group->count); | |
2039 | spin_unlock(&cluster->lock); | |
2040 | ||
34d52cb6 LZ |
2041 | ctl = block_group->free_space_ctl; |
2042 | ||
fa9c0d79 | 2043 | /* now return any extents the cluster had on it */ |
34d52cb6 | 2044 | spin_lock(&ctl->tree_lock); |
fa9c0d79 | 2045 | ret = __btrfs_return_cluster_to_free_space(block_group, cluster); |
34d52cb6 | 2046 | spin_unlock(&ctl->tree_lock); |
fa9c0d79 CM |
2047 | |
2048 | /* finally drop our ref */ | |
2049 | btrfs_put_block_group(block_group); | |
2050 | return ret; | |
2051 | } | |
2052 | ||
96303081 JB |
2053 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, |
2054 | struct btrfs_free_cluster *cluster, | |
4e69b598 | 2055 | struct btrfs_free_space *entry, |
96303081 JB |
2056 | u64 bytes, u64 min_start) |
2057 | { | |
34d52cb6 | 2058 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
96303081 JB |
2059 | int err; |
2060 | u64 search_start = cluster->window_start; | |
2061 | u64 search_bytes = bytes; | |
2062 | u64 ret = 0; | |
2063 | ||
96303081 JB |
2064 | search_start = min_start; |
2065 | search_bytes = bytes; | |
2066 | ||
34d52cb6 | 2067 | err = search_bitmap(ctl, entry, &search_start, &search_bytes); |
96303081 | 2068 | if (err) |
4e69b598 | 2069 | return 0; |
96303081 JB |
2070 | |
2071 | ret = search_start; | |
bb3ac5a4 | 2072 | __bitmap_clear_bits(ctl, entry, ret, bytes); |
96303081 JB |
2073 | |
2074 | return ret; | |
2075 | } | |
2076 | ||
fa9c0d79 CM |
2077 | /* |
2078 | * given a cluster, try to allocate 'bytes' from it, returns 0 | |
2079 | * if it couldn't find anything suitably large, or a logical disk offset | |
2080 | * if things worked out | |
2081 | */ | |
2082 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, | |
2083 | struct btrfs_free_cluster *cluster, u64 bytes, | |
2084 | u64 min_start) | |
2085 | { | |
34d52cb6 | 2086 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
fa9c0d79 CM |
2087 | struct btrfs_free_space *entry = NULL; |
2088 | struct rb_node *node; | |
2089 | u64 ret = 0; | |
2090 | ||
2091 | spin_lock(&cluster->lock); | |
2092 | if (bytes > cluster->max_size) | |
2093 | goto out; | |
2094 | ||
2095 | if (cluster->block_group != block_group) | |
2096 | goto out; | |
2097 | ||
2098 | node = rb_first(&cluster->root); | |
2099 | if (!node) | |
2100 | goto out; | |
2101 | ||
2102 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
fa9c0d79 | 2103 | while(1) { |
4e69b598 JB |
2104 | if (entry->bytes < bytes || |
2105 | (!entry->bitmap && entry->offset < min_start)) { | |
fa9c0d79 CM |
2106 | node = rb_next(&entry->offset_index); |
2107 | if (!node) | |
2108 | break; | |
2109 | entry = rb_entry(node, struct btrfs_free_space, | |
2110 | offset_index); | |
2111 | continue; | |
2112 | } | |
fa9c0d79 | 2113 | |
4e69b598 JB |
2114 | if (entry->bitmap) { |
2115 | ret = btrfs_alloc_from_bitmap(block_group, | |
2116 | cluster, entry, bytes, | |
2117 | min_start); | |
2118 | if (ret == 0) { | |
4e69b598 JB |
2119 | node = rb_next(&entry->offset_index); |
2120 | if (!node) | |
2121 | break; | |
2122 | entry = rb_entry(node, struct btrfs_free_space, | |
2123 | offset_index); | |
2124 | continue; | |
2125 | } | |
2126 | } else { | |
4e69b598 JB |
2127 | ret = entry->offset; |
2128 | ||
2129 | entry->offset += bytes; | |
2130 | entry->bytes -= bytes; | |
2131 | } | |
fa9c0d79 | 2132 | |
5e71b5d5 | 2133 | if (entry->bytes == 0) |
fa9c0d79 | 2134 | rb_erase(&entry->offset_index, &cluster->root); |
fa9c0d79 CM |
2135 | break; |
2136 | } | |
2137 | out: | |
2138 | spin_unlock(&cluster->lock); | |
96303081 | 2139 | |
5e71b5d5 LZ |
2140 | if (!ret) |
2141 | return 0; | |
2142 | ||
34d52cb6 | 2143 | spin_lock(&ctl->tree_lock); |
5e71b5d5 | 2144 | |
34d52cb6 | 2145 | ctl->free_space -= bytes; |
5e71b5d5 | 2146 | if (entry->bytes == 0) { |
34d52cb6 | 2147 | ctl->free_extents--; |
4e69b598 JB |
2148 | if (entry->bitmap) { |
2149 | kfree(entry->bitmap); | |
34d52cb6 LZ |
2150 | ctl->total_bitmaps--; |
2151 | ctl->op->recalc_thresholds(ctl); | |
4e69b598 | 2152 | } |
dc89e982 | 2153 | kmem_cache_free(btrfs_free_space_cachep, entry); |
5e71b5d5 LZ |
2154 | } |
2155 | ||
34d52cb6 | 2156 | spin_unlock(&ctl->tree_lock); |
5e71b5d5 | 2157 | |
fa9c0d79 CM |
2158 | return ret; |
2159 | } | |
2160 | ||
96303081 JB |
2161 | static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group, |
2162 | struct btrfs_free_space *entry, | |
2163 | struct btrfs_free_cluster *cluster, | |
2164 | u64 offset, u64 bytes, u64 min_bytes) | |
2165 | { | |
34d52cb6 | 2166 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
96303081 JB |
2167 | unsigned long next_zero; |
2168 | unsigned long i; | |
2169 | unsigned long search_bits; | |
2170 | unsigned long total_bits; | |
2171 | unsigned long found_bits; | |
2172 | unsigned long start = 0; | |
2173 | unsigned long total_found = 0; | |
4e69b598 | 2174 | int ret; |
96303081 JB |
2175 | bool found = false; |
2176 | ||
2177 | i = offset_to_bit(entry->offset, block_group->sectorsize, | |
2178 | max_t(u64, offset, entry->offset)); | |
d0a365e8 JB |
2179 | search_bits = bytes_to_bits(bytes, block_group->sectorsize); |
2180 | total_bits = bytes_to_bits(min_bytes, block_group->sectorsize); | |
96303081 JB |
2181 | |
2182 | again: | |
2183 | found_bits = 0; | |
2184 | for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i); | |
2185 | i < BITS_PER_BITMAP; | |
2186 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) { | |
2187 | next_zero = find_next_zero_bit(entry->bitmap, | |
2188 | BITS_PER_BITMAP, i); | |
2189 | if (next_zero - i >= search_bits) { | |
2190 | found_bits = next_zero - i; | |
2191 | break; | |
2192 | } | |
2193 | i = next_zero; | |
2194 | } | |
2195 | ||
2196 | if (!found_bits) | |
4e69b598 | 2197 | return -ENOSPC; |
96303081 JB |
2198 | |
2199 | if (!found) { | |
2200 | start = i; | |
2201 | found = true; | |
2202 | } | |
2203 | ||
2204 | total_found += found_bits; | |
2205 | ||
2206 | if (cluster->max_size < found_bits * block_group->sectorsize) | |
2207 | cluster->max_size = found_bits * block_group->sectorsize; | |
2208 | ||
2209 | if (total_found < total_bits) { | |
2210 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero); | |
2211 | if (i - start > total_bits * 2) { | |
2212 | total_found = 0; | |
2213 | cluster->max_size = 0; | |
2214 | found = false; | |
2215 | } | |
2216 | goto again; | |
2217 | } | |
2218 | ||
2219 | cluster->window_start = start * block_group->sectorsize + | |
2220 | entry->offset; | |
34d52cb6 | 2221 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
4e69b598 JB |
2222 | ret = tree_insert_offset(&cluster->root, entry->offset, |
2223 | &entry->offset_index, 1); | |
2224 | BUG_ON(ret); | |
96303081 JB |
2225 | |
2226 | return 0; | |
2227 | } | |
2228 | ||
4e69b598 JB |
2229 | /* |
2230 | * This searches the block group for just extents to fill the cluster with. | |
2231 | */ | |
3de85bb9 JB |
2232 | static noinline int |
2233 | setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, | |
2234 | struct btrfs_free_cluster *cluster, | |
2235 | struct list_head *bitmaps, u64 offset, u64 bytes, | |
2236 | u64 min_bytes) | |
4e69b598 | 2237 | { |
34d52cb6 | 2238 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
4e69b598 JB |
2239 | struct btrfs_free_space *first = NULL; |
2240 | struct btrfs_free_space *entry = NULL; | |
2241 | struct btrfs_free_space *prev = NULL; | |
2242 | struct btrfs_free_space *last; | |
2243 | struct rb_node *node; | |
2244 | u64 window_start; | |
2245 | u64 window_free; | |
2246 | u64 max_extent; | |
2247 | u64 max_gap = 128 * 1024; | |
2248 | ||
34d52cb6 | 2249 | entry = tree_search_offset(ctl, offset, 0, 1); |
4e69b598 JB |
2250 | if (!entry) |
2251 | return -ENOSPC; | |
2252 | ||
2253 | /* | |
2254 | * We don't want bitmaps, so just move along until we find a normal | |
2255 | * extent entry. | |
2256 | */ | |
2257 | while (entry->bitmap) { | |
86d4a77b JB |
2258 | if (list_empty(&entry->list)) |
2259 | list_add_tail(&entry->list, bitmaps); | |
4e69b598 JB |
2260 | node = rb_next(&entry->offset_index); |
2261 | if (!node) | |
2262 | return -ENOSPC; | |
2263 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2264 | } | |
2265 | ||
2266 | window_start = entry->offset; | |
2267 | window_free = entry->bytes; | |
2268 | max_extent = entry->bytes; | |
2269 | first = entry; | |
2270 | last = entry; | |
2271 | prev = entry; | |
2272 | ||
2273 | while (window_free <= min_bytes) { | |
2274 | node = rb_next(&entry->offset_index); | |
2275 | if (!node) | |
2276 | return -ENOSPC; | |
2277 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2278 | ||
86d4a77b JB |
2279 | if (entry->bitmap) { |
2280 | if (list_empty(&entry->list)) | |
2281 | list_add_tail(&entry->list, bitmaps); | |
4e69b598 | 2282 | continue; |
86d4a77b JB |
2283 | } |
2284 | ||
4e69b598 JB |
2285 | /* |
2286 | * we haven't filled the empty size and the window is | |
2287 | * very large. reset and try again | |
2288 | */ | |
2289 | if (entry->offset - (prev->offset + prev->bytes) > max_gap || | |
2290 | entry->offset - window_start > (min_bytes * 2)) { | |
2291 | first = entry; | |
2292 | window_start = entry->offset; | |
2293 | window_free = entry->bytes; | |
2294 | last = entry; | |
2295 | max_extent = entry->bytes; | |
2296 | } else { | |
2297 | last = entry; | |
2298 | window_free += entry->bytes; | |
2299 | if (entry->bytes > max_extent) | |
2300 | max_extent = entry->bytes; | |
2301 | } | |
2302 | prev = entry; | |
2303 | } | |
2304 | ||
2305 | cluster->window_start = first->offset; | |
2306 | ||
2307 | node = &first->offset_index; | |
2308 | ||
2309 | /* | |
2310 | * now we've found our entries, pull them out of the free space | |
2311 | * cache and put them into the cluster rbtree | |
2312 | */ | |
2313 | do { | |
2314 | int ret; | |
2315 | ||
2316 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2317 | node = rb_next(&entry->offset_index); | |
2318 | if (entry->bitmap) | |
2319 | continue; | |
2320 | ||
34d52cb6 | 2321 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
4e69b598 JB |
2322 | ret = tree_insert_offset(&cluster->root, entry->offset, |
2323 | &entry->offset_index, 0); | |
2324 | BUG_ON(ret); | |
2325 | } while (node && entry != last); | |
2326 | ||
2327 | cluster->max_size = max_extent; | |
2328 | ||
2329 | return 0; | |
2330 | } | |
2331 | ||
2332 | /* | |
2333 | * This specifically looks for bitmaps that may work in the cluster, we assume | |
2334 | * that we have already failed to find extents that will work. | |
2335 | */ | |
3de85bb9 JB |
2336 | static noinline int |
2337 | setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, | |
2338 | struct btrfs_free_cluster *cluster, | |
2339 | struct list_head *bitmaps, u64 offset, u64 bytes, | |
2340 | u64 min_bytes) | |
4e69b598 | 2341 | { |
34d52cb6 | 2342 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
4e69b598 JB |
2343 | struct btrfs_free_space *entry; |
2344 | struct rb_node *node; | |
2345 | int ret = -ENOSPC; | |
2346 | ||
34d52cb6 | 2347 | if (ctl->total_bitmaps == 0) |
4e69b598 JB |
2348 | return -ENOSPC; |
2349 | ||
86d4a77b JB |
2350 | /* |
2351 | * First check our cached list of bitmaps and see if there is an entry | |
2352 | * here that will work. | |
2353 | */ | |
2354 | list_for_each_entry(entry, bitmaps, list) { | |
2355 | if (entry->bytes < min_bytes) | |
2356 | continue; | |
2357 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, | |
2358 | bytes, min_bytes); | |
2359 | if (!ret) | |
2360 | return 0; | |
2361 | } | |
2362 | ||
2363 | /* | |
2364 | * If we do have entries on our list and we are here then we didn't find | |
2365 | * anything, so go ahead and get the next entry after the last entry in | |
2366 | * this list and start the search from there. | |
2367 | */ | |
2368 | if (!list_empty(bitmaps)) { | |
2369 | entry = list_entry(bitmaps->prev, struct btrfs_free_space, | |
2370 | list); | |
2371 | node = rb_next(&entry->offset_index); | |
2372 | if (!node) | |
2373 | return -ENOSPC; | |
2374 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2375 | goto search; | |
2376 | } | |
2377 | ||
34d52cb6 | 2378 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1); |
4e69b598 JB |
2379 | if (!entry) |
2380 | return -ENOSPC; | |
2381 | ||
86d4a77b | 2382 | search: |
4e69b598 JB |
2383 | node = &entry->offset_index; |
2384 | do { | |
2385 | entry = rb_entry(node, struct btrfs_free_space, offset_index); | |
2386 | node = rb_next(&entry->offset_index); | |
2387 | if (!entry->bitmap) | |
2388 | continue; | |
2389 | if (entry->bytes < min_bytes) | |
2390 | continue; | |
2391 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, | |
2392 | bytes, min_bytes); | |
2393 | } while (ret && node); | |
2394 | ||
2395 | return ret; | |
2396 | } | |
2397 | ||
fa9c0d79 CM |
2398 | /* |
2399 | * here we try to find a cluster of blocks in a block group. The goal | |
2400 | * is to find at least bytes free and up to empty_size + bytes free. | |
2401 | * We might not find them all in one contiguous area. | |
2402 | * | |
2403 | * returns zero and sets up cluster if things worked out, otherwise | |
2404 | * it returns -enospc | |
2405 | */ | |
2406 | int btrfs_find_space_cluster(struct btrfs_trans_handle *trans, | |
451d7585 | 2407 | struct btrfs_root *root, |
fa9c0d79 CM |
2408 | struct btrfs_block_group_cache *block_group, |
2409 | struct btrfs_free_cluster *cluster, | |
2410 | u64 offset, u64 bytes, u64 empty_size) | |
2411 | { | |
34d52cb6 | 2412 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
86d4a77b JB |
2413 | struct list_head bitmaps; |
2414 | struct btrfs_free_space *entry, *tmp; | |
fa9c0d79 | 2415 | u64 min_bytes; |
fa9c0d79 CM |
2416 | int ret; |
2417 | ||
2418 | /* for metadata, allow allocates with more holes */ | |
451d7585 CM |
2419 | if (btrfs_test_opt(root, SSD_SPREAD)) { |
2420 | min_bytes = bytes + empty_size; | |
2421 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { | |
fa9c0d79 CM |
2422 | /* |
2423 | * we want to do larger allocations when we are | |
2424 | * flushing out the delayed refs, it helps prevent | |
2425 | * making more work as we go along. | |
2426 | */ | |
2427 | if (trans->transaction->delayed_refs.flushing) | |
2428 | min_bytes = max(bytes, (bytes + empty_size) >> 1); | |
2429 | else | |
2430 | min_bytes = max(bytes, (bytes + empty_size) >> 4); | |
2431 | } else | |
2432 | min_bytes = max(bytes, (bytes + empty_size) >> 2); | |
2433 | ||
34d52cb6 | 2434 | spin_lock(&ctl->tree_lock); |
7d0d2e8e JB |
2435 | |
2436 | /* | |
2437 | * If we know we don't have enough space to make a cluster don't even | |
2438 | * bother doing all the work to try and find one. | |
2439 | */ | |
34d52cb6 LZ |
2440 | if (ctl->free_space < min_bytes) { |
2441 | spin_unlock(&ctl->tree_lock); | |
7d0d2e8e JB |
2442 | return -ENOSPC; |
2443 | } | |
2444 | ||
fa9c0d79 CM |
2445 | spin_lock(&cluster->lock); |
2446 | ||
2447 | /* someone already found a cluster, hooray */ | |
2448 | if (cluster->block_group) { | |
2449 | ret = 0; | |
2450 | goto out; | |
2451 | } | |
fa9c0d79 | 2452 | |
86d4a77b JB |
2453 | INIT_LIST_HEAD(&bitmaps); |
2454 | ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset, | |
2455 | bytes, min_bytes); | |
4e69b598 | 2456 | if (ret) |
86d4a77b JB |
2457 | ret = setup_cluster_bitmap(block_group, cluster, &bitmaps, |
2458 | offset, bytes, min_bytes); | |
2459 | ||
2460 | /* Clear our temporary list */ | |
2461 | list_for_each_entry_safe(entry, tmp, &bitmaps, list) | |
2462 | list_del_init(&entry->list); | |
fa9c0d79 | 2463 | |
4e69b598 JB |
2464 | if (!ret) { |
2465 | atomic_inc(&block_group->count); | |
2466 | list_add_tail(&cluster->block_group_list, | |
2467 | &block_group->cluster_list); | |
2468 | cluster->block_group = block_group; | |
fa9c0d79 | 2469 | } |
fa9c0d79 CM |
2470 | out: |
2471 | spin_unlock(&cluster->lock); | |
34d52cb6 | 2472 | spin_unlock(&ctl->tree_lock); |
fa9c0d79 CM |
2473 | |
2474 | return ret; | |
2475 | } | |
2476 | ||
2477 | /* | |
2478 | * simple code to zero out a cluster | |
2479 | */ | |
2480 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) | |
2481 | { | |
2482 | spin_lock_init(&cluster->lock); | |
2483 | spin_lock_init(&cluster->refill_lock); | |
6bef4d31 | 2484 | cluster->root = RB_ROOT; |
fa9c0d79 CM |
2485 | cluster->max_size = 0; |
2486 | INIT_LIST_HEAD(&cluster->block_group_list); | |
2487 | cluster->block_group = NULL; | |
2488 | } | |
2489 | ||
f7039b1d LD |
2490 | int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, |
2491 | u64 *trimmed, u64 start, u64 end, u64 minlen) | |
2492 | { | |
34d52cb6 | 2493 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
f7039b1d LD |
2494 | struct btrfs_free_space *entry = NULL; |
2495 | struct btrfs_fs_info *fs_info = block_group->fs_info; | |
2496 | u64 bytes = 0; | |
2497 | u64 actually_trimmed; | |
2498 | int ret = 0; | |
2499 | ||
2500 | *trimmed = 0; | |
2501 | ||
2502 | while (start < end) { | |
34d52cb6 | 2503 | spin_lock(&ctl->tree_lock); |
f7039b1d | 2504 | |
34d52cb6 LZ |
2505 | if (ctl->free_space < minlen) { |
2506 | spin_unlock(&ctl->tree_lock); | |
f7039b1d LD |
2507 | break; |
2508 | } | |
2509 | ||
34d52cb6 | 2510 | entry = tree_search_offset(ctl, start, 0, 1); |
f7039b1d | 2511 | if (!entry) |
34d52cb6 LZ |
2512 | entry = tree_search_offset(ctl, |
2513 | offset_to_bitmap(ctl, start), | |
f7039b1d LD |
2514 | 1, 1); |
2515 | ||
2516 | if (!entry || entry->offset >= end) { | |
34d52cb6 | 2517 | spin_unlock(&ctl->tree_lock); |
f7039b1d LD |
2518 | break; |
2519 | } | |
2520 | ||
2521 | if (entry->bitmap) { | |
34d52cb6 | 2522 | ret = search_bitmap(ctl, entry, &start, &bytes); |
f7039b1d LD |
2523 | if (!ret) { |
2524 | if (start >= end) { | |
34d52cb6 | 2525 | spin_unlock(&ctl->tree_lock); |
f7039b1d LD |
2526 | break; |
2527 | } | |
2528 | bytes = min(bytes, end - start); | |
34d52cb6 | 2529 | bitmap_clear_bits(ctl, entry, start, bytes); |
f7039b1d | 2530 | if (entry->bytes == 0) |
34d52cb6 | 2531 | free_bitmap(ctl, entry); |
f7039b1d LD |
2532 | } else { |
2533 | start = entry->offset + BITS_PER_BITMAP * | |
2534 | block_group->sectorsize; | |
34d52cb6 | 2535 | spin_unlock(&ctl->tree_lock); |
f7039b1d LD |
2536 | ret = 0; |
2537 | continue; | |
2538 | } | |
2539 | } else { | |
2540 | start = entry->offset; | |
2541 | bytes = min(entry->bytes, end - start); | |
34d52cb6 | 2542 | unlink_free_space(ctl, entry); |
f789b684 | 2543 | kmem_cache_free(btrfs_free_space_cachep, entry); |
f7039b1d LD |
2544 | } |
2545 | ||
34d52cb6 | 2546 | spin_unlock(&ctl->tree_lock); |
f7039b1d LD |
2547 | |
2548 | if (bytes >= minlen) { | |
fb25e914 JB |
2549 | struct btrfs_space_info *space_info; |
2550 | int update = 0; | |
2551 | ||
2552 | space_info = block_group->space_info; | |
2553 | spin_lock(&space_info->lock); | |
2554 | spin_lock(&block_group->lock); | |
2555 | if (!block_group->ro) { | |
2556 | block_group->reserved += bytes; | |
2557 | space_info->bytes_reserved += bytes; | |
2558 | update = 1; | |
2559 | } | |
2560 | spin_unlock(&block_group->lock); | |
2561 | spin_unlock(&space_info->lock); | |
f7039b1d LD |
2562 | |
2563 | ret = btrfs_error_discard_extent(fs_info->extent_root, | |
2564 | start, | |
2565 | bytes, | |
2566 | &actually_trimmed); | |
2567 | ||
34d52cb6 | 2568 | btrfs_add_free_space(block_group, start, bytes); |
fb25e914 JB |
2569 | if (update) { |
2570 | spin_lock(&space_info->lock); | |
2571 | spin_lock(&block_group->lock); | |
2572 | if (block_group->ro) | |
2573 | space_info->bytes_readonly += bytes; | |
2574 | block_group->reserved -= bytes; | |
2575 | space_info->bytes_reserved -= bytes; | |
2576 | spin_unlock(&space_info->lock); | |
2577 | spin_unlock(&block_group->lock); | |
2578 | } | |
f7039b1d LD |
2579 | |
2580 | if (ret) | |
2581 | break; | |
2582 | *trimmed += actually_trimmed; | |
2583 | } | |
2584 | start += bytes; | |
2585 | bytes = 0; | |
2586 | ||
2587 | if (fatal_signal_pending(current)) { | |
2588 | ret = -ERESTARTSYS; | |
2589 | break; | |
2590 | } | |
2591 | ||
2592 | cond_resched(); | |
2593 | } | |
2594 | ||
2595 | return ret; | |
2596 | } | |
581bb050 LZ |
2597 | |
2598 | /* | |
2599 | * Find the left-most item in the cache tree, and then return the | |
2600 | * smallest inode number in the item. | |
2601 | * | |
2602 | * Note: the returned inode number may not be the smallest one in | |
2603 | * the tree, if the left-most item is a bitmap. | |
2604 | */ | |
2605 | u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root) | |
2606 | { | |
2607 | struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl; | |
2608 | struct btrfs_free_space *entry = NULL; | |
2609 | u64 ino = 0; | |
2610 | ||
2611 | spin_lock(&ctl->tree_lock); | |
2612 | ||
2613 | if (RB_EMPTY_ROOT(&ctl->free_space_offset)) | |
2614 | goto out; | |
2615 | ||
2616 | entry = rb_entry(rb_first(&ctl->free_space_offset), | |
2617 | struct btrfs_free_space, offset_index); | |
2618 | ||
2619 | if (!entry->bitmap) { | |
2620 | ino = entry->offset; | |
2621 | ||
2622 | unlink_free_space(ctl, entry); | |
2623 | entry->offset++; | |
2624 | entry->bytes--; | |
2625 | if (!entry->bytes) | |
2626 | kmem_cache_free(btrfs_free_space_cachep, entry); | |
2627 | else | |
2628 | link_free_space(ctl, entry); | |
2629 | } else { | |
2630 | u64 offset = 0; | |
2631 | u64 count = 1; | |
2632 | int ret; | |
2633 | ||
2634 | ret = search_bitmap(ctl, entry, &offset, &count); | |
2635 | BUG_ON(ret); | |
2636 | ||
2637 | ino = offset; | |
2638 | bitmap_clear_bits(ctl, entry, offset, 1); | |
2639 | if (entry->bytes == 0) | |
2640 | free_bitmap(ctl, entry); | |
2641 | } | |
2642 | out: | |
2643 | spin_unlock(&ctl->tree_lock); | |
2644 | ||
2645 | return ino; | |
2646 | } | |
82d5902d LZ |
2647 | |
2648 | struct inode *lookup_free_ino_inode(struct btrfs_root *root, | |
2649 | struct btrfs_path *path) | |
2650 | { | |
2651 | struct inode *inode = NULL; | |
2652 | ||
2653 | spin_lock(&root->cache_lock); | |
2654 | if (root->cache_inode) | |
2655 | inode = igrab(root->cache_inode); | |
2656 | spin_unlock(&root->cache_lock); | |
2657 | if (inode) | |
2658 | return inode; | |
2659 | ||
2660 | inode = __lookup_free_space_inode(root, path, 0); | |
2661 | if (IS_ERR(inode)) | |
2662 | return inode; | |
2663 | ||
2664 | spin_lock(&root->cache_lock); | |
7841cb28 | 2665 | if (!btrfs_fs_closing(root->fs_info)) |
82d5902d LZ |
2666 | root->cache_inode = igrab(inode); |
2667 | spin_unlock(&root->cache_lock); | |
2668 | ||
2669 | return inode; | |
2670 | } | |
2671 | ||
2672 | int create_free_ino_inode(struct btrfs_root *root, | |
2673 | struct btrfs_trans_handle *trans, | |
2674 | struct btrfs_path *path) | |
2675 | { | |
2676 | return __create_free_space_inode(root, trans, path, | |
2677 | BTRFS_FREE_INO_OBJECTID, 0); | |
2678 | } | |
2679 | ||
2680 | int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root) | |
2681 | { | |
2682 | struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; | |
2683 | struct btrfs_path *path; | |
2684 | struct inode *inode; | |
2685 | int ret = 0; | |
2686 | u64 root_gen = btrfs_root_generation(&root->root_item); | |
2687 | ||
4b9465cb CM |
2688 | if (!btrfs_test_opt(root, INODE_MAP_CACHE)) |
2689 | return 0; | |
2690 | ||
82d5902d LZ |
2691 | /* |
2692 | * If we're unmounting then just return, since this does a search on the | |
2693 | * normal root and not the commit root and we could deadlock. | |
2694 | */ | |
7841cb28 | 2695 | if (btrfs_fs_closing(fs_info)) |
82d5902d LZ |
2696 | return 0; |
2697 | ||
2698 | path = btrfs_alloc_path(); | |
2699 | if (!path) | |
2700 | return 0; | |
2701 | ||
2702 | inode = lookup_free_ino_inode(root, path); | |
2703 | if (IS_ERR(inode)) | |
2704 | goto out; | |
2705 | ||
2706 | if (root_gen != BTRFS_I(inode)->generation) | |
2707 | goto out_put; | |
2708 | ||
2709 | ret = __load_free_space_cache(root, inode, ctl, path, 0); | |
2710 | ||
2711 | if (ret < 0) | |
2712 | printk(KERN_ERR "btrfs: failed to load free ino cache for " | |
2713 | "root %llu\n", root->root_key.objectid); | |
2714 | out_put: | |
2715 | iput(inode); | |
2716 | out: | |
2717 | btrfs_free_path(path); | |
2718 | return ret; | |
2719 | } | |
2720 | ||
2721 | int btrfs_write_out_ino_cache(struct btrfs_root *root, | |
2722 | struct btrfs_trans_handle *trans, | |
2723 | struct btrfs_path *path) | |
2724 | { | |
2725 | struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; | |
2726 | struct inode *inode; | |
2727 | int ret; | |
2728 | ||
4b9465cb CM |
2729 | if (!btrfs_test_opt(root, INODE_MAP_CACHE)) |
2730 | return 0; | |
2731 | ||
82d5902d LZ |
2732 | inode = lookup_free_ino_inode(root, path); |
2733 | if (IS_ERR(inode)) | |
2734 | return 0; | |
2735 | ||
2736 | ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0); | |
c09544e0 JB |
2737 | if (ret) { |
2738 | btrfs_delalloc_release_metadata(inode, inode->i_size); | |
2739 | #ifdef DEBUG | |
82d5902d LZ |
2740 | printk(KERN_ERR "btrfs: failed to write free ino cache " |
2741 | "for root %llu\n", root->root_key.objectid); | |
c09544e0 JB |
2742 | #endif |
2743 | } | |
82d5902d LZ |
2744 | |
2745 | iput(inode); | |
2746 | return ret; | |
2747 | } |