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