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