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