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