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