blob: d634a7e42207ea9d8e81e3e4a0f85f9bd3426891 [file] [log] [blame]
Josef Bacik0f9dd462008-09-23 13:14:11 -04001/*
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
Josef Bacik96303082009-07-13 21:29:25 -040019#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040022#include <linux/math64.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040023#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040024#include "free-space-cache.h"
25#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040026#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000027#include "extent_io.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040028
Josef Bacik96303082009-07-13 21:29:25 -040029#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
30#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
31
Josef Bacik0cb59c92010-07-02 12:14:14 -040032static void recalculate_thresholds(struct btrfs_block_group_cache
33 *block_group);
34static int link_free_space(struct btrfs_block_group_cache *block_group,
35 struct btrfs_free_space *info);
36
Josef Bacik0af3d002010-06-21 14:48:16 -040037struct inode *lookup_free_space_inode(struct btrfs_root *root,
38 struct btrfs_block_group_cache
39 *block_group, struct btrfs_path *path)
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
49 spin_lock(&block_group->lock);
50 if (block_group->inode)
51 inode = igrab(block_group->inode);
52 spin_unlock(&block_group->lock);
53 if (inode)
54 return inode;
55
56 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
57 key.offset = block_group->key.objectid;
58 key.type = 0;
59
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61 if (ret < 0)
62 return ERR_PTR(ret);
63 if (ret > 0) {
64 btrfs_release_path(root, path);
65 return ERR_PTR(-ENOENT);
66 }
67
68 leaf = path->nodes[0];
69 header = btrfs_item_ptr(leaf, path->slots[0],
70 struct btrfs_free_space_header);
71 btrfs_free_space_key(leaf, header, &disk_key);
72 btrfs_disk_key_to_cpu(&location, &disk_key);
73 btrfs_release_path(root, path);
74
75 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
76 if (!inode)
77 return ERR_PTR(-ENOENT);
78 if (IS_ERR(inode))
79 return inode;
80 if (is_bad_inode(inode)) {
81 iput(inode);
82 return ERR_PTR(-ENOENT);
83 }
84
Miao Xieadae52b2011-03-31 09:43:23 +000085 inode->i_mapping->flags &= ~__GFP_FS;
86
Josef Bacik0af3d002010-06-21 14:48:16 -040087 spin_lock(&block_group->lock);
88 if (!root->fs_info->closing) {
89 block_group->inode = igrab(inode);
90 block_group->iref = 1;
91 }
92 spin_unlock(&block_group->lock);
93
94 return inode;
95}
96
97int create_free_space_inode(struct btrfs_root *root,
98 struct btrfs_trans_handle *trans,
99 struct btrfs_block_group_cache *block_group,
100 struct btrfs_path *path)
101{
102 struct btrfs_key key;
103 struct btrfs_disk_key disk_key;
104 struct btrfs_free_space_header *header;
105 struct btrfs_inode_item *inode_item;
106 struct extent_buffer *leaf;
107 u64 objectid;
108 int ret;
109
110 ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
111 if (ret < 0)
112 return ret;
113
114 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
115 if (ret)
116 return ret;
117
118 leaf = path->nodes[0];
119 inode_item = btrfs_item_ptr(leaf, path->slots[0],
120 struct btrfs_inode_item);
121 btrfs_item_key(leaf, &disk_key, path->slots[0]);
122 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
123 sizeof(*inode_item));
124 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
125 btrfs_set_inode_size(leaf, inode_item, 0);
126 btrfs_set_inode_nbytes(leaf, inode_item, 0);
127 btrfs_set_inode_uid(leaf, inode_item, 0);
128 btrfs_set_inode_gid(leaf, inode_item, 0);
129 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
130 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
131 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
132 btrfs_set_inode_nlink(leaf, inode_item, 1);
133 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
134 btrfs_set_inode_block_group(leaf, inode_item,
135 block_group->key.objectid);
136 btrfs_mark_buffer_dirty(leaf);
137 btrfs_release_path(root, path);
138
139 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
140 key.offset = block_group->key.objectid;
141 key.type = 0;
142
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 sizeof(struct btrfs_free_space_header));
145 if (ret < 0) {
146 btrfs_release_path(root, path);
147 return ret;
148 }
149 leaf = path->nodes[0];
150 header = btrfs_item_ptr(leaf, path->slots[0],
151 struct btrfs_free_space_header);
152 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
153 btrfs_set_free_space_key(leaf, header, &disk_key);
154 btrfs_mark_buffer_dirty(leaf);
155 btrfs_release_path(root, path);
156
157 return 0;
158}
159
160int btrfs_truncate_free_space_cache(struct btrfs_root *root,
161 struct btrfs_trans_handle *trans,
162 struct btrfs_path *path,
163 struct inode *inode)
164{
165 loff_t oldsize;
166 int ret = 0;
167
168 trans->block_rsv = root->orphan_block_rsv;
169 ret = btrfs_block_rsv_check(trans, root,
170 root->orphan_block_rsv,
171 0, 5);
172 if (ret)
173 return ret;
174
175 oldsize = i_size_read(inode);
176 btrfs_i_size_write(inode, 0);
177 truncate_pagecache(inode, oldsize, 0);
178
179 /*
180 * We don't need an orphan item because truncating the free space cache
181 * will never be split across transactions.
182 */
183 ret = btrfs_truncate_inode_items(trans, root, inode,
184 0, BTRFS_EXTENT_DATA_KEY);
185 if (ret) {
186 WARN_ON(1);
187 return ret;
188 }
189
190 return btrfs_update_inode(trans, root, inode);
191}
192
Josef Bacik9d66e232010-08-25 16:54:15 -0400193static int readahead_cache(struct inode *inode)
194{
195 struct file_ra_state *ra;
196 unsigned long last_index;
197
198 ra = kzalloc(sizeof(*ra), GFP_NOFS);
199 if (!ra)
200 return -ENOMEM;
201
202 file_ra_state_init(ra, inode->i_mapping);
203 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
204
205 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
206
207 kfree(ra);
208
209 return 0;
210}
211
212int load_free_space_cache(struct btrfs_fs_info *fs_info,
213 struct btrfs_block_group_cache *block_group)
214{
215 struct btrfs_root *root = fs_info->tree_root;
216 struct inode *inode;
217 struct btrfs_free_space_header *header;
218 struct extent_buffer *leaf;
219 struct page *page;
220 struct btrfs_path *path;
221 u32 *checksums = NULL, *crc;
222 char *disk_crcs = NULL;
223 struct btrfs_key key;
224 struct list_head bitmaps;
225 u64 num_entries;
226 u64 num_bitmaps;
227 u64 generation;
Josef Bacik43be2142011-04-01 14:55:00 +0000228 u64 used = btrfs_block_group_used(&block_group->item);
Josef Bacik9d66e232010-08-25 16:54:15 -0400229 u32 cur_crc = ~(u32)0;
230 pgoff_t index = 0;
231 unsigned long first_page_offset;
232 int num_checksums;
233 int ret = 0;
234
235 /*
236 * If we're unmounting then just return, since this does a search on the
237 * normal root and not the commit root and we could deadlock.
238 */
239 smp_mb();
240 if (fs_info->closing)
241 return 0;
242
243 /*
244 * If this block group has been marked to be cleared for one reason or
245 * another then we can't trust the on disk cache, so just return.
246 */
247 spin_lock(&block_group->lock);
248 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400249 spin_unlock(&block_group->lock);
250 return 0;
251 }
252 spin_unlock(&block_group->lock);
253
254 INIT_LIST_HEAD(&bitmaps);
255
256 path = btrfs_alloc_path();
257 if (!path)
258 return 0;
259
260 inode = lookup_free_space_inode(root, block_group, path);
261 if (IS_ERR(inode)) {
262 btrfs_free_path(path);
263 return 0;
264 }
265
266 /* Nothing in the space cache, goodbye */
267 if (!i_size_read(inode)) {
268 btrfs_free_path(path);
269 goto out;
270 }
271
272 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
273 key.offset = block_group->key.objectid;
274 key.type = 0;
275
276 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
277 if (ret) {
278 btrfs_free_path(path);
279 goto out;
280 }
281
282 leaf = path->nodes[0];
283 header = btrfs_item_ptr(leaf, path->slots[0],
284 struct btrfs_free_space_header);
285 num_entries = btrfs_free_space_entries(leaf, header);
286 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
287 generation = btrfs_free_space_generation(leaf, header);
288 btrfs_free_path(path);
289
290 if (BTRFS_I(inode)->generation != generation) {
291 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
292 " not match free space cache generation (%llu) for "
293 "block group %llu\n",
294 (unsigned long long)BTRFS_I(inode)->generation,
295 (unsigned long long)generation,
296 (unsigned long long)block_group->key.objectid);
Josef Bacik2b209822010-12-03 13:17:53 -0500297 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400298 }
299
300 if (!num_entries)
301 goto out;
302
303 /* Setup everything for doing checksumming */
304 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
305 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
306 if (!checksums)
307 goto out;
308 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
309 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
310 if (!disk_crcs)
311 goto out;
312
313 ret = readahead_cache(inode);
314 if (ret) {
315 ret = 0;
316 goto out;
317 }
318
319 while (1) {
320 struct btrfs_free_space_entry *entry;
321 struct btrfs_free_space *e;
322 void *addr;
323 unsigned long offset = 0;
324 unsigned long start_offset = 0;
325 int need_loop = 0;
326
327 if (!num_entries && !num_bitmaps)
328 break;
329
330 if (index == 0) {
331 start_offset = first_page_offset;
332 offset = start_offset;
333 }
334
335 page = grab_cache_page(inode->i_mapping, index);
336 if (!page) {
337 ret = 0;
338 goto free_cache;
339 }
340
341 if (!PageUptodate(page)) {
342 btrfs_readpage(NULL, page);
343 lock_page(page);
344 if (!PageUptodate(page)) {
345 unlock_page(page);
346 page_cache_release(page);
347 printk(KERN_ERR "btrfs: error reading free "
348 "space cache: %llu\n",
349 (unsigned long long)
350 block_group->key.objectid);
351 goto free_cache;
352 }
353 }
354 addr = kmap(page);
355
356 if (index == 0) {
357 u64 *gen;
358
359 memcpy(disk_crcs, addr, first_page_offset);
360 gen = addr + (sizeof(u32) * num_checksums);
361 if (*gen != BTRFS_I(inode)->generation) {
362 printk(KERN_ERR "btrfs: space cache generation"
363 " (%llu) does not match inode (%llu) "
364 "for block group %llu\n",
365 (unsigned long long)*gen,
366 (unsigned long long)
367 BTRFS_I(inode)->generation,
368 (unsigned long long)
369 block_group->key.objectid);
370 kunmap(page);
371 unlock_page(page);
372 page_cache_release(page);
373 goto free_cache;
374 }
375 crc = (u32 *)disk_crcs;
376 }
377 entry = addr + start_offset;
378
379 /* First lets check our crc before we do anything fun */
380 cur_crc = ~(u32)0;
381 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
382 PAGE_CACHE_SIZE - start_offset);
383 btrfs_csum_final(cur_crc, (char *)&cur_crc);
384 if (cur_crc != *crc) {
385 printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
386 "block group %llu\n", index,
387 (unsigned long long)block_group->key.objectid);
388 kunmap(page);
389 unlock_page(page);
390 page_cache_release(page);
391 goto free_cache;
392 }
393 crc++;
394
395 while (1) {
396 if (!num_entries)
397 break;
398
399 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500400 e = kmem_cache_zalloc(btrfs_free_space_cachep,
401 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400402 if (!e) {
403 kunmap(page);
404 unlock_page(page);
405 page_cache_release(page);
406 goto free_cache;
407 }
408
409 e->offset = le64_to_cpu(entry->offset);
410 e->bytes = le64_to_cpu(entry->bytes);
411 if (!e->bytes) {
412 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500413 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400414 unlock_page(page);
415 page_cache_release(page);
416 goto free_cache;
417 }
418
419 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
420 spin_lock(&block_group->tree_lock);
421 ret = link_free_space(block_group, e);
422 spin_unlock(&block_group->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400423 if (ret) {
424 printk(KERN_ERR "Duplicate entries in "
425 "free space cache, dumping\n");
426 kunmap(page);
427 unlock_page(page);
428 page_cache_release(page);
429 goto free_cache;
430 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400431 } else {
432 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
433 if (!e->bitmap) {
434 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500435 kmem_cache_free(
436 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400437 unlock_page(page);
438 page_cache_release(page);
439 goto free_cache;
440 }
441 spin_lock(&block_group->tree_lock);
442 ret = link_free_space(block_group, e);
443 block_group->total_bitmaps++;
444 recalculate_thresholds(block_group);
445 spin_unlock(&block_group->tree_lock);
446 list_add_tail(&e->list, &bitmaps);
Josef Bacik207dde82011-05-13 14:49:23 -0400447 if (ret) {
448 printk(KERN_ERR "Duplicate entries in "
449 "free space cache, dumping\n");
450 kunmap(page);
451 unlock_page(page);
452 page_cache_release(page);
453 goto free_cache;
454 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400455 }
456
457 num_entries--;
458 offset += sizeof(struct btrfs_free_space_entry);
459 if (offset + sizeof(struct btrfs_free_space_entry) >=
460 PAGE_CACHE_SIZE)
461 break;
462 entry++;
463 }
464
465 /*
466 * We read an entry out of this page, we need to move on to the
467 * next page.
468 */
469 if (need_loop) {
470 kunmap(page);
471 goto next;
472 }
473
474 /*
475 * We add the bitmaps at the end of the entries in order that
476 * the bitmap entries are added to the cache.
477 */
478 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
479 list_del_init(&e->list);
480 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
481 kunmap(page);
482 num_bitmaps--;
483next:
484 unlock_page(page);
485 page_cache_release(page);
486 index++;
487 }
488
Josef Bacik43be2142011-04-01 14:55:00 +0000489 spin_lock(&block_group->tree_lock);
490 if (block_group->free_space != (block_group->key.offset - used -
491 block_group->bytes_super)) {
492 spin_unlock(&block_group->tree_lock);
493 printk(KERN_ERR "block group %llu has an wrong amount of free "
494 "space\n", block_group->key.objectid);
495 ret = 0;
496 goto free_cache;
497 }
498 spin_unlock(&block_group->tree_lock);
499
Josef Bacik9d66e232010-08-25 16:54:15 -0400500 ret = 1;
501out:
502 kfree(checksums);
503 kfree(disk_crcs);
504 iput(inode);
505 return ret;
506
507free_cache:
508 /* This cache is bogus, make sure it gets cleared */
509 spin_lock(&block_group->lock);
510 block_group->disk_cache_state = BTRFS_DC_CLEAR;
511 spin_unlock(&block_group->lock);
512 btrfs_remove_free_space_cache(block_group);
513 goto out;
514}
515
Josef Bacik0cb59c92010-07-02 12:14:14 -0400516int btrfs_write_out_cache(struct btrfs_root *root,
517 struct btrfs_trans_handle *trans,
518 struct btrfs_block_group_cache *block_group,
519 struct btrfs_path *path)
520{
521 struct btrfs_free_space_header *header;
522 struct extent_buffer *leaf;
523 struct inode *inode;
524 struct rb_node *node;
525 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400526 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400527 struct page *page;
528 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000529 struct btrfs_free_cluster *cluster = NULL;
530 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400531 struct list_head bitmap_list;
532 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000533 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400534 u64 bytes = 0;
535 u32 *crc, *checksums;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400536 unsigned long first_page_offset;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400537 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400538 int entries = 0;
539 int bitmaps = 0;
540 int ret = 0;
Josef Bacik43be2142011-04-01 14:55:00 +0000541 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400542 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400543
544 root = root->fs_info->tree_root;
545
546 INIT_LIST_HEAD(&bitmap_list);
547
548 spin_lock(&block_group->lock);
549 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
550 spin_unlock(&block_group->lock);
551 return 0;
552 }
553 spin_unlock(&block_group->lock);
554
555 inode = lookup_free_space_inode(root, block_group, path);
556 if (IS_ERR(inode))
557 return 0;
558
559 if (!i_size_read(inode)) {
560 iput(inode);
561 return 0;
562 }
563
Josef Bacik2b209822010-12-03 13:17:53 -0500564 node = rb_first(&block_group->free_space_offset);
565 if (!node) {
566 iput(inode);
567 return 0;
568 }
569
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400570 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
571 PAGE_CACHE_SHIFT;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400572 filemap_write_and_wait(inode->i_mapping);
573 btrfs_wait_ordered_range(inode, inode->i_size &
574 ~(root->sectorsize - 1), (u64)-1);
575
576 /* We need a checksum per page. */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400577 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400578 if (!crc) {
579 iput(inode);
580 return 0;
581 }
582
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400583 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
584 if (!pages) {
585 kfree(crc);
586 iput(inode);
587 return 0;
588 }
589
Josef Bacik0cb59c92010-07-02 12:14:14 -0400590 /* Since the first page has all of our checksums and our generation we
591 * need to calculate the offset into the page that we can start writing
592 * our entries.
593 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400594 first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400595
Josef Bacik43be2142011-04-01 14:55:00 +0000596 /* Get the cluster for this block_group if it exists */
597 if (!list_empty(&block_group->cluster_list))
598 cluster = list_entry(block_group->cluster_list.next,
599 struct btrfs_free_cluster,
600 block_group_list);
601
602 /*
603 * We shouldn't have switched the pinned extents yet so this is the
604 * right one
605 */
606 unpin = root->fs_info->pinned_extents;
607
Josef Bacik0cb59c92010-07-02 12:14:14 -0400608 /*
609 * Lock all pages first so we can lock the extent safely.
610 *
611 * NOTE: Because we hold the ref the entire time we're going to write to
612 * the page find_get_page should never fail, so we don't do a check
613 * after find_get_page at this point. Just putting this here so people
614 * know and don't freak out.
615 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400616 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400617 page = grab_cache_page(inode->i_mapping, index);
618 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400619 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400620
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400621 for (i = 0; i < num_pages; i++) {
622 unlock_page(pages[i]);
623 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400624 }
625 goto out_free;
626 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400627 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400628 index++;
629 }
630
631 index = 0;
632 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
633 0, &cached_state, GFP_NOFS);
634
Josef Bacik43be2142011-04-01 14:55:00 +0000635 /*
636 * When searching for pinned extents, we need to start at our start
637 * offset.
638 */
639 start = block_group->key.objectid;
640
Josef Bacik0cb59c92010-07-02 12:14:14 -0400641 /* Write out the extent entries */
642 do {
643 struct btrfs_free_space_entry *entry;
644 void *addr;
645 unsigned long offset = 0;
646 unsigned long start_offset = 0;
647
Josef Bacik43be2142011-04-01 14:55:00 +0000648 next_page = false;
649
Josef Bacik0cb59c92010-07-02 12:14:14 -0400650 if (index == 0) {
651 start_offset = first_page_offset;
652 offset = start_offset;
653 }
654
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400655 if (index >= num_pages) {
656 out_of_space = true;
657 break;
658 }
659
660 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400661
662 addr = kmap(page);
663 entry = addr + start_offset;
664
665 memset(addr, 0, PAGE_CACHE_SIZE);
Josef Bacik43be2142011-04-01 14:55:00 +0000666 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400667 struct btrfs_free_space *e;
668
669 e = rb_entry(node, struct btrfs_free_space, offset_index);
670 entries++;
671
672 entry->offset = cpu_to_le64(e->offset);
673 entry->bytes = cpu_to_le64(e->bytes);
674 if (e->bitmap) {
675 entry->type = BTRFS_FREE_SPACE_BITMAP;
676 list_add_tail(&e->list, &bitmap_list);
677 bitmaps++;
678 } else {
679 entry->type = BTRFS_FREE_SPACE_EXTENT;
680 }
681 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000682 if (!node && cluster) {
683 node = rb_first(&cluster->root);
684 cluster = NULL;
685 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400686 offset += sizeof(struct btrfs_free_space_entry);
687 if (offset + sizeof(struct btrfs_free_space_entry) >=
688 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000689 next_page = true;
690 entry++;
691 }
692
693 /*
694 * We want to add any pinned extents to our free space cache
695 * so we don't leak the space
696 */
697 while (!next_page && (start < block_group->key.objectid +
698 block_group->key.offset)) {
699 ret = find_first_extent_bit(unpin, start, &start, &end,
700 EXTENT_DIRTY);
701 if (ret) {
702 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400703 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000704 }
705
706 /* This pinned extent is out of our range */
707 if (start >= block_group->key.objectid +
708 block_group->key.offset)
709 break;
710
711 len = block_group->key.objectid +
712 block_group->key.offset - start;
713 len = min(len, end + 1 - start);
714
715 entries++;
716 entry->offset = cpu_to_le64(start);
717 entry->bytes = cpu_to_le64(len);
718 entry->type = BTRFS_FREE_SPACE_EXTENT;
719
720 start = end + 1;
721 offset += sizeof(struct btrfs_free_space_entry);
722 if (offset + sizeof(struct btrfs_free_space_entry) >=
723 PAGE_CACHE_SIZE)
724 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400725 entry++;
726 }
727 *crc = ~(u32)0;
728 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
729 PAGE_CACHE_SIZE - start_offset);
730 kunmap(page);
731
732 btrfs_csum_final(*crc, (char *)crc);
733 crc++;
734
735 bytes += PAGE_CACHE_SIZE;
736
Josef Bacik0cb59c92010-07-02 12:14:14 -0400737 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000738 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400739
740 /* Write out the bitmaps */
741 list_for_each_safe(pos, n, &bitmap_list) {
742 void *addr;
743 struct btrfs_free_space *entry =
744 list_entry(pos, struct btrfs_free_space, list);
745
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400746 if (index >= num_pages) {
747 out_of_space = true;
748 break;
749 }
Chris Masonf65647c2011-04-18 08:55:34 -0400750 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400751
752 addr = kmap(page);
753 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
754 *crc = ~(u32)0;
755 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
756 kunmap(page);
757 btrfs_csum_final(*crc, (char *)crc);
758 crc++;
759 bytes += PAGE_CACHE_SIZE;
760
Josef Bacik0cb59c92010-07-02 12:14:14 -0400761 list_del_init(&entry->list);
762 index++;
763 }
764
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400765 if (out_of_space) {
766 btrfs_drop_pages(pages, num_pages);
767 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
768 i_size_read(inode) - 1, &cached_state,
769 GFP_NOFS);
770 ret = 0;
771 goto out_free;
772 }
773
Josef Bacik0cb59c92010-07-02 12:14:14 -0400774 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400775 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400776 void *addr;
777
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400778 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400779 addr = kmap(page);
780 memset(addr, 0, PAGE_CACHE_SIZE);
781 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400782 bytes += PAGE_CACHE_SIZE;
783 index++;
784 }
785
Josef Bacik0cb59c92010-07-02 12:14:14 -0400786 /* Write the checksums and trans id to the first page */
787 {
788 void *addr;
789 u64 *gen;
790
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400791 page = pages[0];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400792
793 addr = kmap(page);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400794 memcpy(addr, checksums, sizeof(u32) * num_pages);
795 gen = addr + (sizeof(u32) * num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400796 *gen = trans->transid;
797 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400798 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400799
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400800 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
801 bytes, &cached_state);
802 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400803 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
804 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
805
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400806 if (ret) {
807 ret = 0;
808 goto out_free;
809 }
810
811 BTRFS_I(inode)->generation = trans->transid;
812
Josef Bacik0cb59c92010-07-02 12:14:14 -0400813 filemap_write_and_wait(inode->i_mapping);
814
815 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
816 key.offset = block_group->key.objectid;
817 key.type = 0;
818
819 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
820 if (ret < 0) {
821 ret = 0;
822 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
823 EXTENT_DIRTY | EXTENT_DELALLOC |
824 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
825 goto out_free;
826 }
827 leaf = path->nodes[0];
828 if (ret > 0) {
829 struct btrfs_key found_key;
830 BUG_ON(!path->slots[0]);
831 path->slots[0]--;
832 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
833 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
834 found_key.offset != block_group->key.objectid) {
835 ret = 0;
836 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
837 EXTENT_DIRTY | EXTENT_DELALLOC |
838 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
839 GFP_NOFS);
840 btrfs_release_path(root, path);
841 goto out_free;
842 }
843 }
844 header = btrfs_item_ptr(leaf, path->slots[0],
845 struct btrfs_free_space_header);
846 btrfs_set_free_space_entries(leaf, header, entries);
847 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
848 btrfs_set_free_space_generation(leaf, header, trans->transid);
849 btrfs_mark_buffer_dirty(leaf);
850 btrfs_release_path(root, path);
851
852 ret = 1;
853
854out_free:
855 if (ret == 0) {
856 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
857 spin_lock(&block_group->lock);
858 block_group->disk_cache_state = BTRFS_DC_ERROR;
859 spin_unlock(&block_group->lock);
860 BTRFS_I(inode)->generation = 0;
861 }
862 kfree(checksums);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400863 kfree(pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400864 btrfs_update_inode(trans, root, inode);
865 iput(inode);
866 return ret;
867}
868
Josef Bacik96303082009-07-13 21:29:25 -0400869static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
870 u64 offset)
871{
872 BUG_ON(offset < bitmap_start);
873 offset -= bitmap_start;
874 return (unsigned long)(div64_u64(offset, sectorsize));
875}
876
877static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
878{
879 return (unsigned long)(div64_u64(bytes, sectorsize));
880}
881
882static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
883 u64 offset)
884{
885 u64 bitmap_start;
886 u64 bytes_per_bitmap;
887
888 bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
889 bitmap_start = offset - block_group->key.objectid;
890 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
891 bitmap_start *= bytes_per_bitmap;
892 bitmap_start += block_group->key.objectid;
893
894 return bitmap_start;
895}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400896
897static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400898 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400899{
900 struct rb_node **p = &root->rb_node;
901 struct rb_node *parent = NULL;
902 struct btrfs_free_space *info;
903
904 while (*p) {
905 parent = *p;
906 info = rb_entry(parent, struct btrfs_free_space, offset_index);
907
Josef Bacik96303082009-07-13 21:29:25 -0400908 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400909 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400910 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400911 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400912 } else {
913 /*
914 * we could have a bitmap entry and an extent entry
915 * share the same offset. If this is the case, we want
916 * the extent entry to always be found first if we do a
917 * linear search through the tree, since we want to have
918 * the quickest allocation time, and allocating from an
919 * extent is faster than allocating from a bitmap. So
920 * if we're inserting a bitmap and we find an entry at
921 * this offset, we want to go right, or after this entry
922 * logically. If we are inserting an extent and we've
923 * found a bitmap, we want to go left, or before
924 * logically.
925 */
926 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -0400927 if (info->bitmap) {
928 WARN_ON_ONCE(1);
929 return -EEXIST;
930 }
Josef Bacik96303082009-07-13 21:29:25 -0400931 p = &(*p)->rb_right;
932 } else {
Josef Bacik207dde82011-05-13 14:49:23 -0400933 if (!info->bitmap) {
934 WARN_ON_ONCE(1);
935 return -EEXIST;
936 }
Josef Bacik96303082009-07-13 21:29:25 -0400937 p = &(*p)->rb_left;
938 }
939 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400940 }
941
942 rb_link_node(node, parent, p);
943 rb_insert_color(node, root);
944
945 return 0;
946}
947
948/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400949 * searches the tree for the given offset.
950 *
Josef Bacik96303082009-07-13 21:29:25 -0400951 * fuzzy - If this is set, then we are trying to make an allocation, and we just
952 * want a section that has at least bytes size and comes at or after the given
953 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400954 */
Josef Bacik96303082009-07-13 21:29:25 -0400955static struct btrfs_free_space *
956tree_search_offset(struct btrfs_block_group_cache *block_group,
957 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400958{
Josef Bacik96303082009-07-13 21:29:25 -0400959 struct rb_node *n = block_group->free_space_offset.rb_node;
960 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400961
Josef Bacik96303082009-07-13 21:29:25 -0400962 /* find entry that is closest to the 'offset' */
963 while (1) {
964 if (!n) {
965 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400966 break;
967 }
Josef Bacik96303082009-07-13 21:29:25 -0400968
969 entry = rb_entry(n, struct btrfs_free_space, offset_index);
970 prev = entry;
971
972 if (offset < entry->offset)
973 n = n->rb_left;
974 else if (offset > entry->offset)
975 n = n->rb_right;
976 else
977 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400978 }
979
Josef Bacik96303082009-07-13 21:29:25 -0400980 if (bitmap_only) {
981 if (!entry)
982 return NULL;
983 if (entry->bitmap)
984 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400985
Josef Bacik96303082009-07-13 21:29:25 -0400986 /*
987 * bitmap entry and extent entry may share same offset,
988 * in that case, bitmap entry comes after extent entry.
989 */
990 n = rb_next(n);
991 if (!n)
992 return NULL;
993 entry = rb_entry(n, struct btrfs_free_space, offset_index);
994 if (entry->offset != offset)
995 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400996
Josef Bacik96303082009-07-13 21:29:25 -0400997 WARN_ON(!entry->bitmap);
998 return entry;
999 } else if (entry) {
1000 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001001 /*
Josef Bacik96303082009-07-13 21:29:25 -04001002 * if previous extent entry covers the offset,
1003 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001004 */
Josef Bacik96303082009-07-13 21:29:25 -04001005 n = &entry->offset_index;
1006 while (1) {
1007 n = rb_prev(n);
1008 if (!n)
1009 break;
1010 prev = rb_entry(n, struct btrfs_free_space,
1011 offset_index);
1012 if (!prev->bitmap) {
1013 if (prev->offset + prev->bytes > offset)
1014 entry = prev;
1015 break;
1016 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001017 }
Josef Bacik96303082009-07-13 21:29:25 -04001018 }
1019 return entry;
1020 }
1021
1022 if (!prev)
1023 return NULL;
1024
1025 /* find last entry before the 'offset' */
1026 entry = prev;
1027 if (entry->offset > offset) {
1028 n = rb_prev(&entry->offset_index);
1029 if (n) {
1030 entry = rb_entry(n, struct btrfs_free_space,
1031 offset_index);
1032 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001033 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001034 if (fuzzy)
1035 return entry;
1036 else
1037 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001038 }
1039 }
1040
Josef Bacik96303082009-07-13 21:29:25 -04001041 if (entry->bitmap) {
1042 n = &entry->offset_index;
1043 while (1) {
1044 n = rb_prev(n);
1045 if (!n)
1046 break;
1047 prev = rb_entry(n, struct btrfs_free_space,
1048 offset_index);
1049 if (!prev->bitmap) {
1050 if (prev->offset + prev->bytes > offset)
1051 return prev;
1052 break;
1053 }
1054 }
1055 if (entry->offset + BITS_PER_BITMAP *
1056 block_group->sectorsize > offset)
1057 return entry;
1058 } else if (entry->offset + entry->bytes > offset)
1059 return entry;
1060
1061 if (!fuzzy)
1062 return NULL;
1063
1064 while (1) {
1065 if (entry->bitmap) {
1066 if (entry->offset + BITS_PER_BITMAP *
1067 block_group->sectorsize > offset)
1068 break;
1069 } else {
1070 if (entry->offset + entry->bytes > offset)
1071 break;
1072 }
1073
1074 n = rb_next(&entry->offset_index);
1075 if (!n)
1076 return NULL;
1077 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1078 }
1079 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001080}
1081
Li Zefanf333adb2010-11-09 14:57:39 +08001082static inline void
1083__unlink_free_space(struct btrfs_block_group_cache *block_group,
1084 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001085{
1086 rb_erase(&info->offset_index, &block_group->free_space_offset);
Josef Bacik96303082009-07-13 21:29:25 -04001087 block_group->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001088}
1089
1090static void unlink_free_space(struct btrfs_block_group_cache *block_group,
1091 struct btrfs_free_space *info)
1092{
1093 __unlink_free_space(block_group, info);
Josef Bacik817d52f2009-07-13 21:29:25 -04001094 block_group->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001095}
1096
1097static int link_free_space(struct btrfs_block_group_cache *block_group,
1098 struct btrfs_free_space *info)
1099{
1100 int ret = 0;
1101
Josef Bacik96303082009-07-13 21:29:25 -04001102 BUG_ON(!info->bitmap && !info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001103 ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001104 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001105 if (ret)
1106 return ret;
1107
Josef Bacik817d52f2009-07-13 21:29:25 -04001108 block_group->free_space += info->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001109 block_group->free_extents++;
1110 return ret;
1111}
1112
1113static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
1114{
Josef Bacik25891f72009-09-11 16:11:20 -04001115 u64 max_bytes;
1116 u64 bitmap_bytes;
1117 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001118 u64 size = block_group->key.offset;
Josef Bacik96303082009-07-13 21:29:25 -04001119
1120 /*
1121 * The goal is to keep the total amount of memory used per 1gb of space
1122 * at or below 32k, so we need to adjust how much memory we allow to be
1123 * used by extent based free space tracking
1124 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001125 if (size < 1024 * 1024 * 1024)
1126 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1127 else
1128 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1129 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001130
Josef Bacik25891f72009-09-11 16:11:20 -04001131 /*
1132 * we want to account for 1 more bitmap than what we have so we can make
1133 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1134 * we add more bitmaps.
1135 */
1136 bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001137
Josef Bacik25891f72009-09-11 16:11:20 -04001138 if (bitmap_bytes >= max_bytes) {
1139 block_group->extents_thresh = 0;
1140 return;
Josef Bacik96303082009-07-13 21:29:25 -04001141 }
Josef Bacik25891f72009-09-11 16:11:20 -04001142
1143 /*
1144 * we want the extent entry threshold to always be at most 1/2 the maxw
1145 * bytes we can have, or whatever is less than that.
1146 */
1147 extent_bytes = max_bytes - bitmap_bytes;
1148 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1149
1150 block_group->extents_thresh =
1151 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001152}
1153
Josef Bacik817d52f2009-07-13 21:29:25 -04001154static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
1155 struct btrfs_free_space *info, u64 offset,
1156 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001157{
1158 unsigned long start, end;
1159 unsigned long i;
1160
Josef Bacik817d52f2009-07-13 21:29:25 -04001161 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1162 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04001163 BUG_ON(end > BITS_PER_BITMAP);
1164
1165 for (i = start; i < end; i++)
1166 clear_bit(i, info->bitmap);
1167
1168 info->bytes -= bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -04001169 block_group->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001170}
1171
Josef Bacik817d52f2009-07-13 21:29:25 -04001172static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
1173 struct btrfs_free_space *info, u64 offset,
1174 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001175{
1176 unsigned long start, end;
1177 unsigned long i;
1178
Josef Bacik817d52f2009-07-13 21:29:25 -04001179 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1180 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04001181 BUG_ON(end > BITS_PER_BITMAP);
1182
1183 for (i = start; i < end; i++)
1184 set_bit(i, info->bitmap);
1185
1186 info->bytes += bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -04001187 block_group->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001188}
1189
1190static int search_bitmap(struct btrfs_block_group_cache *block_group,
1191 struct btrfs_free_space *bitmap_info, u64 *offset,
1192 u64 *bytes)
1193{
1194 unsigned long found_bits = 0;
1195 unsigned long bits, i;
1196 unsigned long next_zero;
1197
1198 i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
1199 max_t(u64, *offset, bitmap_info->offset));
1200 bits = bytes_to_bits(*bytes, block_group->sectorsize);
1201
1202 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1203 i < BITS_PER_BITMAP;
1204 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1205 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1206 BITS_PER_BITMAP, i);
1207 if ((next_zero - i) >= bits) {
1208 found_bits = next_zero - i;
1209 break;
1210 }
1211 i = next_zero;
1212 }
1213
1214 if (found_bits) {
1215 *offset = (u64)(i * block_group->sectorsize) +
1216 bitmap_info->offset;
1217 *bytes = (u64)(found_bits) * block_group->sectorsize;
1218 return 0;
1219 }
1220
1221 return -1;
1222}
1223
1224static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
1225 *block_group, u64 *offset,
1226 u64 *bytes, int debug)
1227{
1228 struct btrfs_free_space *entry;
1229 struct rb_node *node;
1230 int ret;
1231
1232 if (!block_group->free_space_offset.rb_node)
1233 return NULL;
1234
1235 entry = tree_search_offset(block_group,
1236 offset_to_bitmap(block_group, *offset),
1237 0, 1);
1238 if (!entry)
1239 return NULL;
1240
1241 for (node = &entry->offset_index; node; node = rb_next(node)) {
1242 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1243 if (entry->bytes < *bytes)
1244 continue;
1245
1246 if (entry->bitmap) {
1247 ret = search_bitmap(block_group, entry, offset, bytes);
1248 if (!ret)
1249 return entry;
1250 continue;
1251 }
1252
1253 *offset = entry->offset;
1254 *bytes = entry->bytes;
1255 return entry;
1256 }
1257
1258 return NULL;
1259}
1260
1261static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
1262 struct btrfs_free_space *info, u64 offset)
1263{
1264 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1265 int max_bitmaps = (int)div64_u64(block_group->key.offset +
1266 bytes_per_bg - 1, bytes_per_bg);
1267 BUG_ON(block_group->total_bitmaps >= max_bitmaps);
1268
1269 info->offset = offset_to_bitmap(block_group, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001270 info->bytes = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001271 link_free_space(block_group, info);
1272 block_group->total_bitmaps++;
1273
1274 recalculate_thresholds(block_group);
1275}
1276
Li Zefanedf6e2d2010-11-09 14:50:07 +08001277static void free_bitmap(struct btrfs_block_group_cache *block_group,
1278 struct btrfs_free_space *bitmap_info)
1279{
1280 unlink_free_space(block_group, bitmap_info);
1281 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001282 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001283 block_group->total_bitmaps--;
1284 recalculate_thresholds(block_group);
1285}
1286
Josef Bacik96303082009-07-13 21:29:25 -04001287static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
1288 struct btrfs_free_space *bitmap_info,
1289 u64 *offset, u64 *bytes)
1290{
1291 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001292 u64 search_start, search_bytes;
1293 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001294
1295again:
1296 end = bitmap_info->offset +
1297 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
1298
Josef Bacik6606bb92009-07-31 11:03:58 -04001299 /*
1300 * XXX - this can go away after a few releases.
1301 *
1302 * since the only user of btrfs_remove_free_space is the tree logging
1303 * stuff, and the only way to test that is under crash conditions, we
1304 * want to have this debug stuff here just in case somethings not
1305 * working. Search the bitmap for the space we are trying to use to
1306 * make sure its actually there. If its not there then we need to stop
1307 * because something has gone wrong.
1308 */
1309 search_start = *offset;
1310 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001311 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik6606bb92009-07-31 11:03:58 -04001312 ret = search_bitmap(block_group, bitmap_info, &search_start,
1313 &search_bytes);
1314 BUG_ON(ret < 0 || search_start != *offset);
1315
Josef Bacik96303082009-07-13 21:29:25 -04001316 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001317 bitmap_clear_bits(block_group, bitmap_info, *offset,
1318 end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001319 *bytes -= end - *offset + 1;
1320 *offset = end + 1;
1321 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001322 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001323 *bytes = 0;
1324 }
1325
1326 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001327 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001328 if (!bitmap_info->bytes)
1329 free_bitmap(block_group, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001330
Josef Bacik6606bb92009-07-31 11:03:58 -04001331 /*
1332 * no entry after this bitmap, but we still have bytes to
1333 * remove, so something has gone wrong.
1334 */
1335 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001336 return -EINVAL;
1337
Josef Bacik6606bb92009-07-31 11:03:58 -04001338 bitmap_info = rb_entry(next, struct btrfs_free_space,
1339 offset_index);
1340
1341 /*
1342 * if the next entry isn't a bitmap we need to return to let the
1343 * extent stuff do its work.
1344 */
Josef Bacik96303082009-07-13 21:29:25 -04001345 if (!bitmap_info->bitmap)
1346 return -EAGAIN;
1347
Josef Bacik6606bb92009-07-31 11:03:58 -04001348 /*
1349 * Ok the next item is a bitmap, but it may not actually hold
1350 * the information for the rest of this free space stuff, so
1351 * look for it, and if we don't find it return so we can try
1352 * everything over again.
1353 */
1354 search_start = *offset;
1355 search_bytes = *bytes;
1356 ret = search_bitmap(block_group, bitmap_info, &search_start,
1357 &search_bytes);
1358 if (ret < 0 || search_start != *offset)
1359 return -EAGAIN;
1360
Josef Bacik96303082009-07-13 21:29:25 -04001361 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001362 } else if (!bitmap_info->bytes)
1363 free_bitmap(block_group, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001364
1365 return 0;
1366}
1367
1368static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
1369 struct btrfs_free_space *info)
1370{
1371 struct btrfs_free_space *bitmap_info;
1372 int added = 0;
1373 u64 bytes, offset, end;
1374 int ret;
1375
1376 /*
1377 * If we are below the extents threshold then we can add this as an
1378 * extent, and don't have to deal with the bitmap
1379 */
Josef Bacik32cb0842011-03-18 16:16:21 -04001380 if (block_group->free_extents < block_group->extents_thresh) {
1381 /*
1382 * If this block group has some small extents we don't want to
1383 * use up all of our free slots in the cache with them, we want
1384 * to reserve them to larger extents, however if we have plent
1385 * of cache left then go ahead an dadd them, no sense in adding
1386 * the overhead of a bitmap if we don't have to.
1387 */
1388 if (info->bytes <= block_group->sectorsize * 4) {
1389 if (block_group->free_extents * 2 <=
1390 block_group->extents_thresh)
1391 return 0;
1392 } else {
1393 return 0;
1394 }
1395 }
Josef Bacik96303082009-07-13 21:29:25 -04001396
1397 /*
1398 * some block groups are so tiny they can't be enveloped by a bitmap, so
1399 * don't even bother to create a bitmap for this
1400 */
1401 if (BITS_PER_BITMAP * block_group->sectorsize >
1402 block_group->key.offset)
1403 return 0;
1404
1405 bytes = info->bytes;
1406 offset = info->offset;
1407
1408again:
1409 bitmap_info = tree_search_offset(block_group,
1410 offset_to_bitmap(block_group, offset),
1411 1, 0);
1412 if (!bitmap_info) {
1413 BUG_ON(added);
1414 goto new_bitmap;
1415 }
1416
1417 end = bitmap_info->offset +
1418 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
1419
1420 if (offset >= bitmap_info->offset && offset + bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001421 bitmap_set_bits(block_group, bitmap_info, offset,
1422 end - offset);
Josef Bacik96303082009-07-13 21:29:25 -04001423 bytes -= end - offset;
1424 offset = end;
1425 added = 0;
1426 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001427 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001428 bytes = 0;
1429 } else {
1430 BUG();
1431 }
1432
1433 if (!bytes) {
1434 ret = 1;
1435 goto out;
1436 } else
1437 goto again;
1438
1439new_bitmap:
1440 if (info && info->bitmap) {
1441 add_new_bitmap(block_group, info, offset);
1442 added = 1;
1443 info = NULL;
1444 goto again;
1445 } else {
1446 spin_unlock(&block_group->tree_lock);
1447
1448 /* no pre-allocated info, allocate a new one */
1449 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001450 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1451 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001452 if (!info) {
1453 spin_lock(&block_group->tree_lock);
1454 ret = -ENOMEM;
1455 goto out;
1456 }
1457 }
1458
1459 /* allocate the bitmap */
1460 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1461 spin_lock(&block_group->tree_lock);
1462 if (!info->bitmap) {
1463 ret = -ENOMEM;
1464 goto out;
1465 }
1466 goto again;
1467 }
1468
1469out:
1470 if (info) {
1471 if (info->bitmap)
1472 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001473 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001474 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001475
1476 return ret;
1477}
1478
Li Zefan120d66e2010-11-09 14:56:50 +08001479bool try_merge_free_space(struct btrfs_block_group_cache *block_group,
Li Zefanf333adb2010-11-09 14:57:39 +08001480 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001481{
Li Zefan120d66e2010-11-09 14:56:50 +08001482 struct btrfs_free_space *left_info;
1483 struct btrfs_free_space *right_info;
1484 bool merged = false;
1485 u64 offset = info->offset;
1486 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001487
Josef Bacik0f9dd462008-09-23 13:14:11 -04001488 /*
1489 * first we want to see if there is free space adjacent to the range we
1490 * are adding, if there is remove that struct and add a new one to
1491 * cover the entire range
1492 */
Josef Bacik96303082009-07-13 21:29:25 -04001493 right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
1494 if (right_info && rb_prev(&right_info->offset_index))
1495 left_info = rb_entry(rb_prev(&right_info->offset_index),
1496 struct btrfs_free_space, offset_index);
1497 else
1498 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001499
Josef Bacik96303082009-07-13 21:29:25 -04001500 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001501 if (update_stat)
1502 unlink_free_space(block_group, right_info);
1503 else
1504 __unlink_free_space(block_group, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001505 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001506 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001507 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001508 }
1509
Josef Bacik96303082009-07-13 21:29:25 -04001510 if (left_info && !left_info->bitmap &&
1511 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001512 if (update_stat)
1513 unlink_free_space(block_group, left_info);
1514 else
1515 __unlink_free_space(block_group, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001516 info->offset = left_info->offset;
1517 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001518 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001519 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001520 }
1521
Li Zefan120d66e2010-11-09 14:56:50 +08001522 return merged;
1523}
1524
1525int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1526 u64 offset, u64 bytes)
1527{
1528 struct btrfs_free_space *info;
1529 int ret = 0;
1530
Josef Bacikdc89e982011-01-28 17:05:48 -05001531 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001532 if (!info)
1533 return -ENOMEM;
1534
1535 info->offset = offset;
1536 info->bytes = bytes;
1537
1538 spin_lock(&block_group->tree_lock);
1539
Li Zefanf333adb2010-11-09 14:57:39 +08001540 if (try_merge_free_space(block_group, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001541 goto link;
1542
1543 /*
1544 * There was no extent directly to the left or right of this new
1545 * extent then we know we're going to have to allocate a new extent, so
1546 * before we do that see if we need to drop this into a bitmap
1547 */
1548 ret = insert_into_bitmap(block_group, info);
1549 if (ret < 0) {
1550 goto out;
1551 } else if (ret) {
1552 ret = 0;
1553 goto out;
1554 }
1555link:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001556 ret = link_free_space(block_group, info);
1557 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001558 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001559out:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001560 spin_unlock(&block_group->tree_lock);
1561
Josef Bacik0f9dd462008-09-23 13:14:11 -04001562 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001563 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001564 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001565 }
1566
Josef Bacik0f9dd462008-09-23 13:14:11 -04001567 return ret;
1568}
1569
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001570int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1571 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001572{
1573 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001574 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001575 int ret = 0;
1576
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001577 spin_lock(&block_group->tree_lock);
1578
Josef Bacik96303082009-07-13 21:29:25 -04001579again:
1580 info = tree_search_offset(block_group, offset, 0, 0);
1581 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001582 /*
1583 * oops didn't find an extent that matched the space we wanted
1584 * to remove, look for a bitmap instead
1585 */
1586 info = tree_search_offset(block_group,
1587 offset_to_bitmap(block_group, offset),
1588 1, 0);
1589 if (!info) {
1590 WARN_ON(1);
1591 goto out_lock;
1592 }
Josef Bacik96303082009-07-13 21:29:25 -04001593 }
1594
1595 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1596 u64 end;
1597 next_info = rb_entry(rb_next(&info->offset_index),
1598 struct btrfs_free_space,
1599 offset_index);
1600
1601 if (next_info->bitmap)
1602 end = next_info->offset + BITS_PER_BITMAP *
1603 block_group->sectorsize - 1;
1604 else
1605 end = next_info->offset + next_info->bytes;
1606
1607 if (next_info->bytes < bytes ||
1608 next_info->offset > offset || offset > end) {
1609 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1610 " trying to use %llu\n",
1611 (unsigned long long)info->offset,
1612 (unsigned long long)info->bytes,
1613 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001614 WARN_ON(1);
1615 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001616 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001617 }
Josef Bacik96303082009-07-13 21:29:25 -04001618
1619 info = next_info;
1620 }
1621
1622 if (info->bytes == bytes) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001623 unlink_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -04001624 if (info->bitmap) {
1625 kfree(info->bitmap);
1626 block_group->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001627 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001628 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001629 goto out_lock;
1630 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001631
Josef Bacik96303082009-07-13 21:29:25 -04001632 if (!info->bitmap && info->offset == offset) {
1633 unlink_free_space(block_group, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001634 info->offset += bytes;
1635 info->bytes -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001636 link_free_space(block_group, info);
1637 goto out_lock;
1638 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001639
Josef Bacik96303082009-07-13 21:29:25 -04001640 if (!info->bitmap && info->offset <= offset &&
1641 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001642 u64 old_start = info->offset;
1643 /*
1644 * we're freeing space in the middle of the info,
1645 * this can happen during tree log replay
1646 *
1647 * first unlink the old info and then
1648 * insert it again after the hole we're creating
1649 */
1650 unlink_free_space(block_group, info);
1651 if (offset + bytes < info->offset + info->bytes) {
1652 u64 old_end = info->offset + info->bytes;
1653
1654 info->offset = offset + bytes;
1655 info->bytes = old_end - info->offset;
1656 ret = link_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -04001657 WARN_ON(ret);
1658 if (ret)
1659 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001660 } else {
1661 /* the hole we're creating ends at the end
1662 * of the info struct, just free the info
1663 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001664 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001665 }
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001666 spin_unlock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001667
1668 /* step two, insert a new info struct to cover
1669 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001670 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001671 ret = btrfs_add_free_space(block_group, old_start,
1672 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001673 WARN_ON(ret);
1674 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001675 }
Josef Bacik96303082009-07-13 21:29:25 -04001676
1677 ret = remove_from_bitmap(block_group, info, &offset, &bytes);
1678 if (ret == -EAGAIN)
1679 goto again;
1680 BUG_ON(ret);
1681out_lock:
1682 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001683out:
Josef Bacik25179202008-10-29 14:49:05 -04001684 return ret;
1685}
1686
Josef Bacik0f9dd462008-09-23 13:14:11 -04001687void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1688 u64 bytes)
1689{
1690 struct btrfs_free_space *info;
1691 struct rb_node *n;
1692 int count = 0;
1693
1694 for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
1695 info = rb_entry(n, struct btrfs_free_space, offset_index);
1696 if (info->bytes >= bytes)
1697 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001698 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001699 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001700 (unsigned long long)info->bytes,
1701 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001702 }
Josef Bacik96303082009-07-13 21:29:25 -04001703 printk(KERN_INFO "block group has cluster?: %s\n",
1704 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001705 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1706 "\n", count);
1707}
1708
1709u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
1710{
1711 struct btrfs_free_space *info;
1712 struct rb_node *n;
1713 u64 ret = 0;
1714
1715 for (n = rb_first(&block_group->free_space_offset); n;
1716 n = rb_next(n)) {
1717 info = rb_entry(n, struct btrfs_free_space, offset_index);
1718 ret += info->bytes;
1719 }
1720
1721 return ret;
1722}
1723
Chris Masonfa9c0d792009-04-03 09:47:43 -04001724/*
1725 * for a given cluster, put all of its extents back into the free
1726 * space cache. If the block group passed doesn't match the block group
1727 * pointed to by the cluster, someone else raced in and freed the
1728 * cluster already. In that case, we just return without changing anything
1729 */
1730static int
1731__btrfs_return_cluster_to_free_space(
1732 struct btrfs_block_group_cache *block_group,
1733 struct btrfs_free_cluster *cluster)
1734{
1735 struct btrfs_free_space *entry;
1736 struct rb_node *node;
1737
1738 spin_lock(&cluster->lock);
1739 if (cluster->block_group != block_group)
1740 goto out;
1741
Josef Bacik96303082009-07-13 21:29:25 -04001742 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001743 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001744 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001745
Chris Masonfa9c0d792009-04-03 09:47:43 -04001746 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001747 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001748 bool bitmap;
1749
Chris Masonfa9c0d792009-04-03 09:47:43 -04001750 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1751 node = rb_next(&entry->offset_index);
1752 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001753
1754 bitmap = (entry->bitmap != NULL);
1755 if (!bitmap)
1756 try_merge_free_space(block_group, entry, false);
Josef Bacik96303082009-07-13 21:29:25 -04001757 tree_insert_offset(&block_group->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001758 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001759 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001760 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001761
Chris Masonfa9c0d792009-04-03 09:47:43 -04001762out:
1763 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001764 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001765 return 0;
1766}
1767
Josef Bacik0f9dd462008-09-23 13:14:11 -04001768void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1769{
1770 struct btrfs_free_space *info;
1771 struct rb_node *node;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001772 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001773 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001774
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001775 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001776 while ((head = block_group->cluster_list.next) !=
1777 &block_group->cluster_list) {
1778 cluster = list_entry(head, struct btrfs_free_cluster,
1779 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001780
1781 WARN_ON(cluster->block_group != block_group);
1782 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001783 if (need_resched()) {
1784 spin_unlock(&block_group->tree_lock);
1785 cond_resched();
1786 spin_lock(&block_group->tree_lock);
1787 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001788 }
1789
Josef Bacik96303082009-07-13 21:29:25 -04001790 while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
1791 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacika4f01622011-04-25 19:43:52 -04001792 if (!info->bitmap) {
1793 unlink_free_space(block_group, info);
1794 kmem_cache_free(btrfs_free_space_cachep, info);
1795 } else {
1796 free_bitmap(block_group, info);
1797 }
1798
Josef Bacik0f9dd462008-09-23 13:14:11 -04001799 if (need_resched()) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001800 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001801 cond_resched();
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001802 spin_lock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001803 }
1804 }
Josef Bacik96303082009-07-13 21:29:25 -04001805
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001806 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001807}
1808
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001809u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1810 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001811{
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001812 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001813 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001814 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001815
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001816 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001817 entry = find_free_space(block_group, &offset, &bytes_search, 0);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001818 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001819 goto out;
1820
1821 ret = offset;
1822 if (entry->bitmap) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001823 bitmap_clear_bits(block_group, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001824 if (!entry->bytes)
1825 free_bitmap(block_group, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001826 } else {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001827 unlink_free_space(block_group, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001828 entry->offset += bytes;
1829 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001830 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001831 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001832 else
1833 link_free_space(block_group, entry);
1834 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001835
Josef Bacik96303082009-07-13 21:29:25 -04001836out:
1837 spin_unlock(&block_group->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001838
Josef Bacik0f9dd462008-09-23 13:14:11 -04001839 return ret;
1840}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001841
1842/*
1843 * given a cluster, put all of its extents back into the free space
1844 * cache. If a block group is passed, this function will only free
1845 * a cluster that belongs to the passed block group.
1846 *
1847 * Otherwise, it'll get a reference on the block group pointed to by the
1848 * cluster and remove the cluster from it.
1849 */
1850int btrfs_return_cluster_to_free_space(
1851 struct btrfs_block_group_cache *block_group,
1852 struct btrfs_free_cluster *cluster)
1853{
1854 int ret;
1855
1856 /* first, get a safe pointer to the block group */
1857 spin_lock(&cluster->lock);
1858 if (!block_group) {
1859 block_group = cluster->block_group;
1860 if (!block_group) {
1861 spin_unlock(&cluster->lock);
1862 return 0;
1863 }
1864 } else if (cluster->block_group != block_group) {
1865 /* someone else has already freed it don't redo their work */
1866 spin_unlock(&cluster->lock);
1867 return 0;
1868 }
1869 atomic_inc(&block_group->count);
1870 spin_unlock(&cluster->lock);
1871
1872 /* now return any extents the cluster had on it */
1873 spin_lock(&block_group->tree_lock);
1874 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1875 spin_unlock(&block_group->tree_lock);
1876
1877 /* finally drop our ref */
1878 btrfs_put_block_group(block_group);
1879 return ret;
1880}
1881
Josef Bacik96303082009-07-13 21:29:25 -04001882static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1883 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001884 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001885 u64 bytes, u64 min_start)
1886{
Josef Bacik96303082009-07-13 21:29:25 -04001887 int err;
1888 u64 search_start = cluster->window_start;
1889 u64 search_bytes = bytes;
1890 u64 ret = 0;
1891
Josef Bacik96303082009-07-13 21:29:25 -04001892 search_start = min_start;
1893 search_bytes = bytes;
1894
1895 err = search_bitmap(block_group, entry, &search_start,
1896 &search_bytes);
1897 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04001898 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04001899
1900 ret = search_start;
Josef Bacik817d52f2009-07-13 21:29:25 -04001901 bitmap_clear_bits(block_group, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001902
1903 return ret;
1904}
1905
Chris Masonfa9c0d792009-04-03 09:47:43 -04001906/*
1907 * given a cluster, try to allocate 'bytes' from it, returns 0
1908 * if it couldn't find anything suitably large, or a logical disk offset
1909 * if things worked out
1910 */
1911u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1912 struct btrfs_free_cluster *cluster, u64 bytes,
1913 u64 min_start)
1914{
1915 struct btrfs_free_space *entry = NULL;
1916 struct rb_node *node;
1917 u64 ret = 0;
1918
1919 spin_lock(&cluster->lock);
1920 if (bytes > cluster->max_size)
1921 goto out;
1922
1923 if (cluster->block_group != block_group)
1924 goto out;
1925
1926 node = rb_first(&cluster->root);
1927 if (!node)
1928 goto out;
1929
1930 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001931 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001932 if (entry->bytes < bytes ||
1933 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001934 struct rb_node *node;
1935
1936 node = rb_next(&entry->offset_index);
1937 if (!node)
1938 break;
1939 entry = rb_entry(node, struct btrfs_free_space,
1940 offset_index);
1941 continue;
1942 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001943
Josef Bacik4e69b592011-03-21 10:11:24 -04001944 if (entry->bitmap) {
1945 ret = btrfs_alloc_from_bitmap(block_group,
1946 cluster, entry, bytes,
1947 min_start);
1948 if (ret == 0) {
1949 struct rb_node *node;
1950 node = rb_next(&entry->offset_index);
1951 if (!node)
1952 break;
1953 entry = rb_entry(node, struct btrfs_free_space,
1954 offset_index);
1955 continue;
1956 }
1957 } else {
1958
1959 ret = entry->offset;
1960
1961 entry->offset += bytes;
1962 entry->bytes -= bytes;
1963 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001964
Li Zefan5e71b5d2010-11-09 14:55:34 +08001965 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04001966 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001967 break;
1968 }
1969out:
1970 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001971
Li Zefan5e71b5d2010-11-09 14:55:34 +08001972 if (!ret)
1973 return 0;
1974
1975 spin_lock(&block_group->tree_lock);
1976
1977 block_group->free_space -= bytes;
1978 if (entry->bytes == 0) {
1979 block_group->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04001980 if (entry->bitmap) {
1981 kfree(entry->bitmap);
1982 block_group->total_bitmaps--;
1983 recalculate_thresholds(block_group);
1984 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001985 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08001986 }
1987
1988 spin_unlock(&block_group->tree_lock);
1989
Chris Masonfa9c0d792009-04-03 09:47:43 -04001990 return ret;
1991}
1992
Josef Bacik96303082009-07-13 21:29:25 -04001993static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1994 struct btrfs_free_space *entry,
1995 struct btrfs_free_cluster *cluster,
1996 u64 offset, u64 bytes, u64 min_bytes)
1997{
1998 unsigned long next_zero;
1999 unsigned long i;
2000 unsigned long search_bits;
2001 unsigned long total_bits;
2002 unsigned long found_bits;
2003 unsigned long start = 0;
2004 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002005 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002006 bool found = false;
2007
2008 i = offset_to_bit(entry->offset, block_group->sectorsize,
2009 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002010 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2011 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002012
2013again:
2014 found_bits = 0;
2015 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2016 i < BITS_PER_BITMAP;
2017 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2018 next_zero = find_next_zero_bit(entry->bitmap,
2019 BITS_PER_BITMAP, i);
2020 if (next_zero - i >= search_bits) {
2021 found_bits = next_zero - i;
2022 break;
2023 }
2024 i = next_zero;
2025 }
2026
2027 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002028 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002029
2030 if (!found) {
2031 start = i;
2032 found = true;
2033 }
2034
2035 total_found += found_bits;
2036
2037 if (cluster->max_size < found_bits * block_group->sectorsize)
2038 cluster->max_size = found_bits * block_group->sectorsize;
2039
2040 if (total_found < total_bits) {
2041 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2042 if (i - start > total_bits * 2) {
2043 total_found = 0;
2044 cluster->max_size = 0;
2045 found = false;
2046 }
2047 goto again;
2048 }
2049
2050 cluster->window_start = start * block_group->sectorsize +
2051 entry->offset;
Josef Bacik4e69b592011-03-21 10:11:24 -04002052 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2053 ret = tree_insert_offset(&cluster->root, entry->offset,
2054 &entry->offset_index, 1);
2055 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002056
2057 return 0;
2058}
2059
Chris Masonfa9c0d792009-04-03 09:47:43 -04002060/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002061 * This searches the block group for just extents to fill the cluster with.
2062 */
2063static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2064 struct btrfs_free_cluster *cluster,
2065 u64 offset, u64 bytes, u64 min_bytes)
2066{
2067 struct btrfs_free_space *first = NULL;
2068 struct btrfs_free_space *entry = NULL;
2069 struct btrfs_free_space *prev = NULL;
2070 struct btrfs_free_space *last;
2071 struct rb_node *node;
2072 u64 window_start;
2073 u64 window_free;
2074 u64 max_extent;
2075 u64 max_gap = 128 * 1024;
2076
2077 entry = tree_search_offset(block_group, offset, 0, 1);
2078 if (!entry)
2079 return -ENOSPC;
2080
2081 /*
2082 * We don't want bitmaps, so just move along until we find a normal
2083 * extent entry.
2084 */
2085 while (entry->bitmap) {
2086 node = rb_next(&entry->offset_index);
2087 if (!node)
2088 return -ENOSPC;
2089 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2090 }
2091
2092 window_start = entry->offset;
2093 window_free = entry->bytes;
2094 max_extent = entry->bytes;
2095 first = entry;
2096 last = entry;
2097 prev = entry;
2098
2099 while (window_free <= min_bytes) {
2100 node = rb_next(&entry->offset_index);
2101 if (!node)
2102 return -ENOSPC;
2103 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2104
2105 if (entry->bitmap)
2106 continue;
2107 /*
2108 * we haven't filled the empty size and the window is
2109 * very large. reset and try again
2110 */
2111 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2112 entry->offset - window_start > (min_bytes * 2)) {
2113 first = entry;
2114 window_start = entry->offset;
2115 window_free = entry->bytes;
2116 last = entry;
2117 max_extent = entry->bytes;
2118 } else {
2119 last = entry;
2120 window_free += entry->bytes;
2121 if (entry->bytes > max_extent)
2122 max_extent = entry->bytes;
2123 }
2124 prev = entry;
2125 }
2126
2127 cluster->window_start = first->offset;
2128
2129 node = &first->offset_index;
2130
2131 /*
2132 * now we've found our entries, pull them out of the free space
2133 * cache and put them into the cluster rbtree
2134 */
2135 do {
2136 int ret;
2137
2138 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2139 node = rb_next(&entry->offset_index);
2140 if (entry->bitmap)
2141 continue;
2142
2143 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2144 ret = tree_insert_offset(&cluster->root, entry->offset,
2145 &entry->offset_index, 0);
2146 BUG_ON(ret);
2147 } while (node && entry != last);
2148
2149 cluster->max_size = max_extent;
2150
2151 return 0;
2152}
2153
2154/*
2155 * This specifically looks for bitmaps that may work in the cluster, we assume
2156 * that we have already failed to find extents that will work.
2157 */
2158static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2159 struct btrfs_free_cluster *cluster,
2160 u64 offset, u64 bytes, u64 min_bytes)
2161{
2162 struct btrfs_free_space *entry;
2163 struct rb_node *node;
2164 int ret = -ENOSPC;
2165
2166 if (block_group->total_bitmaps == 0)
2167 return -ENOSPC;
2168
2169 entry = tree_search_offset(block_group,
2170 offset_to_bitmap(block_group, offset),
2171 0, 1);
2172 if (!entry)
2173 return -ENOSPC;
2174
2175 node = &entry->offset_index;
2176 do {
2177 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2178 node = rb_next(&entry->offset_index);
2179 if (!entry->bitmap)
2180 continue;
2181 if (entry->bytes < min_bytes)
2182 continue;
2183 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2184 bytes, min_bytes);
2185 } while (ret && node);
2186
2187 return ret;
2188}
2189
2190/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002191 * here we try to find a cluster of blocks in a block group. The goal
2192 * is to find at least bytes free and up to empty_size + bytes free.
2193 * We might not find them all in one contiguous area.
2194 *
2195 * returns zero and sets up cluster if things worked out, otherwise
2196 * it returns -enospc
2197 */
2198int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002199 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002200 struct btrfs_block_group_cache *block_group,
2201 struct btrfs_free_cluster *cluster,
2202 u64 offset, u64 bytes, u64 empty_size)
2203{
Chris Masonfa9c0d792009-04-03 09:47:43 -04002204 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002205 int ret;
2206
2207 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002208 if (btrfs_test_opt(root, SSD_SPREAD)) {
2209 min_bytes = bytes + empty_size;
2210 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002211 /*
2212 * we want to do larger allocations when we are
2213 * flushing out the delayed refs, it helps prevent
2214 * making more work as we go along.
2215 */
2216 if (trans->transaction->delayed_refs.flushing)
2217 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2218 else
2219 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2220 } else
2221 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2222
2223 spin_lock(&block_group->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002224
2225 /*
2226 * If we know we don't have enough space to make a cluster don't even
2227 * bother doing all the work to try and find one.
2228 */
2229 if (block_group->free_space < min_bytes) {
2230 spin_unlock(&block_group->tree_lock);
2231 return -ENOSPC;
2232 }
2233
Chris Masonfa9c0d792009-04-03 09:47:43 -04002234 spin_lock(&cluster->lock);
2235
2236 /* someone already found a cluster, hooray */
2237 if (cluster->block_group) {
2238 ret = 0;
2239 goto out;
2240 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002241
2242 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2243 min_bytes);
2244 if (ret)
2245 ret = setup_cluster_bitmap(block_group, cluster, offset,
2246 bytes, min_bytes);
2247
2248 if (!ret) {
2249 atomic_inc(&block_group->count);
2250 list_add_tail(&cluster->block_group_list,
2251 &block_group->cluster_list);
2252 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002253 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002254out:
2255 spin_unlock(&cluster->lock);
2256 spin_unlock(&block_group->tree_lock);
2257
2258 return ret;
2259}
2260
2261/*
2262 * simple code to zero out a cluster
2263 */
2264void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2265{
2266 spin_lock_init(&cluster->lock);
2267 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002268 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002269 cluster->max_size = 0;
2270 INIT_LIST_HEAD(&cluster->block_group_list);
2271 cluster->block_group = NULL;
2272}
2273
Li Dongyangf7039b12011-03-24 10:24:28 +00002274int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2275 u64 *trimmed, u64 start, u64 end, u64 minlen)
2276{
2277 struct btrfs_free_space *entry = NULL;
2278 struct btrfs_fs_info *fs_info = block_group->fs_info;
2279 u64 bytes = 0;
2280 u64 actually_trimmed;
2281 int ret = 0;
2282
2283 *trimmed = 0;
2284
2285 while (start < end) {
2286 spin_lock(&block_group->tree_lock);
2287
2288 if (block_group->free_space < minlen) {
2289 spin_unlock(&block_group->tree_lock);
2290 break;
2291 }
2292
2293 entry = tree_search_offset(block_group, start, 0, 1);
2294 if (!entry)
2295 entry = tree_search_offset(block_group,
2296 offset_to_bitmap(block_group,
2297 start),
2298 1, 1);
2299
2300 if (!entry || entry->offset >= end) {
2301 spin_unlock(&block_group->tree_lock);
2302 break;
2303 }
2304
2305 if (entry->bitmap) {
2306 ret = search_bitmap(block_group, entry, &start, &bytes);
2307 if (!ret) {
2308 if (start >= end) {
2309 spin_unlock(&block_group->tree_lock);
2310 break;
2311 }
2312 bytes = min(bytes, end - start);
2313 bitmap_clear_bits(block_group, entry,
2314 start, bytes);
2315 if (entry->bytes == 0)
2316 free_bitmap(block_group, entry);
2317 } else {
2318 start = entry->offset + BITS_PER_BITMAP *
2319 block_group->sectorsize;
2320 spin_unlock(&block_group->tree_lock);
2321 ret = 0;
2322 continue;
2323 }
2324 } else {
2325 start = entry->offset;
2326 bytes = min(entry->bytes, end - start);
2327 unlink_free_space(block_group, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002328 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002329 }
2330
2331 spin_unlock(&block_group->tree_lock);
2332
2333 if (bytes >= minlen) {
2334 int update_ret;
2335 update_ret = btrfs_update_reserved_bytes(block_group,
2336 bytes, 1, 1);
2337
2338 ret = btrfs_error_discard_extent(fs_info->extent_root,
2339 start,
2340 bytes,
2341 &actually_trimmed);
2342
2343 btrfs_add_free_space(block_group,
2344 start, bytes);
2345 if (!update_ret)
2346 btrfs_update_reserved_bytes(block_group,
2347 bytes, 0, 1);
2348
2349 if (ret)
2350 break;
2351 *trimmed += actually_trimmed;
2352 }
2353 start += bytes;
2354 bytes = 0;
2355
2356 if (fatal_signal_pending(current)) {
2357 ret = -ERESTARTSYS;
2358 break;
2359 }
2360
2361 cond_resched();
2362 }
2363
2364 return ret;
2365}