blob: 01a408db5683344fd30559b806bece64698bee93 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacik0f9dd462008-09-23 13:14:11 -04002/*
3 * Copyright (C) 2008 Red Hat. All rights reserved.
Josef Bacik0f9dd462008-09-23 13:14:11 -04004 */
5
Josef Bacik96303082009-07-13 21:29:25 -04006#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -04007#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01008#include <linux/sched/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040010#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040011#include <linux/ratelimit.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090012#include <linux/error-injection.h>
Josef Bacik84de76a2018-09-28 07:17:49 -040013#include <linux/sched/mm.h>
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +090014#include "misc.h"
Josef Bacik0f9dd462008-09-23 13:14:11 -040015#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040016#include "free-space-cache.h"
17#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040018#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000019#include "extent_io.h"
Filipe Manana04216822014-11-27 21:14:15 +000020#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040021#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040022#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040023#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080024#include "discard.h"
Qu Wenruoe4f94342021-09-27 15:21:49 +080025#include "subpage.h"
Josef Bacik26c2c452021-12-03 17:18:03 -050026#include "inode-item.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040027
Feifei Xu0ef64472016-06-01 19:18:24 +080028#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Dennis Zhou5d90c5c2020-01-02 16:26:43 -050029#define MAX_CACHE_BYTES_PER_GIG SZ_64K
30#define FORCE_EXTENT_THRESHOLD SZ_1M
Josef Bacik96303082009-07-13 21:29:25 -040031
Filipe Manana55507ce2014-12-01 17:04:09 +000032struct btrfs_trim_range {
33 u64 start;
34 u64 bytes;
35 struct list_head list;
36};
37
Li Zefan34d52cb2011-03-29 13:46:06 +080038static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040039 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040040static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Nikolay Borisov32e16492021-11-23 14:44:20 +020041 struct btrfs_free_space *info, bool update_stat);
Josef Bacikcd799092020-10-23 09:58:08 -040042static int search_bitmap(struct btrfs_free_space_ctl *ctl,
43 struct btrfs_free_space *bitmap_info, u64 *offset,
44 u64 *bytes, bool for_alloc);
45static void free_bitmap(struct btrfs_free_space_ctl *ctl,
46 struct btrfs_free_space *bitmap_info);
47static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
48 struct btrfs_free_space *info, u64 offset,
Nikolay Borisovf594f132021-11-23 14:44:19 +020049 u64 bytes, bool update_stats);
Josef Bacik0cb59c92010-07-02 12:14:14 -040050
Li Zefan0414efa2011-04-20 10:20:14 +080051static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
52 struct btrfs_path *path,
53 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040054{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040055 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040056 struct btrfs_key key;
57 struct btrfs_key location;
58 struct btrfs_disk_key disk_key;
59 struct btrfs_free_space_header *header;
60 struct extent_buffer *leaf;
61 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040062 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040063 int ret;
64
Josef Bacik0af3d002010-06-21 14:48:16 -040065 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080066 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040067 key.type = 0;
68
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
70 if (ret < 0)
71 return ERR_PTR(ret);
72 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020073 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040074 return ERR_PTR(-ENOENT);
75 }
76
77 leaf = path->nodes[0];
78 header = btrfs_item_ptr(leaf, path->slots[0],
79 struct btrfs_free_space_header);
80 btrfs_free_space_key(leaf, header, &disk_key);
81 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020082 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040083
Josef Bacik84de76a2018-09-28 07:17:49 -040084 /*
85 * We are often under a trans handle at this point, so we need to make
86 * sure NOFS is set to keep us from deadlocking.
87 */
88 nofs_flag = memalloc_nofs_save();
David Sterba0202e832020-05-15 19:35:59 +020089 inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
Filipe Manana4222ea72018-10-24 10:13:03 +010090 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040091 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040092 if (IS_ERR(inode))
93 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040094
Al Viro528c0322012-04-13 11:03:55 -040095 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080096 mapping_gfp_constraint(inode->i_mapping,
97 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000098
Li Zefan0414efa2011-04-20 10:20:14 +080099 return inode;
100}
101
David Sterba32da53862019-10-29 19:20:18 +0100102struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
David Sterba7949f332019-03-20 13:40:19 +0100103 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +0800104{
David Sterba7949f332019-03-20 13:40:19 +0100105 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +0800106 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400107 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800108
109 spin_lock(&block_group->lock);
110 if (block_group->inode)
111 inode = igrab(block_group->inode);
112 spin_unlock(&block_group->lock);
113 if (inode)
114 return inode;
115
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500116 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200117 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800118 if (IS_ERR(inode))
119 return inode;
120
Josef Bacik0af3d002010-06-21 14:48:16 -0400121 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400122 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400123 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400124 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
125 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400126 block_group->disk_cache_state = BTRFS_DC_CLEAR;
127 }
128
Josef Bacik300e4f82011-08-29 14:06:00 -0400129 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400130 block_group->inode = igrab(inode);
131 block_group->iref = 1;
132 }
133 spin_unlock(&block_group->lock);
134
135 return inode;
136}
137
Eric Sandeen48a3b632013-04-25 20:41:01 +0000138static int __create_free_space_inode(struct btrfs_root *root,
139 struct btrfs_trans_handle *trans,
140 struct btrfs_path *path,
141 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400142{
143 struct btrfs_key key;
144 struct btrfs_disk_key disk_key;
145 struct btrfs_free_space_header *header;
146 struct btrfs_inode_item *inode_item;
147 struct extent_buffer *leaf;
Nikolay Borisovf0d12192020-12-03 10:09:49 +0200148 /* We inline CRCs for the free disk space cache */
149 const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC |
150 BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Josef Bacik0af3d002010-06-21 14:48:16 -0400151 int ret;
152
Li Zefan0414efa2011-04-20 10:20:14 +0800153 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400154 if (ret)
155 return ret;
156
157 leaf = path->nodes[0];
158 inode_item = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_inode_item);
160 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100161 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400162 sizeof(*inode_item));
163 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
164 btrfs_set_inode_size(leaf, inode_item, 0);
165 btrfs_set_inode_nbytes(leaf, inode_item, 0);
166 btrfs_set_inode_uid(leaf, inode_item, 0);
167 btrfs_set_inode_gid(leaf, inode_item, 0);
168 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400169 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400170 btrfs_set_inode_nlink(leaf, inode_item, 1);
171 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800172 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400173 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200174 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400175
176 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800177 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400179 ret = btrfs_insert_empty_item(trans, root, path, &key,
180 sizeof(struct btrfs_free_space_header));
181 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200182 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400183 return ret;
184 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700185
Josef Bacik0af3d002010-06-21 14:48:16 -0400186 leaf = path->nodes[0];
187 header = btrfs_item_ptr(leaf, path->slots[0],
188 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100189 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400190 btrfs_set_free_space_key(leaf, header, &disk_key);
191 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200192 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400193
194 return 0;
195}
196
David Sterba4ca75f12019-03-20 13:42:57 +0100197int create_free_space_inode(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100198 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +0800199 struct btrfs_path *path)
200{
201 int ret;
202 u64 ino;
203
Nikolay Borisov543068a2020-12-07 17:32:33 +0200204 ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800205 if (ret < 0)
206 return ret;
207
David Sterba4ca75f12019-03-20 13:42:57 +0100208 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200209 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800210}
211
Boris Burkov36b216c2020-11-18 15:06:25 -0800212/*
213 * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode
214 * handles lookup, otherwise it takes ownership and iputs the inode.
215 * Don't reuse an inode pointer after passing it into this function.
216 */
217int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
218 struct inode *inode,
219 struct btrfs_block_group *block_group)
220{
221 struct btrfs_path *path;
222 struct btrfs_key key;
223 int ret = 0;
224
225 path = btrfs_alloc_path();
226 if (!path)
227 return -ENOMEM;
228
229 if (!inode)
230 inode = lookup_free_space_inode(block_group, path);
231 if (IS_ERR(inode)) {
232 if (PTR_ERR(inode) != -ENOENT)
233 ret = PTR_ERR(inode);
234 goto out;
235 }
236 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
237 if (ret) {
238 btrfs_add_delayed_iput(inode);
239 goto out;
240 }
241 clear_nlink(inode);
242 /* One for the block groups ref */
243 spin_lock(&block_group->lock);
244 if (block_group->iref) {
245 block_group->iref = 0;
246 block_group->inode = NULL;
247 spin_unlock(&block_group->lock);
248 iput(inode);
249 } else {
250 spin_unlock(&block_group->lock);
251 }
252 /* One for the lookup ref */
253 btrfs_add_delayed_iput(inode);
254
255 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
256 key.type = 0;
257 key.offset = block_group->start;
258 ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path,
259 -1, 1);
260 if (ret) {
261 if (ret > 0)
262 ret = 0;
263 goto out;
264 }
265 ret = btrfs_del_item(trans, trans->fs_info->tree_root, path);
266out:
267 btrfs_free_path(path);
268 return ret;
269}
270
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400271int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000272 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400273{
Josef Bacikc8174312011-11-02 09:29:35 -0400274 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000275 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400276
277 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400278 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
279 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400280
Miao Xie7b61cd92013-05-13 13:55:09 +0000281 spin_lock(&rsv->lock);
282 if (rsv->reserved < needed_bytes)
283 ret = -ENOSPC;
284 else
285 ret = 0;
286 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000287 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000288}
289
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500290int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100291 struct btrfs_block_group *block_group,
Josef Bacik9a4a1422021-12-03 17:18:05 -0500292 struct inode *vfs_inode)
Miao Xie7b61cd92013-05-13 13:55:09 +0000293{
Josef Bacikd9ac19c2021-12-03 17:18:09 -0500294 struct btrfs_truncate_control control = {
Josef Bacik71d18b52021-12-03 17:18:15 -0500295 .inode = BTRFS_I(vfs_inode),
Josef Bacikd9ac19c2021-12-03 17:18:09 -0500296 .new_size = 0,
Josef Bacik487e81d2021-12-03 17:18:14 -0500297 .ino = btrfs_ino(BTRFS_I(vfs_inode)),
Josef Bacikd9ac19c2021-12-03 17:18:09 -0500298 .min_type = BTRFS_EXTENT_DATA_KEY,
Josef Bacik655807b2021-12-03 17:18:13 -0500299 .clear_extent_range = true,
Josef Bacikd9ac19c2021-12-03 17:18:09 -0500300 };
Josef Bacik9a4a1422021-12-03 17:18:05 -0500301 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
302 struct btrfs_root *root = inode->root;
303 struct extent_state *cached_state = NULL;
Miao Xie7b61cd92013-05-13 13:55:09 +0000304 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100305 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700306
Chris Mason1bbc6212015-04-06 12:46:08 -0700307 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500308 struct btrfs_path *path = btrfs_alloc_path();
309
310 if (!path) {
311 ret = -ENOMEM;
312 goto fail;
313 }
Filipe Manana35c76642015-04-30 17:47:05 +0100314 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700315 mutex_lock(&trans->transaction->cache_write_mutex);
316 if (!list_empty(&block_group->io_list)) {
317 list_del_init(&block_group->io_list);
318
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400319 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700320 btrfs_put_block_group(block_group);
321 }
322
323 /*
324 * now that we've truncated the cache away, its no longer
325 * setup or written
326 */
327 spin_lock(&block_group->lock);
328 block_group->disk_cache_state = BTRFS_DC_CLEAR;
329 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500330 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700331 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400332
Josef Bacik9a4a1422021-12-03 17:18:05 -0500333 btrfs_i_size_write(inode, 0);
334 truncate_pagecache(vfs_inode, 0);
335
336 lock_extent_bits(&inode->io_tree, 0, (u64)-1, &cached_state);
337 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400338
339 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700340 * We skip the throttling logic for free space cache inodes, so we don't
341 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400342 */
Josef Bacik71d18b52021-12-03 17:18:15 -0500343 ret = btrfs_truncate_inode_items(trans, root, &control);
Josef Bacikc2ddb6122021-12-03 17:18:10 -0500344
Josef Bacik462b7282021-12-03 17:18:11 -0500345 inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
Josef Bacikc2ddb6122021-12-03 17:18:10 -0500346 btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
347
Josef Bacik9a4a1422021-12-03 17:18:05 -0500348 unlock_extent_cached(&inode->io_tree, 0, (u64)-1, &cached_state);
Filipe Manana35c76642015-04-30 17:47:05 +0100349 if (ret)
350 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400351
Josef Bacik9a4a1422021-12-03 17:18:05 -0500352 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700353
Chris Mason1bbc6212015-04-06 12:46:08 -0700354fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100355 if (locked)
356 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100357 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400358 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400359
Li Zefan82d59022011-04-20 10:33:24 +0800360 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400361}
362
David Sterba1d480532017-01-23 17:28:19 +0100363static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400364{
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500365 struct file_ra_state ra;
Josef Bacik9d66e232010-08-25 16:54:15 -0400366 unsigned long last_index;
367
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500368 file_ra_state_init(&ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300369 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400370
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500371 page_cache_sync_readahead(inode->i_mapping, &ra, NULL, 0, last_index);
Josef Bacik9d66e232010-08-25 16:54:15 -0400372}
373
Chris Mason4c6d1d82015-04-06 13:17:20 -0700374static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400375 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400376{
Miao Xie5349d6c2014-06-19 10:42:49 +0800377 int num_pages;
Miao Xie5349d6c2014-06-19 10:42:49 +0800378
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300379 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800380
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400381 /* Make sure we can fit our crcs and generation into the first page */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200382 if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800383 return -ENOSPC;
384
Chris Mason4c6d1d82015-04-06 13:17:20 -0700385 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800386
David Sterba31e818f2015-02-20 18:00:26 +0100387 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400388 if (!io_ctl->pages)
389 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800390
391 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400392 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700393 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800394
Josef Bacika67509c2011-10-05 15:18:58 -0400395 return 0;
396}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900397ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400398
Chris Mason4c6d1d82015-04-06 13:17:20 -0700399static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400400{
401 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700402 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400403}
404
Chris Mason4c6d1d82015-04-06 13:17:20 -0700405static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400406{
407 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400408 io_ctl->cur = NULL;
409 io_ctl->orig = NULL;
410 }
411}
412
Chris Mason4c6d1d82015-04-06 13:17:20 -0700413static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400414{
Josef Bacikb12d6862013-08-26 17:14:08 -0400415 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400416 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700417 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400418 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300419 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400420 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200421 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400422}
423
Chris Mason4c6d1d82015-04-06 13:17:20 -0700424static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400425{
426 int i;
427
428 io_ctl_unmap_page(io_ctl);
429
430 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800431 if (io_ctl->pages[i]) {
Qu Wenruoe4f94342021-09-27 15:21:49 +0800432 btrfs_page_clear_checked(io_ctl->fs_info,
433 io_ctl->pages[i],
434 page_offset(io_ctl->pages[i]),
435 PAGE_SIZE);
Li Zefana1ee5a42012-01-09 14:27:42 +0800436 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300437 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800438 }
Josef Bacika67509c2011-10-05 15:18:58 -0400439 }
440}
441
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900442static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
Josef Bacika67509c2011-10-05 15:18:58 -0400443{
444 struct page *page;
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900445 struct inode *inode = io_ctl->inode;
Josef Bacika67509c2011-10-05 15:18:58 -0400446 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
447 int i;
448
449 for (i = 0; i < io_ctl->num_pages; i++) {
Qu Wenruo32443de2021-01-26 16:34:00 +0800450 int ret;
451
Josef Bacika67509c2011-10-05 15:18:58 -0400452 page = find_or_create_page(inode->i_mapping, i, mask);
453 if (!page) {
454 io_ctl_drop_pages(io_ctl);
455 return -ENOMEM;
456 }
Qu Wenruo32443de2021-01-26 16:34:00 +0800457
458 ret = set_page_extent_mapped(page);
459 if (ret < 0) {
460 unlock_page(page);
461 put_page(page);
462 io_ctl_drop_pages(io_ctl);
463 return ret;
464 }
465
Josef Bacika67509c2011-10-05 15:18:58 -0400466 io_ctl->pages[i] = page;
467 if (uptodate && !PageUptodate(page)) {
468 btrfs_readpage(NULL, page);
469 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400470 if (page->mapping != inode->i_mapping) {
471 btrfs_err(BTRFS_I(inode)->root->fs_info,
472 "free space cache page truncated");
473 io_ctl_drop_pages(io_ctl);
474 return -EIO;
475 }
Josef Bacika67509c2011-10-05 15:18:58 -0400476 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500477 btrfs_err(BTRFS_I(inode)->root->fs_info,
478 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400479 io_ctl_drop_pages(io_ctl);
480 return -EIO;
481 }
482 }
483 }
484
Qu Wenruo32443de2021-01-26 16:34:00 +0800485 for (i = 0; i < io_ctl->num_pages; i++)
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500486 clear_page_dirty_for_io(io_ctl->pages[i]);
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500487
Josef Bacika67509c2011-10-05 15:18:58 -0400488 return 0;
489}
490
Chris Mason4c6d1d82015-04-06 13:17:20 -0700491static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400492{
Josef Bacika67509c2011-10-05 15:18:58 -0400493 io_ctl_map_page(io_ctl, 1);
494
495 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400496 * Skip the csum areas. If we don't check crcs then we just have a
497 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400498 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200499 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
500 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400501
David Sterba6994ca362020-09-15 13:32:34 +0200502 put_unaligned_le64(generation, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400503 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400504}
505
Chris Mason4c6d1d82015-04-06 13:17:20 -0700506static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400507{
David Sterba6994ca362020-09-15 13:32:34 +0200508 u64 cache_gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400509
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400510 /*
511 * Skip the crc area. If we don't check crcs then we just have a 64bit
512 * chunk at the front of the first page.
513 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200514 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
515 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400516
David Sterba6994ca362020-09-15 13:32:34 +0200517 cache_gen = get_unaligned_le64(io_ctl->cur);
518 if (cache_gen != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400519 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200520 "space cache generation (%llu) does not match inode (%llu)",
David Sterba6994ca362020-09-15 13:32:34 +0200521 cache_gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400522 io_ctl_unmap_page(io_ctl);
523 return -EIO;
524 }
525 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400526 return 0;
527}
528
Chris Mason4c6d1d82015-04-06 13:17:20 -0700529static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400530{
531 u32 *tmp;
532 u32 crc = ~(u32)0;
533 unsigned offset = 0;
534
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400535 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800536 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400537
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200538 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
539 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400540 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700541 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400542 tmp += index;
543 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400544}
545
Chris Mason4c6d1d82015-04-06 13:17:20 -0700546static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400547{
548 u32 *tmp, val;
549 u32 crc = ~(u32)0;
550 unsigned offset = 0;
551
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400552 if (index == 0)
553 offset = sizeof(u32) * io_ctl->num_pages;
554
Chris Mason2b108262015-04-06 07:48:20 -0700555 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 tmp += index;
557 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400558
559 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200560 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
561 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400562 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400563 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200564 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400565 io_ctl_unmap_page(io_ctl);
566 return -EIO;
567 }
568
Josef Bacika67509c2011-10-05 15:18:58 -0400569 return 0;
570}
571
Chris Mason4c6d1d82015-04-06 13:17:20 -0700572static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400573 void *bitmap)
574{
575 struct btrfs_free_space_entry *entry;
576
577 if (!io_ctl->cur)
578 return -ENOSPC;
579
580 entry = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200581 put_unaligned_le64(offset, &entry->offset);
582 put_unaligned_le64(bytes, &entry->bytes);
Josef Bacika67509c2011-10-05 15:18:58 -0400583 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
584 BTRFS_FREE_SPACE_EXTENT;
585 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
586 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
587
588 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
589 return 0;
590
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400591 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400592
593 /* No more pages to map */
594 if (io_ctl->index >= io_ctl->num_pages)
595 return 0;
596
597 /* map the next page */
598 io_ctl_map_page(io_ctl, 1);
599 return 0;
600}
601
Chris Mason4c6d1d82015-04-06 13:17:20 -0700602static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400603{
604 if (!io_ctl->cur)
605 return -ENOSPC;
606
607 /*
608 * If we aren't at the start of the current page, unmap this one and
609 * map the next one if there is any left.
610 */
611 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400612 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400613 if (io_ctl->index >= io_ctl->num_pages)
614 return -ENOSPC;
615 io_ctl_map_page(io_ctl, 0);
616 }
617
David Sterba69d24802018-06-29 10:56:44 +0200618 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400619 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400620 if (io_ctl->index < io_ctl->num_pages)
621 io_ctl_map_page(io_ctl, 0);
622 return 0;
623}
624
Chris Mason4c6d1d82015-04-06 13:17:20 -0700625static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400626{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400627 /*
628 * If we're not on the boundary we know we've modified the page and we
629 * need to crc the page.
630 */
631 if (io_ctl->cur != io_ctl->orig)
632 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
633 else
634 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400635
636 while (io_ctl->index < io_ctl->num_pages) {
637 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400638 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400639 }
640}
641
Chris Mason4c6d1d82015-04-06 13:17:20 -0700642static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400643 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400644{
645 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500646 int ret;
647
648 if (!io_ctl->cur) {
649 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
650 if (ret)
651 return ret;
652 }
Josef Bacika67509c2011-10-05 15:18:58 -0400653
654 e = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200655 entry->offset = get_unaligned_le64(&e->offset);
656 entry->bytes = get_unaligned_le64(&e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400657 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400658 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
659 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
660
661 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400662 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400663
664 io_ctl_unmap_page(io_ctl);
665
Josef Bacik2f120c02011-11-10 20:45:05 -0500666 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400667}
668
Chris Mason4c6d1d82015-04-06 13:17:20 -0700669static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400670 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400671{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400672 int ret;
673
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400674 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
675 if (ret)
676 return ret;
677
David Sterba69d24802018-06-29 10:56:44 +0200678 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400679 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400680
681 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400682}
683
David Sterbafa598b02020-12-03 17:18:38 +0100684static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
685{
Nikolay Borisov364be842021-11-23 14:44:22 +0200686 struct btrfs_block_group *block_group = ctl->block_group;
David Sterbafa598b02020-12-03 17:18:38 +0100687 u64 max_bytes;
688 u64 bitmap_bytes;
689 u64 extent_bytes;
690 u64 size = block_group->length;
691 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
692 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
693
694 max_bitmaps = max_t(u64, max_bitmaps, 1);
695
696 ASSERT(ctl->total_bitmaps <= max_bitmaps);
697
698 /*
699 * We are trying to keep the total amount of memory used per 1GiB of
700 * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
701 * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
702 * bitmaps, we may end up using more memory than this.
703 */
704 if (size < SZ_1G)
705 max_bytes = MAX_CACHE_BYTES_PER_GIG;
706 else
707 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
708
709 bitmap_bytes = ctl->total_bitmaps * ctl->unit;
710
711 /*
712 * we want the extent entry threshold to always be at most 1/2 the max
713 * bytes we can have, or whatever is less than that.
714 */
715 extent_bytes = max_bytes - bitmap_bytes;
716 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
717
718 ctl->extents_thresh =
719 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
720}
721
Eric Sandeen48a3b632013-04-25 20:41:01 +0000722static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
723 struct btrfs_free_space_ctl *ctl,
724 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400725{
David Sterba3ffbd682018-06-29 10:56:42 +0200726 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400727 struct btrfs_free_space_header *header;
728 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700729 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400730 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400731 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800732 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400733 u64 num_entries;
734 u64 num_bitmaps;
735 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400736 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400737 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400738
Josef Bacik9d66e232010-08-25 16:54:15 -0400739 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800740 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400741 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400742
743 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800744 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400745 key.type = 0;
746
747 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800748 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400749 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800750 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400751 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400752 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400753 }
754
Li Zefan0414efa2011-04-20 10:20:14 +0800755 ret = -1;
756
Josef Bacik9d66e232010-08-25 16:54:15 -0400757 leaf = path->nodes[0];
758 header = btrfs_item_ptr(leaf, path->slots[0],
759 struct btrfs_free_space_header);
760 num_entries = btrfs_free_space_entries(leaf, header);
761 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
762 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400763 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400764
Miao Xiee570fd22014-06-19 10:42:50 +0800765 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200767 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800768 offset);
769 return 0;
770 }
771
Josef Bacik9d66e232010-08-25 16:54:15 -0400772 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400773 btrfs_err(fs_info,
774 "free space inode generation (%llu) did not match free space cache generation (%llu)",
775 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400776 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400777 }
778
779 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400780 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400781
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400782 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800783 if (ret)
784 return ret;
785
David Sterba1d480532017-01-23 17:28:19 +0100786 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400787
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900788 ret = io_ctl_prepare_pages(&io_ctl, true);
Josef Bacika67509c2011-10-05 15:18:58 -0400789 if (ret)
790 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400791
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400792 ret = io_ctl_check_crc(&io_ctl, 0);
793 if (ret)
794 goto free_cache;
795
Josef Bacika67509c2011-10-05 15:18:58 -0400796 ret = io_ctl_check_generation(&io_ctl, generation);
797 if (ret)
798 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400799
Josef Bacika67509c2011-10-05 15:18:58 -0400800 while (num_entries) {
801 e = kmem_cache_zalloc(btrfs_free_space_cachep,
802 GFP_NOFS);
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800803 if (!e) {
804 ret = -ENOMEM;
Josef Bacik9d66e232010-08-25 16:54:15 -0400805 goto free_cache;
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800806 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400807
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400808 ret = io_ctl_read_entry(&io_ctl, e, &type);
809 if (ret) {
810 kmem_cache_free(btrfs_free_space_cachep, e);
811 goto free_cache;
812 }
813
Josef Bacika67509c2011-10-05 15:18:58 -0400814 if (!e->bytes) {
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800815 ret = -1;
Josef Bacika67509c2011-10-05 15:18:58 -0400816 kmem_cache_free(btrfs_free_space_cachep, e);
817 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400818 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400819
Josef Bacika67509c2011-10-05 15:18:58 -0400820 if (type == BTRFS_FREE_SPACE_EXTENT) {
821 spin_lock(&ctl->tree_lock);
822 ret = link_free_space(ctl, e);
823 spin_unlock(&ctl->tree_lock);
824 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400825 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000826 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500827 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400828 goto free_cache;
829 }
Josef Bacika67509c2011-10-05 15:18:58 -0400830 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400831 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400832 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000833 e->bitmap = kmem_cache_zalloc(
834 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400835 if (!e->bitmap) {
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800836 ret = -ENOMEM;
Josef Bacika67509c2011-10-05 15:18:58 -0400837 kmem_cache_free(
838 btrfs_free_space_cachep, e);
839 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400840 }
Josef Bacika67509c2011-10-05 15:18:58 -0400841 spin_lock(&ctl->tree_lock);
842 ret = link_free_space(ctl, e);
843 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +0100844 recalculate_thresholds(ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400845 spin_unlock(&ctl->tree_lock);
846 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000848 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400849 kmem_cache_free(btrfs_free_space_cachep, e);
850 goto free_cache;
851 }
852 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400853 }
854
Josef Bacika67509c2011-10-05 15:18:58 -0400855 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400856 }
857
Josef Bacik2f120c02011-11-10 20:45:05 -0500858 io_ctl_unmap_page(&io_ctl);
859
Josef Bacika67509c2011-10-05 15:18:58 -0400860 /*
861 * We add the bitmaps at the end of the entries in order that
862 * the bitmap entries are added to the cache.
863 */
864 list_for_each_entry_safe(e, n, &bitmaps, list) {
865 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400866 ret = io_ctl_read_bitmap(&io_ctl, e);
867 if (ret)
868 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400869 }
870
871 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400872 ret = 1;
873out:
Josef Bacika67509c2011-10-05 15:18:58 -0400874 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400875 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400876free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400877 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800878 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400879 goto out;
880}
881
Josef Bacikcd799092020-10-23 09:58:08 -0400882static int copy_free_space_cache(struct btrfs_block_group *block_group,
883 struct btrfs_free_space_ctl *ctl)
884{
885 struct btrfs_free_space *info;
886 struct rb_node *n;
887 int ret = 0;
888
889 while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
890 info = rb_entry(n, struct btrfs_free_space, offset_index);
891 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +0200892 unlink_free_space(ctl, info, true);
Josef Bacikcd799092020-10-23 09:58:08 -0400893 ret = btrfs_add_free_space(block_group, info->offset,
894 info->bytes);
895 kmem_cache_free(btrfs_free_space_cachep, info);
896 } else {
897 u64 offset = info->offset;
898 u64 bytes = ctl->unit;
899
900 while (search_bitmap(ctl, info, &offset, &bytes,
901 false) == 0) {
902 ret = btrfs_add_free_space(block_group, offset,
903 bytes);
904 if (ret)
905 break;
Nikolay Borisovf594f132021-11-23 14:44:19 +0200906 bitmap_clear_bits(ctl, info, offset, bytes, true);
Josef Bacikcd799092020-10-23 09:58:08 -0400907 offset = info->offset;
908 bytes = ctl->unit;
909 }
910 free_bitmap(ctl, info);
911 }
912 cond_resched();
913 }
914 return ret;
915}
916
David Sterba32da53862019-10-29 19:20:18 +0100917int load_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400918{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100919 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800920 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacikcd799092020-10-23 09:58:08 -0400921 struct btrfs_free_space_ctl tmp_ctl = {};
Li Zefan0414efa2011-04-20 10:20:14 +0800922 struct inode *inode;
923 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400924 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800925 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200926 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800927
928 /*
Josef Bacikcd799092020-10-23 09:58:08 -0400929 * Because we could potentially discard our loaded free space, we want
930 * to load everything into a temporary structure first, and then if it's
931 * valid copy it all into the actual free space ctl.
932 */
933 btrfs_init_free_space_ctl(block_group, &tmp_ctl);
934
935 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800936 * If this block group has been marked to be cleared for one reason or
937 * another then we can't trust the on disk cache, so just return.
938 */
939 spin_lock(&block_group->lock);
940 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
941 spin_unlock(&block_group->lock);
942 return 0;
943 }
944 spin_unlock(&block_group->lock);
945
946 path = btrfs_alloc_path();
947 if (!path)
948 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400949 path->search_commit_root = 1;
950 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800951
Filipe Manana4222ea72018-10-24 10:13:03 +0100952 /*
953 * We must pass a path with search_commit_root set to btrfs_iget in
954 * order to avoid a deadlock when allocating extents for the tree root.
955 *
956 * When we are COWing an extent buffer from the tree root, when looking
957 * for a free extent, at extent-tree.c:find_free_extent(), we can find
958 * block group without its free space cache loaded. When we find one
959 * we must load its space cache which requires reading its free space
960 * cache's inode item from the root tree. If this inode item is located
961 * in the same leaf that we started COWing before, then we end up in
962 * deadlock on the extent buffer (trying to read lock it when we
963 * previously write locked it).
964 *
965 * It's safe to read the inode item using the commit root because
966 * block groups, once loaded, stay in memory forever (until they are
967 * removed) as well as their space caches once loaded. New block groups
968 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
969 * we will never try to read their inode item while the fs is mounted.
970 */
David Sterba7949f332019-03-20 13:40:19 +0100971 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800972 if (IS_ERR(inode)) {
973 btrfs_free_path(path);
974 return 0;
975 }
976
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400977 /* We may have converted the inode and made the cache invalid. */
978 spin_lock(&block_group->lock);
979 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
980 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900981 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400982 goto out;
983 }
984 spin_unlock(&block_group->lock);
985
Josef Bacikcd799092020-10-23 09:58:08 -0400986 ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200987 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800988 btrfs_free_path(path);
989 if (ret <= 0)
990 goto out;
991
Josef Bacikcd799092020-10-23 09:58:08 -0400992 matched = (tmp_ctl.free_space == (block_group->length - used -
993 block_group->bytes_super));
Li Zefan0414efa2011-04-20 10:20:14 +0800994
Josef Bacikcd799092020-10-23 09:58:08 -0400995 if (matched) {
996 ret = copy_free_space_cache(block_group, &tmp_ctl);
997 /*
998 * ret == 1 means we successfully loaded the free space cache,
999 * so we need to re-set it here.
1000 */
1001 if (ret == 0)
1002 ret = 1;
1003 } else {
1004 __btrfs_remove_free_space_cache(&tmp_ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001005 btrfs_warn(fs_info,
1006 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +02001007 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +08001008 ret = -1;
1009 }
1010out:
1011 if (ret < 0) {
1012 /* This cache is bogus, make sure it gets cleared */
1013 spin_lock(&block_group->lock);
1014 block_group->disk_cache_state = BTRFS_DC_CLEAR;
1015 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001016 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +08001017
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001018 btrfs_warn(fs_info,
1019 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +02001020 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +08001021 }
1022
Josef Bacik66b53ba2020-10-23 09:58:07 -04001023 spin_lock(&ctl->tree_lock);
1024 btrfs_discard_update_discardable(block_group);
1025 spin_unlock(&ctl->tree_lock);
Li Zefan0414efa2011-04-20 10:20:14 +08001026 iput(inode);
1027 return ret;
1028}
1029
Chris Masond4452bc2014-05-19 20:47:56 -07001030static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -07001031int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001032 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001033 struct btrfs_block_group *block_group,
Chris Masond4452bc2014-05-19 20:47:56 -07001034 int *entries, int *bitmaps,
1035 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001036{
Josef Bacikc09544e2011-08-30 10:19:10 -04001037 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001038 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -07001039 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -07001040 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +00001041 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001042
Josef Bacik43be2142011-04-01 14:55:00 +00001043 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -07001044 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +00001045 cluster = list_entry(block_group->cluster_list.next,
1046 struct btrfs_free_cluster,
1047 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001048 }
Josef Bacik43be2142011-04-01 14:55:00 +00001049
Josef Bacikf75b1302011-10-05 10:00:18 -04001050 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001051 cluster_locked = cluster;
1052 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -04001053 node = rb_first(&cluster->root);
1054 cluster = NULL;
1055 }
1056
Josef Bacik0cb59c92010-07-02 12:14:14 -04001057 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -04001058 while (node) {
1059 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001060
Josef Bacika67509c2011-10-05 15:18:58 -04001061 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -07001062 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +00001063
Chris Masond4452bc2014-05-19 20:47:56 -07001064 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -04001065 e->bitmap);
1066 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -07001067 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -04001068
1069 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -07001070 list_add_tail(&e->list, bitmap_list);
1071 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -04001072 }
1073 node = rb_next(node);
1074 if (!node && cluster) {
1075 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -07001076 cluster_locked = cluster;
1077 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -04001078 cluster = NULL;
1079 }
1080 }
Chris Mason1bbc6212015-04-06 12:46:08 -07001081 if (cluster_locked) {
1082 spin_unlock(&cluster_locked->lock);
1083 cluster_locked = NULL;
1084 }
Filipe Manana55507ce2014-12-01 17:04:09 +00001085
1086 /*
1087 * Make sure we don't miss any range that was removed from our rbtree
1088 * because trimming is running. Otherwise after a umount+mount (or crash
1089 * after committing the transaction) we would leak free space and get
1090 * an inconsistent free space cache report from fsck.
1091 */
1092 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
1093 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
1094 trim_entry->bytes, NULL);
1095 if (ret)
1096 goto fail;
1097 *entries += 1;
1098 }
1099
Chris Masond4452bc2014-05-19 20:47:56 -07001100 return 0;
1101fail:
Chris Mason1bbc6212015-04-06 12:46:08 -07001102 if (cluster_locked)
1103 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -07001104 return -ENOSPC;
1105}
1106
1107static noinline_for_stack int
1108update_cache_item(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct inode *inode,
1111 struct btrfs_path *path, u64 offset,
1112 int entries, int bitmaps)
1113{
1114 struct btrfs_key key;
1115 struct btrfs_free_space_header *header;
1116 struct extent_buffer *leaf;
1117 int ret;
1118
1119 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1120 key.offset = offset;
1121 key.type = 0;
1122
1123 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1124 if (ret < 0) {
1125 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001126 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001127 goto fail;
1128 }
1129 leaf = path->nodes[0];
1130 if (ret > 0) {
1131 struct btrfs_key found_key;
1132 ASSERT(path->slots[0]);
1133 path->slots[0]--;
1134 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1135 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1136 found_key.offset != offset) {
1137 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001138 inode->i_size - 1, EXTENT_DELALLOC, 0,
1139 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001140 btrfs_release_path(path);
1141 goto fail;
1142 }
1143 }
1144
1145 BTRFS_I(inode)->generation = trans->transid;
1146 header = btrfs_item_ptr(leaf, path->slots[0],
1147 struct btrfs_free_space_header);
1148 btrfs_set_free_space_entries(leaf, header, entries);
1149 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1150 btrfs_set_free_space_generation(leaf, header, trans->transid);
1151 btrfs_mark_buffer_dirty(leaf);
1152 btrfs_release_path(path);
1153
1154 return 0;
1155
1156fail:
1157 return -1;
1158}
1159
David Sterba6701bdb2019-03-20 13:49:09 +01001160static noinline_for_stack int write_pinned_extent_entries(
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001161 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001162 struct btrfs_block_group *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001163 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001164 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001165{
1166 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001167 struct extent_io_tree *unpin = NULL;
1168 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001169
Miao Xie5349d6c2014-06-19 10:42:49 +08001170 if (!block_group)
1171 return 0;
1172
Josef Bacika67509c2011-10-05 15:18:58 -04001173 /*
1174 * We want to add any pinned extents to our free space cache
1175 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001176 *
Li Zefandb804f22012-01-10 16:41:01 +08001177 * We shouldn't have switched the pinned extents yet so this is the
1178 * right one
1179 */
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001180 unpin = &trans->transaction->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001181
David Sterbab3470b52019-10-23 18:48:22 +02001182 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001183
David Sterbab3470b52019-10-23 18:48:22 +02001184 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001185 ret = find_first_extent_bit(unpin, start,
1186 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001187 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001188 if (ret)
1189 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001190
Josef Bacika67509c2011-10-05 15:18:58 -04001191 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001192 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001193 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001194
Li Zefandb804f22012-01-10 16:41:01 +08001195 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001196 extent_end = min(block_group->start + block_group->length,
1197 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001198 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001199
Chris Masond4452bc2014-05-19 20:47:56 -07001200 *entries += 1;
1201 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001202 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001203 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001204
Li Zefandb804f22012-01-10 16:41:01 +08001205 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001206 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001207
Miao Xie5349d6c2014-06-19 10:42:49 +08001208 return 0;
1209}
1210
1211static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001212write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001213{
Geliang Tang7ae16812015-12-18 22:17:00 +08001214 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001215 int ret;
1216
Josef Bacik0cb59c92010-07-02 12:14:14 -04001217 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001218 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001219 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001220 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001221 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001222 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001223 }
1224
Miao Xie5349d6c2014-06-19 10:42:49 +08001225 return 0;
1226}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001227
Miao Xie5349d6c2014-06-19 10:42:49 +08001228static int flush_dirty_cache(struct inode *inode)
1229{
1230 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001231
Josef Bacik0ef8b722013-10-25 16:13:35 -04001232 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001233 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001234 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001235 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001236
Miao Xie5349d6c2014-06-19 10:42:49 +08001237 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001238}
1239
1240static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001241cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001242{
Geliang Tang7ae16812015-12-18 22:17:00 +08001243 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001244
Geliang Tang7ae16812015-12-18 22:17:00 +08001245 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001246 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001247}
1248
1249static void noinline_for_stack
1250cleanup_write_cache_enospc(struct inode *inode,
1251 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001252 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001253{
Chris Masond4452bc2014-05-19 20:47:56 -07001254 io_ctl_drop_pages(io_ctl);
1255 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001256 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001257}
1258
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001259static int __btrfs_wait_cache_io(struct btrfs_root *root,
1260 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001261 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001262 struct btrfs_io_ctl *io_ctl,
1263 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001264{
1265 int ret;
1266 struct inode *inode = io_ctl->inode;
1267
Chris Mason1bbc6212015-04-06 12:46:08 -07001268 if (!inode)
1269 return 0;
1270
Chris Masonc9dc4c62015-04-04 17:14:42 -07001271 /* Flush the dirty pages in the cache file. */
1272 ret = flush_dirty_cache(inode);
1273 if (ret)
1274 goto out;
1275
1276 /* Update the cache item to tell everyone this cache file is valid. */
1277 ret = update_cache_item(trans, root, inode, path, offset,
1278 io_ctl->entries, io_ctl->bitmaps);
1279out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001280 if (ret) {
1281 invalidate_inode_pages2(inode->i_mapping);
1282 BTRFS_I(inode)->generation = 0;
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001283 if (block_group)
1284 btrfs_debug(root->fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001285 "failed to write free space cache for block group %llu error %d",
1286 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001287 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001288 btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonc9dc4c62015-04-04 17:14:42 -07001289
1290 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001291 /* the dirty list is protected by the dirty_bgs_lock */
1292 spin_lock(&trans->transaction->dirty_bgs_lock);
1293
1294 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001295 spin_lock(&block_group->lock);
1296
1297 /*
1298 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001299 * the dirty list while waiting for IO. Otherwise our
1300 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001301 */
1302 if (!ret && list_empty(&block_group->dirty_list))
1303 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1304 else if (ret)
1305 block_group->disk_cache_state = BTRFS_DC_ERROR;
1306
1307 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001308 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001309 io_ctl->inode = NULL;
1310 iput(inode);
1311 }
1312
1313 return ret;
1314
1315}
1316
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001317int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001318 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001319 struct btrfs_path *path)
1320{
1321 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1322 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001323 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001324}
1325
Chris Masond4452bc2014-05-19 20:47:56 -07001326/**
Nikolay Borisovf092cf32021-01-22 11:57:56 +02001327 * Write out cached info to an inode
1328 *
1329 * @root: root the inode belongs to
1330 * @inode: freespace inode we are writing out
1331 * @ctl: free space cache we are going to write out
1332 * @block_group: block_group for this cache if it belongs to a block_group
1333 * @io_ctl: holds context for the io
1334 * @trans: the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001335 *
1336 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001337 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001338 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001339 */
1340static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1341 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001342 struct btrfs_block_group *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001343 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001344 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001345{
1346 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001347 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001348 int entries = 0;
1349 int bitmaps = 0;
1350 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001351 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001352
1353 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001354 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001355
Chris Masonc9dc4c62015-04-04 17:14:42 -07001356 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001357 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001358 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001359 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001360
Miao Xiee570fd22014-06-19 10:42:50 +08001361 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1362 down_write(&block_group->data_rwsem);
1363 spin_lock(&block_group->lock);
1364 if (block_group->delalloc_bytes) {
1365 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1366 spin_unlock(&block_group->lock);
1367 up_write(&block_group->data_rwsem);
1368 BTRFS_I(inode)->generation = 0;
1369 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001370 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001371 goto out;
1372 }
1373 spin_unlock(&block_group->lock);
1374 }
1375
Chris Masond4452bc2014-05-19 20:47:56 -07001376 /* Lock all pages first so we can lock the extent safely. */
Johannes Thumshirn7a195f62020-02-12 00:10:20 +09001377 ret = io_ctl_prepare_pages(io_ctl, false);
Omar Sandovalb8605452015-02-24 02:47:06 -08001378 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001379 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001380
1381 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001382 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001383
Chris Masonc9dc4c62015-04-04 17:14:42 -07001384 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001385
Filipe Manana55507ce2014-12-01 17:04:09 +00001386 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001387 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001388 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001389 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001390 block_group, &entries, &bitmaps,
1391 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001392 if (ret)
1393 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001394
Miao Xie5349d6c2014-06-19 10:42:49 +08001395 /*
1396 * Some spaces that are freed in the current transaction are pinned,
1397 * they will be added into free space cache after the transaction is
1398 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001399 *
1400 * If this changes while we are working we'll get added back to
1401 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001402 */
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001403 ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001404 if (ret)
1405 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001406
Filipe Manana55507ce2014-12-01 17:04:09 +00001407 /*
1408 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1409 * locked while doing it because a concurrent trim can be manipulating
1410 * or freeing the bitmap.
1411 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001412 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001413 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001414 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001415 if (ret)
1416 goto out_nospc;
1417
1418 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001419 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001420
1421 /* Everything is written out, now we dirty the pages in the file. */
Nikolay Borisov088545f2020-06-03 08:55:36 +03001422 ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
1423 io_ctl->num_pages, 0, i_size_read(inode),
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -05001424 &cached_state, false);
Miao Xie5349d6c2014-06-19 10:42:49 +08001425 if (ret)
1426 goto out_nospc;
1427
Miao Xiee570fd22014-06-19 10:42:50 +08001428 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1429 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001430 /*
1431 * Release the pages and unlock the extent, we will flush
1432 * them out later
1433 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001434 io_ctl_drop_pages(io_ctl);
Filipe Mananabbc37d62020-08-14 11:04:09 +01001435 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001436
1437 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001438 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001439
Chris Masonc9dc4c62015-04-04 17:14:42 -07001440 /*
1441 * at this point the pages are under IO and we're happy,
Randy Dunlap260db432020-08-04 19:48:34 -07001442 * The caller is responsible for waiting on them and updating
Chris Masonc9dc4c62015-04-04 17:14:42 -07001443 * the cache and the inode
1444 */
1445 io_ctl->entries = entries;
1446 io_ctl->bitmaps = bitmaps;
1447
1448 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001449 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001450 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001451
Chris Masonc9dc4c62015-04-04 17:14:42 -07001452 return 0;
1453
Chris Masona3bdccc2015-04-24 11:00:00 -07001454out_nospc_locked:
1455 cleanup_bitmap_list(&bitmap_list);
1456 spin_unlock(&ctl->tree_lock);
1457 mutex_unlock(&ctl->cache_writeout_mutex);
1458
Josef Bacika67509c2011-10-05 15:18:58 -04001459out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001460 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001461
Josef Bacikb77000e2017-11-15 16:20:52 -05001462out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001463 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1464 up_write(&block_group->data_rwsem);
1465
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001466out:
1467 io_ctl->inode = NULL;
1468 io_ctl_free(io_ctl);
1469 if (ret) {
1470 invalidate_inode_pages2(inode->i_mapping);
1471 BTRFS_I(inode)->generation = 0;
1472 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001473 btrfs_update_inode(trans, root, BTRFS_I(inode));
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001474 if (must_iput)
1475 iput(inode);
1476 return ret;
Li Zefan0414efa2011-04-20 10:20:14 +08001477}
1478
David Sterbafe041532019-03-20 13:51:56 +01001479int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001480 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +08001481 struct btrfs_path *path)
1482{
David Sterbafe041532019-03-20 13:51:56 +01001483 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001484 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1485 struct inode *inode;
1486 int ret = 0;
1487
Li Zefan0414efa2011-04-20 10:20:14 +08001488 spin_lock(&block_group->lock);
1489 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1490 spin_unlock(&block_group->lock);
1491 return 0;
1492 }
1493 spin_unlock(&block_group->lock);
1494
David Sterba7949f332019-03-20 13:40:19 +01001495 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001496 if (IS_ERR(inode))
1497 return 0;
1498
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001499 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1500 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001501 if (ret) {
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001502 btrfs_debug(fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001503 "failed to write free space cache for block group %llu error %d",
1504 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001505 spin_lock(&block_group->lock);
1506 block_group->disk_cache_state = BTRFS_DC_ERROR;
1507 spin_unlock(&block_group->lock);
1508
1509 block_group->io_ctl.inode = NULL;
1510 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001511 }
1512
Chris Masonc9dc4c62015-04-04 17:14:42 -07001513 /*
1514 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1515 * to wait for IO and put the inode
1516 */
1517
Josef Bacik0cb59c92010-07-02 12:14:14 -04001518 return ret;
1519}
1520
Li Zefan34d52cb2011-03-29 13:46:06 +08001521static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001522 u64 offset)
1523{
Josef Bacikb12d6862013-08-26 17:14:08 -04001524 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001525 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001526 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001527}
1528
Li Zefan34d52cb2011-03-29 13:46:06 +08001529static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001530{
Li Zefan34d52cb2011-03-29 13:46:06 +08001531 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001532}
1533
Li Zefan34d52cb2011-03-29 13:46:06 +08001534static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001535 u64 offset)
1536{
1537 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001538 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001539
Li Zefan34d52cb2011-03-29 13:46:06 +08001540 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1541 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001542 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001543 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001544 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001545
1546 return bitmap_start;
1547}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001548
1549static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001550 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001551{
1552 struct rb_node **p = &root->rb_node;
1553 struct rb_node *parent = NULL;
1554 struct btrfs_free_space *info;
1555
1556 while (*p) {
1557 parent = *p;
1558 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1559
Josef Bacik96303082009-07-13 21:29:25 -04001560 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001561 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001562 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001563 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001564 } else {
1565 /*
1566 * we could have a bitmap entry and an extent entry
1567 * share the same offset. If this is the case, we want
1568 * the extent entry to always be found first if we do a
1569 * linear search through the tree, since we want to have
1570 * the quickest allocation time, and allocating from an
1571 * extent is faster than allocating from a bitmap. So
1572 * if we're inserting a bitmap and we find an entry at
1573 * this offset, we want to go right, or after this entry
1574 * logically. If we are inserting an extent and we've
1575 * found a bitmap, we want to go left, or before
1576 * logically.
1577 */
1578 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001579 if (info->bitmap) {
1580 WARN_ON_ONCE(1);
1581 return -EEXIST;
1582 }
Josef Bacik96303082009-07-13 21:29:25 -04001583 p = &(*p)->rb_right;
1584 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001585 if (!info->bitmap) {
1586 WARN_ON_ONCE(1);
1587 return -EEXIST;
1588 }
Josef Bacik96303082009-07-13 21:29:25 -04001589 p = &(*p)->rb_left;
1590 }
1591 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001592 }
1593
1594 rb_link_node(node, parent, p);
1595 rb_insert_color(node, root);
1596
1597 return 0;
1598}
1599
1600/*
Josef Bacik59c7b562021-11-18 16:33:15 -05001601 * This is a little subtle. We *only* have ->max_extent_size set if we actually
1602 * searched through the bitmap and figured out the largest ->max_extent_size,
1603 * otherwise it's 0. In the case that it's 0 we don't want to tell the
1604 * allocator the wrong thing, we want to use the actual real max_extent_size
1605 * we've found already if it's larger, or we want to use ->bytes.
1606 *
1607 * This matters because find_free_space() will skip entries who's ->bytes is
1608 * less than the required bytes. So if we didn't search down this bitmap, we
1609 * may pick some previous entry that has a smaller ->max_extent_size than we
1610 * have. For example, assume we have two entries, one that has
1611 * ->max_extent_size set to 4K and ->bytes set to 1M. A second entry hasn't set
1612 * ->max_extent_size yet, has ->bytes set to 8K and it's contiguous. We will
1613 * call into find_free_space(), and return with max_extent_size == 4K, because
1614 * that first bitmap entry had ->max_extent_size set, but the second one did
1615 * not. If instead we returned 8K we'd come in searching for 8K, and find the
1616 * 8K contiguous range.
1617 *
1618 * Consider the other case, we have 2 8K chunks in that second entry and still
1619 * don't have ->max_extent_size set. We'll return 16K, and the next time the
1620 * allocator comes in it'll fully search our second bitmap, and this time it'll
1621 * get an uptodate value of 8K as the maximum chunk size. Then we'll get the
1622 * right allocation the next loop through.
1623 */
1624static inline u64 get_max_extent_size(const struct btrfs_free_space *entry)
1625{
1626 if (entry->bitmap && entry->max_extent_size)
1627 return entry->max_extent_size;
1628 return entry->bytes;
1629}
1630
1631/*
1632 * We want the largest entry to be leftmost, so this is inverted from what you'd
1633 * normally expect.
1634 */
1635static bool entry_less(struct rb_node *node, const struct rb_node *parent)
1636{
1637 const struct btrfs_free_space *entry, *exist;
1638
1639 entry = rb_entry(node, struct btrfs_free_space, bytes_index);
1640 exist = rb_entry(parent, struct btrfs_free_space, bytes_index);
1641 return get_max_extent_size(exist) < get_max_extent_size(entry);
1642}
1643
1644/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001645 * searches the tree for the given offset.
1646 *
Josef Bacik96303082009-07-13 21:29:25 -04001647 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1648 * want a section that has at least bytes size and comes at or after the given
1649 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001650 */
Josef Bacik96303082009-07-13 21:29:25 -04001651static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001652tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001653 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001654{
Li Zefan34d52cb2011-03-29 13:46:06 +08001655 struct rb_node *n = ctl->free_space_offset.rb_node;
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001656 struct btrfs_free_space *entry = NULL, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001657
Josef Bacik96303082009-07-13 21:29:25 -04001658 /* find entry that is closest to the 'offset' */
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001659 while (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001660 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1661 prev = entry;
1662
1663 if (offset < entry->offset)
1664 n = n->rb_left;
1665 else if (offset > entry->offset)
1666 n = n->rb_right;
1667 else
1668 break;
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001669
1670 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001671 }
1672
Josef Bacik96303082009-07-13 21:29:25 -04001673 if (bitmap_only) {
1674 if (!entry)
1675 return NULL;
1676 if (entry->bitmap)
1677 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001678
Josef Bacik96303082009-07-13 21:29:25 -04001679 /*
1680 * bitmap entry and extent entry may share same offset,
1681 * in that case, bitmap entry comes after extent entry.
1682 */
1683 n = rb_next(n);
1684 if (!n)
1685 return NULL;
1686 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1687 if (entry->offset != offset)
1688 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001689
Josef Bacik96303082009-07-13 21:29:25 -04001690 WARN_ON(!entry->bitmap);
1691 return entry;
1692 } else if (entry) {
1693 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001694 /*
Josef Bacik96303082009-07-13 21:29:25 -04001695 * if previous extent entry covers the offset,
1696 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001697 */
Miao Xiede6c4112012-10-18 08:18:01 +00001698 n = rb_prev(&entry->offset_index);
1699 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001700 prev = rb_entry(n, struct btrfs_free_space,
1701 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001702 if (!prev->bitmap &&
1703 prev->offset + prev->bytes > offset)
1704 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001705 }
Josef Bacik96303082009-07-13 21:29:25 -04001706 }
1707 return entry;
1708 }
1709
1710 if (!prev)
1711 return NULL;
1712
1713 /* find last entry before the 'offset' */
1714 entry = prev;
1715 if (entry->offset > offset) {
1716 n = rb_prev(&entry->offset_index);
1717 if (n) {
1718 entry = rb_entry(n, struct btrfs_free_space,
1719 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001720 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001721 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001722 if (fuzzy)
1723 return entry;
1724 else
1725 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001726 }
1727 }
1728
Josef Bacik96303082009-07-13 21:29:25 -04001729 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001730 n = rb_prev(&entry->offset_index);
1731 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001732 prev = rb_entry(n, struct btrfs_free_space,
1733 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001734 if (!prev->bitmap &&
1735 prev->offset + prev->bytes > offset)
1736 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001737 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001738 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001739 return entry;
1740 } else if (entry->offset + entry->bytes > offset)
1741 return entry;
1742
1743 if (!fuzzy)
1744 return NULL;
1745
1746 while (1) {
Nikolay Borisov167c0bd2021-11-22 17:16:46 +02001747 n = rb_next(&entry->offset_index);
1748 if (!n)
1749 return NULL;
1750 entry = rb_entry(n, struct btrfs_free_space, offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04001751 if (entry->bitmap) {
1752 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001753 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001754 break;
1755 } else {
1756 if (entry->offset + entry->bytes > offset)
1757 break;
1758 }
Josef Bacik96303082009-07-13 21:29:25 -04001759 }
1760 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001761}
1762
Nikolay Borisov32e16492021-11-23 14:44:20 +02001763static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1764 struct btrfs_free_space *info,
1765 bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001766{
Li Zefan34d52cb2011-03-29 13:46:06 +08001767 rb_erase(&info->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05001768 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001769 ctl->free_extents--;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001770
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001771 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001772 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001773 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
1774 }
Li Zefanf333adb2010-11-09 14:57:39 +08001775
Nikolay Borisov32e16492021-11-23 14:44:20 +02001776 if (update_stat)
1777 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001778}
1779
Li Zefan34d52cb2011-03-29 13:46:06 +08001780static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001781 struct btrfs_free_space *info)
1782{
1783 int ret = 0;
1784
Josef Bacikb12d6862013-08-26 17:14:08 -04001785 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001786 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001787 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001788 if (ret)
1789 return ret;
1790
Josef Bacik59c7b562021-11-18 16:33:15 -05001791 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
1792
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001793 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001794 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001795 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
1796 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001797
Li Zefan34d52cb2011-03-29 13:46:06 +08001798 ctl->free_space += info->bytes;
1799 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001800 return ret;
1801}
1802
Josef Bacik59c7b562021-11-18 16:33:15 -05001803static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl,
1804 struct btrfs_free_space *info)
1805{
1806 ASSERT(info->bitmap);
1807
1808 /*
1809 * If our entry is empty it's because we're on a cluster and we don't
1810 * want to re-link it into our ctl bytes index.
1811 */
1812 if (RB_EMPTY_NODE(&info->bytes_index))
1813 return;
1814
1815 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
1816 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
1817}
1818
Nikolay Borisovf594f132021-11-23 14:44:19 +02001819static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1820 struct btrfs_free_space *info,
1821 u64 offset, u64 bytes, bool update_stat)
Josef Bacik96303082009-07-13 21:29:25 -04001822{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001823 unsigned long start, count, end;
1824 int extent_delta = -1;
Josef Bacik96303082009-07-13 21:29:25 -04001825
Li Zefan34d52cb2011-03-29 13:46:06 +08001826 start = offset_to_bit(info->offset, ctl->unit, offset);
1827 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001828 end = start + count;
1829 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001830
Li Zefanf38b6e72011-03-14 13:40:51 +08001831 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001832
1833 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001834 if (info->max_extent_size > ctl->unit)
1835 info->max_extent_size = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001836
Josef Bacik59c7b562021-11-18 16:33:15 -05001837 relink_bitmap_entry(ctl, info);
1838
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001839 if (start && test_bit(start - 1, info->bitmap))
1840 extent_delta++;
1841
1842 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1843 extent_delta++;
1844
1845 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001846 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001847 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001848 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
1849 }
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001850
Nikolay Borisovf594f132021-11-23 14:44:19 +02001851 if (update_stat)
1852 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001853}
1854
Li Zefan34d52cb2011-03-29 13:46:06 +08001855static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001856 struct btrfs_free_space *info, u64 offset,
1857 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001858{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001859 unsigned long start, count, end;
1860 int extent_delta = 1;
Josef Bacik96303082009-07-13 21:29:25 -04001861
Li Zefan34d52cb2011-03-29 13:46:06 +08001862 start = offset_to_bit(info->offset, ctl->unit, offset);
1863 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001864 end = start + count;
1865 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001866
Li Zefanf38b6e72011-03-14 13:40:51 +08001867 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001868
Josef Bacik59c7b562021-11-18 16:33:15 -05001869 /*
1870 * We set some bytes, we have no idea what the max extent size is
1871 * anymore.
1872 */
1873 info->max_extent_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001874 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001875 ctl->free_space += bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001876
Josef Bacik59c7b562021-11-18 16:33:15 -05001877 relink_bitmap_entry(ctl, info);
1878
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001879 if (start && test_bit(start - 1, info->bitmap))
1880 extent_delta--;
1881
1882 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1883 extent_delta--;
1884
1885 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001886 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001887 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001888 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
1889 }
Josef Bacik96303082009-07-13 21:29:25 -04001890}
1891
Miao Xiea4820392013-09-09 13:19:42 +08001892/*
1893 * If we can not find suitable extent, we will use bytes to record
1894 * the size of the max extent.
1895 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001896static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001897 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001898 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001899{
1900 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001901 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001902 unsigned long bits, i;
1903 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001904 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001905
Josef Bacikcef40482015-10-02 16:09:42 -04001906 /*
1907 * Skip searching the bitmap if we don't have a contiguous section that
1908 * is large enough for this allocation.
1909 */
Josef Bacik0584f712015-10-02 16:12:23 -04001910 if (for_alloc &&
1911 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001912 bitmap_info->max_extent_size < *bytes) {
1913 *bytes = bitmap_info->max_extent_size;
1914 return -1;
1915 }
1916
Li Zefan34d52cb2011-03-29 13:46:06 +08001917 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001918 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001919 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001920
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001921 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001922 if (for_alloc && bits == 1) {
1923 found_bits = 1;
1924 break;
1925 }
Josef Bacik96303082009-07-13 21:29:25 -04001926 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1927 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001928 extent_bits = next_zero - i;
1929 if (extent_bits >= bits) {
1930 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001931 break;
Miao Xiea4820392013-09-09 13:19:42 +08001932 } else if (extent_bits > max_bits) {
1933 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001934 }
1935 i = next_zero;
1936 }
1937
1938 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001939 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1940 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001941 return 0;
1942 }
1943
Miao Xiea4820392013-09-09 13:19:42 +08001944 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001945 bitmap_info->max_extent_size = *bytes;
Josef Bacik59c7b562021-11-18 16:33:15 -05001946 relink_bitmap_entry(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001947 return -1;
1948}
1949
Miao Xiea4820392013-09-09 13:19:42 +08001950/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001951static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001952find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Josef Bacik59c7b562021-11-18 16:33:15 -05001953 unsigned long align, u64 *max_extent_size, bool use_bytes_index)
Josef Bacik96303082009-07-13 21:29:25 -04001954{
1955 struct btrfs_free_space *entry;
1956 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001957 u64 tmp;
1958 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001959 int ret;
1960
Li Zefan34d52cb2011-03-29 13:46:06 +08001961 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001962 goto out;
Josef Bacik59c7b562021-11-18 16:33:15 -05001963again:
1964 if (use_bytes_index) {
1965 node = rb_first_cached(&ctl->free_space_bytes);
1966 } else {
1967 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset),
1968 0, 1);
1969 if (!entry)
1970 goto out;
1971 node = &entry->offset_index;
1972 }
Josef Bacik96303082009-07-13 21:29:25 -04001973
Josef Bacik59c7b562021-11-18 16:33:15 -05001974 for (; node; node = rb_next(node)) {
1975 if (use_bytes_index)
1976 entry = rb_entry(node, struct btrfs_free_space,
1977 bytes_index);
1978 else
1979 entry = rb_entry(node, struct btrfs_free_space,
1980 offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04001981
Josef Bacik59c7b562021-11-18 16:33:15 -05001982 /*
1983 * If we are using the bytes index then all subsequent entries
1984 * in this tree are going to be < bytes, so simply set the max
1985 * extent size and exit the loop.
1986 *
1987 * If we're using the offset index then we need to keep going
1988 * through the rest of the tree.
1989 */
Miao Xiea4820392013-09-09 13:19:42 +08001990 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001991 *max_extent_size = max(get_max_extent_size(entry),
1992 *max_extent_size);
Josef Bacik59c7b562021-11-18 16:33:15 -05001993 if (use_bytes_index)
1994 break;
Josef Bacik96303082009-07-13 21:29:25 -04001995 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001996 }
Josef Bacik96303082009-07-13 21:29:25 -04001997
David Woodhouse53b381b2013-01-29 18:40:14 -05001998 /* make sure the space returned is big enough
1999 * to match our requested alignment
2000 */
2001 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08002002 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01002003 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05002004 tmp = tmp * align + ctl->start;
2005 align_off = tmp - entry->offset;
2006 } else {
2007 align_off = 0;
2008 tmp = entry->offset;
2009 }
2010
Josef Bacik59c7b562021-11-18 16:33:15 -05002011 /*
2012 * We don't break here if we're using the bytes index because we
2013 * may have another entry that has the correct alignment that is
2014 * the right size, so we don't want to miss that possibility.
2015 * At worst this adds another loop through the logic, but if we
2016 * broke here we could prematurely ENOSPC.
2017 */
Miao Xiea4820392013-09-09 13:19:42 +08002018 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002019 *max_extent_size = max(get_max_extent_size(entry),
2020 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05002021 continue;
Miao Xiea4820392013-09-09 13:19:42 +08002022 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002023
Josef Bacik96303082009-07-13 21:29:25 -04002024 if (entry->bitmap) {
Josef Bacik59c7b562021-11-18 16:33:15 -05002025 struct rb_node *old_next = rb_next(node);
Miao Xiea4820392013-09-09 13:19:42 +08002026 u64 size = *bytes;
2027
Josef Bacik0584f712015-10-02 16:12:23 -04002028 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05002029 if (!ret) {
2030 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08002031 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04002032 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04002033 } else {
2034 *max_extent_size =
2035 max(get_max_extent_size(entry),
2036 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05002037 }
Josef Bacik59c7b562021-11-18 16:33:15 -05002038
2039 /*
2040 * The bitmap may have gotten re-arranged in the space
2041 * index here because the max_extent_size may have been
2042 * updated. Start from the beginning again if this
2043 * happened.
2044 */
2045 if (use_bytes_index && old_next != rb_next(node))
2046 goto again;
Josef Bacik96303082009-07-13 21:29:25 -04002047 continue;
2048 }
2049
David Woodhouse53b381b2013-01-29 18:40:14 -05002050 *offset = tmp;
2051 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04002052 return entry;
2053 }
Miao Xiea4820392013-09-09 13:19:42 +08002054out:
Josef Bacik96303082009-07-13 21:29:25 -04002055 return NULL;
2056}
2057
Li Zefan34d52cb2011-03-29 13:46:06 +08002058static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04002059 struct btrfs_free_space *info, u64 offset)
2060{
Li Zefan34d52cb2011-03-29 13:46:06 +08002061 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04002062 info->bytes = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002063 info->bitmap_extents = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02002064 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08002065 link_free_space(ctl, info);
2066 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +01002067 recalculate_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04002068}
2069
Li Zefan34d52cb2011-03-29 13:46:06 +08002070static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08002071 struct btrfs_free_space *bitmap_info)
2072{
Dennis Zhou27f0afc2020-01-02 16:26:45 -05002073 /*
2074 * Normally when this is called, the bitmap is completely empty. However,
2075 * if we are blowing up the free space cache for one reason or another
2076 * via __btrfs_remove_free_space_cache(), then it may not be freed and
2077 * we may leave stats on the table.
2078 */
2079 if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
2080 ctl->discardable_extents[BTRFS_STAT_CURR] -=
2081 bitmap_info->bitmap_extents;
2082 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
2083
2084 }
Nikolay Borisov32e16492021-11-23 14:44:20 +02002085 unlink_free_space(ctl, bitmap_info, true);
Christophe Leroy3acd4852019-08-21 15:05:55 +00002086 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002087 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08002088 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01002089 recalculate_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002090}
2091
Li Zefan34d52cb2011-03-29 13:46:06 +08002092static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04002093 struct btrfs_free_space *bitmap_info,
2094 u64 *offset, u64 *bytes)
2095{
2096 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04002097 u64 search_start, search_bytes;
2098 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002099
2100again:
Li Zefan34d52cb2011-03-29 13:46:06 +08002101 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04002102
Josef Bacik6606bb92009-07-31 11:03:58 -04002103 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04002104 * We need to search for bits in this bitmap. We could only cover some
2105 * of the extent in this bitmap thanks to how we add space, so we need
2106 * to search for as much as it as we can and clear that amount, and then
2107 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04002108 */
2109 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002110 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00002111 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04002112 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
2113 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002114 if (ret < 0 || search_start != *offset)
2115 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04002116
Josef Bacikbdb7d302012-06-27 15:10:56 -04002117 /* We may have found more bits than what we need */
2118 search_bytes = min(search_bytes, *bytes);
2119
2120 /* Cannot clear past the end of the bitmap */
2121 search_bytes = min(search_bytes, end - search_start + 1);
2122
Nikolay Borisovf594f132021-11-23 14:44:19 +02002123 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes, true);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002124 *offset += search_bytes;
2125 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04002126
2127 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002128 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002129 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002130 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002131
Josef Bacik6606bb92009-07-31 11:03:58 -04002132 /*
2133 * no entry after this bitmap, but we still have bytes to
2134 * remove, so something has gone wrong.
2135 */
2136 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04002137 return -EINVAL;
2138
Josef Bacik6606bb92009-07-31 11:03:58 -04002139 bitmap_info = rb_entry(next, struct btrfs_free_space,
2140 offset_index);
2141
2142 /*
2143 * if the next entry isn't a bitmap we need to return to let the
2144 * extent stuff do its work.
2145 */
Josef Bacik96303082009-07-13 21:29:25 -04002146 if (!bitmap_info->bitmap)
2147 return -EAGAIN;
2148
Josef Bacik6606bb92009-07-31 11:03:58 -04002149 /*
2150 * Ok the next item is a bitmap, but it may not actually hold
2151 * the information for the rest of this free space stuff, so
2152 * look for it, and if we don't find it return so we can try
2153 * everything over again.
2154 */
2155 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002156 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08002157 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04002158 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04002159 if (ret < 0 || search_start != *offset)
2160 return -EAGAIN;
2161
Josef Bacik96303082009-07-13 21:29:25 -04002162 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08002163 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002164 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002165
2166 return 0;
2167}
2168
Josef Bacik2cdc3422011-05-27 14:07:49 -04002169static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
2170 struct btrfs_free_space *info, u64 offset,
Dennis Zhouda080fe2019-12-13 16:22:13 -08002171 u64 bytes, enum btrfs_trim_state trim_state)
Josef Bacik2cdc3422011-05-27 14:07:49 -04002172{
2173 u64 bytes_to_set = 0;
2174 u64 end;
2175
Dennis Zhouda080fe2019-12-13 16:22:13 -08002176 /*
2177 * This is a tradeoff to make bitmap trim state minimal. We mark the
2178 * whole bitmap untrimmed if at any point we add untrimmed regions.
2179 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002180 if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002181 if (btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002182 ctl->discardable_extents[BTRFS_STAT_CURR] +=
2183 info->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002184 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
2185 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002186 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002187 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002188
Josef Bacik2cdc3422011-05-27 14:07:49 -04002189 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
2190
2191 bytes_to_set = min(end - offset, bytes);
2192
2193 bitmap_set_bits(ctl, info, offset, bytes_to_set);
2194
2195 return bytes_to_set;
2196
2197}
2198
Li Zefan34d52cb2011-03-29 13:46:06 +08002199static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2200 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04002201{
Nikolay Borisov364be842021-11-23 14:44:22 +02002202 struct btrfs_block_group *block_group = ctl->block_group;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002203 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002204 bool forced = false;
2205
2206#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002207 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002208 forced = true;
2209#endif
Josef Bacik96303082009-07-13 21:29:25 -04002210
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05002211 /* This is a way to reclaim large regions from the bitmaps. */
2212 if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
2213 return false;
2214
Josef Bacik96303082009-07-13 21:29:25 -04002215 /*
2216 * If we are below the extents threshold then we can add this as an
2217 * extent, and don't have to deal with the bitmap
2218 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002219 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002220 /*
2221 * If this block group has some small extents we don't want to
2222 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002223 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002224 * of cache left then go ahead an dadd them, no sense in adding
2225 * the overhead of a bitmap if we don't have to.
2226 */
Dennis Zhouf9bb6152020-01-02 16:26:44 -05002227 if (info->bytes <= fs_info->sectorsize * 8) {
2228 if (ctl->free_extents * 3 <= ctl->extents_thresh)
Li Zefan34d52cb2011-03-29 13:46:06 +08002229 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002230 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002231 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002232 }
2233 }
Josef Bacik96303082009-07-13 21:29:25 -04002234
2235 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002236 * The original block groups from mkfs can be really small, like 8
2237 * megabytes, so don't bother with a bitmap for those entries. However
2238 * some block groups can be smaller than what a bitmap would cover but
2239 * are still large enough that they could overflow the 32k memory limit,
2240 * so allow those block groups to still be allowed to have a bitmap
2241 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002242 */
David Sterbab3470b52019-10-23 18:48:22 +02002243 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002244 return false;
2245
2246 return true;
2247}
2248
David Sterba20e55062015-11-19 11:42:28 +01002249static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002250 .use_bitmap = use_bitmap,
2251};
2252
Li Zefan34d52cb2011-03-29 13:46:06 +08002253static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2254 struct btrfs_free_space *info)
2255{
2256 struct btrfs_free_space *bitmap_info;
David Sterba32da53862019-10-29 19:20:18 +01002257 struct btrfs_block_group *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002258 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002259 u64 bytes, offset, bytes_added;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002260 enum btrfs_trim_state trim_state;
Li Zefan34d52cb2011-03-29 13:46:06 +08002261 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002262
2263 bytes = info->bytes;
2264 offset = info->offset;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002265 trim_state = info->trim_state;
Josef Bacik96303082009-07-13 21:29:25 -04002266
Li Zefan34d52cb2011-03-29 13:46:06 +08002267 if (!ctl->op->use_bitmap(ctl, info))
2268 return 0;
2269
Josef Bacik2cdc3422011-05-27 14:07:49 -04002270 if (ctl->op == &free_space_op)
Nikolay Borisov364be842021-11-23 14:44:22 +02002271 block_group = ctl->block_group;
Chris Mason38e87882011-06-10 16:36:57 -04002272again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002273 /*
2274 * Since we link bitmaps right into the cluster we need to see if we
2275 * have a cluster here, and if so and it has our bitmap we need to add
2276 * the free space to that bitmap.
2277 */
2278 if (block_group && !list_empty(&block_group->cluster_list)) {
2279 struct btrfs_free_cluster *cluster;
2280 struct rb_node *node;
2281 struct btrfs_free_space *entry;
2282
2283 cluster = list_entry(block_group->cluster_list.next,
2284 struct btrfs_free_cluster,
2285 block_group_list);
2286 spin_lock(&cluster->lock);
2287 node = rb_first(&cluster->root);
2288 if (!node) {
2289 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002290 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002291 }
2292
2293 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2294 if (!entry->bitmap) {
2295 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002296 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002297 }
2298
2299 if (entry->offset == offset_to_bitmap(ctl, offset)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08002300 bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
2301 bytes, trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002302 bytes -= bytes_added;
2303 offset += bytes_added;
2304 }
2305 spin_unlock(&cluster->lock);
2306 if (!bytes) {
2307 ret = 1;
2308 goto out;
2309 }
2310 }
Chris Mason38e87882011-06-10 16:36:57 -04002311
2312no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002313 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002314 1, 0);
2315 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002316 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002317 goto new_bitmap;
2318 }
2319
Dennis Zhouda080fe2019-12-13 16:22:13 -08002320 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
2321 trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002322 bytes -= bytes_added;
2323 offset += bytes_added;
2324 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002325
2326 if (!bytes) {
2327 ret = 1;
2328 goto out;
2329 } else
2330 goto again;
2331
2332new_bitmap:
2333 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002334 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002335 added = 1;
2336 info = NULL;
2337 goto again;
2338 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002339 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002340
2341 /* no pre-allocated info, allocate a new one */
2342 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002343 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2344 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002345 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002346 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002347 ret = -ENOMEM;
2348 goto out;
2349 }
2350 }
2351
2352 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002353 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2354 GFP_NOFS);
Dennis Zhouda080fe2019-12-13 16:22:13 -08002355 info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002357 if (!info->bitmap) {
2358 ret = -ENOMEM;
2359 goto out;
2360 }
2361 goto again;
2362 }
2363
2364out:
2365 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002366 if (info->bitmap)
2367 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2368 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002369 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002370 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002371
2372 return ret;
2373}
2374
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002375/*
2376 * Free space merging rules:
2377 * 1) Merge trimmed areas together
2378 * 2) Let untrimmed areas coalesce with trimmed areas
2379 * 3) Always pull neighboring regions from bitmaps
2380 *
2381 * The above rules are for when we merge free space based on btrfs_trim_state.
2382 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2383 * same reason: to promote larger extent regions which makes life easier for
2384 * find_free_extent(). Rule 2 enables coalescing based on the common path
2385 * being returning free space from btrfs_finish_extent_commit(). So when free
2386 * space is trimmed, it will prevent aggregating trimmed new region and
2387 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2388 * and provide find_free_extent() with the largest extents possible hoping for
2389 * the reuse path.
2390 */
Chris Mason945d8962011-05-22 12:33:42 -04002391static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002392 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002393{
Josef Bacikbf53d462020-07-27 10:28:05 -04002394 struct btrfs_free_space *left_info = NULL;
Li Zefan120d66e2010-11-09 14:56:50 +08002395 struct btrfs_free_space *right_info;
2396 bool merged = false;
2397 u64 offset = info->offset;
2398 u64 bytes = info->bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002399 const bool is_trimmed = btrfs_free_space_trimmed(info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002400
Josef Bacik0f9dd462008-09-23 13:14:11 -04002401 /*
2402 * first we want to see if there is free space adjacent to the range we
2403 * are adding, if there is remove that struct and add a new one to
2404 * cover the entire range
2405 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002406 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002407 if (right_info && rb_prev(&right_info->offset_index))
2408 left_info = rb_entry(rb_prev(&right_info->offset_index),
2409 struct btrfs_free_space, offset_index);
Josef Bacikbf53d462020-07-27 10:28:05 -04002410 else if (!right_info)
Li Zefan34d52cb2011-03-29 13:46:06 +08002411 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002412
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002413 /* See try_merge_free_space() comment. */
2414 if (right_info && !right_info->bitmap &&
2415 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002416 unlink_free_space(ctl, right_info, update_stat);
Josef Bacik6226cb02009-04-03 10:14:18 -04002417 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002418 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002419 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002420 }
2421
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002422 /* See try_merge_free_space() comment. */
Josef Bacik96303082009-07-13 21:29:25 -04002423 if (left_info && !left_info->bitmap &&
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002424 left_info->offset + left_info->bytes == offset &&
2425 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002426 unlink_free_space(ctl, left_info, update_stat);
Josef Bacik6226cb02009-04-03 10:14:18 -04002427 info->offset = left_info->offset;
2428 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002429 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002430 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002431 }
2432
Li Zefan120d66e2010-11-09 14:56:50 +08002433 return merged;
2434}
2435
Filipe Manana20005522014-08-29 13:35:13 +01002436static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2437 struct btrfs_free_space *info,
2438 bool update_stat)
2439{
2440 struct btrfs_free_space *bitmap;
2441 unsigned long i;
2442 unsigned long j;
2443 const u64 end = info->offset + info->bytes;
2444 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2445 u64 bytes;
2446
2447 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2448 if (!bitmap)
2449 return false;
2450
2451 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2452 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2453 if (j == i)
2454 return false;
2455 bytes = (j - i) * ctl->unit;
2456 info->bytes += bytes;
2457
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002458 /* See try_merge_free_space() comment. */
2459 if (!btrfs_free_space_trimmed(bitmap))
2460 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2461
Nikolay Borisovf594f132021-11-23 14:44:19 +02002462 bitmap_clear_bits(ctl, bitmap, end, bytes, update_stat);
Filipe Manana20005522014-08-29 13:35:13 +01002463
2464 if (!bitmap->bytes)
2465 free_bitmap(ctl, bitmap);
2466
2467 return true;
2468}
2469
2470static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2471 struct btrfs_free_space *info,
2472 bool update_stat)
2473{
2474 struct btrfs_free_space *bitmap;
2475 u64 bitmap_offset;
2476 unsigned long i;
2477 unsigned long j;
2478 unsigned long prev_j;
2479 u64 bytes;
2480
2481 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2482 /* If we're on a boundary, try the previous logical bitmap. */
2483 if (bitmap_offset == info->offset) {
2484 if (info->offset == 0)
2485 return false;
2486 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2487 }
2488
2489 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2490 if (!bitmap)
2491 return false;
2492
2493 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2494 j = 0;
2495 prev_j = (unsigned long)-1;
2496 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2497 if (j > i)
2498 break;
2499 prev_j = j;
2500 }
2501 if (prev_j == i)
2502 return false;
2503
2504 if (prev_j == (unsigned long)-1)
2505 bytes = (i + 1) * ctl->unit;
2506 else
2507 bytes = (i - prev_j) * ctl->unit;
2508
2509 info->offset -= bytes;
2510 info->bytes += bytes;
2511
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002512 /* See try_merge_free_space() comment. */
2513 if (!btrfs_free_space_trimmed(bitmap))
2514 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2515
Nikolay Borisovf594f132021-11-23 14:44:19 +02002516 bitmap_clear_bits(ctl, bitmap, info->offset, bytes, update_stat);
Filipe Manana20005522014-08-29 13:35:13 +01002517
2518 if (!bitmap->bytes)
2519 free_bitmap(ctl, bitmap);
2520
2521 return true;
2522}
2523
2524/*
2525 * We prefer always to allocate from extent entries, both for clustered and
2526 * non-clustered allocation requests. So when attempting to add a new extent
2527 * entry, try to see if there's adjacent free space in bitmap entries, and if
2528 * there is, migrate that space from the bitmaps to the extent.
2529 * Like this we get better chances of satisfying space allocation requests
2530 * because we attempt to satisfy them based on a single cache entry, and never
2531 * on 2 or more entries - even if the entries represent a contiguous free space
2532 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2533 * ends).
2534 */
2535static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2536 struct btrfs_free_space *info,
2537 bool update_stat)
2538{
2539 /*
2540 * Only work with disconnected entries, as we can change their offset,
2541 * and must be extent entries.
2542 */
2543 ASSERT(!info->bitmap);
2544 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2545
2546 if (ctl->total_bitmaps > 0) {
2547 bool stole_end;
2548 bool stole_front = false;
2549
2550 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2551 if (ctl->total_bitmaps > 0)
2552 stole_front = steal_from_bitmap_to_front(ctl, info,
2553 update_stat);
2554
2555 if (stole_end || stole_front)
2556 try_merge_free_space(ctl, info, update_stat);
2557 }
2558}
2559
Nikolay Borisov290ef192021-11-23 14:44:21 +02002560int __btrfs_add_free_space(struct btrfs_block_group *block_group,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002561 u64 offset, u64 bytes,
2562 enum btrfs_trim_state trim_state)
Li Zefan120d66e2010-11-09 14:56:50 +08002563{
Nikolay Borisov290ef192021-11-23 14:44:21 +02002564 struct btrfs_fs_info *fs_info = block_group->fs_info;
2565 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan120d66e2010-11-09 14:56:50 +08002566 struct btrfs_free_space *info;
2567 int ret = 0;
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002568 u64 filter_bytes = bytes;
Li Zefan120d66e2010-11-09 14:56:50 +08002569
Naohiro Aota169e0da2021-02-04 19:21:52 +09002570 ASSERT(!btrfs_is_zoned(fs_info));
2571
Josef Bacikdc89e982011-01-28 17:05:48 -05002572 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002573 if (!info)
2574 return -ENOMEM;
2575
2576 info->offset = offset;
2577 info->bytes = bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002578 info->trim_state = trim_state;
Filipe Manana20005522014-08-29 13:35:13 +01002579 RB_CLEAR_NODE(&info->offset_index);
Josef Bacik59c7b562021-11-18 16:33:15 -05002580 RB_CLEAR_NODE(&info->bytes_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002581
Li Zefan34d52cb2011-03-29 13:46:06 +08002582 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002583
Li Zefan34d52cb2011-03-29 13:46:06 +08002584 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002585 goto link;
2586
2587 /*
2588 * There was no extent directly to the left or right of this new
2589 * extent then we know we're going to have to allocate a new extent, so
2590 * before we do that see if we need to drop this into a bitmap
2591 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002592 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002593 if (ret < 0) {
2594 goto out;
2595 } else if (ret) {
2596 ret = 0;
2597 goto out;
2598 }
2599link:
Filipe Manana20005522014-08-29 13:35:13 +01002600 /*
2601 * Only steal free space from adjacent bitmaps if we're sure we're not
2602 * going to add the new free space to existing bitmap entries - because
2603 * that would mean unnecessary work that would be reverted. Therefore
2604 * attempt to steal space from bitmaps if we're adding an extent entry.
2605 */
2606 steal_from_bitmap(ctl, info, true);
2607
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002608 filter_bytes = max(filter_bytes, info->bytes);
2609
Li Zefan34d52cb2011-03-29 13:46:06 +08002610 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002611 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002612 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002613out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002614 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002615 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002616
Josef Bacik0f9dd462008-09-23 13:14:11 -04002617 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002618 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002619 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002620 }
2621
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002622 if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
2623 btrfs_discard_check_filter(block_group, filter_bytes);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002624 btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002625 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002626
Josef Bacik0f9dd462008-09-23 13:14:11 -04002627 return ret;
2628}
2629
Naohiro Aota169e0da2021-02-04 19:21:52 +09002630static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group,
2631 u64 bytenr, u64 size, bool used)
2632{
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002633 struct btrfs_fs_info *fs_info = block_group->fs_info;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002634 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2635 u64 offset = bytenr - block_group->start;
2636 u64 to_free, to_unusable;
Johannes Thumshirn77233c22021-08-09 20:41:17 +09002637 const int bg_reclaim_threshold = READ_ONCE(fs_info->bg_reclaim_threshold);
Naohiro Aota98173252021-08-19 21:19:10 +09002638 bool initial = (size == block_group->length);
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002639 u64 reclaimable_unusable;
Naohiro Aota98173252021-08-19 21:19:10 +09002640
2641 WARN_ON(!initial && offset + size > block_group->zone_capacity);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002642
2643 spin_lock(&ctl->tree_lock);
2644 if (!used)
2645 to_free = size;
Naohiro Aota98173252021-08-19 21:19:10 +09002646 else if (initial)
2647 to_free = block_group->zone_capacity;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002648 else if (offset >= block_group->alloc_offset)
2649 to_free = size;
2650 else if (offset + size <= block_group->alloc_offset)
2651 to_free = 0;
2652 else
2653 to_free = offset + size - block_group->alloc_offset;
2654 to_unusable = size - to_free;
2655
2656 ctl->free_space += to_free;
Naohiro Aotabadae9c2021-03-03 17:55:48 +09002657 /*
2658 * If the block group is read-only, we should account freed space into
2659 * bytes_readonly.
2660 */
2661 if (!block_group->ro)
2662 block_group->zone_unusable += to_unusable;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002663 spin_unlock(&ctl->tree_lock);
2664 if (!used) {
2665 spin_lock(&block_group->lock);
2666 block_group->alloc_offset -= size;
2667 spin_unlock(&block_group->lock);
2668 }
2669
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002670 reclaimable_unusable = block_group->zone_unusable -
2671 (block_group->length - block_group->zone_capacity);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002672 /* All the region is now unusable. Mark it as unused and reclaim */
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002673 if (block_group->zone_unusable == block_group->length) {
Naohiro Aota169e0da2021-02-04 19:21:52 +09002674 btrfs_mark_bg_unused(block_group);
Johannes Thumshirn77233c22021-08-09 20:41:17 +09002675 } else if (bg_reclaim_threshold &&
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002676 reclaimable_unusable >=
2677 div_factor_fine(block_group->zone_capacity,
2678 bg_reclaim_threshold)) {
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002679 btrfs_mark_bg_to_reclaim(block_group);
2680 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002681
2682 return 0;
2683}
2684
David Sterba32da53862019-10-29 19:20:18 +01002685int btrfs_add_free_space(struct btrfs_block_group *block_group,
Josef Bacik478b4d92019-06-20 15:37:43 -04002686 u64 bytenr, u64 size)
2687{
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002688 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2689
Naohiro Aota169e0da2021-02-04 19:21:52 +09002690 if (btrfs_is_zoned(block_group->fs_info))
2691 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2692 true);
2693
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002694 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2695 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2696
Nikolay Borisov290ef192021-11-23 14:44:21 +02002697 return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
Josef Bacik478b4d92019-06-20 15:37:43 -04002698}
2699
Naohiro Aota169e0da2021-02-04 19:21:52 +09002700int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
2701 u64 bytenr, u64 size)
2702{
2703 if (btrfs_is_zoned(block_group->fs_info))
2704 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2705 false);
2706
2707 return btrfs_add_free_space(block_group, bytenr, size);
2708}
2709
Dennis Zhoub0643e52019-12-13 16:22:14 -08002710/*
2711 * This is a subtle distinction because when adding free space back in general,
2712 * we want it to be added as untrimmed for async. But in the case where we add
2713 * it on loading of a block group, we want to consider it trimmed.
2714 */
2715int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
2716 u64 bytenr, u64 size)
2717{
2718 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2719
Naohiro Aota169e0da2021-02-04 19:21:52 +09002720 if (btrfs_is_zoned(block_group->fs_info))
2721 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2722 true);
2723
Dennis Zhoub0643e52019-12-13 16:22:14 -08002724 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
2725 btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
2726 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2727
Nikolay Borisov290ef192021-11-23 14:44:21 +02002728 return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002729}
2730
David Sterba32da53862019-10-29 19:20:18 +01002731int btrfs_remove_free_space(struct btrfs_block_group *block_group,
Josef Bacik6226cb02009-04-03 10:14:18 -04002732 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002733{
Li Zefan34d52cb2011-03-29 13:46:06 +08002734 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002735 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002736 int ret;
2737 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002738
Naohiro Aota011b41b2021-02-04 19:21:55 +09002739 if (btrfs_is_zoned(block_group->fs_info)) {
2740 /*
2741 * This can happen with conventional zones when replaying log.
2742 * Since the allocation info of tree-log nodes are not recorded
2743 * to the extent-tree, calculate_alloc_pointer() failed to
2744 * advance the allocation pointer after last allocated tree log
2745 * node blocks.
2746 *
2747 * This function is called from
2748 * btrfs_pin_extent_for_log_replay() when replaying the log.
2749 * Advance the pointer not to overwrite the tree-log nodes.
2750 */
Naohiro Aota0ae79c62021-08-09 13:13:44 +09002751 if (block_group->start + block_group->alloc_offset <
2752 offset + bytes) {
2753 block_group->alloc_offset =
2754 offset + bytes - block_group->start;
2755 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002756 return 0;
Naohiro Aota011b41b2021-02-04 19:21:55 +09002757 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002758
Li Zefan34d52cb2011-03-29 13:46:06 +08002759 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002760
Josef Bacik96303082009-07-13 21:29:25 -04002761again:
Josef Bacikb0175112012-12-18 11:39:19 -05002762 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002763 if (!bytes)
2764 goto out_lock;
2765
Li Zefan34d52cb2011-03-29 13:46:06 +08002766 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002767 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002768 /*
2769 * oops didn't find an extent that matched the space we wanted
2770 * to remove, look for a bitmap instead
2771 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002772 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002773 1, 0);
2774 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002775 /*
2776 * If we found a partial bit of our free space in a
2777 * bitmap but then couldn't find the other part this may
2778 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002779 */
Josef Bacikb0175112012-12-18 11:39:19 -05002780 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002781 goto out_lock;
2782 }
Josef Bacik96303082009-07-13 21:29:25 -04002783 }
2784
Josef Bacikb0175112012-12-18 11:39:19 -05002785 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002786 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002787 unlink_free_space(ctl, info, true);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002788 if (offset == info->offset) {
2789 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002790
Josef Bacikbdb7d302012-06-27 15:10:56 -04002791 info->bytes -= to_free;
2792 info->offset += to_free;
2793 if (info->bytes) {
2794 ret = link_free_space(ctl, info);
2795 WARN_ON(ret);
2796 } else {
2797 kmem_cache_free(btrfs_free_space_cachep, info);
2798 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002799
Josef Bacikbdb7d302012-06-27 15:10:56 -04002800 offset += to_free;
2801 bytes -= to_free;
2802 goto again;
2803 } else {
2804 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002805
Josef Bacikbdb7d302012-06-27 15:10:56 -04002806 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002807 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002808 WARN_ON(ret);
2809 if (ret)
2810 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002811
Josef Bacikbdb7d302012-06-27 15:10:56 -04002812 /* Not enough bytes in this entry to satisfy us */
2813 if (old_end < offset + bytes) {
2814 bytes -= old_end - offset;
2815 offset = old_end;
2816 goto again;
2817 } else if (old_end == offset + bytes) {
2818 /* all done */
2819 goto out_lock;
2820 }
2821 spin_unlock(&ctl->tree_lock);
2822
Nikolay Borisov290ef192021-11-23 14:44:21 +02002823 ret = __btrfs_add_free_space(block_group,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002824 offset + bytes,
2825 old_end - (offset + bytes),
2826 info->trim_state);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002827 WARN_ON(ret);
2828 goto out;
2829 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002830 }
Josef Bacik96303082009-07-13 21:29:25 -04002831
Li Zefan34d52cb2011-03-29 13:46:06 +08002832 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002833 if (ret == -EAGAIN) {
2834 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002835 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002836 }
Josef Bacik96303082009-07-13 21:29:25 -04002837out_lock:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002838 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002839 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002840out:
Josef Bacik25179202008-10-29 14:49:05 -04002841 return ret;
2842}
2843
David Sterba32da53862019-10-29 19:20:18 +01002844void btrfs_dump_free_space(struct btrfs_block_group *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -04002845 u64 bytes)
2846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002847 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002848 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002849 struct btrfs_free_space *info;
2850 struct rb_node *n;
2851 int count = 0;
2852
Naohiro Aota169e0da2021-02-04 19:21:52 +09002853 /*
2854 * Zoned btrfs does not use free space tree and cluster. Just print
2855 * out the free space after the allocation offset.
2856 */
2857 if (btrfs_is_zoned(fs_info)) {
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09002858 btrfs_info(fs_info, "free space %llu active %d",
2859 block_group->zone_capacity - block_group->alloc_offset,
2860 block_group->zone_is_active);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002861 return;
2862 }
2863
Filipe Manana9084cb62018-10-22 10:43:06 +01002864 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002865 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002866 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002867 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002868 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002869 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002870 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002871 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002872 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002873 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002875 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002876 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002877 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002878}
2879
Josef Bacikcd799092020-10-23 09:58:08 -04002880void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
2881 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002883 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002884
Li Zefan34d52cb2011-03-29 13:46:06 +08002885 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002886 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002887 ctl->start = block_group->start;
Nikolay Borisov364be842021-11-23 14:44:22 +02002888 ctl->block_group = block_group;
Li Zefan34d52cb2011-03-29 13:46:06 +08002889 ctl->op = &free_space_op;
Josef Bacik59c7b562021-11-18 16:33:15 -05002890 ctl->free_space_bytes = RB_ROOT_CACHED;
Filipe Manana55507ce2014-12-01 17:04:09 +00002891 INIT_LIST_HEAD(&ctl->trimming_ranges);
2892 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002893
Li Zefan34d52cb2011-03-29 13:46:06 +08002894 /*
2895 * we only want to have 32k of ram per block group for keeping
2896 * track of free space, and if we pass 1/2 of that we want to
2897 * start converting things over to using bitmaps
2898 */
Byongho Leeee221842015-12-15 01:42:10 +09002899 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002900}
2901
Chris Masonfa9c0d792009-04-03 09:47:43 -04002902/*
2903 * for a given cluster, put all of its extents back into the free
2904 * space cache. If the block group passed doesn't match the block group
2905 * pointed to by the cluster, someone else raced in and freed the
2906 * cluster already. In that case, we just return without changing anything
2907 */
Anand Jain69b0e092020-06-03 18:10:18 +08002908static void __btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002909 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002910 struct btrfs_free_cluster *cluster)
2911{
Li Zefan34d52cb2011-03-29 13:46:06 +08002912 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002913 struct btrfs_free_space *entry;
2914 struct rb_node *node;
2915
2916 spin_lock(&cluster->lock);
Josef Bacik95c85fb2021-01-25 16:42:35 -05002917 if (cluster->block_group != block_group) {
2918 spin_unlock(&cluster->lock);
2919 return;
2920 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002921
Josef Bacik96303082009-07-13 21:29:25 -04002922 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002923 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002924 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002925
Chris Masonfa9c0d792009-04-03 09:47:43 -04002926 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002927 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002928 bool bitmap;
2929
Chris Masonfa9c0d792009-04-03 09:47:43 -04002930 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2931 node = rb_next(&entry->offset_index);
2932 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002933 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002934
2935 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002936 if (!bitmap) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002937 /* Merging treats extents as if they were new */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002938 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002939 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002940 ctl->discardable_bytes[BTRFS_STAT_CURR] -=
2941 entry->bytes;
2942 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002943
Li Zefan34d52cb2011-03-29 13:46:06 +08002944 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002945 steal_from_bitmap(ctl, entry, false);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002946
2947 /* As we insert directly, update these statistics */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002948 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002949 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002950 ctl->discardable_bytes[BTRFS_STAT_CURR] +=
2951 entry->bytes;
2952 }
Filipe Manana20005522014-08-29 13:35:13 +01002953 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002954 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002955 entry->offset, &entry->offset_index, bitmap);
Josef Bacik59c7b562021-11-18 16:33:15 -05002956 rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes,
2957 entry_less);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002958 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002959 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002960 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002961 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002962}
2963
Eric Sandeen48a3b632013-04-25 20:41:01 +00002964static void __btrfs_remove_free_space_cache_locked(
2965 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002966{
2967 struct btrfs_free_space *info;
2968 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002969
Li Zefan581bb052011-04-20 10:06:11 +08002970 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2971 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002972 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002973 unlink_free_space(ctl, info, true);
Josef Bacik9b90f512011-06-24 16:02:51 +00002974 kmem_cache_free(btrfs_free_space_cachep, info);
2975 } else {
2976 free_bitmap(ctl, info);
2977 }
David Sterba351810c2015-01-08 15:20:54 +01002978
2979 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002980 }
Chris Mason09655372011-05-21 09:27:38 -04002981}
2982
2983void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2984{
2985 spin_lock(&ctl->tree_lock);
2986 __btrfs_remove_free_space_cache_locked(ctl);
Nikolay Borisov364be842021-11-23 14:44:22 +02002987 if (ctl->block_group)
2988 btrfs_discard_update_discardable(ctl->block_group);
Li Zefan581bb052011-04-20 10:06:11 +08002989 spin_unlock(&ctl->tree_lock);
2990}
2991
David Sterba32da53862019-10-29 19:20:18 +01002992void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002993{
Li Zefan34d52cb2011-03-29 13:46:06 +08002994 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002995 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002996 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002997
Li Zefan34d52cb2011-03-29 13:46:06 +08002998 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002999 while ((head = block_group->cluster_list.next) !=
3000 &block_group->cluster_list) {
3001 cluster = list_entry(head, struct btrfs_free_cluster,
3002 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003003
3004 WARN_ON(cluster->block_group != block_group);
3005 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01003006
3007 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003008 }
Chris Mason09655372011-05-21 09:27:38 -04003009 __btrfs_remove_free_space_cache_locked(ctl);
Josef Bacik66b53ba2020-10-23 09:58:07 -04003010 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08003011 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003012
Josef Bacik0f9dd462008-09-23 13:14:11 -04003013}
3014
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003015/**
3016 * btrfs_is_free_space_trimmed - see if everything is trimmed
3017 * @block_group: block_group of interest
3018 *
3019 * Walk @block_group's free space rb_tree to determine if everything is trimmed.
3020 */
3021bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
3022{
3023 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3024 struct btrfs_free_space *info;
3025 struct rb_node *node;
3026 bool ret = true;
3027
3028 spin_lock(&ctl->tree_lock);
3029 node = rb_first(&ctl->free_space_offset);
3030
3031 while (node) {
3032 info = rb_entry(node, struct btrfs_free_space, offset_index);
3033
3034 if (!btrfs_free_space_trimmed(info)) {
3035 ret = false;
3036 break;
3037 }
3038
3039 node = rb_next(node);
3040 }
3041
3042 spin_unlock(&ctl->tree_lock);
3043 return ret;
3044}
3045
David Sterba32da53862019-10-29 19:20:18 +01003046u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08003047 u64 offset, u64 bytes, u64 empty_size,
3048 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04003049{
Li Zefan34d52cb2011-03-29 13:46:06 +08003050 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003051 struct btrfs_discard_ctl *discard_ctl =
3052 &block_group->fs_info->discard_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04003053 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04003054 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04003055 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003056 u64 align_gap = 0;
3057 u64 align_gap_len = 0;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003058 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Josef Bacik59c7b562021-11-18 16:33:15 -05003059 bool use_bytes_index = (offset == block_group->start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003060
Naohiro Aota2eda5702021-02-04 19:21:53 +09003061 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3062
Li Zefan34d52cb2011-03-29 13:46:06 +08003063 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05003064 entry = find_free_space(ctl, &offset, &bytes_search,
Josef Bacik59c7b562021-11-18 16:33:15 -05003065 block_group->full_stripe_len, max_extent_size,
3066 use_bytes_index);
Josef Bacik6226cb02009-04-03 10:14:18 -04003067 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04003068 goto out;
3069
3070 ret = offset;
3071 if (entry->bitmap) {
Nikolay Borisovf594f132021-11-23 14:44:19 +02003072 bitmap_clear_bits(ctl, entry, offset, bytes, true);
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003073
3074 if (!btrfs_free_space_trimmed(entry))
3075 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3076
Li Zefanedf6e2d2010-11-09 14:50:07 +08003077 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08003078 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04003079 } else {
Nikolay Borisov32e16492021-11-23 14:44:20 +02003080 unlink_free_space(ctl, entry, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05003081 align_gap_len = offset - entry->offset;
3082 align_gap = entry->offset;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003083 align_gap_trim_state = entry->trim_state;
David Woodhouse53b381b2013-01-29 18:40:14 -05003084
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003085 if (!btrfs_free_space_trimmed(entry))
3086 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3087
David Woodhouse53b381b2013-01-29 18:40:14 -05003088 entry->offset = offset + bytes;
3089 WARN_ON(entry->bytes < bytes + align_gap_len);
3090
3091 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04003092 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05003093 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04003094 else
Li Zefan34d52cb2011-03-29 13:46:06 +08003095 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04003096 }
Josef Bacik96303082009-07-13 21:29:25 -04003097out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04003098 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08003099 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04003100
David Woodhouse53b381b2013-01-29 18:40:14 -05003101 if (align_gap_len)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003102 __btrfs_add_free_space(block_group, align_gap, align_gap_len,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003103 align_gap_trim_state);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003104 return ret;
3105}
Chris Masonfa9c0d792009-04-03 09:47:43 -04003106
3107/*
3108 * given a cluster, put all of its extents back into the free space
3109 * cache. If a block group is passed, this function will only free
3110 * a cluster that belongs to the passed block group.
3111 *
3112 * Otherwise, it'll get a reference on the block group pointed to by the
3113 * cluster and remove the cluster from it.
3114 */
Anand Jain69b0e092020-06-03 18:10:18 +08003115void btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01003116 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003117 struct btrfs_free_cluster *cluster)
3118{
Li Zefan34d52cb2011-03-29 13:46:06 +08003119 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003120
3121 /* first, get a safe pointer to the block group */
3122 spin_lock(&cluster->lock);
3123 if (!block_group) {
3124 block_group = cluster->block_group;
3125 if (!block_group) {
3126 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003127 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003128 }
3129 } else if (cluster->block_group != block_group) {
3130 /* someone else has already freed it don't redo their work */
3131 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003132 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003133 }
Anand Jainb5790d52020-06-03 18:10:20 +08003134 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003135 spin_unlock(&cluster->lock);
3136
Li Zefan34d52cb2011-03-29 13:46:06 +08003137 ctl = block_group->free_space_ctl;
3138
Chris Masonfa9c0d792009-04-03 09:47:43 -04003139 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08003140 spin_lock(&ctl->tree_lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003141 __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08003142 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003143
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003144 btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
3145
Chris Masonfa9c0d792009-04-03 09:47:43 -04003146 /* finally drop our ref */
3147 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003148}
3149
David Sterba32da53862019-10-29 19:20:18 +01003150static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003151 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04003152 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08003153 u64 bytes, u64 min_start,
3154 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04003155{
Li Zefan34d52cb2011-03-29 13:46:06 +08003156 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003157 int err;
3158 u64 search_start = cluster->window_start;
3159 u64 search_bytes = bytes;
3160 u64 ret = 0;
3161
Josef Bacik96303082009-07-13 21:29:25 -04003162 search_start = min_start;
3163 search_bytes = bytes;
3164
Josef Bacik0584f712015-10-02 16:12:23 -04003165 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08003166 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04003167 *max_extent_size = max(get_max_extent_size(entry),
3168 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003169 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08003170 }
Josef Bacik96303082009-07-13 21:29:25 -04003171
3172 ret = search_start;
Nikolay Borisovf594f132021-11-23 14:44:19 +02003173 bitmap_clear_bits(ctl, entry, ret, bytes, false);
Josef Bacik96303082009-07-13 21:29:25 -04003174
3175 return ret;
3176}
3177
Chris Masonfa9c0d792009-04-03 09:47:43 -04003178/*
3179 * given a cluster, try to allocate 'bytes' from it, returns 0
3180 * if it couldn't find anything suitably large, or a logical disk offset
3181 * if things worked out
3182 */
David Sterba32da53862019-10-29 19:20:18 +01003183u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003184 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003185 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003186{
Li Zefan34d52cb2011-03-29 13:46:06 +08003187 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003188 struct btrfs_discard_ctl *discard_ctl =
3189 &block_group->fs_info->discard_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003190 struct btrfs_free_space *entry = NULL;
3191 struct rb_node *node;
3192 u64 ret = 0;
3193
Naohiro Aota2eda5702021-02-04 19:21:53 +09003194 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3195
Chris Masonfa9c0d792009-04-03 09:47:43 -04003196 spin_lock(&cluster->lock);
3197 if (bytes > cluster->max_size)
3198 goto out;
3199
3200 if (cluster->block_group != block_group)
3201 goto out;
3202
3203 node = rb_first(&cluster->root);
3204 if (!node)
3205 goto out;
3206
3207 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303208 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04003209 if (entry->bytes < bytes)
3210 *max_extent_size = max(get_max_extent_size(entry),
3211 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08003212
Josef Bacik4e69b592011-03-21 10:11:24 -04003213 if (entry->bytes < bytes ||
3214 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003215 node = rb_next(&entry->offset_index);
3216 if (!node)
3217 break;
3218 entry = rb_entry(node, struct btrfs_free_space,
3219 offset_index);
3220 continue;
3221 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003222
Josef Bacik4e69b592011-03-21 10:11:24 -04003223 if (entry->bitmap) {
3224 ret = btrfs_alloc_from_bitmap(block_group,
3225 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003226 cluster->window_start,
3227 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003228 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003229 node = rb_next(&entry->offset_index);
3230 if (!node)
3231 break;
3232 entry = rb_entry(node, struct btrfs_free_space,
3233 offset_index);
3234 continue;
3235 }
Josef Bacik9b230622012-01-26 15:01:12 -05003236 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003237 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04003238 ret = entry->offset;
3239
3240 entry->offset += bytes;
3241 entry->bytes -= bytes;
3242 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003243
Chris Masonfa9c0d792009-04-03 09:47:43 -04003244 break;
3245 }
3246out:
3247 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04003248
Li Zefan5e71b5d2010-11-09 14:55:34 +08003249 if (!ret)
3250 return 0;
3251
Li Zefan34d52cb2011-03-29 13:46:06 +08003252 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003253
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003254 if (!btrfs_free_space_trimmed(entry))
3255 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3256
Li Zefan34d52cb2011-03-29 13:46:06 +08003257 ctl->free_space -= bytes;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003258 if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
3259 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
Nikolay Borisov3c179162021-02-08 10:26:54 +02003260
3261 spin_lock(&cluster->lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003262 if (entry->bytes == 0) {
Nikolay Borisov3c179162021-02-08 10:26:54 +02003263 rb_erase(&entry->offset_index, &cluster->root);
Li Zefan34d52cb2011-03-29 13:46:06 +08003264 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003265 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003266 kmem_cache_free(btrfs_free_space_bitmap_cachep,
3267 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08003268 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01003269 recalculate_thresholds(ctl);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003270 } else if (!btrfs_free_space_trimmed(entry)) {
3271 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003272 }
Josef Bacikdc89e982011-01-28 17:05:48 -05003273 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003274 }
3275
Nikolay Borisov3c179162021-02-08 10:26:54 +02003276 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003277 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003278
Chris Masonfa9c0d792009-04-03 09:47:43 -04003279 return ret;
3280}
3281
David Sterba32da53862019-10-29 19:20:18 +01003282static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003283 struct btrfs_free_space *entry,
3284 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003285 u64 offset, u64 bytes,
3286 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04003287{
Li Zefan34d52cb2011-03-29 13:46:06 +08003288 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003289 unsigned long next_zero;
3290 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003291 unsigned long want_bits;
3292 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003293 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04003294 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003295 unsigned long start = 0;
3296 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003297 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04003298
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003299 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04003300 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003301 want_bits = bytes_to_bits(bytes, ctl->unit);
3302 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04003303
Josef Bacikcef40482015-10-02 16:09:42 -04003304 /*
3305 * Don't bother looking for a cluster in this bitmap if it's heavily
3306 * fragmented.
3307 */
3308 if (entry->max_extent_size &&
3309 entry->max_extent_size < cont1_bytes)
3310 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04003311again:
3312 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06003313 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04003314 next_zero = find_next_zero_bit(entry->bitmap,
3315 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003316 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04003317 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04003318 if (found_bits > max_bits)
3319 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003320 break;
3321 }
Josef Bacikcef40482015-10-02 16:09:42 -04003322 if (next_zero - i > max_bits)
3323 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04003324 i = next_zero;
3325 }
3326
Josef Bacikcef40482015-10-02 16:09:42 -04003327 if (!found_bits) {
3328 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04003329 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04003330 }
Josef Bacik96303082009-07-13 21:29:25 -04003331
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003332 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04003333 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05003334 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003335 }
3336
3337 total_found += found_bits;
3338
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003339 if (cluster->max_size < found_bits * ctl->unit)
3340 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04003341
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003342 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
3343 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04003344 goto again;
3345 }
3346
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003347 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08003348 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05003349 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
3350
3351 /*
3352 * We need to know if we're currently on the normal space index when we
3353 * manipulate the bitmap so that we know we need to remove and re-insert
3354 * it into the space_index tree. Clear the bytes_index node here so the
3355 * bitmap manipulation helpers know not to mess with the space_index
3356 * until this bitmap entry is added back into the normal cache.
3357 */
3358 RB_CLEAR_NODE(&entry->bytes_index);
3359
Josef Bacik4e69b592011-03-21 10:11:24 -04003360 ret = tree_insert_offset(&cluster->root, entry->offset,
3361 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04003362 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04003363
Josef Bacik3f7de032011-11-10 08:29:20 -05003364 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003365 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04003366 return 0;
3367}
3368
Chris Masonfa9c0d792009-04-03 09:47:43 -04003369/*
Josef Bacik4e69b592011-03-21 10:11:24 -04003370 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003371 * Try to find a cluster with at least bytes total bytes, at least one
3372 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04003373 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003374static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003375setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003376 struct btrfs_free_cluster *cluster,
3377 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003378 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003379{
Li Zefan34d52cb2011-03-29 13:46:06 +08003380 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04003381 struct btrfs_free_space *first = NULL;
3382 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003383 struct btrfs_free_space *last;
3384 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04003385 u64 window_free;
3386 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003387 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003388
Li Zefan34d52cb2011-03-29 13:46:06 +08003389 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04003390 if (!entry)
3391 return -ENOSPC;
3392
3393 /*
3394 * We don't want bitmaps, so just move along until we find a normal
3395 * extent entry.
3396 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003397 while (entry->bitmap || entry->bytes < min_bytes) {
3398 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04003399 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003400 node = rb_next(&entry->offset_index);
3401 if (!node)
3402 return -ENOSPC;
3403 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3404 }
3405
Josef Bacik4e69b592011-03-21 10:11:24 -04003406 window_free = entry->bytes;
3407 max_extent = entry->bytes;
3408 first = entry;
3409 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04003410
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003411 for (node = rb_next(&entry->offset_index); node;
3412 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003413 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3414
Josef Bacik86d4a772011-05-25 13:03:16 -04003415 if (entry->bitmap) {
3416 if (list_empty(&entry->list))
3417 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003418 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04003419 }
3420
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003421 if (entry->bytes < min_bytes)
3422 continue;
3423
3424 last = entry;
3425 window_free += entry->bytes;
3426 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04003427 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003428 }
3429
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003430 if (window_free < bytes || max_extent < cont1_bytes)
3431 return -ENOSPC;
3432
Josef Bacik4e69b592011-03-21 10:11:24 -04003433 cluster->window_start = first->offset;
3434
3435 node = &first->offset_index;
3436
3437 /*
3438 * now we've found our entries, pull them out of the free space
3439 * cache and put them into the cluster rbtree
3440 */
3441 do {
3442 int ret;
3443
3444 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3445 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003446 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003447 continue;
3448
Li Zefan34d52cb2011-03-29 13:46:06 +08003449 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05003450 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003451 ret = tree_insert_offset(&cluster->root, entry->offset,
3452 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05003453 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04003454 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04003455 } while (node && entry != last);
3456
3457 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003458 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04003459 return 0;
3460}
3461
3462/*
3463 * This specifically looks for bitmaps that may work in the cluster, we assume
3464 * that we have already failed to find extents that will work.
3465 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003466static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003467setup_cluster_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003468 struct btrfs_free_cluster *cluster,
3469 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003470 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003471{
Li Zefan34d52cb2011-03-29 13:46:06 +08003472 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003473 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003474 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003475 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003476
Li Zefan34d52cb2011-03-29 13:46:06 +08003477 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003478 return -ENOSPC;
3479
Josef Bacik86d4a772011-05-25 13:03:16 -04003480 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003481 * The bitmap that covers offset won't be in the list unless offset
3482 * is just its start offset.
3483 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003484 if (!list_empty(bitmaps))
3485 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3486
3487 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003488 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3489 if (entry && list_empty(&entry->list))
3490 list_add(&entry->list, bitmaps);
3491 }
3492
Josef Bacik86d4a772011-05-25 13:03:16 -04003493 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003494 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003495 continue;
3496 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003497 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003498 if (!ret)
3499 return 0;
3500 }
3501
3502 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003503 * The bitmaps list has all the bitmaps that record free space
3504 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003505 */
Li Zefan52621cb2011-11-20 07:33:38 -05003506 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003507}
3508
3509/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003510 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003511 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003512 * We might not find them all in one contiguous area.
3513 *
3514 * returns zero and sets up cluster if things worked out, otherwise
3515 * it returns -enospc
3516 */
David Sterba32da53862019-10-29 19:20:18 +01003517int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003518 struct btrfs_free_cluster *cluster,
3519 u64 offset, u64 bytes, u64 empty_size)
3520{
David Sterba2ceeae22019-03-20 13:53:49 +01003521 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003522 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003523 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003524 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003525 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003526 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003527 int ret;
3528
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003529 /*
3530 * Choose the minimum extent size we'll require for this
3531 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3532 * For metadata, allow allocates with smaller extents. For
3533 * data, keep it dense.
3534 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003536 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003537 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003538 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003539 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003540 } else {
3541 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003542 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003543 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003544
Li Zefan34d52cb2011-03-29 13:46:06 +08003545 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003546
3547 /*
3548 * If we know we don't have enough space to make a cluster don't even
3549 * bother doing all the work to try and find one.
3550 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003551 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003552 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003553 return -ENOSPC;
3554 }
3555
Chris Masonfa9c0d792009-04-03 09:47:43 -04003556 spin_lock(&cluster->lock);
3557
3558 /* someone already found a cluster, hooray */
3559 if (cluster->block_group) {
3560 ret = 0;
3561 goto out;
3562 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003563
Josef Bacik3f7de032011-11-10 08:29:20 -05003564 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3565 min_bytes);
3566
Josef Bacik86d4a772011-05-25 13:03:16 -04003567 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003568 bytes + empty_size,
3569 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003570 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003571 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003572 offset, bytes + empty_size,
3573 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003574
3575 /* Clear our temporary list */
3576 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3577 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003578
3579 if (!ret) {
Anand Jainb5790d52020-06-03 18:10:20 +08003580 btrfs_get_block_group(block_group);
Josef Bacik4e69b592011-03-21 10:11:24 -04003581 list_add_tail(&cluster->block_group_list,
3582 &block_group->cluster_list);
3583 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003584 } else {
3585 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003586 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003587out:
3588 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003589 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003590
3591 return ret;
3592}
3593
3594/*
3595 * simple code to zero out a cluster
3596 */
3597void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3598{
3599 spin_lock_init(&cluster->lock);
3600 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003601 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003602 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003603 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003604 INIT_LIST_HEAD(&cluster->block_group_list);
3605 cluster->block_group = NULL;
3606}
3607
David Sterba32da53862019-10-29 19:20:18 +01003608static int do_trimming(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003609 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003610 u64 reserved_start, u64 reserved_bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003611 enum btrfs_trim_state reserved_trim_state,
Filipe Manana55507ce2014-12-01 17:04:09 +00003612 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003613{
3614 struct btrfs_space_info *space_info = block_group->space_info;
3615 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003616 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003617 int ret;
3618 int update = 0;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003619 const u64 end = start + bytes;
3620 const u64 reserved_end = reserved_start + reserved_bytes;
3621 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003622 u64 trimmed = 0;
3623
3624 spin_lock(&space_info->lock);
3625 spin_lock(&block_group->lock);
3626 if (!block_group->ro) {
3627 block_group->reserved += reserved_bytes;
3628 space_info->bytes_reserved += reserved_bytes;
3629 update = 1;
3630 }
3631 spin_unlock(&block_group->lock);
3632 spin_unlock(&space_info->lock);
3633
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003634 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003635 if (!ret) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003636 *total_trimmed += trimmed;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003637 trim_state = BTRFS_TRIM_STATE_TRIMMED;
3638 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003639
Filipe Manana55507ce2014-12-01 17:04:09 +00003640 mutex_lock(&ctl->cache_writeout_mutex);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003641 if (reserved_start < start)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003642 __btrfs_add_free_space(block_group, reserved_start,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003643 start - reserved_start,
3644 reserved_trim_state);
3645 if (start + bytes < reserved_start + reserved_bytes)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003646 __btrfs_add_free_space(block_group, end, reserved_end - end,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003647 reserved_trim_state);
Nikolay Borisov290ef192021-11-23 14:44:21 +02003648 __btrfs_add_free_space(block_group, start, bytes, trim_state);
Filipe Manana55507ce2014-12-01 17:04:09 +00003649 list_del(&trim_entry->list);
3650 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003651
3652 if (update) {
3653 spin_lock(&space_info->lock);
3654 spin_lock(&block_group->lock);
3655 if (block_group->ro)
3656 space_info->bytes_readonly += reserved_bytes;
3657 block_group->reserved -= reserved_bytes;
3658 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003659 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003660 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003661 }
3662
3663 return ret;
3664}
3665
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003666/*
3667 * If @async is set, then we will trim 1 region and return.
3668 */
David Sterba32da53862019-10-29 19:20:18 +01003669static int trim_no_bitmap(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003670 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3671 bool async)
Li Dongyangf7039b12011-03-24 10:24:28 +00003672{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003673 struct btrfs_discard_ctl *discard_ctl =
3674 &block_group->fs_info->discard_ctl;
Li Zefan34d52cb2011-03-29 13:46:06 +08003675 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003676 struct btrfs_free_space *entry;
3677 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003678 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003679 u64 extent_start;
3680 u64 extent_bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003681 enum btrfs_trim_state extent_trim_state;
Li Zefan7fe1e642011-12-29 14:47:27 +08003682 u64 bytes;
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003683 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Dongyangf7039b12011-03-24 10:24:28 +00003684
3685 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003686 struct btrfs_trim_range trim_entry;
3687
3688 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003689 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003690
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003691 if (ctl->free_space < minlen)
3692 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003693
Li Zefan34d52cb2011-03-29 13:46:06 +08003694 entry = tree_search_offset(ctl, start, 0, 1);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003695 if (!entry)
3696 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003697
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003698 /* Skip bitmaps and if async, already trimmed entries */
3699 while (entry->bitmap ||
3700 (async && btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003701 node = rb_next(&entry->offset_index);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003702 if (!node)
3703 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003704 entry = rb_entry(node, struct btrfs_free_space,
3705 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003706 }
3707
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003708 if (entry->offset >= end)
3709 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003710
3711 extent_start = entry->offset;
3712 extent_bytes = entry->bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003713 extent_trim_state = entry->trim_state;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003714 if (async) {
3715 start = entry->offset;
3716 bytes = entry->bytes;
3717 if (bytes < minlen) {
3718 spin_unlock(&ctl->tree_lock);
3719 mutex_unlock(&ctl->cache_writeout_mutex);
3720 goto next;
3721 }
Nikolay Borisov32e16492021-11-23 14:44:20 +02003722 unlink_free_space(ctl, entry, true);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003723 /*
3724 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3725 * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
3726 * X when we come back around. So trim it now.
3727 */
3728 if (max_discard_size &&
3729 bytes >= (max_discard_size +
3730 BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003731 bytes = max_discard_size;
3732 extent_bytes = max_discard_size;
3733 entry->offset += max_discard_size;
3734 entry->bytes -= max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003735 link_free_space(ctl, entry);
3736 } else {
3737 kmem_cache_free(btrfs_free_space_cachep, entry);
3738 }
3739 } else {
3740 start = max(start, extent_start);
3741 bytes = min(extent_start + extent_bytes, end) - start;
3742 if (bytes < minlen) {
3743 spin_unlock(&ctl->tree_lock);
3744 mutex_unlock(&ctl->cache_writeout_mutex);
3745 goto next;
3746 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003747
Nikolay Borisov32e16492021-11-23 14:44:20 +02003748 unlink_free_space(ctl, entry, true);
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003749 kmem_cache_free(btrfs_free_space_cachep, entry);
3750 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003751
Li Zefan34d52cb2011-03-29 13:46:06 +08003752 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003753 trim_entry.start = extent_start;
3754 trim_entry.bytes = extent_bytes;
3755 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3756 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003757
Li Zefan7fe1e642011-12-29 14:47:27 +08003758 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003759 extent_start, extent_bytes, extent_trim_state,
3760 &trim_entry);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003761 if (ret) {
3762 block_group->discard_cursor = start + bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003763 break;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003764 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003765next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003766 start += bytes;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003767 block_group->discard_cursor = start;
3768 if (async && *total_trimmed)
3769 break;
Li Dongyangf7039b12011-03-24 10:24:28 +00003770
3771 if (fatal_signal_pending(current)) {
3772 ret = -ERESTARTSYS;
3773 break;
3774 }
3775
3776 cond_resched();
3777 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003778
3779 return ret;
3780
3781out_unlock:
3782 block_group->discard_cursor = btrfs_block_group_end(block_group);
3783 spin_unlock(&ctl->tree_lock);
3784 mutex_unlock(&ctl->cache_writeout_mutex);
3785
Li Zefan7fe1e642011-12-29 14:47:27 +08003786 return ret;
3787}
3788
Dennis Zhouda080fe2019-12-13 16:22:13 -08003789/*
3790 * If we break out of trimming a bitmap prematurely, we should reset the
3791 * trimming bit. In a rather contrieved case, it's possible to race here so
3792 * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
3793 *
3794 * start = start of bitmap
3795 * end = near end of bitmap
3796 *
3797 * Thread 1: Thread 2:
3798 * trim_bitmaps(start)
3799 * trim_bitmaps(end)
3800 * end_trimming_bitmap()
3801 * reset_trimming_bitmap()
3802 */
3803static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
3804{
3805 struct btrfs_free_space *entry;
3806
3807 spin_lock(&ctl->tree_lock);
3808 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003809 if (entry) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003810 if (btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003811 ctl->discardable_extents[BTRFS_STAT_CURR] +=
3812 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003813 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
3814 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003815 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003816 }
3817
Dennis Zhouda080fe2019-12-13 16:22:13 -08003818 spin_unlock(&ctl->tree_lock);
3819}
3820
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003821static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
3822 struct btrfs_free_space *entry)
Dennis Zhouda080fe2019-12-13 16:22:13 -08003823{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003824 if (btrfs_free_space_trimming_bitmap(entry)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003825 entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003826 ctl->discardable_extents[BTRFS_STAT_CURR] -=
3827 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003828 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003829 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003830}
3831
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003832/*
3833 * If @async is set, then we will trim 1 region and return.
3834 */
David Sterba32da53862019-10-29 19:20:18 +01003835static int trim_bitmaps(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003836 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003837 u64 maxlen, bool async)
Li Zefan7fe1e642011-12-29 14:47:27 +08003838{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003839 struct btrfs_discard_ctl *discard_ctl =
3840 &block_group->fs_info->discard_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003841 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3842 struct btrfs_free_space *entry;
3843 int ret = 0;
3844 int ret2;
3845 u64 bytes;
3846 u64 offset = offset_to_bitmap(ctl, start);
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003847 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Zefan7fe1e642011-12-29 14:47:27 +08003848
3849 while (offset < end) {
3850 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003851 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003852
Filipe Manana55507ce2014-12-01 17:04:09 +00003853 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003854 spin_lock(&ctl->tree_lock);
3855
3856 if (ctl->free_space < minlen) {
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003857 block_group->discard_cursor =
3858 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003859 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003860 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003861 break;
3862 }
3863
3864 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003865 /*
3866 * Bitmaps are marked trimmed lossily now to prevent constant
3867 * discarding of the same bitmap (the reason why we are bound
3868 * by the filters). So, retrim the block group bitmaps when we
3869 * are preparing to punt to the unused_bgs list. This uses
3870 * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
3871 * which is the only discard index which sets minlen to 0.
3872 */
3873 if (!entry || (async && minlen && start == offset &&
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003874 btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003875 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003876 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003877 next_bitmap = true;
3878 goto next;
3879 }
3880
Dennis Zhouda080fe2019-12-13 16:22:13 -08003881 /*
3882 * Async discard bitmap trimming begins at by setting the start
3883 * to be key.objectid and the offset_to_bitmap() aligns to the
3884 * start of the bitmap. This lets us know we are fully
3885 * scanning the bitmap rather than only some portion of it.
3886 */
3887 if (start == offset)
3888 entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
3889
Li Zefan7fe1e642011-12-29 14:47:27 +08003890 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003891 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003892 if (ret2 || start >= end) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003893 /*
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003894 * We lossily consider a bitmap trimmed if we only skip
3895 * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
Dennis Zhouda080fe2019-12-13 16:22:13 -08003896 */
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003897 if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003898 end_trimming_bitmap(ctl, entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003899 else
3900 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003901 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003902 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003903 next_bitmap = true;
3904 goto next;
3905 }
3906
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003907 /*
3908 * We already trimmed a region, but are using the locking above
3909 * to reset the trim_state.
3910 */
3911 if (async && *total_trimmed) {
3912 spin_unlock(&ctl->tree_lock);
3913 mutex_unlock(&ctl->cache_writeout_mutex);
3914 goto out;
3915 }
3916
Li Zefan7fe1e642011-12-29 14:47:27 +08003917 bytes = min(bytes, end - start);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003918 if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003919 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003920 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003921 goto next;
3922 }
3923
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003924 /*
3925 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3926 * If X < @minlen, we won't trim X when we come back around.
3927 * So trim it now. We differ here from trimming extents as we
3928 * don't keep individual state per bit.
3929 */
3930 if (async &&
3931 max_discard_size &&
3932 bytes > (max_discard_size + minlen))
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003933 bytes = max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003934
Nikolay Borisovf594f132021-11-23 14:44:19 +02003935 bitmap_clear_bits(ctl, entry, start, bytes, true);
Li Zefan7fe1e642011-12-29 14:47:27 +08003936 if (entry->bytes == 0)
3937 free_bitmap(ctl, entry);
3938
3939 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003940 trim_entry.start = start;
3941 trim_entry.bytes = bytes;
3942 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3943 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003944
3945 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003946 start, bytes, 0, &trim_entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003947 if (ret) {
3948 reset_trimming_bitmap(ctl, offset);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003949 block_group->discard_cursor =
3950 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003951 break;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003952 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003953next:
3954 if (next_bitmap) {
3955 offset += BITS_PER_BITMAP * ctl->unit;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003956 start = offset;
Li Zefan7fe1e642011-12-29 14:47:27 +08003957 } else {
3958 start += bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003959 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003960 block_group->discard_cursor = start;
Li Zefan7fe1e642011-12-29 14:47:27 +08003961
3962 if (fatal_signal_pending(current)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003963 if (start != offset)
3964 reset_trimming_bitmap(ctl, offset);
Li Zefan7fe1e642011-12-29 14:47:27 +08003965 ret = -ERESTARTSYS;
3966 break;
3967 }
3968
3969 cond_resched();
3970 }
3971
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003972 if (offset >= end)
3973 block_group->discard_cursor = end;
3974
3975out:
Li Zefan7fe1e642011-12-29 14:47:27 +08003976 return ret;
3977}
3978
David Sterba32da53862019-10-29 19:20:18 +01003979int btrfs_trim_block_group(struct btrfs_block_group *block_group,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003980 u64 *trimmed, u64 start, u64 end, u64 minlen)
3981{
Dennis Zhouda080fe2019-12-13 16:22:13 -08003982 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003983 int ret;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003984 u64 rem = 0;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003985
Naohiro Aota2eda5702021-02-04 19:21:53 +09003986 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3987
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003988 *trimmed = 0;
3989
3990 spin_lock(&block_group->lock);
3991 if (block_group->removed) {
3992 spin_unlock(&block_group->lock);
3993 return 0;
3994 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003995 btrfs_freeze_block_group(block_group);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003996 spin_unlock(&block_group->lock);
3997
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003998 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003999 if (ret)
4000 goto out;
4001
Dennis Zhou7fe6d452020-01-02 16:26:39 -05004002 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
Dennis Zhouda080fe2019-12-13 16:22:13 -08004003 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
4004 /* If we ended in the middle of a bitmap, reset the trimming flag */
4005 if (rem)
4006 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04004007out:
Filipe Manana6b7304a2020-05-08 11:01:47 +01004008 btrfs_unfreeze_block_group(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00004009 return ret;
4010}
Li Zefan581bb052011-04-20 10:06:11 +08004011
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004012int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
4013 u64 *trimmed, u64 start, u64 end, u64 minlen,
4014 bool async)
4015{
4016 int ret;
4017
4018 *trimmed = 0;
4019
4020 spin_lock(&block_group->lock);
4021 if (block_group->removed) {
4022 spin_unlock(&block_group->lock);
4023 return 0;
4024 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01004025 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004026 spin_unlock(&block_group->lock);
4027
4028 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
Filipe Manana6b7304a2020-05-08 11:01:47 +01004029 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004030
4031 return ret;
4032}
4033
4034int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
4035 u64 *trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05004036 u64 maxlen, bool async)
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004037{
4038 int ret;
4039
4040 *trimmed = 0;
4041
4042 spin_lock(&block_group->lock);
4043 if (block_group->removed) {
4044 spin_unlock(&block_group->lock);
4045 return 0;
4046 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01004047 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004048 spin_unlock(&block_group->lock);
4049
Dennis Zhou7fe6d452020-01-02 16:26:39 -05004050 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
4051 async);
4052
Filipe Manana6b7304a2020-05-08 11:01:47 +01004053 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004054
4055 return ret;
4056}
4057
Boris Burkov94846222020-11-18 15:06:22 -08004058bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info)
4059{
4060 return btrfs_super_cache_generation(fs_info->super_copy);
4061}
4062
Boris Burkov36b216c2020-11-18 15:06:25 -08004063static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info,
4064 struct btrfs_trans_handle *trans)
4065{
4066 struct btrfs_block_group *block_group;
4067 struct rb_node *node;
Tom Rix77364fa2021-04-30 11:06:55 -07004068 int ret = 0;
Boris Burkov36b216c2020-11-18 15:06:25 -08004069
4070 btrfs_info(fs_info, "cleaning free space cache v1");
4071
4072 node = rb_first(&fs_info->block_group_cache_tree);
4073 while (node) {
4074 block_group = rb_entry(node, struct btrfs_block_group, cache_node);
4075 ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
4076 if (ret)
4077 goto out;
4078 node = rb_next(node);
4079 }
4080out:
4081 return ret;
4082}
4083
Boris Burkov94846222020-11-18 15:06:22 -08004084int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active)
4085{
4086 struct btrfs_trans_handle *trans;
4087 int ret;
4088
4089 /*
Boris Burkov36b216c2020-11-18 15:06:25 -08004090 * update_super_roots will appropriately set or unset
4091 * super_copy->cache_generation based on SPACE_CACHE and
4092 * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a
4093 * transaction commit whether we are enabling space cache v1 and don't
4094 * have any other work to do, or are disabling it and removing free
4095 * space inodes.
Boris Burkov94846222020-11-18 15:06:22 -08004096 */
4097 trans = btrfs_start_transaction(fs_info->tree_root, 0);
4098 if (IS_ERR(trans))
4099 return PTR_ERR(trans);
4100
Boris Burkov36b216c2020-11-18 15:06:25 -08004101 if (!active) {
Boris Burkov94846222020-11-18 15:06:22 -08004102 set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
Boris Burkov36b216c2020-11-18 15:06:25 -08004103 ret = cleanup_free_space_cache_v1(fs_info, trans);
4104 if (ret) {
4105 btrfs_abort_transaction(trans, ret);
4106 btrfs_end_transaction(trans);
4107 goto out;
4108 }
4109 }
Boris Burkov94846222020-11-18 15:06:22 -08004110
4111 ret = btrfs_commit_transaction(trans);
Boris Burkov36b216c2020-11-18 15:06:25 -08004112out:
Boris Burkov94846222020-11-18 15:06:22 -08004113 clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
4114
4115 return ret;
4116}
4117
Josef Bacik74255aa2013-03-15 09:47:08 -04004118#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04004119/*
4120 * Use this if you need to make a bitmap or extent entry specifically, it
4121 * doesn't do any of the merging that add_free_space does, this acts a lot like
4122 * how the free space cache loading stuff works, so you can get really weird
4123 * configurations.
4124 */
David Sterba32da53862019-10-29 19:20:18 +01004125int test_add_free_space_entry(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004126 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04004127{
Josef Bacikdc11dd52013-08-14 15:05:12 -04004128 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4129 struct btrfs_free_space *info = NULL, *bitmap_info;
4130 void *map = NULL;
Dennis Zhouda080fe2019-12-13 16:22:13 -08004131 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004132 u64 bytes_added;
4133 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04004134
Josef Bacikdc11dd52013-08-14 15:05:12 -04004135again:
4136 if (!info) {
4137 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
4138 if (!info)
4139 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04004140 }
4141
Josef Bacikdc11dd52013-08-14 15:05:12 -04004142 if (!bitmap) {
4143 spin_lock(&ctl->tree_lock);
4144 info->offset = offset;
4145 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04004146 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004147 ret = link_free_space(ctl, info);
4148 spin_unlock(&ctl->tree_lock);
4149 if (ret)
4150 kmem_cache_free(btrfs_free_space_cachep, info);
4151 return ret;
4152 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004153
Josef Bacikdc11dd52013-08-14 15:05:12 -04004154 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00004155 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004156 if (!map) {
4157 kmem_cache_free(btrfs_free_space_cachep, info);
4158 return -ENOMEM;
4159 }
4160 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004161
Josef Bacikdc11dd52013-08-14 15:05:12 -04004162 spin_lock(&ctl->tree_lock);
4163 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4164 1, 0);
4165 if (!bitmap_info) {
4166 info->bitmap = map;
4167 map = NULL;
4168 add_new_bitmap(ctl, info, offset);
4169 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01004170 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004171 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004172
Dennis Zhouda080fe2019-12-13 16:22:13 -08004173 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
4174 trim_state);
Josef Bacikcef40482015-10-02 16:09:42 -04004175
Josef Bacikdc11dd52013-08-14 15:05:12 -04004176 bytes -= bytes_added;
4177 offset += bytes_added;
4178 spin_unlock(&ctl->tree_lock);
4179
4180 if (bytes)
4181 goto again;
4182
Filipe Manana20005522014-08-29 13:35:13 +01004183 if (info)
4184 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00004185 if (map)
4186 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004187 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004188}
4189
4190/*
4191 * Checks to see if the given range is in the free space cache. This is really
4192 * just used to check the absence of space, so if there is free space in the
4193 * range at all we will return 1.
4194 */
David Sterba32da53862019-10-29 19:20:18 +01004195int test_check_exists(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004196 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04004197{
4198 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4199 struct btrfs_free_space *info;
4200 int ret = 0;
4201
4202 spin_lock(&ctl->tree_lock);
4203 info = tree_search_offset(ctl, offset, 0, 0);
4204 if (!info) {
4205 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4206 1, 0);
4207 if (!info)
4208 goto out;
4209 }
4210
4211have_info:
4212 if (info->bitmap) {
4213 u64 bit_off, bit_bytes;
4214 struct rb_node *n;
4215 struct btrfs_free_space *tmp;
4216
4217 bit_off = offset;
4218 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04004219 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04004220 if (!ret) {
4221 if (bit_off == offset) {
4222 ret = 1;
4223 goto out;
4224 } else if (bit_off > offset &&
4225 offset + bytes > bit_off) {
4226 ret = 1;
4227 goto out;
4228 }
4229 }
4230
4231 n = rb_prev(&info->offset_index);
4232 while (n) {
4233 tmp = rb_entry(n, struct btrfs_free_space,
4234 offset_index);
4235 if (tmp->offset + tmp->bytes < offset)
4236 break;
4237 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004238 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004239 continue;
4240 }
4241 info = tmp;
4242 goto have_info;
4243 }
4244
4245 n = rb_next(&info->offset_index);
4246 while (n) {
4247 tmp = rb_entry(n, struct btrfs_free_space,
4248 offset_index);
4249 if (offset + bytes < tmp->offset)
4250 break;
4251 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004252 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004253 continue;
4254 }
4255 info = tmp;
4256 goto have_info;
4257 }
4258
Filipe Manana20005522014-08-29 13:35:13 +01004259 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004260 goto out;
4261 }
4262
4263 if (info->offset == offset) {
4264 ret = 1;
4265 goto out;
4266 }
4267
4268 if (offset > info->offset && offset < info->offset + info->bytes)
4269 ret = 1;
4270out:
4271 spin_unlock(&ctl->tree_lock);
4272 return ret;
4273}
Josef Bacikdc11dd52013-08-14 15:05:12 -04004274#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */