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