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