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