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