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