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