blob: 053103be73c79b894036db4cc1f1bbe0a0e13ab6 [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"
Chris Masonfa9c0d792009-04-03 09:47:43 -040026
Feifei Xu0ef64472016-06-01 19:18:24 +080027#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Dennis Zhou5d90c5c2020-01-02 16:26:43 -050028#define MAX_CACHE_BYTES_PER_GIG SZ_64K
29#define FORCE_EXTENT_THRESHOLD SZ_1M
Josef Bacik96303082009-07-13 21:29:25 -040030
Filipe Manana55507ce2014-12-01 17:04:09 +000031struct btrfs_trim_range {
32 u64 start;
33 u64 bytes;
34 struct list_head list;
35};
36
Li Zefan34d52cb2011-03-29 13:46:06 +080037static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040038 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040039static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Nikolay Borisov32e16492021-11-23 14:44:20 +020040 struct btrfs_free_space *info, bool update_stat);
Josef Bacikcd799092020-10-23 09:58:08 -040041static int search_bitmap(struct btrfs_free_space_ctl *ctl,
42 struct btrfs_free_space *bitmap_info, u64 *offset,
43 u64 *bytes, bool for_alloc);
44static void free_bitmap(struct btrfs_free_space_ctl *ctl,
45 struct btrfs_free_space *bitmap_info);
46static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
47 struct btrfs_free_space *info, u64 offset,
Nikolay Borisovf594f132021-11-23 14:44:19 +020048 u64 bytes, bool update_stats);
Josef Bacik0cb59c92010-07-02 12:14:14 -040049
Li Zefan0414efa2011-04-20 10:20:14 +080050static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
51 struct btrfs_path *path,
52 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040054 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040055 struct btrfs_key key;
56 struct btrfs_key location;
57 struct btrfs_disk_key disk_key;
58 struct btrfs_free_space_header *header;
59 struct extent_buffer *leaf;
60 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040061 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040062 int ret;
63
Josef Bacik0af3d002010-06-21 14:48:16 -040064 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080065 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040066 key.type = 0;
67
68 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
69 if (ret < 0)
70 return ERR_PTR(ret);
71 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020072 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040073 return ERR_PTR(-ENOENT);
74 }
75
76 leaf = path->nodes[0];
77 header = btrfs_item_ptr(leaf, path->slots[0],
78 struct btrfs_free_space_header);
79 btrfs_free_space_key(leaf, header, &disk_key);
80 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020081 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040082
Josef Bacik84de76a2018-09-28 07:17:49 -040083 /*
84 * We are often under a trans handle at this point, so we need to make
85 * sure NOFS is set to keep us from deadlocking.
86 */
87 nofs_flag = memalloc_nofs_save();
David Sterba0202e832020-05-15 19:35:59 +020088 inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
Filipe Manana4222ea72018-10-24 10:13:03 +010089 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040090 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040091 if (IS_ERR(inode))
92 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040093
Al Viro528c0322012-04-13 11:03:55 -040094 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080095 mapping_gfp_constraint(inode->i_mapping,
96 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000097
Li Zefan0414efa2011-04-20 10:20:14 +080098 return inode;
99}
100
David Sterba32da53862019-10-29 19:20:18 +0100101struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
David Sterba7949f332019-03-20 13:40:19 +0100102 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +0800103{
David Sterba7949f332019-03-20 13:40:19 +0100104 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +0800105 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400106 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800107
108 spin_lock(&block_group->lock);
109 if (block_group->inode)
110 inode = igrab(block_group->inode);
111 spin_unlock(&block_group->lock);
112 if (inode)
113 return inode;
114
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500115 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200116 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800117 if (IS_ERR(inode))
118 return inode;
119
Josef Bacik0af3d002010-06-21 14:48:16 -0400120 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400121 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400122 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400123 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
124 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400125 block_group->disk_cache_state = BTRFS_DC_CLEAR;
126 }
127
Josef Bacik300e4f82011-08-29 14:06:00 -0400128 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400129 block_group->inode = igrab(inode);
130 block_group->iref = 1;
131 }
132 spin_unlock(&block_group->lock);
133
134 return inode;
135}
136
Eric Sandeen48a3b632013-04-25 20:41:01 +0000137static int __create_free_space_inode(struct btrfs_root *root,
138 struct btrfs_trans_handle *trans,
139 struct btrfs_path *path,
140 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400141{
142 struct btrfs_key key;
143 struct btrfs_disk_key disk_key;
144 struct btrfs_free_space_header *header;
145 struct btrfs_inode_item *inode_item;
146 struct extent_buffer *leaf;
Nikolay Borisovf0d12192020-12-03 10:09:49 +0200147 /* We inline CRCs for the free disk space cache */
148 const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC |
149 BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Josef Bacik0af3d002010-06-21 14:48:16 -0400150 int ret;
151
Li Zefan0414efa2011-04-20 10:20:14 +0800152 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400153 if (ret)
154 return ret;
155
156 leaf = path->nodes[0];
157 inode_item = btrfs_item_ptr(leaf, path->slots[0],
158 struct btrfs_inode_item);
159 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100160 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400161 sizeof(*inode_item));
162 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
163 btrfs_set_inode_size(leaf, inode_item, 0);
164 btrfs_set_inode_nbytes(leaf, inode_item, 0);
165 btrfs_set_inode_uid(leaf, inode_item, 0);
166 btrfs_set_inode_gid(leaf, inode_item, 0);
167 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400168 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169 btrfs_set_inode_nlink(leaf, inode_item, 1);
170 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800171 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400172 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200173 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400174
175 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800176 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 ret = btrfs_insert_empty_item(trans, root, path, &key,
179 sizeof(struct btrfs_free_space_header));
180 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200181 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400182 return ret;
183 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700184
Josef Bacik0af3d002010-06-21 14:48:16 -0400185 leaf = path->nodes[0];
186 header = btrfs_item_ptr(leaf, path->slots[0],
187 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100188 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400189 btrfs_set_free_space_key(leaf, header, &disk_key);
190 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200191 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400192
193 return 0;
194}
195
David Sterba4ca75f12019-03-20 13:42:57 +0100196int create_free_space_inode(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100197 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +0800198 struct btrfs_path *path)
199{
200 int ret;
201 u64 ino;
202
Nikolay Borisov543068a2020-12-07 17:32:33 +0200203 ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800204 if (ret < 0)
205 return ret;
206
David Sterba4ca75f12019-03-20 13:42:57 +0100207 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200208 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800209}
210
Boris Burkov36b216c2020-11-18 15:06:25 -0800211/*
212 * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode
213 * handles lookup, otherwise it takes ownership and iputs the inode.
214 * Don't reuse an inode pointer after passing it into this function.
215 */
216int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
217 struct inode *inode,
218 struct btrfs_block_group *block_group)
219{
220 struct btrfs_path *path;
221 struct btrfs_key key;
222 int ret = 0;
223
224 path = btrfs_alloc_path();
225 if (!path)
226 return -ENOMEM;
227
228 if (!inode)
229 inode = lookup_free_space_inode(block_group, path);
230 if (IS_ERR(inode)) {
231 if (PTR_ERR(inode) != -ENOENT)
232 ret = PTR_ERR(inode);
233 goto out;
234 }
235 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
236 if (ret) {
237 btrfs_add_delayed_iput(inode);
238 goto out;
239 }
240 clear_nlink(inode);
241 /* One for the block groups ref */
242 spin_lock(&block_group->lock);
243 if (block_group->iref) {
244 block_group->iref = 0;
245 block_group->inode = NULL;
246 spin_unlock(&block_group->lock);
247 iput(inode);
248 } else {
249 spin_unlock(&block_group->lock);
250 }
251 /* One for the lookup ref */
252 btrfs_add_delayed_iput(inode);
253
254 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
255 key.type = 0;
256 key.offset = block_group->start;
257 ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path,
258 -1, 1);
259 if (ret) {
260 if (ret > 0)
261 ret = 0;
262 goto out;
263 }
264 ret = btrfs_del_item(trans, trans->fs_info->tree_root, path);
265out:
266 btrfs_free_path(path);
267 return ret;
268}
269
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400270int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000271 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400272{
Josef Bacikc8174312011-11-02 09:29:35 -0400273 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000274 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400275
276 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400277 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
278 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400279
Miao Xie7b61cd92013-05-13 13:55:09 +0000280 spin_lock(&rsv->lock);
281 if (rsv->reserved < needed_bytes)
282 ret = -ENOSPC;
283 else
284 ret = 0;
285 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000286 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000287}
288
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500289int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100290 struct btrfs_block_group *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000291 struct inode *inode)
292{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500293 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000294 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100295 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700296
Chris Mason1bbc6212015-04-06 12:46:08 -0700297 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500298 struct btrfs_path *path = btrfs_alloc_path();
299
300 if (!path) {
301 ret = -ENOMEM;
302 goto fail;
303 }
Filipe Manana35c76642015-04-30 17:47:05 +0100304 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700305 mutex_lock(&trans->transaction->cache_write_mutex);
306 if (!list_empty(&block_group->io_list)) {
307 list_del_init(&block_group->io_list);
308
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400309 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700310 btrfs_put_block_group(block_group);
311 }
312
313 /*
314 * now that we've truncated the cache away, its no longer
315 * setup or written
316 */
317 spin_lock(&block_group->lock);
318 block_group->disk_cache_state = BTRFS_DC_CLEAR;
319 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500320 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700321 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400322
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200323 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700324 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400325
326 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700327 * We skip the throttling logic for free space cache inodes, so we don't
328 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400329 */
Nikolay Borisov50743392020-11-02 16:48:55 +0200330 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +0100331 0, BTRFS_EXTENT_DATA_KEY, NULL);
Filipe Manana35c76642015-04-30 17:47:05 +0100332 if (ret)
333 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400334
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200335 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason1bbc6212015-04-06 12:46:08 -0700336
Chris Mason1bbc6212015-04-06 12:46:08 -0700337fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100338 if (locked)
339 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400341 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400342
Li Zefan82d59022011-04-20 10:33:24 +0800343 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400344}
345
David Sterba1d480532017-01-23 17:28:19 +0100346static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400347{
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500348 struct file_ra_state ra;
Josef Bacik9d66e232010-08-25 16:54:15 -0400349 unsigned long last_index;
350
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500351 file_ra_state_init(&ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300352 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400353
Goldwyn Rodrigues98caf952021-07-27 16:17:26 -0500354 page_cache_sync_readahead(inode->i_mapping, &ra, NULL, 0, last_index);
Josef Bacik9d66e232010-08-25 16:54:15 -0400355}
356
Chris Mason4c6d1d82015-04-06 13:17:20 -0700357static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400358 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400359{
Miao Xie5349d6c2014-06-19 10:42:49 +0800360 int num_pages;
Miao Xie5349d6c2014-06-19 10:42:49 +0800361
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300362 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800363
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400364 /* Make sure we can fit our crcs and generation into the first page */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200365 if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800366 return -ENOSPC;
367
Chris Mason4c6d1d82015-04-06 13:17:20 -0700368 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800369
David Sterba31e818f2015-02-20 18:00:26 +0100370 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400371 if (!io_ctl->pages)
372 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800373
374 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400375 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700376 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800377
Josef Bacika67509c2011-10-05 15:18:58 -0400378 return 0;
379}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900380ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400381
Chris Mason4c6d1d82015-04-06 13:17:20 -0700382static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400383{
384 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700385 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400386}
387
Chris Mason4c6d1d82015-04-06 13:17:20 -0700388static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400389{
390 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400391 io_ctl->cur = NULL;
392 io_ctl->orig = NULL;
393 }
394}
395
Chris Mason4c6d1d82015-04-06 13:17:20 -0700396static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400397{
Josef Bacikb12d6862013-08-26 17:14:08 -0400398 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400399 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700400 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400401 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300402 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400403 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200404 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400405}
406
Chris Mason4c6d1d82015-04-06 13:17:20 -0700407static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400408{
409 int i;
410
411 io_ctl_unmap_page(io_ctl);
412
413 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800414 if (io_ctl->pages[i]) {
Qu Wenruoe4f94342021-09-27 15:21:49 +0800415 btrfs_page_clear_checked(io_ctl->fs_info,
416 io_ctl->pages[i],
417 page_offset(io_ctl->pages[i]),
418 PAGE_SIZE);
Li Zefana1ee5a42012-01-09 14:27:42 +0800419 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300420 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800421 }
Josef Bacika67509c2011-10-05 15:18:58 -0400422 }
423}
424
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900425static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
Josef Bacika67509c2011-10-05 15:18:58 -0400426{
427 struct page *page;
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900428 struct inode *inode = io_ctl->inode;
Josef Bacika67509c2011-10-05 15:18:58 -0400429 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
430 int i;
431
432 for (i = 0; i < io_ctl->num_pages; i++) {
Qu Wenruo32443de2021-01-26 16:34:00 +0800433 int ret;
434
Josef Bacika67509c2011-10-05 15:18:58 -0400435 page = find_or_create_page(inode->i_mapping, i, mask);
436 if (!page) {
437 io_ctl_drop_pages(io_ctl);
438 return -ENOMEM;
439 }
Qu Wenruo32443de2021-01-26 16:34:00 +0800440
441 ret = set_page_extent_mapped(page);
442 if (ret < 0) {
443 unlock_page(page);
444 put_page(page);
445 io_ctl_drop_pages(io_ctl);
446 return ret;
447 }
448
Josef Bacika67509c2011-10-05 15:18:58 -0400449 io_ctl->pages[i] = page;
450 if (uptodate && !PageUptodate(page)) {
451 btrfs_readpage(NULL, page);
452 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400453 if (page->mapping != inode->i_mapping) {
454 btrfs_err(BTRFS_I(inode)->root->fs_info,
455 "free space cache page truncated");
456 io_ctl_drop_pages(io_ctl);
457 return -EIO;
458 }
Josef Bacika67509c2011-10-05 15:18:58 -0400459 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500460 btrfs_err(BTRFS_I(inode)->root->fs_info,
461 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400462 io_ctl_drop_pages(io_ctl);
463 return -EIO;
464 }
465 }
466 }
467
Qu Wenruo32443de2021-01-26 16:34:00 +0800468 for (i = 0; i < io_ctl->num_pages; i++)
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500469 clear_page_dirty_for_io(io_ctl->pages[i]);
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500470
Josef Bacika67509c2011-10-05 15:18:58 -0400471 return 0;
472}
473
Chris Mason4c6d1d82015-04-06 13:17:20 -0700474static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400475{
Josef Bacika67509c2011-10-05 15:18:58 -0400476 io_ctl_map_page(io_ctl, 1);
477
478 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400479 * Skip the csum areas. If we don't check crcs then we just have a
480 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400481 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200482 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
483 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400484
David Sterba6994ca362020-09-15 13:32:34 +0200485 put_unaligned_le64(generation, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400486 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400487}
488
Chris Mason4c6d1d82015-04-06 13:17:20 -0700489static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400490{
David Sterba6994ca362020-09-15 13:32:34 +0200491 u64 cache_gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400492
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400493 /*
494 * Skip the crc area. If we don't check crcs then we just have a 64bit
495 * chunk at the front of the first page.
496 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200497 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
498 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400499
David Sterba6994ca362020-09-15 13:32:34 +0200500 cache_gen = get_unaligned_le64(io_ctl->cur);
501 if (cache_gen != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400502 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200503 "space cache generation (%llu) does not match inode (%llu)",
David Sterba6994ca362020-09-15 13:32:34 +0200504 cache_gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400505 io_ctl_unmap_page(io_ctl);
506 return -EIO;
507 }
508 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400509 return 0;
510}
511
Chris Mason4c6d1d82015-04-06 13:17:20 -0700512static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400513{
514 u32 *tmp;
515 u32 crc = ~(u32)0;
516 unsigned offset = 0;
517
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400518 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800519 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400520
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200521 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
522 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400523 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700524 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400525 tmp += index;
526 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400527}
528
Chris Mason4c6d1d82015-04-06 13:17:20 -0700529static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400530{
531 u32 *tmp, val;
532 u32 crc = ~(u32)0;
533 unsigned offset = 0;
534
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400535 if (index == 0)
536 offset = sizeof(u32) * io_ctl->num_pages;
537
Chris Mason2b108262015-04-06 07:48:20 -0700538 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400539 tmp += index;
540 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400541
542 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200543 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
544 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400545 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400546 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200547 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400548 io_ctl_unmap_page(io_ctl);
549 return -EIO;
550 }
551
Josef Bacika67509c2011-10-05 15:18:58 -0400552 return 0;
553}
554
Chris Mason4c6d1d82015-04-06 13:17:20 -0700555static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400556 void *bitmap)
557{
558 struct btrfs_free_space_entry *entry;
559
560 if (!io_ctl->cur)
561 return -ENOSPC;
562
563 entry = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200564 put_unaligned_le64(offset, &entry->offset);
565 put_unaligned_le64(bytes, &entry->bytes);
Josef Bacika67509c2011-10-05 15:18:58 -0400566 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
567 BTRFS_FREE_SPACE_EXTENT;
568 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
569 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
570
571 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
572 return 0;
573
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400574 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400575
576 /* No more pages to map */
577 if (io_ctl->index >= io_ctl->num_pages)
578 return 0;
579
580 /* map the next page */
581 io_ctl_map_page(io_ctl, 1);
582 return 0;
583}
584
Chris Mason4c6d1d82015-04-06 13:17:20 -0700585static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400586{
587 if (!io_ctl->cur)
588 return -ENOSPC;
589
590 /*
591 * If we aren't at the start of the current page, unmap this one and
592 * map the next one if there is any left.
593 */
594 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400595 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400596 if (io_ctl->index >= io_ctl->num_pages)
597 return -ENOSPC;
598 io_ctl_map_page(io_ctl, 0);
599 }
600
David Sterba69d24802018-06-29 10:56:44 +0200601 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400602 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400603 if (io_ctl->index < io_ctl->num_pages)
604 io_ctl_map_page(io_ctl, 0);
605 return 0;
606}
607
Chris Mason4c6d1d82015-04-06 13:17:20 -0700608static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400609{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400610 /*
611 * If we're not on the boundary we know we've modified the page and we
612 * need to crc the page.
613 */
614 if (io_ctl->cur != io_ctl->orig)
615 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
616 else
617 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400618
619 while (io_ctl->index < io_ctl->num_pages) {
620 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400621 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400622 }
623}
624
Chris Mason4c6d1d82015-04-06 13:17:20 -0700625static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400626 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400627{
628 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500629 int ret;
630
631 if (!io_ctl->cur) {
632 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
633 if (ret)
634 return ret;
635 }
Josef Bacika67509c2011-10-05 15:18:58 -0400636
637 e = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200638 entry->offset = get_unaligned_le64(&e->offset);
639 entry->bytes = get_unaligned_le64(&e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400640 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400641 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
642 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
643
644 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400645 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400646
647 io_ctl_unmap_page(io_ctl);
648
Josef Bacik2f120c02011-11-10 20:45:05 -0500649 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400650}
651
Chris Mason4c6d1d82015-04-06 13:17:20 -0700652static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400653 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400654{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400655 int ret;
656
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400657 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
658 if (ret)
659 return ret;
660
David Sterba69d24802018-06-29 10:56:44 +0200661 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400662 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400663
664 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400665}
666
David Sterbafa598b02020-12-03 17:18:38 +0100667static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
668{
669 struct btrfs_block_group *block_group = ctl->private;
670 u64 max_bytes;
671 u64 bitmap_bytes;
672 u64 extent_bytes;
673 u64 size = block_group->length;
674 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
675 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
676
677 max_bitmaps = max_t(u64, max_bitmaps, 1);
678
679 ASSERT(ctl->total_bitmaps <= max_bitmaps);
680
681 /*
682 * We are trying to keep the total amount of memory used per 1GiB of
683 * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
684 * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
685 * bitmaps, we may end up using more memory than this.
686 */
687 if (size < SZ_1G)
688 max_bytes = MAX_CACHE_BYTES_PER_GIG;
689 else
690 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
691
692 bitmap_bytes = ctl->total_bitmaps * ctl->unit;
693
694 /*
695 * we want the extent entry threshold to always be at most 1/2 the max
696 * bytes we can have, or whatever is less than that.
697 */
698 extent_bytes = max_bytes - bitmap_bytes;
699 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
700
701 ctl->extents_thresh =
702 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
703}
704
Eric Sandeen48a3b632013-04-25 20:41:01 +0000705static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
706 struct btrfs_free_space_ctl *ctl,
707 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400708{
David Sterba3ffbd682018-06-29 10:56:42 +0200709 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400710 struct btrfs_free_space_header *header;
711 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700712 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400713 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400714 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800715 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400716 u64 num_entries;
717 u64 num_bitmaps;
718 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400719 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400720 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400721
Josef Bacik9d66e232010-08-25 16:54:15 -0400722 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800723 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400724 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400725
726 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800727 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400728 key.type = 0;
729
730 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800731 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400732 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800733 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400734 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400735 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400736 }
737
Li Zefan0414efa2011-04-20 10:20:14 +0800738 ret = -1;
739
Josef Bacik9d66e232010-08-25 16:54:15 -0400740 leaf = path->nodes[0];
741 header = btrfs_item_ptr(leaf, path->slots[0],
742 struct btrfs_free_space_header);
743 num_entries = btrfs_free_space_entries(leaf, header);
744 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
745 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400746 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400747
Miao Xiee570fd22014-06-19 10:42:50 +0800748 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400749 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200750 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800751 offset);
752 return 0;
753 }
754
Josef Bacik9d66e232010-08-25 16:54:15 -0400755 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400756 btrfs_err(fs_info,
757 "free space inode generation (%llu) did not match free space cache generation (%llu)",
758 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400759 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400760 }
761
762 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400763 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400764
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400765 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800766 if (ret)
767 return ret;
768
David Sterba1d480532017-01-23 17:28:19 +0100769 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400770
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900771 ret = io_ctl_prepare_pages(&io_ctl, true);
Josef Bacika67509c2011-10-05 15:18:58 -0400772 if (ret)
773 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400774
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400775 ret = io_ctl_check_crc(&io_ctl, 0);
776 if (ret)
777 goto free_cache;
778
Josef Bacika67509c2011-10-05 15:18:58 -0400779 ret = io_ctl_check_generation(&io_ctl, generation);
780 if (ret)
781 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400782
Josef Bacika67509c2011-10-05 15:18:58 -0400783 while (num_entries) {
784 e = kmem_cache_zalloc(btrfs_free_space_cachep,
785 GFP_NOFS);
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800786 if (!e) {
787 ret = -ENOMEM;
Josef Bacik9d66e232010-08-25 16:54:15 -0400788 goto free_cache;
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800789 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400790
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400791 ret = io_ctl_read_entry(&io_ctl, e, &type);
792 if (ret) {
793 kmem_cache_free(btrfs_free_space_cachep, e);
794 goto free_cache;
795 }
796
Josef Bacika67509c2011-10-05 15:18:58 -0400797 if (!e->bytes) {
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800798 ret = -1;
Josef Bacika67509c2011-10-05 15:18:58 -0400799 kmem_cache_free(btrfs_free_space_cachep, e);
800 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400801 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400802
Josef Bacika67509c2011-10-05 15:18:58 -0400803 if (type == BTRFS_FREE_SPACE_EXTENT) {
804 spin_lock(&ctl->tree_lock);
805 ret = link_free_space(ctl, e);
806 spin_unlock(&ctl->tree_lock);
807 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400808 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000809 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500810 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400811 goto free_cache;
812 }
Josef Bacika67509c2011-10-05 15:18:58 -0400813 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400814 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400815 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000816 e->bitmap = kmem_cache_zalloc(
817 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400818 if (!e->bitmap) {
Zhihao Cheng3cc64e72020-11-20 09:08:04 +0800819 ret = -ENOMEM;
Josef Bacika67509c2011-10-05 15:18:58 -0400820 kmem_cache_free(
821 btrfs_free_space_cachep, e);
822 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400823 }
Josef Bacika67509c2011-10-05 15:18:58 -0400824 spin_lock(&ctl->tree_lock);
825 ret = link_free_space(ctl, e);
826 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +0100827 recalculate_thresholds(ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400828 spin_unlock(&ctl->tree_lock);
829 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400830 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000831 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400832 kmem_cache_free(btrfs_free_space_cachep, e);
833 goto free_cache;
834 }
835 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400836 }
837
Josef Bacika67509c2011-10-05 15:18:58 -0400838 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400839 }
840
Josef Bacik2f120c02011-11-10 20:45:05 -0500841 io_ctl_unmap_page(&io_ctl);
842
Josef Bacika67509c2011-10-05 15:18:58 -0400843 /*
844 * We add the bitmaps at the end of the entries in order that
845 * the bitmap entries are added to the cache.
846 */
847 list_for_each_entry_safe(e, n, &bitmaps, list) {
848 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400849 ret = io_ctl_read_bitmap(&io_ctl, e);
850 if (ret)
851 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400852 }
853
854 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400855 ret = 1;
856out:
Josef Bacika67509c2011-10-05 15:18:58 -0400857 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400858 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400859free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400860 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800861 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400862 goto out;
863}
864
Josef Bacikcd799092020-10-23 09:58:08 -0400865static int copy_free_space_cache(struct btrfs_block_group *block_group,
866 struct btrfs_free_space_ctl *ctl)
867{
868 struct btrfs_free_space *info;
869 struct rb_node *n;
870 int ret = 0;
871
872 while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
873 info = rb_entry(n, struct btrfs_free_space, offset_index);
874 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +0200875 unlink_free_space(ctl, info, true);
Josef Bacikcd799092020-10-23 09:58:08 -0400876 ret = btrfs_add_free_space(block_group, info->offset,
877 info->bytes);
878 kmem_cache_free(btrfs_free_space_cachep, info);
879 } else {
880 u64 offset = info->offset;
881 u64 bytes = ctl->unit;
882
883 while (search_bitmap(ctl, info, &offset, &bytes,
884 false) == 0) {
885 ret = btrfs_add_free_space(block_group, offset,
886 bytes);
887 if (ret)
888 break;
Nikolay Borisovf594f132021-11-23 14:44:19 +0200889 bitmap_clear_bits(ctl, info, offset, bytes, true);
Josef Bacikcd799092020-10-23 09:58:08 -0400890 offset = info->offset;
891 bytes = ctl->unit;
892 }
893 free_bitmap(ctl, info);
894 }
895 cond_resched();
896 }
897 return ret;
898}
899
David Sterba32da53862019-10-29 19:20:18 +0100900int load_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400901{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100902 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800903 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacikcd799092020-10-23 09:58:08 -0400904 struct btrfs_free_space_ctl tmp_ctl = {};
Li Zefan0414efa2011-04-20 10:20:14 +0800905 struct inode *inode;
906 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400907 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800908 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200909 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800910
911 /*
Josef Bacikcd799092020-10-23 09:58:08 -0400912 * Because we could potentially discard our loaded free space, we want
913 * to load everything into a temporary structure first, and then if it's
914 * valid copy it all into the actual free space ctl.
915 */
916 btrfs_init_free_space_ctl(block_group, &tmp_ctl);
917
918 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800919 * If this block group has been marked to be cleared for one reason or
920 * another then we can't trust the on disk cache, so just return.
921 */
922 spin_lock(&block_group->lock);
923 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
924 spin_unlock(&block_group->lock);
925 return 0;
926 }
927 spin_unlock(&block_group->lock);
928
929 path = btrfs_alloc_path();
930 if (!path)
931 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400932 path->search_commit_root = 1;
933 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800934
Filipe Manana4222ea72018-10-24 10:13:03 +0100935 /*
936 * We must pass a path with search_commit_root set to btrfs_iget in
937 * order to avoid a deadlock when allocating extents for the tree root.
938 *
939 * When we are COWing an extent buffer from the tree root, when looking
940 * for a free extent, at extent-tree.c:find_free_extent(), we can find
941 * block group without its free space cache loaded. When we find one
942 * we must load its space cache which requires reading its free space
943 * cache's inode item from the root tree. If this inode item is located
944 * in the same leaf that we started COWing before, then we end up in
945 * deadlock on the extent buffer (trying to read lock it when we
946 * previously write locked it).
947 *
948 * It's safe to read the inode item using the commit root because
949 * block groups, once loaded, stay in memory forever (until they are
950 * removed) as well as their space caches once loaded. New block groups
951 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
952 * we will never try to read their inode item while the fs is mounted.
953 */
David Sterba7949f332019-03-20 13:40:19 +0100954 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800955 if (IS_ERR(inode)) {
956 btrfs_free_path(path);
957 return 0;
958 }
959
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400960 /* We may have converted the inode and made the cache invalid. */
961 spin_lock(&block_group->lock);
962 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
963 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900964 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400965 goto out;
966 }
967 spin_unlock(&block_group->lock);
968
Josef Bacikcd799092020-10-23 09:58:08 -0400969 ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200970 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800971 btrfs_free_path(path);
972 if (ret <= 0)
973 goto out;
974
Josef Bacikcd799092020-10-23 09:58:08 -0400975 matched = (tmp_ctl.free_space == (block_group->length - used -
976 block_group->bytes_super));
Li Zefan0414efa2011-04-20 10:20:14 +0800977
Josef Bacikcd799092020-10-23 09:58:08 -0400978 if (matched) {
979 ret = copy_free_space_cache(block_group, &tmp_ctl);
980 /*
981 * ret == 1 means we successfully loaded the free space cache,
982 * so we need to re-set it here.
983 */
984 if (ret == 0)
985 ret = 1;
986 } else {
987 __btrfs_remove_free_space_cache(&tmp_ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400988 btrfs_warn(fs_info,
989 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +0200990 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800991 ret = -1;
992 }
993out:
994 if (ret < 0) {
995 /* This cache is bogus, make sure it gets cleared */
996 spin_lock(&block_group->lock);
997 block_group->disk_cache_state = BTRFS_DC_CLEAR;
998 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800999 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +08001000
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001001 btrfs_warn(fs_info,
1002 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +02001003 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +08001004 }
1005
Josef Bacik66b53ba2020-10-23 09:58:07 -04001006 spin_lock(&ctl->tree_lock);
1007 btrfs_discard_update_discardable(block_group);
1008 spin_unlock(&ctl->tree_lock);
Li Zefan0414efa2011-04-20 10:20:14 +08001009 iput(inode);
1010 return ret;
1011}
1012
Chris Masond4452bc2014-05-19 20:47:56 -07001013static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -07001014int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001015 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001016 struct btrfs_block_group *block_group,
Chris Masond4452bc2014-05-19 20:47:56 -07001017 int *entries, int *bitmaps,
1018 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001019{
Josef Bacikc09544e2011-08-30 10:19:10 -04001020 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001021 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -07001022 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -07001023 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +00001024 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001025
Josef Bacik43be2142011-04-01 14:55:00 +00001026 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -07001027 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +00001028 cluster = list_entry(block_group->cluster_list.next,
1029 struct btrfs_free_cluster,
1030 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001031 }
Josef Bacik43be2142011-04-01 14:55:00 +00001032
Josef Bacikf75b1302011-10-05 10:00:18 -04001033 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001034 cluster_locked = cluster;
1035 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -04001036 node = rb_first(&cluster->root);
1037 cluster = NULL;
1038 }
1039
Josef Bacik0cb59c92010-07-02 12:14:14 -04001040 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -04001041 while (node) {
1042 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001043
Josef Bacika67509c2011-10-05 15:18:58 -04001044 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -07001045 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +00001046
Chris Masond4452bc2014-05-19 20:47:56 -07001047 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -04001048 e->bitmap);
1049 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -07001050 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -04001051
1052 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -07001053 list_add_tail(&e->list, bitmap_list);
1054 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -04001055 }
1056 node = rb_next(node);
1057 if (!node && cluster) {
1058 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -07001059 cluster_locked = cluster;
1060 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -04001061 cluster = NULL;
1062 }
1063 }
Chris Mason1bbc6212015-04-06 12:46:08 -07001064 if (cluster_locked) {
1065 spin_unlock(&cluster_locked->lock);
1066 cluster_locked = NULL;
1067 }
Filipe Manana55507ce2014-12-01 17:04:09 +00001068
1069 /*
1070 * Make sure we don't miss any range that was removed from our rbtree
1071 * because trimming is running. Otherwise after a umount+mount (or crash
1072 * after committing the transaction) we would leak free space and get
1073 * an inconsistent free space cache report from fsck.
1074 */
1075 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
1076 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
1077 trim_entry->bytes, NULL);
1078 if (ret)
1079 goto fail;
1080 *entries += 1;
1081 }
1082
Chris Masond4452bc2014-05-19 20:47:56 -07001083 return 0;
1084fail:
Chris Mason1bbc6212015-04-06 12:46:08 -07001085 if (cluster_locked)
1086 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -07001087 return -ENOSPC;
1088}
1089
1090static noinline_for_stack int
1091update_cache_item(struct btrfs_trans_handle *trans,
1092 struct btrfs_root *root,
1093 struct inode *inode,
1094 struct btrfs_path *path, u64 offset,
1095 int entries, int bitmaps)
1096{
1097 struct btrfs_key key;
1098 struct btrfs_free_space_header *header;
1099 struct extent_buffer *leaf;
1100 int ret;
1101
1102 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1103 key.offset = offset;
1104 key.type = 0;
1105
1106 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1107 if (ret < 0) {
1108 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001109 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001110 goto fail;
1111 }
1112 leaf = path->nodes[0];
1113 if (ret > 0) {
1114 struct btrfs_key found_key;
1115 ASSERT(path->slots[0]);
1116 path->slots[0]--;
1117 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1118 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1119 found_key.offset != offset) {
1120 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001121 inode->i_size - 1, EXTENT_DELALLOC, 0,
1122 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001123 btrfs_release_path(path);
1124 goto fail;
1125 }
1126 }
1127
1128 BTRFS_I(inode)->generation = trans->transid;
1129 header = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_free_space_header);
1131 btrfs_set_free_space_entries(leaf, header, entries);
1132 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1133 btrfs_set_free_space_generation(leaf, header, trans->transid);
1134 btrfs_mark_buffer_dirty(leaf);
1135 btrfs_release_path(path);
1136
1137 return 0;
1138
1139fail:
1140 return -1;
1141}
1142
David Sterba6701bdb2019-03-20 13:49:09 +01001143static noinline_for_stack int write_pinned_extent_entries(
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001144 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001145 struct btrfs_block_group *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001146 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001147 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001148{
1149 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001150 struct extent_io_tree *unpin = NULL;
1151 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001152
Miao Xie5349d6c2014-06-19 10:42:49 +08001153 if (!block_group)
1154 return 0;
1155
Josef Bacika67509c2011-10-05 15:18:58 -04001156 /*
1157 * We want to add any pinned extents to our free space cache
1158 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001159 *
Li Zefandb804f22012-01-10 16:41:01 +08001160 * We shouldn't have switched the pinned extents yet so this is the
1161 * right one
1162 */
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001163 unpin = &trans->transaction->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001164
David Sterbab3470b52019-10-23 18:48:22 +02001165 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001166
David Sterbab3470b52019-10-23 18:48:22 +02001167 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001168 ret = find_first_extent_bit(unpin, start,
1169 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001170 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001171 if (ret)
1172 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001173
Josef Bacika67509c2011-10-05 15:18:58 -04001174 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001175 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001176 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001177
Li Zefandb804f22012-01-10 16:41:01 +08001178 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001179 extent_end = min(block_group->start + block_group->length,
1180 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001181 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001182
Chris Masond4452bc2014-05-19 20:47:56 -07001183 *entries += 1;
1184 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001185 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001186 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001187
Li Zefandb804f22012-01-10 16:41:01 +08001188 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001189 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001190
Miao Xie5349d6c2014-06-19 10:42:49 +08001191 return 0;
1192}
1193
1194static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001195write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001196{
Geliang Tang7ae16812015-12-18 22:17:00 +08001197 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001198 int ret;
1199
Josef Bacik0cb59c92010-07-02 12:14:14 -04001200 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001201 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001202 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001203 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001204 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001205 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001206 }
1207
Miao Xie5349d6c2014-06-19 10:42:49 +08001208 return 0;
1209}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001210
Miao Xie5349d6c2014-06-19 10:42:49 +08001211static int flush_dirty_cache(struct inode *inode)
1212{
1213 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001214
Josef Bacik0ef8b722013-10-25 16:13:35 -04001215 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001216 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001217 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001218 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001219
Miao Xie5349d6c2014-06-19 10:42:49 +08001220 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001221}
1222
1223static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001224cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001225{
Geliang Tang7ae16812015-12-18 22:17:00 +08001226 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001227
Geliang Tang7ae16812015-12-18 22:17:00 +08001228 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001229 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001230}
1231
1232static void noinline_for_stack
1233cleanup_write_cache_enospc(struct inode *inode,
1234 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001235 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001236{
Chris Masond4452bc2014-05-19 20:47:56 -07001237 io_ctl_drop_pages(io_ctl);
1238 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001239 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001240}
1241
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001242static int __btrfs_wait_cache_io(struct btrfs_root *root,
1243 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001244 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001245 struct btrfs_io_ctl *io_ctl,
1246 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001247{
1248 int ret;
1249 struct inode *inode = io_ctl->inode;
1250
Chris Mason1bbc6212015-04-06 12:46:08 -07001251 if (!inode)
1252 return 0;
1253
Chris Masonc9dc4c62015-04-04 17:14:42 -07001254 /* Flush the dirty pages in the cache file. */
1255 ret = flush_dirty_cache(inode);
1256 if (ret)
1257 goto out;
1258
1259 /* Update the cache item to tell everyone this cache file is valid. */
1260 ret = update_cache_item(trans, root, inode, path, offset,
1261 io_ctl->entries, io_ctl->bitmaps);
1262out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001263 if (ret) {
1264 invalidate_inode_pages2(inode->i_mapping);
1265 BTRFS_I(inode)->generation = 0;
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001266 if (block_group)
1267 btrfs_debug(root->fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001268 "failed to write free space cache for block group %llu error %d",
1269 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001270 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001271 btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonc9dc4c62015-04-04 17:14:42 -07001272
1273 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001274 /* the dirty list is protected by the dirty_bgs_lock */
1275 spin_lock(&trans->transaction->dirty_bgs_lock);
1276
1277 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001278 spin_lock(&block_group->lock);
1279
1280 /*
1281 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001282 * the dirty list while waiting for IO. Otherwise our
1283 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001284 */
1285 if (!ret && list_empty(&block_group->dirty_list))
1286 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1287 else if (ret)
1288 block_group->disk_cache_state = BTRFS_DC_ERROR;
1289
1290 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001291 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001292 io_ctl->inode = NULL;
1293 iput(inode);
1294 }
1295
1296 return ret;
1297
1298}
1299
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001300int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001301 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001302 struct btrfs_path *path)
1303{
1304 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1305 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001306 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001307}
1308
Chris Masond4452bc2014-05-19 20:47:56 -07001309/**
Nikolay Borisovf092cf32021-01-22 11:57:56 +02001310 * Write out cached info to an inode
1311 *
1312 * @root: root the inode belongs to
1313 * @inode: freespace inode we are writing out
1314 * @ctl: free space cache we are going to write out
1315 * @block_group: block_group for this cache if it belongs to a block_group
1316 * @io_ctl: holds context for the io
1317 * @trans: the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001318 *
1319 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001320 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001321 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001322 */
1323static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1324 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001325 struct btrfs_block_group *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001326 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001327 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001328{
1329 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001330 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001331 int entries = 0;
1332 int bitmaps = 0;
1333 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001334 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001335
1336 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001337 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001338
Chris Masonc9dc4c62015-04-04 17:14:42 -07001339 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001340 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001341 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001342 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001343
Miao Xiee570fd22014-06-19 10:42:50 +08001344 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1345 down_write(&block_group->data_rwsem);
1346 spin_lock(&block_group->lock);
1347 if (block_group->delalloc_bytes) {
1348 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1349 spin_unlock(&block_group->lock);
1350 up_write(&block_group->data_rwsem);
1351 BTRFS_I(inode)->generation = 0;
1352 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001353 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001354 goto out;
1355 }
1356 spin_unlock(&block_group->lock);
1357 }
1358
Chris Masond4452bc2014-05-19 20:47:56 -07001359 /* Lock all pages first so we can lock the extent safely. */
Johannes Thumshirn7a195f62020-02-12 00:10:20 +09001360 ret = io_ctl_prepare_pages(io_ctl, false);
Omar Sandovalb8605452015-02-24 02:47:06 -08001361 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001362 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001363
1364 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001365 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001366
Chris Masonc9dc4c62015-04-04 17:14:42 -07001367 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001368
Filipe Manana55507ce2014-12-01 17:04:09 +00001369 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001370 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001371 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001372 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001373 block_group, &entries, &bitmaps,
1374 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001375 if (ret)
1376 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001377
Miao Xie5349d6c2014-06-19 10:42:49 +08001378 /*
1379 * Some spaces that are freed in the current transaction are pinned,
1380 * they will be added into free space cache after the transaction is
1381 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001382 *
1383 * If this changes while we are working we'll get added back to
1384 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001385 */
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001386 ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001387 if (ret)
1388 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001389
Filipe Manana55507ce2014-12-01 17:04:09 +00001390 /*
1391 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1392 * locked while doing it because a concurrent trim can be manipulating
1393 * or freeing the bitmap.
1394 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001395 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001396 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001397 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001398 if (ret)
1399 goto out_nospc;
1400
1401 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001402 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001403
1404 /* Everything is written out, now we dirty the pages in the file. */
Nikolay Borisov088545f2020-06-03 08:55:36 +03001405 ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
1406 io_ctl->num_pages, 0, i_size_read(inode),
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -05001407 &cached_state, false);
Miao Xie5349d6c2014-06-19 10:42:49 +08001408 if (ret)
1409 goto out_nospc;
1410
Miao Xiee570fd22014-06-19 10:42:50 +08001411 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1412 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001413 /*
1414 * Release the pages and unlock the extent, we will flush
1415 * them out later
1416 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001417 io_ctl_drop_pages(io_ctl);
Filipe Mananabbc37d62020-08-14 11:04:09 +01001418 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001419
1420 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001421 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001422
Chris Masonc9dc4c62015-04-04 17:14:42 -07001423 /*
1424 * at this point the pages are under IO and we're happy,
Randy Dunlap260db432020-08-04 19:48:34 -07001425 * The caller is responsible for waiting on them and updating
Chris Masonc9dc4c62015-04-04 17:14:42 -07001426 * the cache and the inode
1427 */
1428 io_ctl->entries = entries;
1429 io_ctl->bitmaps = bitmaps;
1430
1431 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001432 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001433 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001434
Chris Masonc9dc4c62015-04-04 17:14:42 -07001435 return 0;
1436
Chris Masona3bdccc2015-04-24 11:00:00 -07001437out_nospc_locked:
1438 cleanup_bitmap_list(&bitmap_list);
1439 spin_unlock(&ctl->tree_lock);
1440 mutex_unlock(&ctl->cache_writeout_mutex);
1441
Josef Bacika67509c2011-10-05 15:18:58 -04001442out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001443 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001444
Josef Bacikb77000e2017-11-15 16:20:52 -05001445out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001446 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1447 up_write(&block_group->data_rwsem);
1448
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001449out:
1450 io_ctl->inode = NULL;
1451 io_ctl_free(io_ctl);
1452 if (ret) {
1453 invalidate_inode_pages2(inode->i_mapping);
1454 BTRFS_I(inode)->generation = 0;
1455 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001456 btrfs_update_inode(trans, root, BTRFS_I(inode));
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001457 if (must_iput)
1458 iput(inode);
1459 return ret;
Li Zefan0414efa2011-04-20 10:20:14 +08001460}
1461
David Sterbafe041532019-03-20 13:51:56 +01001462int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001463 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +08001464 struct btrfs_path *path)
1465{
David Sterbafe041532019-03-20 13:51:56 +01001466 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001467 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1468 struct inode *inode;
1469 int ret = 0;
1470
Li Zefan0414efa2011-04-20 10:20:14 +08001471 spin_lock(&block_group->lock);
1472 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1473 spin_unlock(&block_group->lock);
1474 return 0;
1475 }
1476 spin_unlock(&block_group->lock);
1477
David Sterba7949f332019-03-20 13:40:19 +01001478 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001479 if (IS_ERR(inode))
1480 return 0;
1481
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001482 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1483 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001484 if (ret) {
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001485 btrfs_debug(fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001486 "failed to write free space cache for block group %llu error %d",
1487 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001488 spin_lock(&block_group->lock);
1489 block_group->disk_cache_state = BTRFS_DC_ERROR;
1490 spin_unlock(&block_group->lock);
1491
1492 block_group->io_ctl.inode = NULL;
1493 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001494 }
1495
Chris Masonc9dc4c62015-04-04 17:14:42 -07001496 /*
1497 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1498 * to wait for IO and put the inode
1499 */
1500
Josef Bacik0cb59c92010-07-02 12:14:14 -04001501 return ret;
1502}
1503
Li Zefan34d52cb2011-03-29 13:46:06 +08001504static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001505 u64 offset)
1506{
Josef Bacikb12d6862013-08-26 17:14:08 -04001507 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001508 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001509 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001510}
1511
Li Zefan34d52cb2011-03-29 13:46:06 +08001512static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001513{
Li Zefan34d52cb2011-03-29 13:46:06 +08001514 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001515}
1516
Li Zefan34d52cb2011-03-29 13:46:06 +08001517static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001518 u64 offset)
1519{
1520 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001521 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001522
Li Zefan34d52cb2011-03-29 13:46:06 +08001523 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1524 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001525 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001526 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001527 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001528
1529 return bitmap_start;
1530}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001531
1532static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001533 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001534{
1535 struct rb_node **p = &root->rb_node;
1536 struct rb_node *parent = NULL;
1537 struct btrfs_free_space *info;
1538
1539 while (*p) {
1540 parent = *p;
1541 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1542
Josef Bacik96303082009-07-13 21:29:25 -04001543 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001544 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001545 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001546 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001547 } else {
1548 /*
1549 * we could have a bitmap entry and an extent entry
1550 * share the same offset. If this is the case, we want
1551 * the extent entry to always be found first if we do a
1552 * linear search through the tree, since we want to have
1553 * the quickest allocation time, and allocating from an
1554 * extent is faster than allocating from a bitmap. So
1555 * if we're inserting a bitmap and we find an entry at
1556 * this offset, we want to go right, or after this entry
1557 * logically. If we are inserting an extent and we've
1558 * found a bitmap, we want to go left, or before
1559 * logically.
1560 */
1561 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001562 if (info->bitmap) {
1563 WARN_ON_ONCE(1);
1564 return -EEXIST;
1565 }
Josef Bacik96303082009-07-13 21:29:25 -04001566 p = &(*p)->rb_right;
1567 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001568 if (!info->bitmap) {
1569 WARN_ON_ONCE(1);
1570 return -EEXIST;
1571 }
Josef Bacik96303082009-07-13 21:29:25 -04001572 p = &(*p)->rb_left;
1573 }
1574 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001575 }
1576
1577 rb_link_node(node, parent, p);
1578 rb_insert_color(node, root);
1579
1580 return 0;
1581}
1582
1583/*
Josef Bacik59c7b562021-11-18 16:33:15 -05001584 * This is a little subtle. We *only* have ->max_extent_size set if we actually
1585 * searched through the bitmap and figured out the largest ->max_extent_size,
1586 * otherwise it's 0. In the case that it's 0 we don't want to tell the
1587 * allocator the wrong thing, we want to use the actual real max_extent_size
1588 * we've found already if it's larger, or we want to use ->bytes.
1589 *
1590 * This matters because find_free_space() will skip entries who's ->bytes is
1591 * less than the required bytes. So if we didn't search down this bitmap, we
1592 * may pick some previous entry that has a smaller ->max_extent_size than we
1593 * have. For example, assume we have two entries, one that has
1594 * ->max_extent_size set to 4K and ->bytes set to 1M. A second entry hasn't set
1595 * ->max_extent_size yet, has ->bytes set to 8K and it's contiguous. We will
1596 * call into find_free_space(), and return with max_extent_size == 4K, because
1597 * that first bitmap entry had ->max_extent_size set, but the second one did
1598 * not. If instead we returned 8K we'd come in searching for 8K, and find the
1599 * 8K contiguous range.
1600 *
1601 * Consider the other case, we have 2 8K chunks in that second entry and still
1602 * don't have ->max_extent_size set. We'll return 16K, and the next time the
1603 * allocator comes in it'll fully search our second bitmap, and this time it'll
1604 * get an uptodate value of 8K as the maximum chunk size. Then we'll get the
1605 * right allocation the next loop through.
1606 */
1607static inline u64 get_max_extent_size(const struct btrfs_free_space *entry)
1608{
1609 if (entry->bitmap && entry->max_extent_size)
1610 return entry->max_extent_size;
1611 return entry->bytes;
1612}
1613
1614/*
1615 * We want the largest entry to be leftmost, so this is inverted from what you'd
1616 * normally expect.
1617 */
1618static bool entry_less(struct rb_node *node, const struct rb_node *parent)
1619{
1620 const struct btrfs_free_space *entry, *exist;
1621
1622 entry = rb_entry(node, struct btrfs_free_space, bytes_index);
1623 exist = rb_entry(parent, struct btrfs_free_space, bytes_index);
1624 return get_max_extent_size(exist) < get_max_extent_size(entry);
1625}
1626
1627/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001628 * searches the tree for the given offset.
1629 *
Josef Bacik96303082009-07-13 21:29:25 -04001630 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1631 * want a section that has at least bytes size and comes at or after the given
1632 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001633 */
Josef Bacik96303082009-07-13 21:29:25 -04001634static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001635tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001636 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001637{
Li Zefan34d52cb2011-03-29 13:46:06 +08001638 struct rb_node *n = ctl->free_space_offset.rb_node;
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001639 struct btrfs_free_space *entry = NULL, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001640
Josef Bacik96303082009-07-13 21:29:25 -04001641 /* find entry that is closest to the 'offset' */
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001642 while (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001643 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1644 prev = entry;
1645
1646 if (offset < entry->offset)
1647 n = n->rb_left;
1648 else if (offset > entry->offset)
1649 n = n->rb_right;
1650 else
1651 break;
Nikolay Borisovf1a8fc62021-11-23 09:23:42 +02001652
1653 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001654 }
1655
Josef Bacik96303082009-07-13 21:29:25 -04001656 if (bitmap_only) {
1657 if (!entry)
1658 return NULL;
1659 if (entry->bitmap)
1660 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001661
Josef Bacik96303082009-07-13 21:29:25 -04001662 /*
1663 * bitmap entry and extent entry may share same offset,
1664 * in that case, bitmap entry comes after extent entry.
1665 */
1666 n = rb_next(n);
1667 if (!n)
1668 return NULL;
1669 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1670 if (entry->offset != offset)
1671 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001672
Josef Bacik96303082009-07-13 21:29:25 -04001673 WARN_ON(!entry->bitmap);
1674 return entry;
1675 } else if (entry) {
1676 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001677 /*
Josef Bacik96303082009-07-13 21:29:25 -04001678 * if previous extent entry covers the offset,
1679 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001680 */
Miao Xiede6c4112012-10-18 08:18:01 +00001681 n = rb_prev(&entry->offset_index);
1682 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001683 prev = rb_entry(n, struct btrfs_free_space,
1684 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001685 if (!prev->bitmap &&
1686 prev->offset + prev->bytes > offset)
1687 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001688 }
Josef Bacik96303082009-07-13 21:29:25 -04001689 }
1690 return entry;
1691 }
1692
1693 if (!prev)
1694 return NULL;
1695
1696 /* find last entry before the 'offset' */
1697 entry = prev;
1698 if (entry->offset > offset) {
1699 n = rb_prev(&entry->offset_index);
1700 if (n) {
1701 entry = rb_entry(n, struct btrfs_free_space,
1702 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001703 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001704 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001705 if (fuzzy)
1706 return entry;
1707 else
1708 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001709 }
1710 }
1711
Josef Bacik96303082009-07-13 21:29:25 -04001712 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001713 n = rb_prev(&entry->offset_index);
1714 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001715 prev = rb_entry(n, struct btrfs_free_space,
1716 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001717 if (!prev->bitmap &&
1718 prev->offset + prev->bytes > offset)
1719 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001720 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001721 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001722 return entry;
1723 } else if (entry->offset + entry->bytes > offset)
1724 return entry;
1725
1726 if (!fuzzy)
1727 return NULL;
1728
1729 while (1) {
Nikolay Borisov167c0bd2021-11-22 17:16:46 +02001730 n = rb_next(&entry->offset_index);
1731 if (!n)
1732 return NULL;
1733 entry = rb_entry(n, struct btrfs_free_space, offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04001734 if (entry->bitmap) {
1735 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001736 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001737 break;
1738 } else {
1739 if (entry->offset + entry->bytes > offset)
1740 break;
1741 }
Josef Bacik96303082009-07-13 21:29:25 -04001742 }
1743 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001744}
1745
Nikolay Borisov32e16492021-11-23 14:44:20 +02001746static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1747 struct btrfs_free_space *info,
1748 bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001749{
Li Zefan34d52cb2011-03-29 13:46:06 +08001750 rb_erase(&info->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05001751 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001752 ctl->free_extents--;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001753
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001754 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001755 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001756 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
1757 }
Li Zefanf333adb2010-11-09 14:57:39 +08001758
Nikolay Borisov32e16492021-11-23 14:44:20 +02001759 if (update_stat)
1760 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001761}
1762
Li Zefan34d52cb2011-03-29 13:46:06 +08001763static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001764 struct btrfs_free_space *info)
1765{
1766 int ret = 0;
1767
Josef Bacikb12d6862013-08-26 17:14:08 -04001768 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001769 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001770 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001771 if (ret)
1772 return ret;
1773
Josef Bacik59c7b562021-11-18 16:33:15 -05001774 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
1775
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001776 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001777 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001778 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
1779 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001780
Li Zefan34d52cb2011-03-29 13:46:06 +08001781 ctl->free_space += info->bytes;
1782 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001783 return ret;
1784}
1785
Josef Bacik59c7b562021-11-18 16:33:15 -05001786static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl,
1787 struct btrfs_free_space *info)
1788{
1789 ASSERT(info->bitmap);
1790
1791 /*
1792 * If our entry is empty it's because we're on a cluster and we don't
1793 * want to re-link it into our ctl bytes index.
1794 */
1795 if (RB_EMPTY_NODE(&info->bytes_index))
1796 return;
1797
1798 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
1799 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
1800}
1801
Nikolay Borisovf594f132021-11-23 14:44:19 +02001802static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1803 struct btrfs_free_space *info,
1804 u64 offset, u64 bytes, bool update_stat)
Josef Bacik96303082009-07-13 21:29:25 -04001805{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001806 unsigned long start, count, end;
1807 int extent_delta = -1;
Josef Bacik96303082009-07-13 21:29:25 -04001808
Li Zefan34d52cb2011-03-29 13:46:06 +08001809 start = offset_to_bit(info->offset, ctl->unit, offset);
1810 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001811 end = start + count;
1812 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001813
Li Zefanf38b6e72011-03-14 13:40:51 +08001814 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001815
1816 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001817 if (info->max_extent_size > ctl->unit)
1818 info->max_extent_size = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001819
Josef Bacik59c7b562021-11-18 16:33:15 -05001820 relink_bitmap_entry(ctl, info);
1821
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001822 if (start && test_bit(start - 1, info->bitmap))
1823 extent_delta++;
1824
1825 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1826 extent_delta++;
1827
1828 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001829 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001830 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001831 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
1832 }
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001833
Nikolay Borisovf594f132021-11-23 14:44:19 +02001834 if (update_stat)
1835 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001836}
1837
Li Zefan34d52cb2011-03-29 13:46:06 +08001838static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001839 struct btrfs_free_space *info, u64 offset,
1840 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001841{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001842 unsigned long start, count, end;
1843 int extent_delta = 1;
Josef Bacik96303082009-07-13 21:29:25 -04001844
Li Zefan34d52cb2011-03-29 13:46:06 +08001845 start = offset_to_bit(info->offset, ctl->unit, offset);
1846 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001847 end = start + count;
1848 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001849
Li Zefanf38b6e72011-03-14 13:40:51 +08001850 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001851
Josef Bacik59c7b562021-11-18 16:33:15 -05001852 /*
1853 * We set some bytes, we have no idea what the max extent size is
1854 * anymore.
1855 */
1856 info->max_extent_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001857 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001858 ctl->free_space += bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001859
Josef Bacik59c7b562021-11-18 16:33:15 -05001860 relink_bitmap_entry(ctl, info);
1861
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001862 if (start && test_bit(start - 1, info->bitmap))
1863 extent_delta--;
1864
1865 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1866 extent_delta--;
1867
1868 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001869 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001870 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001871 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
1872 }
Josef Bacik96303082009-07-13 21:29:25 -04001873}
1874
Miao Xiea4820392013-09-09 13:19:42 +08001875/*
1876 * If we can not find suitable extent, we will use bytes to record
1877 * the size of the max extent.
1878 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001879static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001880 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001881 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001882{
1883 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001884 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001885 unsigned long bits, i;
1886 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001887 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001888
Josef Bacikcef40482015-10-02 16:09:42 -04001889 /*
1890 * Skip searching the bitmap if we don't have a contiguous section that
1891 * is large enough for this allocation.
1892 */
Josef Bacik0584f712015-10-02 16:12:23 -04001893 if (for_alloc &&
1894 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001895 bitmap_info->max_extent_size < *bytes) {
1896 *bytes = bitmap_info->max_extent_size;
1897 return -1;
1898 }
1899
Li Zefan34d52cb2011-03-29 13:46:06 +08001900 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001901 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001902 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001903
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001904 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001905 if (for_alloc && bits == 1) {
1906 found_bits = 1;
1907 break;
1908 }
Josef Bacik96303082009-07-13 21:29:25 -04001909 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1910 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001911 extent_bits = next_zero - i;
1912 if (extent_bits >= bits) {
1913 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001914 break;
Miao Xiea4820392013-09-09 13:19:42 +08001915 } else if (extent_bits > max_bits) {
1916 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001917 }
1918 i = next_zero;
1919 }
1920
1921 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001922 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1923 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001924 return 0;
1925 }
1926
Miao Xiea4820392013-09-09 13:19:42 +08001927 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001928 bitmap_info->max_extent_size = *bytes;
Josef Bacik59c7b562021-11-18 16:33:15 -05001929 relink_bitmap_entry(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001930 return -1;
1931}
1932
Miao Xiea4820392013-09-09 13:19:42 +08001933/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001934static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001935find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Josef Bacik59c7b562021-11-18 16:33:15 -05001936 unsigned long align, u64 *max_extent_size, bool use_bytes_index)
Josef Bacik96303082009-07-13 21:29:25 -04001937{
1938 struct btrfs_free_space *entry;
1939 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001940 u64 tmp;
1941 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001942 int ret;
1943
Li Zefan34d52cb2011-03-29 13:46:06 +08001944 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001945 goto out;
Josef Bacik59c7b562021-11-18 16:33:15 -05001946again:
1947 if (use_bytes_index) {
1948 node = rb_first_cached(&ctl->free_space_bytes);
1949 } else {
1950 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset),
1951 0, 1);
1952 if (!entry)
1953 goto out;
1954 node = &entry->offset_index;
1955 }
Josef Bacik96303082009-07-13 21:29:25 -04001956
Josef Bacik59c7b562021-11-18 16:33:15 -05001957 for (; node; node = rb_next(node)) {
1958 if (use_bytes_index)
1959 entry = rb_entry(node, struct btrfs_free_space,
1960 bytes_index);
1961 else
1962 entry = rb_entry(node, struct btrfs_free_space,
1963 offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04001964
Josef Bacik59c7b562021-11-18 16:33:15 -05001965 /*
1966 * If we are using the bytes index then all subsequent entries
1967 * in this tree are going to be < bytes, so simply set the max
1968 * extent size and exit the loop.
1969 *
1970 * If we're using the offset index then we need to keep going
1971 * through the rest of the tree.
1972 */
Miao Xiea4820392013-09-09 13:19:42 +08001973 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001974 *max_extent_size = max(get_max_extent_size(entry),
1975 *max_extent_size);
Josef Bacik59c7b562021-11-18 16:33:15 -05001976 if (use_bytes_index)
1977 break;
Josef Bacik96303082009-07-13 21:29:25 -04001978 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001979 }
Josef Bacik96303082009-07-13 21:29:25 -04001980
David Woodhouse53b381b2013-01-29 18:40:14 -05001981 /* make sure the space returned is big enough
1982 * to match our requested alignment
1983 */
1984 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001985 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001986 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001987 tmp = tmp * align + ctl->start;
1988 align_off = tmp - entry->offset;
1989 } else {
1990 align_off = 0;
1991 tmp = entry->offset;
1992 }
1993
Josef Bacik59c7b562021-11-18 16:33:15 -05001994 /*
1995 * We don't break here if we're using the bytes index because we
1996 * may have another entry that has the correct alignment that is
1997 * the right size, so we don't want to miss that possibility.
1998 * At worst this adds another loop through the logic, but if we
1999 * broke here we could prematurely ENOSPC.
2000 */
Miao Xiea4820392013-09-09 13:19:42 +08002001 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002002 *max_extent_size = max(get_max_extent_size(entry),
2003 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05002004 continue;
Miao Xiea4820392013-09-09 13:19:42 +08002005 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002006
Josef Bacik96303082009-07-13 21:29:25 -04002007 if (entry->bitmap) {
Josef Bacik59c7b562021-11-18 16:33:15 -05002008 struct rb_node *old_next = rb_next(node);
Miao Xiea4820392013-09-09 13:19:42 +08002009 u64 size = *bytes;
2010
Josef Bacik0584f712015-10-02 16:12:23 -04002011 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05002012 if (!ret) {
2013 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08002014 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04002015 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04002016 } else {
2017 *max_extent_size =
2018 max(get_max_extent_size(entry),
2019 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05002020 }
Josef Bacik59c7b562021-11-18 16:33:15 -05002021
2022 /*
2023 * The bitmap may have gotten re-arranged in the space
2024 * index here because the max_extent_size may have been
2025 * updated. Start from the beginning again if this
2026 * happened.
2027 */
2028 if (use_bytes_index && old_next != rb_next(node))
2029 goto again;
Josef Bacik96303082009-07-13 21:29:25 -04002030 continue;
2031 }
2032
David Woodhouse53b381b2013-01-29 18:40:14 -05002033 *offset = tmp;
2034 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04002035 return entry;
2036 }
Miao Xiea4820392013-09-09 13:19:42 +08002037out:
Josef Bacik96303082009-07-13 21:29:25 -04002038 return NULL;
2039}
2040
Li Zefan34d52cb2011-03-29 13:46:06 +08002041static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04002042 struct btrfs_free_space *info, u64 offset)
2043{
Li Zefan34d52cb2011-03-29 13:46:06 +08002044 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04002045 info->bytes = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002046 info->bitmap_extents = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02002047 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08002048 link_free_space(ctl, info);
2049 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +01002050 recalculate_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04002051}
2052
Li Zefan34d52cb2011-03-29 13:46:06 +08002053static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08002054 struct btrfs_free_space *bitmap_info)
2055{
Dennis Zhou27f0afc2020-01-02 16:26:45 -05002056 /*
2057 * Normally when this is called, the bitmap is completely empty. However,
2058 * if we are blowing up the free space cache for one reason or another
2059 * via __btrfs_remove_free_space_cache(), then it may not be freed and
2060 * we may leave stats on the table.
2061 */
2062 if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
2063 ctl->discardable_extents[BTRFS_STAT_CURR] -=
2064 bitmap_info->bitmap_extents;
2065 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
2066
2067 }
Nikolay Borisov32e16492021-11-23 14:44:20 +02002068 unlink_free_space(ctl, bitmap_info, true);
Christophe Leroy3acd4852019-08-21 15:05:55 +00002069 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002070 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08002071 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01002072 recalculate_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002073}
2074
Li Zefan34d52cb2011-03-29 13:46:06 +08002075static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04002076 struct btrfs_free_space *bitmap_info,
2077 u64 *offset, u64 *bytes)
2078{
2079 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04002080 u64 search_start, search_bytes;
2081 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002082
2083again:
Li Zefan34d52cb2011-03-29 13:46:06 +08002084 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04002085
Josef Bacik6606bb92009-07-31 11:03:58 -04002086 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04002087 * We need to search for bits in this bitmap. We could only cover some
2088 * of the extent in this bitmap thanks to how we add space, so we need
2089 * to search for as much as it as we can and clear that amount, and then
2090 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04002091 */
2092 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002093 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00002094 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04002095 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
2096 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002097 if (ret < 0 || search_start != *offset)
2098 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04002099
Josef Bacikbdb7d302012-06-27 15:10:56 -04002100 /* We may have found more bits than what we need */
2101 search_bytes = min(search_bytes, *bytes);
2102
2103 /* Cannot clear past the end of the bitmap */
2104 search_bytes = min(search_bytes, end - search_start + 1);
2105
Nikolay Borisovf594f132021-11-23 14:44:19 +02002106 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes, true);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002107 *offset += search_bytes;
2108 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04002109
2110 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002111 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002112 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002113 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002114
Josef Bacik6606bb92009-07-31 11:03:58 -04002115 /*
2116 * no entry after this bitmap, but we still have bytes to
2117 * remove, so something has gone wrong.
2118 */
2119 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04002120 return -EINVAL;
2121
Josef Bacik6606bb92009-07-31 11:03:58 -04002122 bitmap_info = rb_entry(next, struct btrfs_free_space,
2123 offset_index);
2124
2125 /*
2126 * if the next entry isn't a bitmap we need to return to let the
2127 * extent stuff do its work.
2128 */
Josef Bacik96303082009-07-13 21:29:25 -04002129 if (!bitmap_info->bitmap)
2130 return -EAGAIN;
2131
Josef Bacik6606bb92009-07-31 11:03:58 -04002132 /*
2133 * Ok the next item is a bitmap, but it may not actually hold
2134 * the information for the rest of this free space stuff, so
2135 * look for it, and if we don't find it return so we can try
2136 * everything over again.
2137 */
2138 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002139 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08002140 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04002141 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04002142 if (ret < 0 || search_start != *offset)
2143 return -EAGAIN;
2144
Josef Bacik96303082009-07-13 21:29:25 -04002145 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08002146 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002147 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002148
2149 return 0;
2150}
2151
Josef Bacik2cdc3422011-05-27 14:07:49 -04002152static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
2153 struct btrfs_free_space *info, u64 offset,
Dennis Zhouda080fe2019-12-13 16:22:13 -08002154 u64 bytes, enum btrfs_trim_state trim_state)
Josef Bacik2cdc3422011-05-27 14:07:49 -04002155{
2156 u64 bytes_to_set = 0;
2157 u64 end;
2158
Dennis Zhouda080fe2019-12-13 16:22:13 -08002159 /*
2160 * This is a tradeoff to make bitmap trim state minimal. We mark the
2161 * whole bitmap untrimmed if at any point we add untrimmed regions.
2162 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002163 if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002164 if (btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002165 ctl->discardable_extents[BTRFS_STAT_CURR] +=
2166 info->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002167 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
2168 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002169 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002170 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002171
Josef Bacik2cdc3422011-05-27 14:07:49 -04002172 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
2173
2174 bytes_to_set = min(end - offset, bytes);
2175
2176 bitmap_set_bits(ctl, info, offset, bytes_to_set);
2177
2178 return bytes_to_set;
2179
2180}
2181
Li Zefan34d52cb2011-03-29 13:46:06 +08002182static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2183 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04002184{
David Sterba32da53862019-10-29 19:20:18 +01002185 struct btrfs_block_group *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002186 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002187 bool forced = false;
2188
2189#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002190 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002191 forced = true;
2192#endif
Josef Bacik96303082009-07-13 21:29:25 -04002193
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05002194 /* This is a way to reclaim large regions from the bitmaps. */
2195 if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
2196 return false;
2197
Josef Bacik96303082009-07-13 21:29:25 -04002198 /*
2199 * If we are below the extents threshold then we can add this as an
2200 * extent, and don't have to deal with the bitmap
2201 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002202 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002203 /*
2204 * If this block group has some small extents we don't want to
2205 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002206 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002207 * of cache left then go ahead an dadd them, no sense in adding
2208 * the overhead of a bitmap if we don't have to.
2209 */
Dennis Zhouf9bb6152020-01-02 16:26:44 -05002210 if (info->bytes <= fs_info->sectorsize * 8) {
2211 if (ctl->free_extents * 3 <= ctl->extents_thresh)
Li Zefan34d52cb2011-03-29 13:46:06 +08002212 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002213 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002214 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002215 }
2216 }
Josef Bacik96303082009-07-13 21:29:25 -04002217
2218 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002219 * The original block groups from mkfs can be really small, like 8
2220 * megabytes, so don't bother with a bitmap for those entries. However
2221 * some block groups can be smaller than what a bitmap would cover but
2222 * are still large enough that they could overflow the 32k memory limit,
2223 * so allow those block groups to still be allowed to have a bitmap
2224 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002225 */
David Sterbab3470b52019-10-23 18:48:22 +02002226 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002227 return false;
2228
2229 return true;
2230}
2231
David Sterba20e55062015-11-19 11:42:28 +01002232static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002233 .use_bitmap = use_bitmap,
2234};
2235
Li Zefan34d52cb2011-03-29 13:46:06 +08002236static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2237 struct btrfs_free_space *info)
2238{
2239 struct btrfs_free_space *bitmap_info;
David Sterba32da53862019-10-29 19:20:18 +01002240 struct btrfs_block_group *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002241 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002242 u64 bytes, offset, bytes_added;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002243 enum btrfs_trim_state trim_state;
Li Zefan34d52cb2011-03-29 13:46:06 +08002244 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002245
2246 bytes = info->bytes;
2247 offset = info->offset;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002248 trim_state = info->trim_state;
Josef Bacik96303082009-07-13 21:29:25 -04002249
Li Zefan34d52cb2011-03-29 13:46:06 +08002250 if (!ctl->op->use_bitmap(ctl, info))
2251 return 0;
2252
Josef Bacik2cdc3422011-05-27 14:07:49 -04002253 if (ctl->op == &free_space_op)
2254 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002255again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002256 /*
2257 * Since we link bitmaps right into the cluster we need to see if we
2258 * have a cluster here, and if so and it has our bitmap we need to add
2259 * the free space to that bitmap.
2260 */
2261 if (block_group && !list_empty(&block_group->cluster_list)) {
2262 struct btrfs_free_cluster *cluster;
2263 struct rb_node *node;
2264 struct btrfs_free_space *entry;
2265
2266 cluster = list_entry(block_group->cluster_list.next,
2267 struct btrfs_free_cluster,
2268 block_group_list);
2269 spin_lock(&cluster->lock);
2270 node = rb_first(&cluster->root);
2271 if (!node) {
2272 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002273 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002274 }
2275
2276 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2277 if (!entry->bitmap) {
2278 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002279 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002280 }
2281
2282 if (entry->offset == offset_to_bitmap(ctl, offset)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08002283 bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
2284 bytes, trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002285 bytes -= bytes_added;
2286 offset += bytes_added;
2287 }
2288 spin_unlock(&cluster->lock);
2289 if (!bytes) {
2290 ret = 1;
2291 goto out;
2292 }
2293 }
Chris Mason38e87882011-06-10 16:36:57 -04002294
2295no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002296 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002297 1, 0);
2298 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002299 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002300 goto new_bitmap;
2301 }
2302
Dennis Zhouda080fe2019-12-13 16:22:13 -08002303 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
2304 trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002305 bytes -= bytes_added;
2306 offset += bytes_added;
2307 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002308
2309 if (!bytes) {
2310 ret = 1;
2311 goto out;
2312 } else
2313 goto again;
2314
2315new_bitmap:
2316 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002317 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002318 added = 1;
2319 info = NULL;
2320 goto again;
2321 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002322 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002323
2324 /* no pre-allocated info, allocate a new one */
2325 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002326 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2327 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002328 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002329 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002330 ret = -ENOMEM;
2331 goto out;
2332 }
2333 }
2334
2335 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002336 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2337 GFP_NOFS);
Dennis Zhouda080fe2019-12-13 16:22:13 -08002338 info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Li Zefan34d52cb2011-03-29 13:46:06 +08002339 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002340 if (!info->bitmap) {
2341 ret = -ENOMEM;
2342 goto out;
2343 }
2344 goto again;
2345 }
2346
2347out:
2348 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002349 if (info->bitmap)
2350 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2351 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002352 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002353 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002354
2355 return ret;
2356}
2357
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002358/*
2359 * Free space merging rules:
2360 * 1) Merge trimmed areas together
2361 * 2) Let untrimmed areas coalesce with trimmed areas
2362 * 3) Always pull neighboring regions from bitmaps
2363 *
2364 * The above rules are for when we merge free space based on btrfs_trim_state.
2365 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2366 * same reason: to promote larger extent regions which makes life easier for
2367 * find_free_extent(). Rule 2 enables coalescing based on the common path
2368 * being returning free space from btrfs_finish_extent_commit(). So when free
2369 * space is trimmed, it will prevent aggregating trimmed new region and
2370 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2371 * and provide find_free_extent() with the largest extents possible hoping for
2372 * the reuse path.
2373 */
Chris Mason945d8962011-05-22 12:33:42 -04002374static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002375 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002376{
Josef Bacikbf53d462020-07-27 10:28:05 -04002377 struct btrfs_free_space *left_info = NULL;
Li Zefan120d66e2010-11-09 14:56:50 +08002378 struct btrfs_free_space *right_info;
2379 bool merged = false;
2380 u64 offset = info->offset;
2381 u64 bytes = info->bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002382 const bool is_trimmed = btrfs_free_space_trimmed(info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002383
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384 /*
2385 * first we want to see if there is free space adjacent to the range we
2386 * are adding, if there is remove that struct and add a new one to
2387 * cover the entire range
2388 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002389 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002390 if (right_info && rb_prev(&right_info->offset_index))
2391 left_info = rb_entry(rb_prev(&right_info->offset_index),
2392 struct btrfs_free_space, offset_index);
Josef Bacikbf53d462020-07-27 10:28:05 -04002393 else if (!right_info)
Li Zefan34d52cb2011-03-29 13:46:06 +08002394 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002395
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002396 /* See try_merge_free_space() comment. */
2397 if (right_info && !right_info->bitmap &&
2398 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002399 unlink_free_space(ctl, right_info, update_stat);
Josef Bacik6226cb02009-04-03 10:14:18 -04002400 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002401 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002402 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002403 }
2404
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002405 /* See try_merge_free_space() comment. */
Josef Bacik96303082009-07-13 21:29:25 -04002406 if (left_info && !left_info->bitmap &&
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002407 left_info->offset + left_info->bytes == offset &&
2408 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002409 unlink_free_space(ctl, left_info, update_stat);
Josef Bacik6226cb02009-04-03 10:14:18 -04002410 info->offset = left_info->offset;
2411 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002412 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002413 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002414 }
2415
Li Zefan120d66e2010-11-09 14:56:50 +08002416 return merged;
2417}
2418
Filipe Manana20005522014-08-29 13:35:13 +01002419static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2420 struct btrfs_free_space *info,
2421 bool update_stat)
2422{
2423 struct btrfs_free_space *bitmap;
2424 unsigned long i;
2425 unsigned long j;
2426 const u64 end = info->offset + info->bytes;
2427 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2428 u64 bytes;
2429
2430 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2431 if (!bitmap)
2432 return false;
2433
2434 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2435 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2436 if (j == i)
2437 return false;
2438 bytes = (j - i) * ctl->unit;
2439 info->bytes += bytes;
2440
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002441 /* See try_merge_free_space() comment. */
2442 if (!btrfs_free_space_trimmed(bitmap))
2443 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2444
Nikolay Borisovf594f132021-11-23 14:44:19 +02002445 bitmap_clear_bits(ctl, bitmap, end, bytes, update_stat);
Filipe Manana20005522014-08-29 13:35:13 +01002446
2447 if (!bitmap->bytes)
2448 free_bitmap(ctl, bitmap);
2449
2450 return true;
2451}
2452
2453static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2454 struct btrfs_free_space *info,
2455 bool update_stat)
2456{
2457 struct btrfs_free_space *bitmap;
2458 u64 bitmap_offset;
2459 unsigned long i;
2460 unsigned long j;
2461 unsigned long prev_j;
2462 u64 bytes;
2463
2464 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2465 /* If we're on a boundary, try the previous logical bitmap. */
2466 if (bitmap_offset == info->offset) {
2467 if (info->offset == 0)
2468 return false;
2469 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2470 }
2471
2472 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2473 if (!bitmap)
2474 return false;
2475
2476 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2477 j = 0;
2478 prev_j = (unsigned long)-1;
2479 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2480 if (j > i)
2481 break;
2482 prev_j = j;
2483 }
2484 if (prev_j == i)
2485 return false;
2486
2487 if (prev_j == (unsigned long)-1)
2488 bytes = (i + 1) * ctl->unit;
2489 else
2490 bytes = (i - prev_j) * ctl->unit;
2491
2492 info->offset -= bytes;
2493 info->bytes += bytes;
2494
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002495 /* See try_merge_free_space() comment. */
2496 if (!btrfs_free_space_trimmed(bitmap))
2497 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2498
Nikolay Borisovf594f132021-11-23 14:44:19 +02002499 bitmap_clear_bits(ctl, bitmap, info->offset, bytes, update_stat);
Filipe Manana20005522014-08-29 13:35:13 +01002500
2501 if (!bitmap->bytes)
2502 free_bitmap(ctl, bitmap);
2503
2504 return true;
2505}
2506
2507/*
2508 * We prefer always to allocate from extent entries, both for clustered and
2509 * non-clustered allocation requests. So when attempting to add a new extent
2510 * entry, try to see if there's adjacent free space in bitmap entries, and if
2511 * there is, migrate that space from the bitmaps to the extent.
2512 * Like this we get better chances of satisfying space allocation requests
2513 * because we attempt to satisfy them based on a single cache entry, and never
2514 * on 2 or more entries - even if the entries represent a contiguous free space
2515 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2516 * ends).
2517 */
2518static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2519 struct btrfs_free_space *info,
2520 bool update_stat)
2521{
2522 /*
2523 * Only work with disconnected entries, as we can change their offset,
2524 * and must be extent entries.
2525 */
2526 ASSERT(!info->bitmap);
2527 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2528
2529 if (ctl->total_bitmaps > 0) {
2530 bool stole_end;
2531 bool stole_front = false;
2532
2533 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2534 if (ctl->total_bitmaps > 0)
2535 stole_front = steal_from_bitmap_to_front(ctl, info,
2536 update_stat);
2537
2538 if (stole_end || stole_front)
2539 try_merge_free_space(ctl, info, update_stat);
2540 }
2541}
2542
Nikolay Borisov290ef192021-11-23 14:44:21 +02002543int __btrfs_add_free_space(struct btrfs_block_group *block_group,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002544 u64 offset, u64 bytes,
2545 enum btrfs_trim_state trim_state)
Li Zefan120d66e2010-11-09 14:56:50 +08002546{
Nikolay Borisov290ef192021-11-23 14:44:21 +02002547 struct btrfs_fs_info *fs_info = block_group->fs_info;
2548 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan120d66e2010-11-09 14:56:50 +08002549 struct btrfs_free_space *info;
2550 int ret = 0;
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002551 u64 filter_bytes = bytes;
Li Zefan120d66e2010-11-09 14:56:50 +08002552
Naohiro Aota169e0da2021-02-04 19:21:52 +09002553 ASSERT(!btrfs_is_zoned(fs_info));
2554
Josef Bacikdc89e982011-01-28 17:05:48 -05002555 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002556 if (!info)
2557 return -ENOMEM;
2558
2559 info->offset = offset;
2560 info->bytes = bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002561 info->trim_state = trim_state;
Filipe Manana20005522014-08-29 13:35:13 +01002562 RB_CLEAR_NODE(&info->offset_index);
Josef Bacik59c7b562021-11-18 16:33:15 -05002563 RB_CLEAR_NODE(&info->bytes_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002564
Li Zefan34d52cb2011-03-29 13:46:06 +08002565 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002566
Li Zefan34d52cb2011-03-29 13:46:06 +08002567 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002568 goto link;
2569
2570 /*
2571 * There was no extent directly to the left or right of this new
2572 * extent then we know we're going to have to allocate a new extent, so
2573 * before we do that see if we need to drop this into a bitmap
2574 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002575 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002576 if (ret < 0) {
2577 goto out;
2578 } else if (ret) {
2579 ret = 0;
2580 goto out;
2581 }
2582link:
Filipe Manana20005522014-08-29 13:35:13 +01002583 /*
2584 * Only steal free space from adjacent bitmaps if we're sure we're not
2585 * going to add the new free space to existing bitmap entries - because
2586 * that would mean unnecessary work that would be reverted. Therefore
2587 * attempt to steal space from bitmaps if we're adding an extent entry.
2588 */
2589 steal_from_bitmap(ctl, info, true);
2590
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002591 filter_bytes = max(filter_bytes, info->bytes);
2592
Li Zefan34d52cb2011-03-29 13:46:06 +08002593 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002594 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002595 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002596out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002597 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002598 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002599
Josef Bacik0f9dd462008-09-23 13:14:11 -04002600 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002601 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002602 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002603 }
2604
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002605 if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
2606 btrfs_discard_check_filter(block_group, filter_bytes);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002607 btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002608 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002609
Josef Bacik0f9dd462008-09-23 13:14:11 -04002610 return ret;
2611}
2612
Naohiro Aota169e0da2021-02-04 19:21:52 +09002613static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group,
2614 u64 bytenr, u64 size, bool used)
2615{
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002616 struct btrfs_fs_info *fs_info = block_group->fs_info;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002617 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2618 u64 offset = bytenr - block_group->start;
2619 u64 to_free, to_unusable;
Johannes Thumshirn77233c22021-08-09 20:41:17 +09002620 const int bg_reclaim_threshold = READ_ONCE(fs_info->bg_reclaim_threshold);
Naohiro Aota98173252021-08-19 21:19:10 +09002621 bool initial = (size == block_group->length);
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002622 u64 reclaimable_unusable;
Naohiro Aota98173252021-08-19 21:19:10 +09002623
2624 WARN_ON(!initial && offset + size > block_group->zone_capacity);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002625
2626 spin_lock(&ctl->tree_lock);
2627 if (!used)
2628 to_free = size;
Naohiro Aota98173252021-08-19 21:19:10 +09002629 else if (initial)
2630 to_free = block_group->zone_capacity;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002631 else if (offset >= block_group->alloc_offset)
2632 to_free = size;
2633 else if (offset + size <= block_group->alloc_offset)
2634 to_free = 0;
2635 else
2636 to_free = offset + size - block_group->alloc_offset;
2637 to_unusable = size - to_free;
2638
2639 ctl->free_space += to_free;
Naohiro Aotabadae9c2021-03-03 17:55:48 +09002640 /*
2641 * If the block group is read-only, we should account freed space into
2642 * bytes_readonly.
2643 */
2644 if (!block_group->ro)
2645 block_group->zone_unusable += to_unusable;
Naohiro Aota169e0da2021-02-04 19:21:52 +09002646 spin_unlock(&ctl->tree_lock);
2647 if (!used) {
2648 spin_lock(&block_group->lock);
2649 block_group->alloc_offset -= size;
2650 spin_unlock(&block_group->lock);
2651 }
2652
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002653 reclaimable_unusable = block_group->zone_unusable -
2654 (block_group->length - block_group->zone_capacity);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002655 /* All the region is now unusable. Mark it as unused and reclaim */
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002656 if (block_group->zone_unusable == block_group->length) {
Naohiro Aota169e0da2021-02-04 19:21:52 +09002657 btrfs_mark_bg_unused(block_group);
Johannes Thumshirn77233c22021-08-09 20:41:17 +09002658 } else if (bg_reclaim_threshold &&
Naohiro Aotad8da0e82021-08-19 21:19:11 +09002659 reclaimable_unusable >=
2660 div_factor_fine(block_group->zone_capacity,
2661 bg_reclaim_threshold)) {
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09002662 btrfs_mark_bg_to_reclaim(block_group);
2663 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002664
2665 return 0;
2666}
2667
David Sterba32da53862019-10-29 19:20:18 +01002668int btrfs_add_free_space(struct btrfs_block_group *block_group,
Josef Bacik478b4d92019-06-20 15:37:43 -04002669 u64 bytenr, u64 size)
2670{
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002671 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2672
Naohiro Aota169e0da2021-02-04 19:21:52 +09002673 if (btrfs_is_zoned(block_group->fs_info))
2674 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2675 true);
2676
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002677 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2678 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2679
Nikolay Borisov290ef192021-11-23 14:44:21 +02002680 return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
Josef Bacik478b4d92019-06-20 15:37:43 -04002681}
2682
Naohiro Aota169e0da2021-02-04 19:21:52 +09002683int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
2684 u64 bytenr, u64 size)
2685{
2686 if (btrfs_is_zoned(block_group->fs_info))
2687 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2688 false);
2689
2690 return btrfs_add_free_space(block_group, bytenr, size);
2691}
2692
Dennis Zhoub0643e52019-12-13 16:22:14 -08002693/*
2694 * This is a subtle distinction because when adding free space back in general,
2695 * we want it to be added as untrimmed for async. But in the case where we add
2696 * it on loading of a block group, we want to consider it trimmed.
2697 */
2698int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
2699 u64 bytenr, u64 size)
2700{
2701 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2702
Naohiro Aota169e0da2021-02-04 19:21:52 +09002703 if (btrfs_is_zoned(block_group->fs_info))
2704 return __btrfs_add_free_space_zoned(block_group, bytenr, size,
2705 true);
2706
Dennis Zhoub0643e52019-12-13 16:22:14 -08002707 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
2708 btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
2709 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2710
Nikolay Borisov290ef192021-11-23 14:44:21 +02002711 return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002712}
2713
David Sterba32da53862019-10-29 19:20:18 +01002714int btrfs_remove_free_space(struct btrfs_block_group *block_group,
Josef Bacik6226cb02009-04-03 10:14:18 -04002715 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002716{
Li Zefan34d52cb2011-03-29 13:46:06 +08002717 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002718 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002719 int ret;
2720 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002721
Naohiro Aota011b41b2021-02-04 19:21:55 +09002722 if (btrfs_is_zoned(block_group->fs_info)) {
2723 /*
2724 * This can happen with conventional zones when replaying log.
2725 * Since the allocation info of tree-log nodes are not recorded
2726 * to the extent-tree, calculate_alloc_pointer() failed to
2727 * advance the allocation pointer after last allocated tree log
2728 * node blocks.
2729 *
2730 * This function is called from
2731 * btrfs_pin_extent_for_log_replay() when replaying the log.
2732 * Advance the pointer not to overwrite the tree-log nodes.
2733 */
Naohiro Aota0ae79c62021-08-09 13:13:44 +09002734 if (block_group->start + block_group->alloc_offset <
2735 offset + bytes) {
2736 block_group->alloc_offset =
2737 offset + bytes - block_group->start;
2738 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002739 return 0;
Naohiro Aota011b41b2021-02-04 19:21:55 +09002740 }
Naohiro Aota169e0da2021-02-04 19:21:52 +09002741
Li Zefan34d52cb2011-03-29 13:46:06 +08002742 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002743
Josef Bacik96303082009-07-13 21:29:25 -04002744again:
Josef Bacikb0175112012-12-18 11:39:19 -05002745 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002746 if (!bytes)
2747 goto out_lock;
2748
Li Zefan34d52cb2011-03-29 13:46:06 +08002749 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002750 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002751 /*
2752 * oops didn't find an extent that matched the space we wanted
2753 * to remove, look for a bitmap instead
2754 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002755 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002756 1, 0);
2757 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002758 /*
2759 * If we found a partial bit of our free space in a
2760 * bitmap but then couldn't find the other part this may
2761 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002762 */
Josef Bacikb0175112012-12-18 11:39:19 -05002763 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002764 goto out_lock;
2765 }
Josef Bacik96303082009-07-13 21:29:25 -04002766 }
2767
Josef Bacikb0175112012-12-18 11:39:19 -05002768 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002769 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002770 unlink_free_space(ctl, info, true);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002771 if (offset == info->offset) {
2772 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002773
Josef Bacikbdb7d302012-06-27 15:10:56 -04002774 info->bytes -= to_free;
2775 info->offset += to_free;
2776 if (info->bytes) {
2777 ret = link_free_space(ctl, info);
2778 WARN_ON(ret);
2779 } else {
2780 kmem_cache_free(btrfs_free_space_cachep, info);
2781 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002782
Josef Bacikbdb7d302012-06-27 15:10:56 -04002783 offset += to_free;
2784 bytes -= to_free;
2785 goto again;
2786 } else {
2787 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002788
Josef Bacikbdb7d302012-06-27 15:10:56 -04002789 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002790 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002791 WARN_ON(ret);
2792 if (ret)
2793 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002794
Josef Bacikbdb7d302012-06-27 15:10:56 -04002795 /* Not enough bytes in this entry to satisfy us */
2796 if (old_end < offset + bytes) {
2797 bytes -= old_end - offset;
2798 offset = old_end;
2799 goto again;
2800 } else if (old_end == offset + bytes) {
2801 /* all done */
2802 goto out_lock;
2803 }
2804 spin_unlock(&ctl->tree_lock);
2805
Nikolay Borisov290ef192021-11-23 14:44:21 +02002806 ret = __btrfs_add_free_space(block_group,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002807 offset + bytes,
2808 old_end - (offset + bytes),
2809 info->trim_state);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002810 WARN_ON(ret);
2811 goto out;
2812 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002813 }
Josef Bacik96303082009-07-13 21:29:25 -04002814
Li Zefan34d52cb2011-03-29 13:46:06 +08002815 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002816 if (ret == -EAGAIN) {
2817 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002818 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002819 }
Josef Bacik96303082009-07-13 21:29:25 -04002820out_lock:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002821 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002822 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002823out:
Josef Bacik25179202008-10-29 14:49:05 -04002824 return ret;
2825}
2826
David Sterba32da53862019-10-29 19:20:18 +01002827void btrfs_dump_free_space(struct btrfs_block_group *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -04002828 u64 bytes)
2829{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002830 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002831 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002832 struct btrfs_free_space *info;
2833 struct rb_node *n;
2834 int count = 0;
2835
Naohiro Aota169e0da2021-02-04 19:21:52 +09002836 /*
2837 * Zoned btrfs does not use free space tree and cluster. Just print
2838 * out the free space after the allocation offset.
2839 */
2840 if (btrfs_is_zoned(fs_info)) {
Naohiro Aotaafba2bc2021-08-19 21:19:17 +09002841 btrfs_info(fs_info, "free space %llu active %d",
2842 block_group->zone_capacity - block_group->alloc_offset,
2843 block_group->zone_is_active);
Naohiro Aota169e0da2021-02-04 19:21:52 +09002844 return;
2845 }
2846
Filipe Manana9084cb62018-10-22 10:43:06 +01002847 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002848 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002849 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002850 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002851 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002852 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002853 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002854 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002855 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002856 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002857 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002858 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002859 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002860 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002861}
2862
Josef Bacikcd799092020-10-23 09:58:08 -04002863void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
2864 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002865{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002866 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002867
Li Zefan34d52cb2011-03-29 13:46:06 +08002868 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002869 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002870 ctl->start = block_group->start;
Li Zefan34d52cb2011-03-29 13:46:06 +08002871 ctl->private = block_group;
2872 ctl->op = &free_space_op;
Josef Bacik59c7b562021-11-18 16:33:15 -05002873 ctl->free_space_bytes = RB_ROOT_CACHED;
Filipe Manana55507ce2014-12-01 17:04:09 +00002874 INIT_LIST_HEAD(&ctl->trimming_ranges);
2875 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002876
Li Zefan34d52cb2011-03-29 13:46:06 +08002877 /*
2878 * we only want to have 32k of ram per block group for keeping
2879 * track of free space, and if we pass 1/2 of that we want to
2880 * start converting things over to using bitmaps
2881 */
Byongho Leeee221842015-12-15 01:42:10 +09002882 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002883}
2884
Chris Masonfa9c0d792009-04-03 09:47:43 -04002885/*
2886 * for a given cluster, put all of its extents back into the free
2887 * space cache. If the block group passed doesn't match the block group
2888 * pointed to by the cluster, someone else raced in and freed the
2889 * cluster already. In that case, we just return without changing anything
2890 */
Anand Jain69b0e092020-06-03 18:10:18 +08002891static void __btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002892 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002893 struct btrfs_free_cluster *cluster)
2894{
Li Zefan34d52cb2011-03-29 13:46:06 +08002895 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002896 struct btrfs_free_space *entry;
2897 struct rb_node *node;
2898
2899 spin_lock(&cluster->lock);
Josef Bacik95c85fb2021-01-25 16:42:35 -05002900 if (cluster->block_group != block_group) {
2901 spin_unlock(&cluster->lock);
2902 return;
2903 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002904
Josef Bacik96303082009-07-13 21:29:25 -04002905 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002906 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002907 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002908
Chris Masonfa9c0d792009-04-03 09:47:43 -04002909 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002910 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002911 bool bitmap;
2912
Chris Masonfa9c0d792009-04-03 09:47:43 -04002913 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2914 node = rb_next(&entry->offset_index);
2915 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002916 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002917
2918 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002919 if (!bitmap) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002920 /* Merging treats extents as if they were new */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002921 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002922 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002923 ctl->discardable_bytes[BTRFS_STAT_CURR] -=
2924 entry->bytes;
2925 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002926
Li Zefan34d52cb2011-03-29 13:46:06 +08002927 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002928 steal_from_bitmap(ctl, entry, false);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002929
2930 /* As we insert directly, update these statistics */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002931 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002932 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002933 ctl->discardable_bytes[BTRFS_STAT_CURR] +=
2934 entry->bytes;
2935 }
Filipe Manana20005522014-08-29 13:35:13 +01002936 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002937 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002938 entry->offset, &entry->offset_index, bitmap);
Josef Bacik59c7b562021-11-18 16:33:15 -05002939 rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes,
2940 entry_less);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002941 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002942 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002943 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002944 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002945}
2946
Eric Sandeen48a3b632013-04-25 20:41:01 +00002947static void __btrfs_remove_free_space_cache_locked(
2948 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002949{
2950 struct btrfs_free_space *info;
2951 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002952
Li Zefan581bb052011-04-20 10:06:11 +08002953 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2954 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002955 if (!info->bitmap) {
Nikolay Borisov32e16492021-11-23 14:44:20 +02002956 unlink_free_space(ctl, info, true);
Josef Bacik9b90f512011-06-24 16:02:51 +00002957 kmem_cache_free(btrfs_free_space_cachep, info);
2958 } else {
2959 free_bitmap(ctl, info);
2960 }
David Sterba351810c2015-01-08 15:20:54 +01002961
2962 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002963 }
Chris Mason09655372011-05-21 09:27:38 -04002964}
2965
2966void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2967{
2968 spin_lock(&ctl->tree_lock);
2969 __btrfs_remove_free_space_cache_locked(ctl);
Dennis Zhou27f0afc2020-01-02 16:26:45 -05002970 if (ctl->private)
Josef Bacik66b53ba2020-10-23 09:58:07 -04002971 btrfs_discard_update_discardable(ctl->private);
Li Zefan581bb052011-04-20 10:06:11 +08002972 spin_unlock(&ctl->tree_lock);
2973}
2974
David Sterba32da53862019-10-29 19:20:18 +01002975void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002976{
Li Zefan34d52cb2011-03-29 13:46:06 +08002977 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002978 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002979 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002980
Li Zefan34d52cb2011-03-29 13:46:06 +08002981 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002982 while ((head = block_group->cluster_list.next) !=
2983 &block_group->cluster_list) {
2984 cluster = list_entry(head, struct btrfs_free_cluster,
2985 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002986
2987 WARN_ON(cluster->block_group != block_group);
2988 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002989
2990 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002991 }
Chris Mason09655372011-05-21 09:27:38 -04002992 __btrfs_remove_free_space_cache_locked(ctl);
Josef Bacik66b53ba2020-10-23 09:58:07 -04002993 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002994 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002995
Josef Bacik0f9dd462008-09-23 13:14:11 -04002996}
2997
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08002998/**
2999 * btrfs_is_free_space_trimmed - see if everything is trimmed
3000 * @block_group: block_group of interest
3001 *
3002 * Walk @block_group's free space rb_tree to determine if everything is trimmed.
3003 */
3004bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
3005{
3006 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3007 struct btrfs_free_space *info;
3008 struct rb_node *node;
3009 bool ret = true;
3010
3011 spin_lock(&ctl->tree_lock);
3012 node = rb_first(&ctl->free_space_offset);
3013
3014 while (node) {
3015 info = rb_entry(node, struct btrfs_free_space, offset_index);
3016
3017 if (!btrfs_free_space_trimmed(info)) {
3018 ret = false;
3019 break;
3020 }
3021
3022 node = rb_next(node);
3023 }
3024
3025 spin_unlock(&ctl->tree_lock);
3026 return ret;
3027}
3028
David Sterba32da53862019-10-29 19:20:18 +01003029u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08003030 u64 offset, u64 bytes, u64 empty_size,
3031 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04003032{
Li Zefan34d52cb2011-03-29 13:46:06 +08003033 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003034 struct btrfs_discard_ctl *discard_ctl =
3035 &block_group->fs_info->discard_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04003036 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04003037 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04003038 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003039 u64 align_gap = 0;
3040 u64 align_gap_len = 0;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003041 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Josef Bacik59c7b562021-11-18 16:33:15 -05003042 bool use_bytes_index = (offset == block_group->start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003043
Naohiro Aota2eda5702021-02-04 19:21:53 +09003044 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3045
Li Zefan34d52cb2011-03-29 13:46:06 +08003046 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05003047 entry = find_free_space(ctl, &offset, &bytes_search,
Josef Bacik59c7b562021-11-18 16:33:15 -05003048 block_group->full_stripe_len, max_extent_size,
3049 use_bytes_index);
Josef Bacik6226cb02009-04-03 10:14:18 -04003050 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04003051 goto out;
3052
3053 ret = offset;
3054 if (entry->bitmap) {
Nikolay Borisovf594f132021-11-23 14:44:19 +02003055 bitmap_clear_bits(ctl, entry, offset, bytes, true);
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003056
3057 if (!btrfs_free_space_trimmed(entry))
3058 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3059
Li Zefanedf6e2d2010-11-09 14:50:07 +08003060 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08003061 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04003062 } else {
Nikolay Borisov32e16492021-11-23 14:44:20 +02003063 unlink_free_space(ctl, entry, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05003064 align_gap_len = offset - entry->offset;
3065 align_gap = entry->offset;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003066 align_gap_trim_state = entry->trim_state;
David Woodhouse53b381b2013-01-29 18:40:14 -05003067
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003068 if (!btrfs_free_space_trimmed(entry))
3069 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3070
David Woodhouse53b381b2013-01-29 18:40:14 -05003071 entry->offset = offset + bytes;
3072 WARN_ON(entry->bytes < bytes + align_gap_len);
3073
3074 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04003075 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05003076 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04003077 else
Li Zefan34d52cb2011-03-29 13:46:06 +08003078 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04003079 }
Josef Bacik96303082009-07-13 21:29:25 -04003080out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04003081 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08003082 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04003083
David Woodhouse53b381b2013-01-29 18:40:14 -05003084 if (align_gap_len)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003085 __btrfs_add_free_space(block_group, align_gap, align_gap_len,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08003086 align_gap_trim_state);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003087 return ret;
3088}
Chris Masonfa9c0d792009-04-03 09:47:43 -04003089
3090/*
3091 * given a cluster, put all of its extents back into the free space
3092 * cache. If a block group is passed, this function will only free
3093 * a cluster that belongs to the passed block group.
3094 *
3095 * Otherwise, it'll get a reference on the block group pointed to by the
3096 * cluster and remove the cluster from it.
3097 */
Anand Jain69b0e092020-06-03 18:10:18 +08003098void btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01003099 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003100 struct btrfs_free_cluster *cluster)
3101{
Li Zefan34d52cb2011-03-29 13:46:06 +08003102 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003103
3104 /* first, get a safe pointer to the block group */
3105 spin_lock(&cluster->lock);
3106 if (!block_group) {
3107 block_group = cluster->block_group;
3108 if (!block_group) {
3109 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003110 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003111 }
3112 } else if (cluster->block_group != block_group) {
3113 /* someone else has already freed it don't redo their work */
3114 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003115 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003116 }
Anand Jainb5790d52020-06-03 18:10:20 +08003117 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003118 spin_unlock(&cluster->lock);
3119
Li Zefan34d52cb2011-03-29 13:46:06 +08003120 ctl = block_group->free_space_ctl;
3121
Chris Masonfa9c0d792009-04-03 09:47:43 -04003122 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08003123 spin_lock(&ctl->tree_lock);
Anand Jain69b0e092020-06-03 18:10:18 +08003124 __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08003125 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003126
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003127 btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
3128
Chris Masonfa9c0d792009-04-03 09:47:43 -04003129 /* finally drop our ref */
3130 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003131}
3132
David Sterba32da53862019-10-29 19:20:18 +01003133static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003134 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04003135 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08003136 u64 bytes, u64 min_start,
3137 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04003138{
Li Zefan34d52cb2011-03-29 13:46:06 +08003139 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003140 int err;
3141 u64 search_start = cluster->window_start;
3142 u64 search_bytes = bytes;
3143 u64 ret = 0;
3144
Josef Bacik96303082009-07-13 21:29:25 -04003145 search_start = min_start;
3146 search_bytes = bytes;
3147
Josef Bacik0584f712015-10-02 16:12:23 -04003148 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08003149 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04003150 *max_extent_size = max(get_max_extent_size(entry),
3151 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003152 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08003153 }
Josef Bacik96303082009-07-13 21:29:25 -04003154
3155 ret = search_start;
Nikolay Borisovf594f132021-11-23 14:44:19 +02003156 bitmap_clear_bits(ctl, entry, ret, bytes, false);
Josef Bacik96303082009-07-13 21:29:25 -04003157
3158 return ret;
3159}
3160
Chris Masonfa9c0d792009-04-03 09:47:43 -04003161/*
3162 * given a cluster, try to allocate 'bytes' from it, returns 0
3163 * if it couldn't find anything suitably large, or a logical disk offset
3164 * if things worked out
3165 */
David Sterba32da53862019-10-29 19:20:18 +01003166u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003167 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003168 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003169{
Li Zefan34d52cb2011-03-29 13:46:06 +08003170 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003171 struct btrfs_discard_ctl *discard_ctl =
3172 &block_group->fs_info->discard_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003173 struct btrfs_free_space *entry = NULL;
3174 struct rb_node *node;
3175 u64 ret = 0;
3176
Naohiro Aota2eda5702021-02-04 19:21:53 +09003177 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3178
Chris Masonfa9c0d792009-04-03 09:47:43 -04003179 spin_lock(&cluster->lock);
3180 if (bytes > cluster->max_size)
3181 goto out;
3182
3183 if (cluster->block_group != block_group)
3184 goto out;
3185
3186 node = rb_first(&cluster->root);
3187 if (!node)
3188 goto out;
3189
3190 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303191 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04003192 if (entry->bytes < bytes)
3193 *max_extent_size = max(get_max_extent_size(entry),
3194 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08003195
Josef Bacik4e69b592011-03-21 10:11:24 -04003196 if (entry->bytes < bytes ||
3197 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003198 node = rb_next(&entry->offset_index);
3199 if (!node)
3200 break;
3201 entry = rb_entry(node, struct btrfs_free_space,
3202 offset_index);
3203 continue;
3204 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003205
Josef Bacik4e69b592011-03-21 10:11:24 -04003206 if (entry->bitmap) {
3207 ret = btrfs_alloc_from_bitmap(block_group,
3208 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003209 cluster->window_start,
3210 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003211 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003212 node = rb_next(&entry->offset_index);
3213 if (!node)
3214 break;
3215 entry = rb_entry(node, struct btrfs_free_space,
3216 offset_index);
3217 continue;
3218 }
Josef Bacik9b230622012-01-26 15:01:12 -05003219 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003220 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04003221 ret = entry->offset;
3222
3223 entry->offset += bytes;
3224 entry->bytes -= bytes;
3225 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003226
Chris Masonfa9c0d792009-04-03 09:47:43 -04003227 break;
3228 }
3229out:
3230 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04003231
Li Zefan5e71b5d2010-11-09 14:55:34 +08003232 if (!ret)
3233 return 0;
3234
Li Zefan34d52cb2011-03-29 13:46:06 +08003235 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003236
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003237 if (!btrfs_free_space_trimmed(entry))
3238 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3239
Li Zefan34d52cb2011-03-29 13:46:06 +08003240 ctl->free_space -= bytes;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003241 if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
3242 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
Nikolay Borisov3c179162021-02-08 10:26:54 +02003243
3244 spin_lock(&cluster->lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003245 if (entry->bytes == 0) {
Nikolay Borisov3c179162021-02-08 10:26:54 +02003246 rb_erase(&entry->offset_index, &cluster->root);
Li Zefan34d52cb2011-03-29 13:46:06 +08003247 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003248 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003249 kmem_cache_free(btrfs_free_space_bitmap_cachep,
3250 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08003251 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01003252 recalculate_thresholds(ctl);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003253 } else if (!btrfs_free_space_trimmed(entry)) {
3254 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003255 }
Josef Bacikdc89e982011-01-28 17:05:48 -05003256 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003257 }
3258
Nikolay Borisov3c179162021-02-08 10:26:54 +02003259 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003260 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003261
Chris Masonfa9c0d792009-04-03 09:47:43 -04003262 return ret;
3263}
3264
David Sterba32da53862019-10-29 19:20:18 +01003265static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003266 struct btrfs_free_space *entry,
3267 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003268 u64 offset, u64 bytes,
3269 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04003270{
Li Zefan34d52cb2011-03-29 13:46:06 +08003271 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003272 unsigned long next_zero;
3273 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003274 unsigned long want_bits;
3275 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003276 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04003277 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003278 unsigned long start = 0;
3279 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003280 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04003281
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003282 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04003283 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003284 want_bits = bytes_to_bits(bytes, ctl->unit);
3285 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04003286
Josef Bacikcef40482015-10-02 16:09:42 -04003287 /*
3288 * Don't bother looking for a cluster in this bitmap if it's heavily
3289 * fragmented.
3290 */
3291 if (entry->max_extent_size &&
3292 entry->max_extent_size < cont1_bytes)
3293 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04003294again:
3295 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06003296 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04003297 next_zero = find_next_zero_bit(entry->bitmap,
3298 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003299 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04003300 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04003301 if (found_bits > max_bits)
3302 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003303 break;
3304 }
Josef Bacikcef40482015-10-02 16:09:42 -04003305 if (next_zero - i > max_bits)
3306 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04003307 i = next_zero;
3308 }
3309
Josef Bacikcef40482015-10-02 16:09:42 -04003310 if (!found_bits) {
3311 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04003312 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04003313 }
Josef Bacik96303082009-07-13 21:29:25 -04003314
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003315 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04003316 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05003317 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003318 }
3319
3320 total_found += found_bits;
3321
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003322 if (cluster->max_size < found_bits * ctl->unit)
3323 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04003324
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003325 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
3326 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04003327 goto again;
3328 }
3329
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003330 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08003331 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05003332 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
3333
3334 /*
3335 * We need to know if we're currently on the normal space index when we
3336 * manipulate the bitmap so that we know we need to remove and re-insert
3337 * it into the space_index tree. Clear the bytes_index node here so the
3338 * bitmap manipulation helpers know not to mess with the space_index
3339 * until this bitmap entry is added back into the normal cache.
3340 */
3341 RB_CLEAR_NODE(&entry->bytes_index);
3342
Josef Bacik4e69b592011-03-21 10:11:24 -04003343 ret = tree_insert_offset(&cluster->root, entry->offset,
3344 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04003345 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04003346
Josef Bacik3f7de032011-11-10 08:29:20 -05003347 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003348 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04003349 return 0;
3350}
3351
Chris Masonfa9c0d792009-04-03 09:47:43 -04003352/*
Josef Bacik4e69b592011-03-21 10:11:24 -04003353 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003354 * Try to find a cluster with at least bytes total bytes, at least one
3355 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04003356 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003357static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003358setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003359 struct btrfs_free_cluster *cluster,
3360 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003361 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003362{
Li Zefan34d52cb2011-03-29 13:46:06 +08003363 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04003364 struct btrfs_free_space *first = NULL;
3365 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003366 struct btrfs_free_space *last;
3367 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04003368 u64 window_free;
3369 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003370 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003371
Li Zefan34d52cb2011-03-29 13:46:06 +08003372 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04003373 if (!entry)
3374 return -ENOSPC;
3375
3376 /*
3377 * We don't want bitmaps, so just move along until we find a normal
3378 * extent entry.
3379 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003380 while (entry->bitmap || entry->bytes < min_bytes) {
3381 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04003382 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003383 node = rb_next(&entry->offset_index);
3384 if (!node)
3385 return -ENOSPC;
3386 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3387 }
3388
Josef Bacik4e69b592011-03-21 10:11:24 -04003389 window_free = entry->bytes;
3390 max_extent = entry->bytes;
3391 first = entry;
3392 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04003393
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003394 for (node = rb_next(&entry->offset_index); node;
3395 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003396 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3397
Josef Bacik86d4a772011-05-25 13:03:16 -04003398 if (entry->bitmap) {
3399 if (list_empty(&entry->list))
3400 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003401 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04003402 }
3403
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003404 if (entry->bytes < min_bytes)
3405 continue;
3406
3407 last = entry;
3408 window_free += entry->bytes;
3409 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04003410 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003411 }
3412
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003413 if (window_free < bytes || max_extent < cont1_bytes)
3414 return -ENOSPC;
3415
Josef Bacik4e69b592011-03-21 10:11:24 -04003416 cluster->window_start = first->offset;
3417
3418 node = &first->offset_index;
3419
3420 /*
3421 * now we've found our entries, pull them out of the free space
3422 * cache and put them into the cluster rbtree
3423 */
3424 do {
3425 int ret;
3426
3427 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3428 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003429 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003430 continue;
3431
Li Zefan34d52cb2011-03-29 13:46:06 +08003432 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik59c7b562021-11-18 16:33:15 -05003433 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003434 ret = tree_insert_offset(&cluster->root, entry->offset,
3435 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05003436 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04003437 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04003438 } while (node && entry != last);
3439
3440 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003441 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04003442 return 0;
3443}
3444
3445/*
3446 * This specifically looks for bitmaps that may work in the cluster, we assume
3447 * that we have already failed to find extents that will work.
3448 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003449static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003450setup_cluster_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003451 struct btrfs_free_cluster *cluster,
3452 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003453 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003454{
Li Zefan34d52cb2011-03-29 13:46:06 +08003455 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003456 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003457 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003458 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003459
Li Zefan34d52cb2011-03-29 13:46:06 +08003460 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003461 return -ENOSPC;
3462
Josef Bacik86d4a772011-05-25 13:03:16 -04003463 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003464 * The bitmap that covers offset won't be in the list unless offset
3465 * is just its start offset.
3466 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003467 if (!list_empty(bitmaps))
3468 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3469
3470 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003471 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3472 if (entry && list_empty(&entry->list))
3473 list_add(&entry->list, bitmaps);
3474 }
3475
Josef Bacik86d4a772011-05-25 13:03:16 -04003476 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003477 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003478 continue;
3479 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003480 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003481 if (!ret)
3482 return 0;
3483 }
3484
3485 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003486 * The bitmaps list has all the bitmaps that record free space
3487 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003488 */
Li Zefan52621cb2011-11-20 07:33:38 -05003489 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003490}
3491
3492/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003493 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003494 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003495 * We might not find them all in one contiguous area.
3496 *
3497 * returns zero and sets up cluster if things worked out, otherwise
3498 * it returns -enospc
3499 */
David Sterba32da53862019-10-29 19:20:18 +01003500int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003501 struct btrfs_free_cluster *cluster,
3502 u64 offset, u64 bytes, u64 empty_size)
3503{
David Sterba2ceeae22019-03-20 13:53:49 +01003504 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003505 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003506 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003507 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003508 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003509 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003510 int ret;
3511
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003512 /*
3513 * Choose the minimum extent size we'll require for this
3514 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3515 * For metadata, allow allocates with smaller extents. For
3516 * data, keep it dense.
3517 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003519 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003520 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003521 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003522 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003523 } else {
3524 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003525 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003526 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003527
Li Zefan34d52cb2011-03-29 13:46:06 +08003528 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003529
3530 /*
3531 * If we know we don't have enough space to make a cluster don't even
3532 * bother doing all the work to try and find one.
3533 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003534 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003535 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003536 return -ENOSPC;
3537 }
3538
Chris Masonfa9c0d792009-04-03 09:47:43 -04003539 spin_lock(&cluster->lock);
3540
3541 /* someone already found a cluster, hooray */
3542 if (cluster->block_group) {
3543 ret = 0;
3544 goto out;
3545 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003546
Josef Bacik3f7de032011-11-10 08:29:20 -05003547 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3548 min_bytes);
3549
Josef Bacik86d4a772011-05-25 13:03:16 -04003550 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003551 bytes + empty_size,
3552 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003553 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003554 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003555 offset, bytes + empty_size,
3556 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003557
3558 /* Clear our temporary list */
3559 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3560 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003561
3562 if (!ret) {
Anand Jainb5790d52020-06-03 18:10:20 +08003563 btrfs_get_block_group(block_group);
Josef Bacik4e69b592011-03-21 10:11:24 -04003564 list_add_tail(&cluster->block_group_list,
3565 &block_group->cluster_list);
3566 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003567 } else {
3568 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003569 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003570out:
3571 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003572 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003573
3574 return ret;
3575}
3576
3577/*
3578 * simple code to zero out a cluster
3579 */
3580void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3581{
3582 spin_lock_init(&cluster->lock);
3583 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003584 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003585 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003586 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003587 INIT_LIST_HEAD(&cluster->block_group_list);
3588 cluster->block_group = NULL;
3589}
3590
David Sterba32da53862019-10-29 19:20:18 +01003591static int do_trimming(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003592 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003593 u64 reserved_start, u64 reserved_bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003594 enum btrfs_trim_state reserved_trim_state,
Filipe Manana55507ce2014-12-01 17:04:09 +00003595 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003596{
3597 struct btrfs_space_info *space_info = block_group->space_info;
3598 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003599 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003600 int ret;
3601 int update = 0;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003602 const u64 end = start + bytes;
3603 const u64 reserved_end = reserved_start + reserved_bytes;
3604 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003605 u64 trimmed = 0;
3606
3607 spin_lock(&space_info->lock);
3608 spin_lock(&block_group->lock);
3609 if (!block_group->ro) {
3610 block_group->reserved += reserved_bytes;
3611 space_info->bytes_reserved += reserved_bytes;
3612 update = 1;
3613 }
3614 spin_unlock(&block_group->lock);
3615 spin_unlock(&space_info->lock);
3616
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003617 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003618 if (!ret) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003619 *total_trimmed += trimmed;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003620 trim_state = BTRFS_TRIM_STATE_TRIMMED;
3621 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003622
Filipe Manana55507ce2014-12-01 17:04:09 +00003623 mutex_lock(&ctl->cache_writeout_mutex);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003624 if (reserved_start < start)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003625 __btrfs_add_free_space(block_group, reserved_start,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003626 start - reserved_start,
3627 reserved_trim_state);
3628 if (start + bytes < reserved_start + reserved_bytes)
Nikolay Borisov290ef192021-11-23 14:44:21 +02003629 __btrfs_add_free_space(block_group, end, reserved_end - end,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003630 reserved_trim_state);
Nikolay Borisov290ef192021-11-23 14:44:21 +02003631 __btrfs_add_free_space(block_group, start, bytes, trim_state);
Filipe Manana55507ce2014-12-01 17:04:09 +00003632 list_del(&trim_entry->list);
3633 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003634
3635 if (update) {
3636 spin_lock(&space_info->lock);
3637 spin_lock(&block_group->lock);
3638 if (block_group->ro)
3639 space_info->bytes_readonly += reserved_bytes;
3640 block_group->reserved -= reserved_bytes;
3641 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003642 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003643 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003644 }
3645
3646 return ret;
3647}
3648
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003649/*
3650 * If @async is set, then we will trim 1 region and return.
3651 */
David Sterba32da53862019-10-29 19:20:18 +01003652static int trim_no_bitmap(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003653 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3654 bool async)
Li Dongyangf7039b12011-03-24 10:24:28 +00003655{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003656 struct btrfs_discard_ctl *discard_ctl =
3657 &block_group->fs_info->discard_ctl;
Li Zefan34d52cb2011-03-29 13:46:06 +08003658 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003659 struct btrfs_free_space *entry;
3660 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003661 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003662 u64 extent_start;
3663 u64 extent_bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003664 enum btrfs_trim_state extent_trim_state;
Li Zefan7fe1e642011-12-29 14:47:27 +08003665 u64 bytes;
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003666 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Dongyangf7039b12011-03-24 10:24:28 +00003667
3668 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003669 struct btrfs_trim_range trim_entry;
3670
3671 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003672 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003673
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003674 if (ctl->free_space < minlen)
3675 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003676
Li Zefan34d52cb2011-03-29 13:46:06 +08003677 entry = tree_search_offset(ctl, start, 0, 1);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003678 if (!entry)
3679 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003680
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003681 /* Skip bitmaps and if async, already trimmed entries */
3682 while (entry->bitmap ||
3683 (async && btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003684 node = rb_next(&entry->offset_index);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003685 if (!node)
3686 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003687 entry = rb_entry(node, struct btrfs_free_space,
3688 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003689 }
3690
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003691 if (entry->offset >= end)
3692 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003693
3694 extent_start = entry->offset;
3695 extent_bytes = entry->bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003696 extent_trim_state = entry->trim_state;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003697 if (async) {
3698 start = entry->offset;
3699 bytes = entry->bytes;
3700 if (bytes < minlen) {
3701 spin_unlock(&ctl->tree_lock);
3702 mutex_unlock(&ctl->cache_writeout_mutex);
3703 goto next;
3704 }
Nikolay Borisov32e16492021-11-23 14:44:20 +02003705 unlink_free_space(ctl, entry, true);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003706 /*
3707 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3708 * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
3709 * X when we come back around. So trim it now.
3710 */
3711 if (max_discard_size &&
3712 bytes >= (max_discard_size +
3713 BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003714 bytes = max_discard_size;
3715 extent_bytes = max_discard_size;
3716 entry->offset += max_discard_size;
3717 entry->bytes -= max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003718 link_free_space(ctl, entry);
3719 } else {
3720 kmem_cache_free(btrfs_free_space_cachep, entry);
3721 }
3722 } else {
3723 start = max(start, extent_start);
3724 bytes = min(extent_start + extent_bytes, end) - start;
3725 if (bytes < minlen) {
3726 spin_unlock(&ctl->tree_lock);
3727 mutex_unlock(&ctl->cache_writeout_mutex);
3728 goto next;
3729 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003730
Nikolay Borisov32e16492021-11-23 14:44:20 +02003731 unlink_free_space(ctl, entry, true);
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003732 kmem_cache_free(btrfs_free_space_cachep, entry);
3733 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003734
Li Zefan34d52cb2011-03-29 13:46:06 +08003735 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003736 trim_entry.start = extent_start;
3737 trim_entry.bytes = extent_bytes;
3738 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3739 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003740
Li Zefan7fe1e642011-12-29 14:47:27 +08003741 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003742 extent_start, extent_bytes, extent_trim_state,
3743 &trim_entry);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003744 if (ret) {
3745 block_group->discard_cursor = start + bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003746 break;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003747 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003748next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003749 start += bytes;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003750 block_group->discard_cursor = start;
3751 if (async && *total_trimmed)
3752 break;
Li Dongyangf7039b12011-03-24 10:24:28 +00003753
3754 if (fatal_signal_pending(current)) {
3755 ret = -ERESTARTSYS;
3756 break;
3757 }
3758
3759 cond_resched();
3760 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003761
3762 return ret;
3763
3764out_unlock:
3765 block_group->discard_cursor = btrfs_block_group_end(block_group);
3766 spin_unlock(&ctl->tree_lock);
3767 mutex_unlock(&ctl->cache_writeout_mutex);
3768
Li Zefan7fe1e642011-12-29 14:47:27 +08003769 return ret;
3770}
3771
Dennis Zhouda080fe2019-12-13 16:22:13 -08003772/*
3773 * If we break out of trimming a bitmap prematurely, we should reset the
3774 * trimming bit. In a rather contrieved case, it's possible to race here so
3775 * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
3776 *
3777 * start = start of bitmap
3778 * end = near end of bitmap
3779 *
3780 * Thread 1: Thread 2:
3781 * trim_bitmaps(start)
3782 * trim_bitmaps(end)
3783 * end_trimming_bitmap()
3784 * reset_trimming_bitmap()
3785 */
3786static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
3787{
3788 struct btrfs_free_space *entry;
3789
3790 spin_lock(&ctl->tree_lock);
3791 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003792 if (entry) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003793 if (btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003794 ctl->discardable_extents[BTRFS_STAT_CURR] +=
3795 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003796 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
3797 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003798 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003799 }
3800
Dennis Zhouda080fe2019-12-13 16:22:13 -08003801 spin_unlock(&ctl->tree_lock);
3802}
3803
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003804static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
3805 struct btrfs_free_space *entry)
Dennis Zhouda080fe2019-12-13 16:22:13 -08003806{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003807 if (btrfs_free_space_trimming_bitmap(entry)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003808 entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003809 ctl->discardable_extents[BTRFS_STAT_CURR] -=
3810 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003811 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003812 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003813}
3814
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003815/*
3816 * If @async is set, then we will trim 1 region and return.
3817 */
David Sterba32da53862019-10-29 19:20:18 +01003818static int trim_bitmaps(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003819 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003820 u64 maxlen, bool async)
Li Zefan7fe1e642011-12-29 14:47:27 +08003821{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003822 struct btrfs_discard_ctl *discard_ctl =
3823 &block_group->fs_info->discard_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003824 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3825 struct btrfs_free_space *entry;
3826 int ret = 0;
3827 int ret2;
3828 u64 bytes;
3829 u64 offset = offset_to_bitmap(ctl, start);
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003830 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Zefan7fe1e642011-12-29 14:47:27 +08003831
3832 while (offset < end) {
3833 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003834 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003835
Filipe Manana55507ce2014-12-01 17:04:09 +00003836 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003837 spin_lock(&ctl->tree_lock);
3838
3839 if (ctl->free_space < minlen) {
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003840 block_group->discard_cursor =
3841 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003842 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003843 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003844 break;
3845 }
3846
3847 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003848 /*
3849 * Bitmaps are marked trimmed lossily now to prevent constant
3850 * discarding of the same bitmap (the reason why we are bound
3851 * by the filters). So, retrim the block group bitmaps when we
3852 * are preparing to punt to the unused_bgs list. This uses
3853 * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
3854 * which is the only discard index which sets minlen to 0.
3855 */
3856 if (!entry || (async && minlen && start == offset &&
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003857 btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003858 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003859 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003860 next_bitmap = true;
3861 goto next;
3862 }
3863
Dennis Zhouda080fe2019-12-13 16:22:13 -08003864 /*
3865 * Async discard bitmap trimming begins at by setting the start
3866 * to be key.objectid and the offset_to_bitmap() aligns to the
3867 * start of the bitmap. This lets us know we are fully
3868 * scanning the bitmap rather than only some portion of it.
3869 */
3870 if (start == offset)
3871 entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
3872
Li Zefan7fe1e642011-12-29 14:47:27 +08003873 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003874 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003875 if (ret2 || start >= end) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003876 /*
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003877 * We lossily consider a bitmap trimmed if we only skip
3878 * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
Dennis Zhouda080fe2019-12-13 16:22:13 -08003879 */
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003880 if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003881 end_trimming_bitmap(ctl, entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003882 else
3883 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003884 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003885 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003886 next_bitmap = true;
3887 goto next;
3888 }
3889
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003890 /*
3891 * We already trimmed a region, but are using the locking above
3892 * to reset the trim_state.
3893 */
3894 if (async && *total_trimmed) {
3895 spin_unlock(&ctl->tree_lock);
3896 mutex_unlock(&ctl->cache_writeout_mutex);
3897 goto out;
3898 }
3899
Li Zefan7fe1e642011-12-29 14:47:27 +08003900 bytes = min(bytes, end - start);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003901 if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003902 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003903 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003904 goto next;
3905 }
3906
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003907 /*
3908 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3909 * If X < @minlen, we won't trim X when we come back around.
3910 * So trim it now. We differ here from trimming extents as we
3911 * don't keep individual state per bit.
3912 */
3913 if (async &&
3914 max_discard_size &&
3915 bytes > (max_discard_size + minlen))
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003916 bytes = max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003917
Nikolay Borisovf594f132021-11-23 14:44:19 +02003918 bitmap_clear_bits(ctl, entry, start, bytes, true);
Li Zefan7fe1e642011-12-29 14:47:27 +08003919 if (entry->bytes == 0)
3920 free_bitmap(ctl, entry);
3921
3922 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003923 trim_entry.start = start;
3924 trim_entry.bytes = bytes;
3925 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3926 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003927
3928 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003929 start, bytes, 0, &trim_entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003930 if (ret) {
3931 reset_trimming_bitmap(ctl, offset);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003932 block_group->discard_cursor =
3933 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003934 break;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003935 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003936next:
3937 if (next_bitmap) {
3938 offset += BITS_PER_BITMAP * ctl->unit;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003939 start = offset;
Li Zefan7fe1e642011-12-29 14:47:27 +08003940 } else {
3941 start += bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003942 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003943 block_group->discard_cursor = start;
Li Zefan7fe1e642011-12-29 14:47:27 +08003944
3945 if (fatal_signal_pending(current)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003946 if (start != offset)
3947 reset_trimming_bitmap(ctl, offset);
Li Zefan7fe1e642011-12-29 14:47:27 +08003948 ret = -ERESTARTSYS;
3949 break;
3950 }
3951
3952 cond_resched();
3953 }
3954
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003955 if (offset >= end)
3956 block_group->discard_cursor = end;
3957
3958out:
Li Zefan7fe1e642011-12-29 14:47:27 +08003959 return ret;
3960}
3961
David Sterba32da53862019-10-29 19:20:18 +01003962int btrfs_trim_block_group(struct btrfs_block_group *block_group,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003963 u64 *trimmed, u64 start, u64 end, u64 minlen)
3964{
Dennis Zhouda080fe2019-12-13 16:22:13 -08003965 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003966 int ret;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003967 u64 rem = 0;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003968
Naohiro Aota2eda5702021-02-04 19:21:53 +09003969 ASSERT(!btrfs_is_zoned(block_group->fs_info));
3970
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003971 *trimmed = 0;
3972
3973 spin_lock(&block_group->lock);
3974 if (block_group->removed) {
3975 spin_unlock(&block_group->lock);
3976 return 0;
3977 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003978 btrfs_freeze_block_group(block_group);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003979 spin_unlock(&block_group->lock);
3980
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003981 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003982 if (ret)
3983 goto out;
3984
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003985 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003986 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
3987 /* If we ended in the middle of a bitmap, reset the trimming flag */
3988 if (rem)
3989 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003990out:
Filipe Manana6b7304a2020-05-08 11:01:47 +01003991 btrfs_unfreeze_block_group(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003992 return ret;
3993}
Li Zefan581bb052011-04-20 10:06:11 +08003994
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003995int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
3996 u64 *trimmed, u64 start, u64 end, u64 minlen,
3997 bool async)
3998{
3999 int ret;
4000
4001 *trimmed = 0;
4002
4003 spin_lock(&block_group->lock);
4004 if (block_group->removed) {
4005 spin_unlock(&block_group->lock);
4006 return 0;
4007 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01004008 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004009 spin_unlock(&block_group->lock);
4010
4011 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
Filipe Manana6b7304a2020-05-08 11:01:47 +01004012 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004013
4014 return ret;
4015}
4016
4017int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
4018 u64 *trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05004019 u64 maxlen, bool async)
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004020{
4021 int ret;
4022
4023 *trimmed = 0;
4024
4025 spin_lock(&block_group->lock);
4026 if (block_group->removed) {
4027 spin_unlock(&block_group->lock);
4028 return 0;
4029 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01004030 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004031 spin_unlock(&block_group->lock);
4032
Dennis Zhou7fe6d452020-01-02 16:26:39 -05004033 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
4034 async);
4035
Filipe Manana6b7304a2020-05-08 11:01:47 +01004036 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08004037
4038 return ret;
4039}
4040
Boris Burkov94846222020-11-18 15:06:22 -08004041bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info)
4042{
4043 return btrfs_super_cache_generation(fs_info->super_copy);
4044}
4045
Boris Burkov36b216c2020-11-18 15:06:25 -08004046static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info,
4047 struct btrfs_trans_handle *trans)
4048{
4049 struct btrfs_block_group *block_group;
4050 struct rb_node *node;
Tom Rix77364fa2021-04-30 11:06:55 -07004051 int ret = 0;
Boris Burkov36b216c2020-11-18 15:06:25 -08004052
4053 btrfs_info(fs_info, "cleaning free space cache v1");
4054
4055 node = rb_first(&fs_info->block_group_cache_tree);
4056 while (node) {
4057 block_group = rb_entry(node, struct btrfs_block_group, cache_node);
4058 ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
4059 if (ret)
4060 goto out;
4061 node = rb_next(node);
4062 }
4063out:
4064 return ret;
4065}
4066
Boris Burkov94846222020-11-18 15:06:22 -08004067int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active)
4068{
4069 struct btrfs_trans_handle *trans;
4070 int ret;
4071
4072 /*
Boris Burkov36b216c2020-11-18 15:06:25 -08004073 * update_super_roots will appropriately set or unset
4074 * super_copy->cache_generation based on SPACE_CACHE and
4075 * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a
4076 * transaction commit whether we are enabling space cache v1 and don't
4077 * have any other work to do, or are disabling it and removing free
4078 * space inodes.
Boris Burkov94846222020-11-18 15:06:22 -08004079 */
4080 trans = btrfs_start_transaction(fs_info->tree_root, 0);
4081 if (IS_ERR(trans))
4082 return PTR_ERR(trans);
4083
Boris Burkov36b216c2020-11-18 15:06:25 -08004084 if (!active) {
Boris Burkov94846222020-11-18 15:06:22 -08004085 set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
Boris Burkov36b216c2020-11-18 15:06:25 -08004086 ret = cleanup_free_space_cache_v1(fs_info, trans);
4087 if (ret) {
4088 btrfs_abort_transaction(trans, ret);
4089 btrfs_end_transaction(trans);
4090 goto out;
4091 }
4092 }
Boris Burkov94846222020-11-18 15:06:22 -08004093
4094 ret = btrfs_commit_transaction(trans);
Boris Burkov36b216c2020-11-18 15:06:25 -08004095out:
Boris Burkov94846222020-11-18 15:06:22 -08004096 clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
4097
4098 return ret;
4099}
4100
Josef Bacik74255aa2013-03-15 09:47:08 -04004101#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04004102/*
4103 * Use this if you need to make a bitmap or extent entry specifically, it
4104 * doesn't do any of the merging that add_free_space does, this acts a lot like
4105 * how the free space cache loading stuff works, so you can get really weird
4106 * configurations.
4107 */
David Sterba32da53862019-10-29 19:20:18 +01004108int test_add_free_space_entry(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004109 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04004110{
Josef Bacikdc11dd52013-08-14 15:05:12 -04004111 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4112 struct btrfs_free_space *info = NULL, *bitmap_info;
4113 void *map = NULL;
Dennis Zhouda080fe2019-12-13 16:22:13 -08004114 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004115 u64 bytes_added;
4116 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04004117
Josef Bacikdc11dd52013-08-14 15:05:12 -04004118again:
4119 if (!info) {
4120 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
4121 if (!info)
4122 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04004123 }
4124
Josef Bacikdc11dd52013-08-14 15:05:12 -04004125 if (!bitmap) {
4126 spin_lock(&ctl->tree_lock);
4127 info->offset = offset;
4128 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04004129 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004130 ret = link_free_space(ctl, info);
4131 spin_unlock(&ctl->tree_lock);
4132 if (ret)
4133 kmem_cache_free(btrfs_free_space_cachep, info);
4134 return ret;
4135 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004136
Josef Bacikdc11dd52013-08-14 15:05:12 -04004137 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00004138 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004139 if (!map) {
4140 kmem_cache_free(btrfs_free_space_cachep, info);
4141 return -ENOMEM;
4142 }
4143 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004144
Josef Bacikdc11dd52013-08-14 15:05:12 -04004145 spin_lock(&ctl->tree_lock);
4146 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4147 1, 0);
4148 if (!bitmap_info) {
4149 info->bitmap = map;
4150 map = NULL;
4151 add_new_bitmap(ctl, info, offset);
4152 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01004153 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004154 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004155
Dennis Zhouda080fe2019-12-13 16:22:13 -08004156 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
4157 trim_state);
Josef Bacikcef40482015-10-02 16:09:42 -04004158
Josef Bacikdc11dd52013-08-14 15:05:12 -04004159 bytes -= bytes_added;
4160 offset += bytes_added;
4161 spin_unlock(&ctl->tree_lock);
4162
4163 if (bytes)
4164 goto again;
4165
Filipe Manana20005522014-08-29 13:35:13 +01004166 if (info)
4167 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00004168 if (map)
4169 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004170 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004171}
4172
4173/*
4174 * Checks to see if the given range is in the free space cache. This is really
4175 * just used to check the absence of space, so if there is free space in the
4176 * range at all we will return 1.
4177 */
David Sterba32da53862019-10-29 19:20:18 +01004178int test_check_exists(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004179 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04004180{
4181 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4182 struct btrfs_free_space *info;
4183 int ret = 0;
4184
4185 spin_lock(&ctl->tree_lock);
4186 info = tree_search_offset(ctl, offset, 0, 0);
4187 if (!info) {
4188 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4189 1, 0);
4190 if (!info)
4191 goto out;
4192 }
4193
4194have_info:
4195 if (info->bitmap) {
4196 u64 bit_off, bit_bytes;
4197 struct rb_node *n;
4198 struct btrfs_free_space *tmp;
4199
4200 bit_off = offset;
4201 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04004202 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04004203 if (!ret) {
4204 if (bit_off == offset) {
4205 ret = 1;
4206 goto out;
4207 } else if (bit_off > offset &&
4208 offset + bytes > bit_off) {
4209 ret = 1;
4210 goto out;
4211 }
4212 }
4213
4214 n = rb_prev(&info->offset_index);
4215 while (n) {
4216 tmp = rb_entry(n, struct btrfs_free_space,
4217 offset_index);
4218 if (tmp->offset + tmp->bytes < offset)
4219 break;
4220 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004221 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004222 continue;
4223 }
4224 info = tmp;
4225 goto have_info;
4226 }
4227
4228 n = rb_next(&info->offset_index);
4229 while (n) {
4230 tmp = rb_entry(n, struct btrfs_free_space,
4231 offset_index);
4232 if (offset + bytes < tmp->offset)
4233 break;
4234 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004235 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004236 continue;
4237 }
4238 info = tmp;
4239 goto have_info;
4240 }
4241
Filipe Manana20005522014-08-29 13:35:13 +01004242 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004243 goto out;
4244 }
4245
4246 if (info->offset == offset) {
4247 ret = 1;
4248 goto out;
4249 }
4250
4251 if (offset > info->offset && offset < info->offset + info->bytes)
4252 ret = 1;
4253out:
4254 spin_unlock(&ctl->tree_lock);
4255 return ret;
4256}
Josef Bacikdc11dd52013-08-14 15:05:12 -04004257#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */