blob: 4d8897879c9cbd98de1032e4e1b6ea0857c3f64a [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>
Josef Bacik0f9dd462008-09-23 13:14:11 -040014#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040015#include "free-space-cache.h"
16#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040017#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000018#include "extent_io.h"
Filipe Manana04216822014-11-27 21:14:15 +000019#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040020#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040021#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040022#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080023#include "discard.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040024
Feifei Xu0ef64472016-06-01 19:18:24 +080025#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Dennis Zhou5d90c5c2020-01-02 16:26:43 -050026#define MAX_CACHE_BYTES_PER_GIG SZ_64K
27#define FORCE_EXTENT_THRESHOLD SZ_1M
Josef Bacik96303082009-07-13 21:29:25 -040028
Filipe Manana55507ce2014-12-01 17:04:09 +000029struct btrfs_trim_range {
30 u64 start;
31 u64 bytes;
32 struct list_head list;
33};
34
Li Zefan34d52cb2011-03-29 13:46:06 +080035static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040036 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040037static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
38 struct btrfs_free_space *info);
Josef Bacikcd799092020-10-23 09:58:08 -040039static int search_bitmap(struct btrfs_free_space_ctl *ctl,
40 struct btrfs_free_space *bitmap_info, u64 *offset,
41 u64 *bytes, bool for_alloc);
42static void free_bitmap(struct btrfs_free_space_ctl *ctl,
43 struct btrfs_free_space *bitmap_info);
44static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
45 struct btrfs_free_space *info, u64 offset,
46 u64 bytes);
Josef Bacik0cb59c92010-07-02 12:14:14 -040047
Li Zefan0414efa2011-04-20 10:20:14 +080048static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
49 struct btrfs_path *path,
50 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040052 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040053 struct btrfs_key key;
54 struct btrfs_key location;
55 struct btrfs_disk_key disk_key;
56 struct btrfs_free_space_header *header;
57 struct extent_buffer *leaf;
58 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040059 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040060 int ret;
61
Josef Bacik0af3d002010-06-21 14:48:16 -040062 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080063 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040064 key.type = 0;
65
66 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
67 if (ret < 0)
68 return ERR_PTR(ret);
69 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020070 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040071 return ERR_PTR(-ENOENT);
72 }
73
74 leaf = path->nodes[0];
75 header = btrfs_item_ptr(leaf, path->slots[0],
76 struct btrfs_free_space_header);
77 btrfs_free_space_key(leaf, header, &disk_key);
78 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020079 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040080
Josef Bacik84de76a2018-09-28 07:17:49 -040081 /*
82 * We are often under a trans handle at this point, so we need to make
83 * sure NOFS is set to keep us from deadlocking.
84 */
85 nofs_flag = memalloc_nofs_save();
David Sterba0202e832020-05-15 19:35:59 +020086 inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
Filipe Manana4222ea72018-10-24 10:13:03 +010087 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040088 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040089 if (IS_ERR(inode))
90 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040091
Al Viro528c0322012-04-13 11:03:55 -040092 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080093 mapping_gfp_constraint(inode->i_mapping,
94 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000095
Li Zefan0414efa2011-04-20 10:20:14 +080096 return inode;
97}
98
David Sterba32da53862019-10-29 19:20:18 +010099struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
David Sterba7949f332019-03-20 13:40:19 +0100100 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +0800101{
David Sterba7949f332019-03-20 13:40:19 +0100102 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +0800103 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400104 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800105
106 spin_lock(&block_group->lock);
107 if (block_group->inode)
108 inode = igrab(block_group->inode);
109 spin_unlock(&block_group->lock);
110 if (inode)
111 return inode;
112
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500113 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200114 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800115 if (IS_ERR(inode))
116 return inode;
117
Josef Bacik0af3d002010-06-21 14:48:16 -0400118 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400119 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400120 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400121 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
122 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400123 block_group->disk_cache_state = BTRFS_DC_CLEAR;
124 }
125
Josef Bacik300e4f82011-08-29 14:06:00 -0400126 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400127 block_group->inode = igrab(inode);
128 block_group->iref = 1;
129 }
130 spin_unlock(&block_group->lock);
131
132 return inode;
133}
134
Eric Sandeen48a3b632013-04-25 20:41:01 +0000135static int __create_free_space_inode(struct btrfs_root *root,
136 struct btrfs_trans_handle *trans,
137 struct btrfs_path *path,
138 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400139{
140 struct btrfs_key key;
141 struct btrfs_disk_key disk_key;
142 struct btrfs_free_space_header *header;
143 struct btrfs_inode_item *inode_item;
144 struct extent_buffer *leaf;
Nikolay Borisovf0d12192020-12-03 10:09:49 +0200145 /* We inline CRCs for the free disk space cache */
146 const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC |
147 BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Josef Bacik0af3d002010-06-21 14:48:16 -0400148 int ret;
149
Li Zefan0414efa2011-04-20 10:20:14 +0800150 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400151 if (ret)
152 return ret;
153
154 leaf = path->nodes[0];
155 inode_item = btrfs_item_ptr(leaf, path->slots[0],
156 struct btrfs_inode_item);
157 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100158 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400159 sizeof(*inode_item));
160 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
161 btrfs_set_inode_size(leaf, inode_item, 0);
162 btrfs_set_inode_nbytes(leaf, inode_item, 0);
163 btrfs_set_inode_uid(leaf, inode_item, 0);
164 btrfs_set_inode_gid(leaf, inode_item, 0);
165 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400166 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400167 btrfs_set_inode_nlink(leaf, inode_item, 1);
168 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800169 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400170 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200171 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400172
173 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800174 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400175 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 sizeof(struct btrfs_free_space_header));
178 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200179 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400180 return ret;
181 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700182
Josef Bacik0af3d002010-06-21 14:48:16 -0400183 leaf = path->nodes[0];
184 header = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100186 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400187 btrfs_set_free_space_key(leaf, header, &disk_key);
188 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200189 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400190
191 return 0;
192}
193
David Sterba4ca75f12019-03-20 13:42:57 +0100194int create_free_space_inode(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100195 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +0800196 struct btrfs_path *path)
197{
198 int ret;
199 u64 ino;
200
David Sterba4ca75f12019-03-20 13:42:57 +0100201 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800202 if (ret < 0)
203 return ret;
204
David Sterba4ca75f12019-03-20 13:42:57 +0100205 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200206 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800207}
208
Boris Burkov36b216c2020-11-18 15:06:25 -0800209/*
210 * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode
211 * handles lookup, otherwise it takes ownership and iputs the inode.
212 * Don't reuse an inode pointer after passing it into this function.
213 */
214int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
215 struct inode *inode,
216 struct btrfs_block_group *block_group)
217{
218 struct btrfs_path *path;
219 struct btrfs_key key;
220 int ret = 0;
221
222 path = btrfs_alloc_path();
223 if (!path)
224 return -ENOMEM;
225
226 if (!inode)
227 inode = lookup_free_space_inode(block_group, path);
228 if (IS_ERR(inode)) {
229 if (PTR_ERR(inode) != -ENOENT)
230 ret = PTR_ERR(inode);
231 goto out;
232 }
233 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
234 if (ret) {
235 btrfs_add_delayed_iput(inode);
236 goto out;
237 }
238 clear_nlink(inode);
239 /* One for the block groups ref */
240 spin_lock(&block_group->lock);
241 if (block_group->iref) {
242 block_group->iref = 0;
243 block_group->inode = NULL;
244 spin_unlock(&block_group->lock);
245 iput(inode);
246 } else {
247 spin_unlock(&block_group->lock);
248 }
249 /* One for the lookup ref */
250 btrfs_add_delayed_iput(inode);
251
252 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
253 key.type = 0;
254 key.offset = block_group->start;
255 ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path,
256 -1, 1);
257 if (ret) {
258 if (ret > 0)
259 ret = 0;
260 goto out;
261 }
262 ret = btrfs_del_item(trans, trans->fs_info->tree_root, path);
263out:
264 btrfs_free_path(path);
265 return ret;
266}
267
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400268int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000269 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400270{
Josef Bacikc8174312011-11-02 09:29:35 -0400271 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000272 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400273
274 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400275 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
276 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400277
Miao Xie7b61cd92013-05-13 13:55:09 +0000278 spin_lock(&rsv->lock);
279 if (rsv->reserved < needed_bytes)
280 ret = -ENOSPC;
281 else
282 ret = 0;
283 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000284 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000285}
286
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500287int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100288 struct btrfs_block_group *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000289 struct inode *inode)
290{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500291 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000292 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100293 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700294
Chris Mason1bbc6212015-04-06 12:46:08 -0700295 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500296 struct btrfs_path *path = btrfs_alloc_path();
297
298 if (!path) {
299 ret = -ENOMEM;
300 goto fail;
301 }
Filipe Manana35c76642015-04-30 17:47:05 +0100302 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700303 mutex_lock(&trans->transaction->cache_write_mutex);
304 if (!list_empty(&block_group->io_list)) {
305 list_del_init(&block_group->io_list);
306
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400307 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700308 btrfs_put_block_group(block_group);
309 }
310
311 /*
312 * now that we've truncated the cache away, its no longer
313 * setup or written
314 */
315 spin_lock(&block_group->lock);
316 block_group->disk_cache_state = BTRFS_DC_CLEAR;
317 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500318 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700319 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400320
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200321 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700322 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400323
324 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700325 * We skip the throttling logic for free space cache inodes, so we don't
326 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400327 */
Nikolay Borisov50743392020-11-02 16:48:55 +0200328 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Josef Bacik0af3d002010-06-21 14:48:16 -0400329 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100330 if (ret)
331 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400332
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200333 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason1bbc6212015-04-06 12:46:08 -0700334
Chris Mason1bbc6212015-04-06 12:46:08 -0700335fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100336 if (locked)
337 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100338 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400339 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400340
Li Zefan82d59022011-04-20 10:33:24 +0800341 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400342}
343
David Sterba1d480532017-01-23 17:28:19 +0100344static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400345{
346 struct file_ra_state *ra;
347 unsigned long last_index;
348
349 ra = kzalloc(sizeof(*ra), GFP_NOFS);
350 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100351 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400352
353 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300354 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400355
356 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
357
358 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400359}
360
Chris Mason4c6d1d82015-04-06 13:17:20 -0700361static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400362 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400363{
Miao Xie5349d6c2014-06-19 10:42:49 +0800364 int num_pages;
Miao Xie5349d6c2014-06-19 10:42:49 +0800365
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300366 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800367
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400368 /* Make sure we can fit our crcs and generation into the first page */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200369 if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800370 return -ENOSPC;
371
Chris Mason4c6d1d82015-04-06 13:17:20 -0700372 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800373
David Sterba31e818f2015-02-20 18:00:26 +0100374 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400375 if (!io_ctl->pages)
376 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800377
378 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400379 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700380 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800381
Josef Bacika67509c2011-10-05 15:18:58 -0400382 return 0;
383}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900384ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400385
Chris Mason4c6d1d82015-04-06 13:17:20 -0700386static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400387{
388 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700389 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400390}
391
Chris Mason4c6d1d82015-04-06 13:17:20 -0700392static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400393{
394 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400395 io_ctl->cur = NULL;
396 io_ctl->orig = NULL;
397 }
398}
399
Chris Mason4c6d1d82015-04-06 13:17:20 -0700400static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400401{
Josef Bacikb12d6862013-08-26 17:14:08 -0400402 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400403 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700404 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400405 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300406 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400407 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200408 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400409}
410
Chris Mason4c6d1d82015-04-06 13:17:20 -0700411static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400412{
413 int i;
414
415 io_ctl_unmap_page(io_ctl);
416
417 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800418 if (io_ctl->pages[i]) {
419 ClearPageChecked(io_ctl->pages[i]);
420 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300421 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800422 }
Josef Bacika67509c2011-10-05 15:18:58 -0400423 }
424}
425
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900426static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
Josef Bacika67509c2011-10-05 15:18:58 -0400427{
428 struct page *page;
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900429 struct inode *inode = io_ctl->inode;
Josef Bacika67509c2011-10-05 15:18:58 -0400430 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
431 int i;
432
433 for (i = 0; i < io_ctl->num_pages; i++) {
434 page = find_or_create_page(inode->i_mapping, i, mask);
435 if (!page) {
436 io_ctl_drop_pages(io_ctl);
437 return -ENOMEM;
438 }
439 io_ctl->pages[i] = page;
440 if (uptodate && !PageUptodate(page)) {
441 btrfs_readpage(NULL, page);
442 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400443 if (page->mapping != inode->i_mapping) {
444 btrfs_err(BTRFS_I(inode)->root->fs_info,
445 "free space cache page truncated");
446 io_ctl_drop_pages(io_ctl);
447 return -EIO;
448 }
Josef Bacika67509c2011-10-05 15:18:58 -0400449 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500450 btrfs_err(BTRFS_I(inode)->root->fs_info,
451 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400452 io_ctl_drop_pages(io_ctl);
453 return -EIO;
454 }
455 }
456 }
457
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500458 for (i = 0; i < io_ctl->num_pages; i++) {
459 clear_page_dirty_for_io(io_ctl->pages[i]);
460 set_page_extent_mapped(io_ctl->pages[i]);
461 }
462
Josef Bacika67509c2011-10-05 15:18:58 -0400463 return 0;
464}
465
Chris Mason4c6d1d82015-04-06 13:17:20 -0700466static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400467{
Josef Bacika67509c2011-10-05 15:18:58 -0400468 io_ctl_map_page(io_ctl, 1);
469
470 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400471 * Skip the csum areas. If we don't check crcs then we just have a
472 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400473 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200474 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
475 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400476
David Sterba6994ca362020-09-15 13:32:34 +0200477 put_unaligned_le64(generation, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400478 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400479}
480
Chris Mason4c6d1d82015-04-06 13:17:20 -0700481static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400482{
David Sterba6994ca362020-09-15 13:32:34 +0200483 u64 cache_gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400484
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400485 /*
486 * Skip the crc area. If we don't check crcs then we just have a 64bit
487 * chunk at the front of the first page.
488 */
Nikolay Borisov7dbdb442020-12-03 10:09:48 +0200489 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
490 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400491
David Sterba6994ca362020-09-15 13:32:34 +0200492 cache_gen = get_unaligned_le64(io_ctl->cur);
493 if (cache_gen != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400494 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200495 "space cache generation (%llu) does not match inode (%llu)",
David Sterba6994ca362020-09-15 13:32:34 +0200496 cache_gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400497 io_ctl_unmap_page(io_ctl);
498 return -EIO;
499 }
500 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400501 return 0;
502}
503
Chris Mason4c6d1d82015-04-06 13:17:20 -0700504static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505{
506 u32 *tmp;
507 u32 crc = ~(u32)0;
508 unsigned offset = 0;
509
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400510 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800511 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400512
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200513 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
514 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400515 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700516 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400517 tmp += index;
518 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400519}
520
Chris Mason4c6d1d82015-04-06 13:17:20 -0700521static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400522{
523 u32 *tmp, val;
524 u32 crc = ~(u32)0;
525 unsigned offset = 0;
526
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400527 if (index == 0)
528 offset = sizeof(u32) * io_ctl->num_pages;
529
Chris Mason2b108262015-04-06 07:48:20 -0700530 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400531 tmp += index;
532 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400533
534 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200535 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
536 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400537 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400538 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200539 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400540 io_ctl_unmap_page(io_ctl);
541 return -EIO;
542 }
543
Josef Bacika67509c2011-10-05 15:18:58 -0400544 return 0;
545}
546
Chris Mason4c6d1d82015-04-06 13:17:20 -0700547static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400548 void *bitmap)
549{
550 struct btrfs_free_space_entry *entry;
551
552 if (!io_ctl->cur)
553 return -ENOSPC;
554
555 entry = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200556 put_unaligned_le64(offset, &entry->offset);
557 put_unaligned_le64(bytes, &entry->bytes);
Josef Bacika67509c2011-10-05 15:18:58 -0400558 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
559 BTRFS_FREE_SPACE_EXTENT;
560 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
561 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
562
563 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
564 return 0;
565
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400566 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400567
568 /* No more pages to map */
569 if (io_ctl->index >= io_ctl->num_pages)
570 return 0;
571
572 /* map the next page */
573 io_ctl_map_page(io_ctl, 1);
574 return 0;
575}
576
Chris Mason4c6d1d82015-04-06 13:17:20 -0700577static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400578{
579 if (!io_ctl->cur)
580 return -ENOSPC;
581
582 /*
583 * If we aren't at the start of the current page, unmap this one and
584 * map the next one if there is any left.
585 */
586 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400587 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400588 if (io_ctl->index >= io_ctl->num_pages)
589 return -ENOSPC;
590 io_ctl_map_page(io_ctl, 0);
591 }
592
David Sterba69d24802018-06-29 10:56:44 +0200593 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400595 if (io_ctl->index < io_ctl->num_pages)
596 io_ctl_map_page(io_ctl, 0);
597 return 0;
598}
599
Chris Mason4c6d1d82015-04-06 13:17:20 -0700600static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400601{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400602 /*
603 * If we're not on the boundary we know we've modified the page and we
604 * need to crc the page.
605 */
606 if (io_ctl->cur != io_ctl->orig)
607 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
608 else
609 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400610
611 while (io_ctl->index < io_ctl->num_pages) {
612 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400613 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400614 }
615}
616
Chris Mason4c6d1d82015-04-06 13:17:20 -0700617static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400618 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400619{
620 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500621 int ret;
622
623 if (!io_ctl->cur) {
624 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
625 if (ret)
626 return ret;
627 }
Josef Bacika67509c2011-10-05 15:18:58 -0400628
629 e = io_ctl->cur;
David Sterba6994ca362020-09-15 13:32:34 +0200630 entry->offset = get_unaligned_le64(&e->offset);
631 entry->bytes = get_unaligned_le64(&e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400632 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400633 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
634 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
635
636 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400637 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400638
639 io_ctl_unmap_page(io_ctl);
640
Josef Bacik2f120c02011-11-10 20:45:05 -0500641 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400642}
643
Chris Mason4c6d1d82015-04-06 13:17:20 -0700644static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400645 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400646{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400647 int ret;
648
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400649 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
650 if (ret)
651 return ret;
652
David Sterba69d24802018-06-29 10:56:44 +0200653 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400654 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400655
656 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400657}
658
David Sterbafa598b02020-12-03 17:18:38 +0100659static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
660{
661 struct btrfs_block_group *block_group = ctl->private;
662 u64 max_bytes;
663 u64 bitmap_bytes;
664 u64 extent_bytes;
665 u64 size = block_group->length;
666 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
667 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
668
669 max_bitmaps = max_t(u64, max_bitmaps, 1);
670
671 ASSERT(ctl->total_bitmaps <= max_bitmaps);
672
673 /*
674 * We are trying to keep the total amount of memory used per 1GiB of
675 * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
676 * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
677 * bitmaps, we may end up using more memory than this.
678 */
679 if (size < SZ_1G)
680 max_bytes = MAX_CACHE_BYTES_PER_GIG;
681 else
682 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
683
684 bitmap_bytes = ctl->total_bitmaps * ctl->unit;
685
686 /*
687 * we want the extent entry threshold to always be at most 1/2 the max
688 * bytes we can have, or whatever is less than that.
689 */
690 extent_bytes = max_bytes - bitmap_bytes;
691 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
692
693 ctl->extents_thresh =
694 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
695}
696
Eric Sandeen48a3b632013-04-25 20:41:01 +0000697static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
698 struct btrfs_free_space_ctl *ctl,
699 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400700{
David Sterba3ffbd682018-06-29 10:56:42 +0200701 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400702 struct btrfs_free_space_header *header;
703 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700704 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400705 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400706 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800707 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400708 u64 num_entries;
709 u64 num_bitmaps;
710 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400711 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400712 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400713
Josef Bacik9d66e232010-08-25 16:54:15 -0400714 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800715 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400716 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400717
718 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800719 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 key.type = 0;
721
722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800723 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400724 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800725 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400726 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400727 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400728 }
729
Li Zefan0414efa2011-04-20 10:20:14 +0800730 ret = -1;
731
Josef Bacik9d66e232010-08-25 16:54:15 -0400732 leaf = path->nodes[0];
733 header = btrfs_item_ptr(leaf, path->slots[0],
734 struct btrfs_free_space_header);
735 num_entries = btrfs_free_space_entries(leaf, header);
736 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
737 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400738 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400739
Miao Xiee570fd22014-06-19 10:42:50 +0800740 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400741 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200742 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800743 offset);
744 return 0;
745 }
746
Josef Bacik9d66e232010-08-25 16:54:15 -0400747 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400748 btrfs_err(fs_info,
749 "free space inode generation (%llu) did not match free space cache generation (%llu)",
750 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400751 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400752 }
753
754 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400755 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400756
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400757 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800758 if (ret)
759 return ret;
760
David Sterba1d480532017-01-23 17:28:19 +0100761 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400762
Johannes Thumshirn7a195f62020-02-12 00:10:20 +0900763 ret = io_ctl_prepare_pages(&io_ctl, true);
Josef Bacika67509c2011-10-05 15:18:58 -0400764 if (ret)
765 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400766
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400767 ret = io_ctl_check_crc(&io_ctl, 0);
768 if (ret)
769 goto free_cache;
770
Josef Bacika67509c2011-10-05 15:18:58 -0400771 ret = io_ctl_check_generation(&io_ctl, generation);
772 if (ret)
773 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400774
Josef Bacika67509c2011-10-05 15:18:58 -0400775 while (num_entries) {
776 e = kmem_cache_zalloc(btrfs_free_space_cachep,
777 GFP_NOFS);
778 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400779 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400780
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400781 ret = io_ctl_read_entry(&io_ctl, e, &type);
782 if (ret) {
783 kmem_cache_free(btrfs_free_space_cachep, e);
784 goto free_cache;
785 }
786
Josef Bacika67509c2011-10-05 15:18:58 -0400787 if (!e->bytes) {
788 kmem_cache_free(btrfs_free_space_cachep, e);
789 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400790 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400791
Josef Bacika67509c2011-10-05 15:18:58 -0400792 if (type == BTRFS_FREE_SPACE_EXTENT) {
793 spin_lock(&ctl->tree_lock);
794 ret = link_free_space(ctl, e);
795 spin_unlock(&ctl->tree_lock);
796 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400797 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000798 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500799 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400800 goto free_cache;
801 }
Josef Bacika67509c2011-10-05 15:18:58 -0400802 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400803 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400804 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000805 e->bitmap = kmem_cache_zalloc(
806 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400807 if (!e->bitmap) {
808 kmem_cache_free(
809 btrfs_free_space_cachep, e);
810 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400811 }
Josef Bacika67509c2011-10-05 15:18:58 -0400812 spin_lock(&ctl->tree_lock);
813 ret = link_free_space(ctl, e);
814 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +0100815 recalculate_thresholds(ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400816 spin_unlock(&ctl->tree_lock);
817 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400818 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000819 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400820 kmem_cache_free(btrfs_free_space_cachep, e);
821 goto free_cache;
822 }
823 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400824 }
825
Josef Bacika67509c2011-10-05 15:18:58 -0400826 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400827 }
828
Josef Bacik2f120c02011-11-10 20:45:05 -0500829 io_ctl_unmap_page(&io_ctl);
830
Josef Bacika67509c2011-10-05 15:18:58 -0400831 /*
832 * We add the bitmaps at the end of the entries in order that
833 * the bitmap entries are added to the cache.
834 */
835 list_for_each_entry_safe(e, n, &bitmaps, list) {
836 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400837 ret = io_ctl_read_bitmap(&io_ctl, e);
838 if (ret)
839 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400840 }
841
842 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400843 ret = 1;
844out:
Josef Bacika67509c2011-10-05 15:18:58 -0400845 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400846 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400847free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400848 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800849 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400850 goto out;
851}
852
Josef Bacikcd799092020-10-23 09:58:08 -0400853static int copy_free_space_cache(struct btrfs_block_group *block_group,
854 struct btrfs_free_space_ctl *ctl)
855{
856 struct btrfs_free_space *info;
857 struct rb_node *n;
858 int ret = 0;
859
860 while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
861 info = rb_entry(n, struct btrfs_free_space, offset_index);
862 if (!info->bitmap) {
863 unlink_free_space(ctl, info);
864 ret = btrfs_add_free_space(block_group, info->offset,
865 info->bytes);
866 kmem_cache_free(btrfs_free_space_cachep, info);
867 } else {
868 u64 offset = info->offset;
869 u64 bytes = ctl->unit;
870
871 while (search_bitmap(ctl, info, &offset, &bytes,
872 false) == 0) {
873 ret = btrfs_add_free_space(block_group, offset,
874 bytes);
875 if (ret)
876 break;
877 bitmap_clear_bits(ctl, info, offset, bytes);
878 offset = info->offset;
879 bytes = ctl->unit;
880 }
881 free_bitmap(ctl, info);
882 }
883 cond_resched();
884 }
885 return ret;
886}
887
David Sterba32da53862019-10-29 19:20:18 +0100888int load_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400889{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100890 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800891 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacikcd799092020-10-23 09:58:08 -0400892 struct btrfs_free_space_ctl tmp_ctl = {};
Li Zefan0414efa2011-04-20 10:20:14 +0800893 struct inode *inode;
894 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400895 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800896 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200897 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800898
899 /*
Josef Bacikcd799092020-10-23 09:58:08 -0400900 * Because we could potentially discard our loaded free space, we want
901 * to load everything into a temporary structure first, and then if it's
902 * valid copy it all into the actual free space ctl.
903 */
904 btrfs_init_free_space_ctl(block_group, &tmp_ctl);
905
906 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800907 * If this block group has been marked to be cleared for one reason or
908 * another then we can't trust the on disk cache, so just return.
909 */
910 spin_lock(&block_group->lock);
911 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
912 spin_unlock(&block_group->lock);
913 return 0;
914 }
915 spin_unlock(&block_group->lock);
916
917 path = btrfs_alloc_path();
918 if (!path)
919 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400920 path->search_commit_root = 1;
921 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800922
Filipe Manana4222ea72018-10-24 10:13:03 +0100923 /*
924 * We must pass a path with search_commit_root set to btrfs_iget in
925 * order to avoid a deadlock when allocating extents for the tree root.
926 *
927 * When we are COWing an extent buffer from the tree root, when looking
928 * for a free extent, at extent-tree.c:find_free_extent(), we can find
929 * block group without its free space cache loaded. When we find one
930 * we must load its space cache which requires reading its free space
931 * cache's inode item from the root tree. If this inode item is located
932 * in the same leaf that we started COWing before, then we end up in
933 * deadlock on the extent buffer (trying to read lock it when we
934 * previously write locked it).
935 *
936 * It's safe to read the inode item using the commit root because
937 * block groups, once loaded, stay in memory forever (until they are
938 * removed) as well as their space caches once loaded. New block groups
939 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
940 * we will never try to read their inode item while the fs is mounted.
941 */
David Sterba7949f332019-03-20 13:40:19 +0100942 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800943 if (IS_ERR(inode)) {
944 btrfs_free_path(path);
945 return 0;
946 }
947
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400948 /* We may have converted the inode and made the cache invalid. */
949 spin_lock(&block_group->lock);
950 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
951 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900952 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400953 goto out;
954 }
955 spin_unlock(&block_group->lock);
956
Josef Bacikcd799092020-10-23 09:58:08 -0400957 ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200958 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800959 btrfs_free_path(path);
960 if (ret <= 0)
961 goto out;
962
Josef Bacikcd799092020-10-23 09:58:08 -0400963 matched = (tmp_ctl.free_space == (block_group->length - used -
964 block_group->bytes_super));
Li Zefan0414efa2011-04-20 10:20:14 +0800965
Josef Bacikcd799092020-10-23 09:58:08 -0400966 if (matched) {
967 ret = copy_free_space_cache(block_group, &tmp_ctl);
968 /*
969 * ret == 1 means we successfully loaded the free space cache,
970 * so we need to re-set it here.
971 */
972 if (ret == 0)
973 ret = 1;
974 } else {
975 __btrfs_remove_free_space_cache(&tmp_ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400976 btrfs_warn(fs_info,
977 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +0200978 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800979 ret = -1;
980 }
981out:
982 if (ret < 0) {
983 /* This cache is bogus, make sure it gets cleared */
984 spin_lock(&block_group->lock);
985 block_group->disk_cache_state = BTRFS_DC_CLEAR;
986 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800987 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800988
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400989 btrfs_warn(fs_info,
990 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +0200991 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800992 }
993
Josef Bacik66b53ba2020-10-23 09:58:07 -0400994 spin_lock(&ctl->tree_lock);
995 btrfs_discard_update_discardable(block_group);
996 spin_unlock(&ctl->tree_lock);
Li Zefan0414efa2011-04-20 10:20:14 +0800997 iput(inode);
998 return ret;
999}
1000
Chris Masond4452bc2014-05-19 20:47:56 -07001001static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -07001002int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001003 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001004 struct btrfs_block_group *block_group,
Chris Masond4452bc2014-05-19 20:47:56 -07001005 int *entries, int *bitmaps,
1006 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001007{
Josef Bacikc09544e2011-08-30 10:19:10 -04001008 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001009 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -07001010 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -07001011 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +00001012 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001013
Josef Bacik43be2142011-04-01 14:55:00 +00001014 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -07001015 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +00001016 cluster = list_entry(block_group->cluster_list.next,
1017 struct btrfs_free_cluster,
1018 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001019 }
Josef Bacik43be2142011-04-01 14:55:00 +00001020
Josef Bacikf75b1302011-10-05 10:00:18 -04001021 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001022 cluster_locked = cluster;
1023 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -04001024 node = rb_first(&cluster->root);
1025 cluster = NULL;
1026 }
1027
Josef Bacik0cb59c92010-07-02 12:14:14 -04001028 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -04001029 while (node) {
1030 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001031
Josef Bacika67509c2011-10-05 15:18:58 -04001032 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -07001033 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +00001034
Chris Masond4452bc2014-05-19 20:47:56 -07001035 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -04001036 e->bitmap);
1037 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -07001038 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -04001039
1040 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -07001041 list_add_tail(&e->list, bitmap_list);
1042 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -04001043 }
1044 node = rb_next(node);
1045 if (!node && cluster) {
1046 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -07001047 cluster_locked = cluster;
1048 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -04001049 cluster = NULL;
1050 }
1051 }
Chris Mason1bbc6212015-04-06 12:46:08 -07001052 if (cluster_locked) {
1053 spin_unlock(&cluster_locked->lock);
1054 cluster_locked = NULL;
1055 }
Filipe Manana55507ce2014-12-01 17:04:09 +00001056
1057 /*
1058 * Make sure we don't miss any range that was removed from our rbtree
1059 * because trimming is running. Otherwise after a umount+mount (or crash
1060 * after committing the transaction) we would leak free space and get
1061 * an inconsistent free space cache report from fsck.
1062 */
1063 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
1064 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
1065 trim_entry->bytes, NULL);
1066 if (ret)
1067 goto fail;
1068 *entries += 1;
1069 }
1070
Chris Masond4452bc2014-05-19 20:47:56 -07001071 return 0;
1072fail:
Chris Mason1bbc6212015-04-06 12:46:08 -07001073 if (cluster_locked)
1074 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -07001075 return -ENOSPC;
1076}
1077
1078static noinline_for_stack int
1079update_cache_item(struct btrfs_trans_handle *trans,
1080 struct btrfs_root *root,
1081 struct inode *inode,
1082 struct btrfs_path *path, u64 offset,
1083 int entries, int bitmaps)
1084{
1085 struct btrfs_key key;
1086 struct btrfs_free_space_header *header;
1087 struct extent_buffer *leaf;
1088 int ret;
1089
1090 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1091 key.offset = offset;
1092 key.type = 0;
1093
1094 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1095 if (ret < 0) {
1096 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001097 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001098 goto fail;
1099 }
1100 leaf = path->nodes[0];
1101 if (ret > 0) {
1102 struct btrfs_key found_key;
1103 ASSERT(path->slots[0]);
1104 path->slots[0]--;
1105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1106 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1107 found_key.offset != offset) {
1108 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001109 inode->i_size - 1, EXTENT_DELALLOC, 0,
1110 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001111 btrfs_release_path(path);
1112 goto fail;
1113 }
1114 }
1115
1116 BTRFS_I(inode)->generation = trans->transid;
1117 header = btrfs_item_ptr(leaf, path->slots[0],
1118 struct btrfs_free_space_header);
1119 btrfs_set_free_space_entries(leaf, header, entries);
1120 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1121 btrfs_set_free_space_generation(leaf, header, trans->transid);
1122 btrfs_mark_buffer_dirty(leaf);
1123 btrfs_release_path(path);
1124
1125 return 0;
1126
1127fail:
1128 return -1;
1129}
1130
David Sterba6701bdb2019-03-20 13:49:09 +01001131static noinline_for_stack int write_pinned_extent_entries(
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001132 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001133 struct btrfs_block_group *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001134 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001135 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001136{
1137 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001138 struct extent_io_tree *unpin = NULL;
1139 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001140
Miao Xie5349d6c2014-06-19 10:42:49 +08001141 if (!block_group)
1142 return 0;
1143
Josef Bacika67509c2011-10-05 15:18:58 -04001144 /*
1145 * We want to add any pinned extents to our free space cache
1146 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001147 *
Li Zefandb804f22012-01-10 16:41:01 +08001148 * We shouldn't have switched the pinned extents yet so this is the
1149 * right one
1150 */
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001151 unpin = &trans->transaction->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001152
David Sterbab3470b52019-10-23 18:48:22 +02001153 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001154
David Sterbab3470b52019-10-23 18:48:22 +02001155 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001156 ret = find_first_extent_bit(unpin, start,
1157 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001158 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001159 if (ret)
1160 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001161
Josef Bacika67509c2011-10-05 15:18:58 -04001162 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001163 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001164 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001165
Li Zefandb804f22012-01-10 16:41:01 +08001166 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001167 extent_end = min(block_group->start + block_group->length,
1168 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001169 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001170
Chris Masond4452bc2014-05-19 20:47:56 -07001171 *entries += 1;
1172 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001173 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001174 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001175
Li Zefandb804f22012-01-10 16:41:01 +08001176 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001177 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001178
Miao Xie5349d6c2014-06-19 10:42:49 +08001179 return 0;
1180}
1181
1182static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001183write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001184{
Geliang Tang7ae16812015-12-18 22:17:00 +08001185 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001186 int ret;
1187
Josef Bacik0cb59c92010-07-02 12:14:14 -04001188 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001189 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001190 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001191 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001192 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001193 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001194 }
1195
Miao Xie5349d6c2014-06-19 10:42:49 +08001196 return 0;
1197}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001198
Miao Xie5349d6c2014-06-19 10:42:49 +08001199static int flush_dirty_cache(struct inode *inode)
1200{
1201 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001202
Josef Bacik0ef8b722013-10-25 16:13:35 -04001203 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001204 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001206 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001207
Miao Xie5349d6c2014-06-19 10:42:49 +08001208 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001209}
1210
1211static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001212cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001213{
Geliang Tang7ae16812015-12-18 22:17:00 +08001214 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001215
Geliang Tang7ae16812015-12-18 22:17:00 +08001216 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001217 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001218}
1219
1220static void noinline_for_stack
1221cleanup_write_cache_enospc(struct inode *inode,
1222 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001223 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001224{
Chris Masond4452bc2014-05-19 20:47:56 -07001225 io_ctl_drop_pages(io_ctl);
1226 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001227 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001228}
1229
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001230static int __btrfs_wait_cache_io(struct btrfs_root *root,
1231 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001232 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001233 struct btrfs_io_ctl *io_ctl,
1234 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001235{
1236 int ret;
1237 struct inode *inode = io_ctl->inode;
1238
Chris Mason1bbc6212015-04-06 12:46:08 -07001239 if (!inode)
1240 return 0;
1241
Chris Masonc9dc4c62015-04-04 17:14:42 -07001242 /* Flush the dirty pages in the cache file. */
1243 ret = flush_dirty_cache(inode);
1244 if (ret)
1245 goto out;
1246
1247 /* Update the cache item to tell everyone this cache file is valid. */
1248 ret = update_cache_item(trans, root, inode, path, offset,
1249 io_ctl->entries, io_ctl->bitmaps);
1250out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001251 if (ret) {
1252 invalidate_inode_pages2(inode->i_mapping);
1253 BTRFS_I(inode)->generation = 0;
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001254 if (block_group)
1255 btrfs_debug(root->fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001256 "failed to write free space cache for block group %llu error %d",
1257 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001258 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001259 btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonc9dc4c62015-04-04 17:14:42 -07001260
1261 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001262 /* the dirty list is protected by the dirty_bgs_lock */
1263 spin_lock(&trans->transaction->dirty_bgs_lock);
1264
1265 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001266 spin_lock(&block_group->lock);
1267
1268 /*
1269 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001270 * the dirty list while waiting for IO. Otherwise our
1271 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001272 */
1273 if (!ret && list_empty(&block_group->dirty_list))
1274 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1275 else if (ret)
1276 block_group->disk_cache_state = BTRFS_DC_ERROR;
1277
1278 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001279 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001280 io_ctl->inode = NULL;
1281 iput(inode);
1282 }
1283
1284 return ret;
1285
1286}
1287
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001288int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001289 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001290 struct btrfs_path *path)
1291{
1292 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1293 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001294 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001295}
1296
Chris Masond4452bc2014-05-19 20:47:56 -07001297/**
1298 * __btrfs_write_out_cache - write out cached info to an inode
1299 * @root - the root the inode belongs to
1300 * @ctl - the free space cache we are going to write out
1301 * @block_group - the block_group for this cache if it belongs to a block_group
1302 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001303 *
1304 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001305 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001306 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001307 */
1308static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1309 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001310 struct btrfs_block_group *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001311 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001312 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001313{
1314 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001315 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001316 int entries = 0;
1317 int bitmaps = 0;
1318 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001319 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001320
1321 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001322 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001323
Chris Masonc9dc4c62015-04-04 17:14:42 -07001324 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001325 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001326 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001327 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001328
Miao Xiee570fd22014-06-19 10:42:50 +08001329 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1330 down_write(&block_group->data_rwsem);
1331 spin_lock(&block_group->lock);
1332 if (block_group->delalloc_bytes) {
1333 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1334 spin_unlock(&block_group->lock);
1335 up_write(&block_group->data_rwsem);
1336 BTRFS_I(inode)->generation = 0;
1337 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001338 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001339 goto out;
1340 }
1341 spin_unlock(&block_group->lock);
1342 }
1343
Chris Masond4452bc2014-05-19 20:47:56 -07001344 /* Lock all pages first so we can lock the extent safely. */
Johannes Thumshirn7a195f62020-02-12 00:10:20 +09001345 ret = io_ctl_prepare_pages(io_ctl, false);
Omar Sandovalb8605452015-02-24 02:47:06 -08001346 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001347 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001348
1349 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001350 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001351
Chris Masonc9dc4c62015-04-04 17:14:42 -07001352 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001353
Filipe Manana55507ce2014-12-01 17:04:09 +00001354 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001355 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001356 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001357 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001358 block_group, &entries, &bitmaps,
1359 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001360 if (ret)
1361 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001362
Miao Xie5349d6c2014-06-19 10:42:49 +08001363 /*
1364 * Some spaces that are freed in the current transaction are pinned,
1365 * they will be added into free space cache after the transaction is
1366 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001367 *
1368 * If this changes while we are working we'll get added back to
1369 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001370 */
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001371 ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001372 if (ret)
1373 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001374
Filipe Manana55507ce2014-12-01 17:04:09 +00001375 /*
1376 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1377 * locked while doing it because a concurrent trim can be manipulating
1378 * or freeing the bitmap.
1379 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001380 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001381 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001382 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001383 if (ret)
1384 goto out_nospc;
1385
1386 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001387 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001388
1389 /* Everything is written out, now we dirty the pages in the file. */
Nikolay Borisov088545f2020-06-03 08:55:36 +03001390 ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
1391 io_ctl->num_pages, 0, i_size_read(inode),
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -05001392 &cached_state, false);
Miao Xie5349d6c2014-06-19 10:42:49 +08001393 if (ret)
1394 goto out_nospc;
1395
Miao Xiee570fd22014-06-19 10:42:50 +08001396 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1397 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001398 /*
1399 * Release the pages and unlock the extent, we will flush
1400 * them out later
1401 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001402 io_ctl_drop_pages(io_ctl);
Filipe Mananabbc37d62020-08-14 11:04:09 +01001403 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001404
1405 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001406 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001407
Chris Masonc9dc4c62015-04-04 17:14:42 -07001408 /*
1409 * at this point the pages are under IO and we're happy,
Randy Dunlap260db432020-08-04 19:48:34 -07001410 * The caller is responsible for waiting on them and updating
Chris Masonc9dc4c62015-04-04 17:14:42 -07001411 * the cache and the inode
1412 */
1413 io_ctl->entries = entries;
1414 io_ctl->bitmaps = bitmaps;
1415
1416 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001417 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001418 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001419
Chris Masonc9dc4c62015-04-04 17:14:42 -07001420 return 0;
1421
Chris Masona3bdccc2015-04-24 11:00:00 -07001422out_nospc_locked:
1423 cleanup_bitmap_list(&bitmap_list);
1424 spin_unlock(&ctl->tree_lock);
1425 mutex_unlock(&ctl->cache_writeout_mutex);
1426
Josef Bacika67509c2011-10-05 15:18:58 -04001427out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001428 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001429
Josef Bacikb77000e2017-11-15 16:20:52 -05001430out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001431 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1432 up_write(&block_group->data_rwsem);
1433
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001434out:
1435 io_ctl->inode = NULL;
1436 io_ctl_free(io_ctl);
1437 if (ret) {
1438 invalidate_inode_pages2(inode->i_mapping);
1439 BTRFS_I(inode)->generation = 0;
1440 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02001441 btrfs_update_inode(trans, root, BTRFS_I(inode));
Johannes Thumshirnfd8efa82020-02-12 00:10:22 +09001442 if (must_iput)
1443 iput(inode);
1444 return ret;
Li Zefan0414efa2011-04-20 10:20:14 +08001445}
1446
David Sterbafe041532019-03-20 13:51:56 +01001447int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001448 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +08001449 struct btrfs_path *path)
1450{
David Sterbafe041532019-03-20 13:51:56 +01001451 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001452 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1453 struct inode *inode;
1454 int ret = 0;
1455
Li Zefan0414efa2011-04-20 10:20:14 +08001456 spin_lock(&block_group->lock);
1457 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1458 spin_unlock(&block_group->lock);
1459 return 0;
1460 }
1461 spin_unlock(&block_group->lock);
1462
David Sterba7949f332019-03-20 13:40:19 +01001463 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001464 if (IS_ERR(inode))
1465 return 0;
1466
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001467 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1468 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001469 if (ret) {
Filipe Mananabbcd1f42020-05-18 17:34:23 +01001470 btrfs_debug(fs_info,
Filipe Manana2e69a7a2020-05-18 17:34:11 +01001471 "failed to write free space cache for block group %llu error %d",
1472 block_group->start, ret);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001473 spin_lock(&block_group->lock);
1474 block_group->disk_cache_state = BTRFS_DC_ERROR;
1475 spin_unlock(&block_group->lock);
1476
1477 block_group->io_ctl.inode = NULL;
1478 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001479 }
1480
Chris Masonc9dc4c62015-04-04 17:14:42 -07001481 /*
1482 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1483 * to wait for IO and put the inode
1484 */
1485
Josef Bacik0cb59c92010-07-02 12:14:14 -04001486 return ret;
1487}
1488
Li Zefan34d52cb2011-03-29 13:46:06 +08001489static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001490 u64 offset)
1491{
Josef Bacikb12d6862013-08-26 17:14:08 -04001492 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001493 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001494 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001495}
1496
Li Zefan34d52cb2011-03-29 13:46:06 +08001497static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001498{
Li Zefan34d52cb2011-03-29 13:46:06 +08001499 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001500}
1501
Li Zefan34d52cb2011-03-29 13:46:06 +08001502static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001503 u64 offset)
1504{
1505 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001506 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001507
Li Zefan34d52cb2011-03-29 13:46:06 +08001508 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1509 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001510 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001511 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001512 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001513
1514 return bitmap_start;
1515}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001516
1517static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001518 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001519{
1520 struct rb_node **p = &root->rb_node;
1521 struct rb_node *parent = NULL;
1522 struct btrfs_free_space *info;
1523
1524 while (*p) {
1525 parent = *p;
1526 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1527
Josef Bacik96303082009-07-13 21:29:25 -04001528 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001529 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001530 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001531 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001532 } else {
1533 /*
1534 * we could have a bitmap entry and an extent entry
1535 * share the same offset. If this is the case, we want
1536 * the extent entry to always be found first if we do a
1537 * linear search through the tree, since we want to have
1538 * the quickest allocation time, and allocating from an
1539 * extent is faster than allocating from a bitmap. So
1540 * if we're inserting a bitmap and we find an entry at
1541 * this offset, we want to go right, or after this entry
1542 * logically. If we are inserting an extent and we've
1543 * found a bitmap, we want to go left, or before
1544 * logically.
1545 */
1546 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001547 if (info->bitmap) {
1548 WARN_ON_ONCE(1);
1549 return -EEXIST;
1550 }
Josef Bacik96303082009-07-13 21:29:25 -04001551 p = &(*p)->rb_right;
1552 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001553 if (!info->bitmap) {
1554 WARN_ON_ONCE(1);
1555 return -EEXIST;
1556 }
Josef Bacik96303082009-07-13 21:29:25 -04001557 p = &(*p)->rb_left;
1558 }
1559 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001560 }
1561
1562 rb_link_node(node, parent, p);
1563 rb_insert_color(node, root);
1564
1565 return 0;
1566}
1567
1568/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001569 * searches the tree for the given offset.
1570 *
Josef Bacik96303082009-07-13 21:29:25 -04001571 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1572 * want a section that has at least bytes size and comes at or after the given
1573 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001574 */
Josef Bacik96303082009-07-13 21:29:25 -04001575static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001576tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001577 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001578{
Li Zefan34d52cb2011-03-29 13:46:06 +08001579 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001580 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001581
Josef Bacik96303082009-07-13 21:29:25 -04001582 /* find entry that is closest to the 'offset' */
1583 while (1) {
1584 if (!n) {
1585 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001586 break;
1587 }
Josef Bacik96303082009-07-13 21:29:25 -04001588
1589 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1590 prev = entry;
1591
1592 if (offset < entry->offset)
1593 n = n->rb_left;
1594 else if (offset > entry->offset)
1595 n = n->rb_right;
1596 else
1597 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001598 }
1599
Josef Bacik96303082009-07-13 21:29:25 -04001600 if (bitmap_only) {
1601 if (!entry)
1602 return NULL;
1603 if (entry->bitmap)
1604 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001605
Josef Bacik96303082009-07-13 21:29:25 -04001606 /*
1607 * bitmap entry and extent entry may share same offset,
1608 * in that case, bitmap entry comes after extent entry.
1609 */
1610 n = rb_next(n);
1611 if (!n)
1612 return NULL;
1613 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1614 if (entry->offset != offset)
1615 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001616
Josef Bacik96303082009-07-13 21:29:25 -04001617 WARN_ON(!entry->bitmap);
1618 return entry;
1619 } else if (entry) {
1620 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001621 /*
Josef Bacik96303082009-07-13 21:29:25 -04001622 * if previous extent entry covers the offset,
1623 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001624 */
Miao Xiede6c4112012-10-18 08:18:01 +00001625 n = rb_prev(&entry->offset_index);
1626 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001627 prev = rb_entry(n, struct btrfs_free_space,
1628 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001629 if (!prev->bitmap &&
1630 prev->offset + prev->bytes > offset)
1631 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001632 }
Josef Bacik96303082009-07-13 21:29:25 -04001633 }
1634 return entry;
1635 }
1636
1637 if (!prev)
1638 return NULL;
1639
1640 /* find last entry before the 'offset' */
1641 entry = prev;
1642 if (entry->offset > offset) {
1643 n = rb_prev(&entry->offset_index);
1644 if (n) {
1645 entry = rb_entry(n, struct btrfs_free_space,
1646 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001647 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001648 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001649 if (fuzzy)
1650 return entry;
1651 else
1652 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001653 }
1654 }
1655
Josef Bacik96303082009-07-13 21:29:25 -04001656 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001657 n = rb_prev(&entry->offset_index);
1658 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001659 prev = rb_entry(n, struct btrfs_free_space,
1660 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001661 if (!prev->bitmap &&
1662 prev->offset + prev->bytes > offset)
1663 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001664 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001665 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001666 return entry;
1667 } else if (entry->offset + entry->bytes > offset)
1668 return entry;
1669
1670 if (!fuzzy)
1671 return NULL;
1672
1673 while (1) {
1674 if (entry->bitmap) {
1675 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001676 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001677 break;
1678 } else {
1679 if (entry->offset + entry->bytes > offset)
1680 break;
1681 }
1682
1683 n = rb_next(&entry->offset_index);
1684 if (!n)
1685 return NULL;
1686 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1687 }
1688 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001689}
1690
Li Zefanf333adb2010-11-09 14:57:39 +08001691static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001692__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001693 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001694{
Li Zefan34d52cb2011-03-29 13:46:06 +08001695 rb_erase(&info->offset_index, &ctl->free_space_offset);
1696 ctl->free_extents--;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001697
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001698 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001699 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001700 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
1701 }
Li Zefanf333adb2010-11-09 14:57:39 +08001702}
1703
Li Zefan34d52cb2011-03-29 13:46:06 +08001704static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001705 struct btrfs_free_space *info)
1706{
Li Zefan34d52cb2011-03-29 13:46:06 +08001707 __unlink_free_space(ctl, info);
1708 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001709}
1710
Li Zefan34d52cb2011-03-29 13:46:06 +08001711static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001712 struct btrfs_free_space *info)
1713{
1714 int ret = 0;
1715
Josef Bacikb12d6862013-08-26 17:14:08 -04001716 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001717 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001718 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001719 if (ret)
1720 return ret;
1721
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001722 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001723 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001724 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
1725 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001726
Li Zefan34d52cb2011-03-29 13:46:06 +08001727 ctl->free_space += info->bytes;
1728 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001729 return ret;
1730}
1731
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001732static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1733 struct btrfs_free_space *info,
1734 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001735{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001736 unsigned long start, count, end;
1737 int extent_delta = -1;
Josef Bacik96303082009-07-13 21:29:25 -04001738
Li Zefan34d52cb2011-03-29 13:46:06 +08001739 start = offset_to_bit(info->offset, ctl->unit, offset);
1740 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001741 end = start + count;
1742 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001743
Li Zefanf38b6e72011-03-14 13:40:51 +08001744 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001745
1746 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001747 if (info->max_extent_size > ctl->unit)
1748 info->max_extent_size = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001749
1750 if (start && test_bit(start - 1, info->bitmap))
1751 extent_delta++;
1752
1753 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1754 extent_delta++;
1755
1756 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001757 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001758 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001759 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
1760 }
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001761}
1762
1763static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1764 struct btrfs_free_space *info, u64 offset,
1765 u64 bytes)
1766{
1767 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001768 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001769}
1770
Li Zefan34d52cb2011-03-29 13:46:06 +08001771static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001772 struct btrfs_free_space *info, u64 offset,
1773 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001774{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001775 unsigned long start, count, end;
1776 int extent_delta = 1;
Josef Bacik96303082009-07-13 21:29:25 -04001777
Li Zefan34d52cb2011-03-29 13:46:06 +08001778 start = offset_to_bit(info->offset, ctl->unit, offset);
1779 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001780 end = start + count;
1781 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001782
Li Zefanf38b6e72011-03-14 13:40:51 +08001783 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001784
1785 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001786 ctl->free_space += bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001787
1788 if (start && test_bit(start - 1, info->bitmap))
1789 extent_delta--;
1790
1791 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1792 extent_delta--;
1793
1794 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001795 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001796 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001797 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
1798 }
Josef Bacik96303082009-07-13 21:29:25 -04001799}
1800
Miao Xiea4820392013-09-09 13:19:42 +08001801/*
1802 * If we can not find suitable extent, we will use bytes to record
1803 * the size of the max extent.
1804 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001805static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001806 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001807 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001808{
1809 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001810 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001811 unsigned long bits, i;
1812 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001813 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001814
Josef Bacikcef40482015-10-02 16:09:42 -04001815 /*
1816 * Skip searching the bitmap if we don't have a contiguous section that
1817 * is large enough for this allocation.
1818 */
Josef Bacik0584f712015-10-02 16:12:23 -04001819 if (for_alloc &&
1820 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001821 bitmap_info->max_extent_size < *bytes) {
1822 *bytes = bitmap_info->max_extent_size;
1823 return -1;
1824 }
1825
Li Zefan34d52cb2011-03-29 13:46:06 +08001826 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001827 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001828 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001829
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001830 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001831 if (for_alloc && bits == 1) {
1832 found_bits = 1;
1833 break;
1834 }
Josef Bacik96303082009-07-13 21:29:25 -04001835 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1836 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001837 extent_bits = next_zero - i;
1838 if (extent_bits >= bits) {
1839 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001840 break;
Miao Xiea4820392013-09-09 13:19:42 +08001841 } else if (extent_bits > max_bits) {
1842 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001843 }
1844 i = next_zero;
1845 }
1846
1847 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001848 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1849 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001850 return 0;
1851 }
1852
Miao Xiea4820392013-09-09 13:19:42 +08001853 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001854 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001855 return -1;
1856}
1857
Josef Bacikad22cf62018-10-12 15:32:33 -04001858static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1859{
1860 if (entry->bitmap)
1861 return entry->max_extent_size;
1862 return entry->bytes;
1863}
1864
Miao Xiea4820392013-09-09 13:19:42 +08001865/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001866static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001867find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001868 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001869{
1870 struct btrfs_free_space *entry;
1871 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001872 u64 tmp;
1873 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001874 int ret;
1875
Li Zefan34d52cb2011-03-29 13:46:06 +08001876 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001877 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001878
Li Zefan34d52cb2011-03-29 13:46:06 +08001879 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001880 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001881 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001882
1883 for (node = &entry->offset_index; node; node = rb_next(node)) {
1884 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001885 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001886 *max_extent_size = max(get_max_extent_size(entry),
1887 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001888 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001889 }
Josef Bacik96303082009-07-13 21:29:25 -04001890
David Woodhouse53b381b2013-01-29 18:40:14 -05001891 /* make sure the space returned is big enough
1892 * to match our requested alignment
1893 */
1894 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001895 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001896 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001897 tmp = tmp * align + ctl->start;
1898 align_off = tmp - entry->offset;
1899 } else {
1900 align_off = 0;
1901 tmp = entry->offset;
1902 }
1903
Miao Xiea4820392013-09-09 13:19:42 +08001904 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001905 *max_extent_size = max(get_max_extent_size(entry),
1906 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001907 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001908 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001909
Josef Bacik96303082009-07-13 21:29:25 -04001910 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001911 u64 size = *bytes;
1912
Josef Bacik0584f712015-10-02 16:12:23 -04001913 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001914 if (!ret) {
1915 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001916 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001917 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001918 } else {
1919 *max_extent_size =
1920 max(get_max_extent_size(entry),
1921 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001922 }
Josef Bacik96303082009-07-13 21:29:25 -04001923 continue;
1924 }
1925
David Woodhouse53b381b2013-01-29 18:40:14 -05001926 *offset = tmp;
1927 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001928 return entry;
1929 }
Miao Xiea4820392013-09-09 13:19:42 +08001930out:
Josef Bacik96303082009-07-13 21:29:25 -04001931 return NULL;
1932}
1933
Li Zefan34d52cb2011-03-29 13:46:06 +08001934static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001935 struct btrfs_free_space *info, u64 offset)
1936{
Li Zefan34d52cb2011-03-29 13:46:06 +08001937 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001938 info->bytes = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001939 info->bitmap_extents = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001940 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001941 link_free_space(ctl, info);
1942 ctl->total_bitmaps++;
David Sterbafa598b02020-12-03 17:18:38 +01001943 recalculate_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001944}
1945
Li Zefan34d52cb2011-03-29 13:46:06 +08001946static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001947 struct btrfs_free_space *bitmap_info)
1948{
Dennis Zhou27f0afc2020-01-02 16:26:45 -05001949 /*
1950 * Normally when this is called, the bitmap is completely empty. However,
1951 * if we are blowing up the free space cache for one reason or another
1952 * via __btrfs_remove_free_space_cache(), then it may not be freed and
1953 * we may leave stats on the table.
1954 */
1955 if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
1956 ctl->discardable_extents[BTRFS_STAT_CURR] -=
1957 bitmap_info->bitmap_extents;
1958 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
1959
1960 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001961 unlink_free_space(ctl, bitmap_info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00001962 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001963 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001964 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01001965 recalculate_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001966}
1967
Li Zefan34d52cb2011-03-29 13:46:06 +08001968static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001969 struct btrfs_free_space *bitmap_info,
1970 u64 *offset, u64 *bytes)
1971{
1972 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001973 u64 search_start, search_bytes;
1974 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001975
1976again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001977 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001978
Josef Bacik6606bb92009-07-31 11:03:58 -04001979 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001980 * We need to search for bits in this bitmap. We could only cover some
1981 * of the extent in this bitmap thanks to how we add space, so we need
1982 * to search for as much as it as we can and clear that amount, and then
1983 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001984 */
1985 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001986 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001987 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001988 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1989 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001990 if (ret < 0 || search_start != *offset)
1991 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001992
Josef Bacikbdb7d302012-06-27 15:10:56 -04001993 /* We may have found more bits than what we need */
1994 search_bytes = min(search_bytes, *bytes);
1995
1996 /* Cannot clear past the end of the bitmap */
1997 search_bytes = min(search_bytes, end - search_start + 1);
1998
1999 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
2000 *offset += search_bytes;
2001 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04002002
2003 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002004 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002005 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002006 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002007
Josef Bacik6606bb92009-07-31 11:03:58 -04002008 /*
2009 * no entry after this bitmap, but we still have bytes to
2010 * remove, so something has gone wrong.
2011 */
2012 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04002013 return -EINVAL;
2014
Josef Bacik6606bb92009-07-31 11:03:58 -04002015 bitmap_info = rb_entry(next, struct btrfs_free_space,
2016 offset_index);
2017
2018 /*
2019 * if the next entry isn't a bitmap we need to return to let the
2020 * extent stuff do its work.
2021 */
Josef Bacik96303082009-07-13 21:29:25 -04002022 if (!bitmap_info->bitmap)
2023 return -EAGAIN;
2024
Josef Bacik6606bb92009-07-31 11:03:58 -04002025 /*
2026 * Ok the next item is a bitmap, but it may not actually hold
2027 * the information for the rest of this free space stuff, so
2028 * look for it, and if we don't find it return so we can try
2029 * everything over again.
2030 */
2031 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002032 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08002033 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04002034 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04002035 if (ret < 0 || search_start != *offset)
2036 return -EAGAIN;
2037
Josef Bacik96303082009-07-13 21:29:25 -04002038 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08002039 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002040 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002041
2042 return 0;
2043}
2044
Josef Bacik2cdc3422011-05-27 14:07:49 -04002045static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
2046 struct btrfs_free_space *info, u64 offset,
Dennis Zhouda080fe2019-12-13 16:22:13 -08002047 u64 bytes, enum btrfs_trim_state trim_state)
Josef Bacik2cdc3422011-05-27 14:07:49 -04002048{
2049 u64 bytes_to_set = 0;
2050 u64 end;
2051
Dennis Zhouda080fe2019-12-13 16:22:13 -08002052 /*
2053 * This is a tradeoff to make bitmap trim state minimal. We mark the
2054 * whole bitmap untrimmed if at any point we add untrimmed regions.
2055 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002056 if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002057 if (btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002058 ctl->discardable_extents[BTRFS_STAT_CURR] +=
2059 info->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002060 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
2061 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002062 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002063 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002064
Josef Bacik2cdc3422011-05-27 14:07:49 -04002065 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
2066
2067 bytes_to_set = min(end - offset, bytes);
2068
2069 bitmap_set_bits(ctl, info, offset, bytes_to_set);
2070
Josef Bacikcef40482015-10-02 16:09:42 -04002071 /*
2072 * We set some bytes, we have no idea what the max extent size is
2073 * anymore.
2074 */
2075 info->max_extent_size = 0;
2076
Josef Bacik2cdc3422011-05-27 14:07:49 -04002077 return bytes_to_set;
2078
2079}
2080
Li Zefan34d52cb2011-03-29 13:46:06 +08002081static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2082 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04002083{
David Sterba32da53862019-10-29 19:20:18 +01002084 struct btrfs_block_group *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002086 bool forced = false;
2087
2088#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002089 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002090 forced = true;
2091#endif
Josef Bacik96303082009-07-13 21:29:25 -04002092
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05002093 /* This is a way to reclaim large regions from the bitmaps. */
2094 if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
2095 return false;
2096
Josef Bacik96303082009-07-13 21:29:25 -04002097 /*
2098 * If we are below the extents threshold then we can add this as an
2099 * extent, and don't have to deal with the bitmap
2100 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002101 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002102 /*
2103 * If this block group has some small extents we don't want to
2104 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002105 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002106 * of cache left then go ahead an dadd them, no sense in adding
2107 * the overhead of a bitmap if we don't have to.
2108 */
Dennis Zhouf9bb6152020-01-02 16:26:44 -05002109 if (info->bytes <= fs_info->sectorsize * 8) {
2110 if (ctl->free_extents * 3 <= ctl->extents_thresh)
Li Zefan34d52cb2011-03-29 13:46:06 +08002111 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002112 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002113 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002114 }
2115 }
Josef Bacik96303082009-07-13 21:29:25 -04002116
2117 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002118 * The original block groups from mkfs can be really small, like 8
2119 * megabytes, so don't bother with a bitmap for those entries. However
2120 * some block groups can be smaller than what a bitmap would cover but
2121 * are still large enough that they could overflow the 32k memory limit,
2122 * so allow those block groups to still be allowed to have a bitmap
2123 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002124 */
David Sterbab3470b52019-10-23 18:48:22 +02002125 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002126 return false;
2127
2128 return true;
2129}
2130
David Sterba20e55062015-11-19 11:42:28 +01002131static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002132 .use_bitmap = use_bitmap,
2133};
2134
Li Zefan34d52cb2011-03-29 13:46:06 +08002135static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2136 struct btrfs_free_space *info)
2137{
2138 struct btrfs_free_space *bitmap_info;
David Sterba32da53862019-10-29 19:20:18 +01002139 struct btrfs_block_group *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002140 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002141 u64 bytes, offset, bytes_added;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002142 enum btrfs_trim_state trim_state;
Li Zefan34d52cb2011-03-29 13:46:06 +08002143 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002144
2145 bytes = info->bytes;
2146 offset = info->offset;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002147 trim_state = info->trim_state;
Josef Bacik96303082009-07-13 21:29:25 -04002148
Li Zefan34d52cb2011-03-29 13:46:06 +08002149 if (!ctl->op->use_bitmap(ctl, info))
2150 return 0;
2151
Josef Bacik2cdc3422011-05-27 14:07:49 -04002152 if (ctl->op == &free_space_op)
2153 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002154again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002155 /*
2156 * Since we link bitmaps right into the cluster we need to see if we
2157 * have a cluster here, and if so and it has our bitmap we need to add
2158 * the free space to that bitmap.
2159 */
2160 if (block_group && !list_empty(&block_group->cluster_list)) {
2161 struct btrfs_free_cluster *cluster;
2162 struct rb_node *node;
2163 struct btrfs_free_space *entry;
2164
2165 cluster = list_entry(block_group->cluster_list.next,
2166 struct btrfs_free_cluster,
2167 block_group_list);
2168 spin_lock(&cluster->lock);
2169 node = rb_first(&cluster->root);
2170 if (!node) {
2171 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002172 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002173 }
2174
2175 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2176 if (!entry->bitmap) {
2177 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002178 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002179 }
2180
2181 if (entry->offset == offset_to_bitmap(ctl, offset)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08002182 bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
2183 bytes, trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002184 bytes -= bytes_added;
2185 offset += bytes_added;
2186 }
2187 spin_unlock(&cluster->lock);
2188 if (!bytes) {
2189 ret = 1;
2190 goto out;
2191 }
2192 }
Chris Mason38e87882011-06-10 16:36:57 -04002193
2194no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002195 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002196 1, 0);
2197 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002198 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002199 goto new_bitmap;
2200 }
2201
Dennis Zhouda080fe2019-12-13 16:22:13 -08002202 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
2203 trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002204 bytes -= bytes_added;
2205 offset += bytes_added;
2206 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002207
2208 if (!bytes) {
2209 ret = 1;
2210 goto out;
2211 } else
2212 goto again;
2213
2214new_bitmap:
2215 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002216 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002217 added = 1;
2218 info = NULL;
2219 goto again;
2220 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002221 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002222
2223 /* no pre-allocated info, allocate a new one */
2224 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002225 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2226 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002227 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002228 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002229 ret = -ENOMEM;
2230 goto out;
2231 }
2232 }
2233
2234 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002235 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2236 GFP_NOFS);
Dennis Zhouda080fe2019-12-13 16:22:13 -08002237 info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Li Zefan34d52cb2011-03-29 13:46:06 +08002238 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002239 if (!info->bitmap) {
2240 ret = -ENOMEM;
2241 goto out;
2242 }
2243 goto again;
2244 }
2245
2246out:
2247 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002248 if (info->bitmap)
2249 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2250 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002251 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002253
2254 return ret;
2255}
2256
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002257/*
2258 * Free space merging rules:
2259 * 1) Merge trimmed areas together
2260 * 2) Let untrimmed areas coalesce with trimmed areas
2261 * 3) Always pull neighboring regions from bitmaps
2262 *
2263 * The above rules are for when we merge free space based on btrfs_trim_state.
2264 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2265 * same reason: to promote larger extent regions which makes life easier for
2266 * find_free_extent(). Rule 2 enables coalescing based on the common path
2267 * being returning free space from btrfs_finish_extent_commit(). So when free
2268 * space is trimmed, it will prevent aggregating trimmed new region and
2269 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2270 * and provide find_free_extent() with the largest extents possible hoping for
2271 * the reuse path.
2272 */
Chris Mason945d8962011-05-22 12:33:42 -04002273static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002274 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002275{
Josef Bacikbf53d462020-07-27 10:28:05 -04002276 struct btrfs_free_space *left_info = NULL;
Li Zefan120d66e2010-11-09 14:56:50 +08002277 struct btrfs_free_space *right_info;
2278 bool merged = false;
2279 u64 offset = info->offset;
2280 u64 bytes = info->bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002281 const bool is_trimmed = btrfs_free_space_trimmed(info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002282
Josef Bacik0f9dd462008-09-23 13:14:11 -04002283 /*
2284 * first we want to see if there is free space adjacent to the range we
2285 * are adding, if there is remove that struct and add a new one to
2286 * cover the entire range
2287 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002288 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002289 if (right_info && rb_prev(&right_info->offset_index))
2290 left_info = rb_entry(rb_prev(&right_info->offset_index),
2291 struct btrfs_free_space, offset_index);
Josef Bacikbf53d462020-07-27 10:28:05 -04002292 else if (!right_info)
Li Zefan34d52cb2011-03-29 13:46:06 +08002293 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002294
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002295 /* See try_merge_free_space() comment. */
2296 if (right_info && !right_info->bitmap &&
2297 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
Li Zefanf333adb2010-11-09 14:57:39 +08002298 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002299 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002300 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002301 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002302 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002303 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002304 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002305 }
2306
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002307 /* See try_merge_free_space() comment. */
Josef Bacik96303082009-07-13 21:29:25 -04002308 if (left_info && !left_info->bitmap &&
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002309 left_info->offset + left_info->bytes == offset &&
2310 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
Li Zefanf333adb2010-11-09 14:57:39 +08002311 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002312 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002313 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002314 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002315 info->offset = left_info->offset;
2316 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002317 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002318 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002319 }
2320
Li Zefan120d66e2010-11-09 14:56:50 +08002321 return merged;
2322}
2323
Filipe Manana20005522014-08-29 13:35:13 +01002324static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2325 struct btrfs_free_space *info,
2326 bool update_stat)
2327{
2328 struct btrfs_free_space *bitmap;
2329 unsigned long i;
2330 unsigned long j;
2331 const u64 end = info->offset + info->bytes;
2332 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2333 u64 bytes;
2334
2335 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2336 if (!bitmap)
2337 return false;
2338
2339 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2340 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2341 if (j == i)
2342 return false;
2343 bytes = (j - i) * ctl->unit;
2344 info->bytes += bytes;
2345
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002346 /* See try_merge_free_space() comment. */
2347 if (!btrfs_free_space_trimmed(bitmap))
2348 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2349
Filipe Manana20005522014-08-29 13:35:13 +01002350 if (update_stat)
2351 bitmap_clear_bits(ctl, bitmap, end, bytes);
2352 else
2353 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2354
2355 if (!bitmap->bytes)
2356 free_bitmap(ctl, bitmap);
2357
2358 return true;
2359}
2360
2361static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2362 struct btrfs_free_space *info,
2363 bool update_stat)
2364{
2365 struct btrfs_free_space *bitmap;
2366 u64 bitmap_offset;
2367 unsigned long i;
2368 unsigned long j;
2369 unsigned long prev_j;
2370 u64 bytes;
2371
2372 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2373 /* If we're on a boundary, try the previous logical bitmap. */
2374 if (bitmap_offset == info->offset) {
2375 if (info->offset == 0)
2376 return false;
2377 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2378 }
2379
2380 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2381 if (!bitmap)
2382 return false;
2383
2384 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2385 j = 0;
2386 prev_j = (unsigned long)-1;
2387 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2388 if (j > i)
2389 break;
2390 prev_j = j;
2391 }
2392 if (prev_j == i)
2393 return false;
2394
2395 if (prev_j == (unsigned long)-1)
2396 bytes = (i + 1) * ctl->unit;
2397 else
2398 bytes = (i - prev_j) * ctl->unit;
2399
2400 info->offset -= bytes;
2401 info->bytes += bytes;
2402
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002403 /* See try_merge_free_space() comment. */
2404 if (!btrfs_free_space_trimmed(bitmap))
2405 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2406
Filipe Manana20005522014-08-29 13:35:13 +01002407 if (update_stat)
2408 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2409 else
2410 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2411
2412 if (!bitmap->bytes)
2413 free_bitmap(ctl, bitmap);
2414
2415 return true;
2416}
2417
2418/*
2419 * We prefer always to allocate from extent entries, both for clustered and
2420 * non-clustered allocation requests. So when attempting to add a new extent
2421 * entry, try to see if there's adjacent free space in bitmap entries, and if
2422 * there is, migrate that space from the bitmaps to the extent.
2423 * Like this we get better chances of satisfying space allocation requests
2424 * because we attempt to satisfy them based on a single cache entry, and never
2425 * on 2 or more entries - even if the entries represent a contiguous free space
2426 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2427 * ends).
2428 */
2429static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2430 struct btrfs_free_space *info,
2431 bool update_stat)
2432{
2433 /*
2434 * Only work with disconnected entries, as we can change their offset,
2435 * and must be extent entries.
2436 */
2437 ASSERT(!info->bitmap);
2438 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2439
2440 if (ctl->total_bitmaps > 0) {
2441 bool stole_end;
2442 bool stole_front = false;
2443
2444 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2445 if (ctl->total_bitmaps > 0)
2446 stole_front = steal_from_bitmap_to_front(ctl, info,
2447 update_stat);
2448
2449 if (stole_end || stole_front)
2450 try_merge_free_space(ctl, info, update_stat);
2451 }
2452}
2453
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002454int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2455 struct btrfs_free_space_ctl *ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002456 u64 offset, u64 bytes,
2457 enum btrfs_trim_state trim_state)
Li Zefan120d66e2010-11-09 14:56:50 +08002458{
Dennis Zhoub0643e52019-12-13 16:22:14 -08002459 struct btrfs_block_group *block_group = ctl->private;
Li Zefan120d66e2010-11-09 14:56:50 +08002460 struct btrfs_free_space *info;
2461 int ret = 0;
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002462 u64 filter_bytes = bytes;
Li Zefan120d66e2010-11-09 14:56:50 +08002463
Josef Bacikdc89e982011-01-28 17:05:48 -05002464 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002465 if (!info)
2466 return -ENOMEM;
2467
2468 info->offset = offset;
2469 info->bytes = bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002470 info->trim_state = trim_state;
Filipe Manana20005522014-08-29 13:35:13 +01002471 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002472
Li Zefan34d52cb2011-03-29 13:46:06 +08002473 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002474
Li Zefan34d52cb2011-03-29 13:46:06 +08002475 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002476 goto link;
2477
2478 /*
2479 * There was no extent directly to the left or right of this new
2480 * extent then we know we're going to have to allocate a new extent, so
2481 * before we do that see if we need to drop this into a bitmap
2482 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002483 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002484 if (ret < 0) {
2485 goto out;
2486 } else if (ret) {
2487 ret = 0;
2488 goto out;
2489 }
2490link:
Filipe Manana20005522014-08-29 13:35:13 +01002491 /*
2492 * Only steal free space from adjacent bitmaps if we're sure we're not
2493 * going to add the new free space to existing bitmap entries - because
2494 * that would mean unnecessary work that would be reverted. Therefore
2495 * attempt to steal space from bitmaps if we're adding an extent entry.
2496 */
2497 steal_from_bitmap(ctl, info, true);
2498
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002499 filter_bytes = max(filter_bytes, info->bytes);
2500
Li Zefan34d52cb2011-03-29 13:46:06 +08002501 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002502 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002503 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002504out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002505 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002506 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002507
Josef Bacik0f9dd462008-09-23 13:14:11 -04002508 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002509 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002510 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002511 }
2512
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002513 if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
2514 btrfs_discard_check_filter(block_group, filter_bytes);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002515 btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002516 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002517
Josef Bacik0f9dd462008-09-23 13:14:11 -04002518 return ret;
2519}
2520
David Sterba32da53862019-10-29 19:20:18 +01002521int btrfs_add_free_space(struct btrfs_block_group *block_group,
Josef Bacik478b4d92019-06-20 15:37:43 -04002522 u64 bytenr, u64 size)
2523{
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002524 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2525
2526 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2527 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2528
Josef Bacik478b4d92019-06-20 15:37:43 -04002529 return __btrfs_add_free_space(block_group->fs_info,
2530 block_group->free_space_ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002531 bytenr, size, trim_state);
Josef Bacik478b4d92019-06-20 15:37:43 -04002532}
2533
Dennis Zhoub0643e52019-12-13 16:22:14 -08002534/*
2535 * This is a subtle distinction because when adding free space back in general,
2536 * we want it to be added as untrimmed for async. But in the case where we add
2537 * it on loading of a block group, we want to consider it trimmed.
2538 */
2539int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
2540 u64 bytenr, u64 size)
2541{
2542 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2543
2544 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
2545 btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
2546 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2547
2548 return __btrfs_add_free_space(block_group->fs_info,
2549 block_group->free_space_ctl,
2550 bytenr, size, trim_state);
2551}
2552
David Sterba32da53862019-10-29 19:20:18 +01002553int btrfs_remove_free_space(struct btrfs_block_group *block_group,
Josef Bacik6226cb02009-04-03 10:14:18 -04002554 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002555{
Li Zefan34d52cb2011-03-29 13:46:06 +08002556 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002557 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002558 int ret;
2559 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002560
Li Zefan34d52cb2011-03-29 13:46:06 +08002561 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002562
Josef Bacik96303082009-07-13 21:29:25 -04002563again:
Josef Bacikb0175112012-12-18 11:39:19 -05002564 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002565 if (!bytes)
2566 goto out_lock;
2567
Li Zefan34d52cb2011-03-29 13:46:06 +08002568 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002569 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002570 /*
2571 * oops didn't find an extent that matched the space we wanted
2572 * to remove, look for a bitmap instead
2573 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002574 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002575 1, 0);
2576 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002577 /*
2578 * If we found a partial bit of our free space in a
2579 * bitmap but then couldn't find the other part this may
2580 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002581 */
Josef Bacikb0175112012-12-18 11:39:19 -05002582 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002583 goto out_lock;
2584 }
Josef Bacik96303082009-07-13 21:29:25 -04002585 }
2586
Josef Bacikb0175112012-12-18 11:39:19 -05002587 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002588 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002589 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002590 if (offset == info->offset) {
2591 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002592
Josef Bacikbdb7d302012-06-27 15:10:56 -04002593 info->bytes -= to_free;
2594 info->offset += to_free;
2595 if (info->bytes) {
2596 ret = link_free_space(ctl, info);
2597 WARN_ON(ret);
2598 } else {
2599 kmem_cache_free(btrfs_free_space_cachep, info);
2600 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002601
Josef Bacikbdb7d302012-06-27 15:10:56 -04002602 offset += to_free;
2603 bytes -= to_free;
2604 goto again;
2605 } else {
2606 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002607
Josef Bacikbdb7d302012-06-27 15:10:56 -04002608 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002609 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002610 WARN_ON(ret);
2611 if (ret)
2612 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002613
Josef Bacikbdb7d302012-06-27 15:10:56 -04002614 /* Not enough bytes in this entry to satisfy us */
2615 if (old_end < offset + bytes) {
2616 bytes -= old_end - offset;
2617 offset = old_end;
2618 goto again;
2619 } else if (old_end == offset + bytes) {
2620 /* all done */
2621 goto out_lock;
2622 }
2623 spin_unlock(&ctl->tree_lock);
2624
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002625 ret = __btrfs_add_free_space(block_group->fs_info, ctl,
2626 offset + bytes,
2627 old_end - (offset + bytes),
2628 info->trim_state);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002629 WARN_ON(ret);
2630 goto out;
2631 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002632 }
Josef Bacik96303082009-07-13 21:29:25 -04002633
Li Zefan34d52cb2011-03-29 13:46:06 +08002634 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002635 if (ret == -EAGAIN) {
2636 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002637 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002638 }
Josef Bacik96303082009-07-13 21:29:25 -04002639out_lock:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002640 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002641 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002642out:
Josef Bacik25179202008-10-29 14:49:05 -04002643 return ret;
2644}
2645
David Sterba32da53862019-10-29 19:20:18 +01002646void btrfs_dump_free_space(struct btrfs_block_group *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -04002647 u64 bytes)
2648{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002649 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002650 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002651 struct btrfs_free_space *info;
2652 struct rb_node *n;
2653 int count = 0;
2654
Filipe Manana9084cb62018-10-22 10:43:06 +01002655 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002656 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002657 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002658 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002659 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002660 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002661 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002662 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002663 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002664 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002665 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002666 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002668 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002669}
2670
Josef Bacikcd799092020-10-23 09:58:08 -04002671void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
2672 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002673{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002674 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002675
Li Zefan34d52cb2011-03-29 13:46:06 +08002676 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002677 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002678 ctl->start = block_group->start;
Li Zefan34d52cb2011-03-29 13:46:06 +08002679 ctl->private = block_group;
2680 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002681 INIT_LIST_HEAD(&ctl->trimming_ranges);
2682 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002683
Li Zefan34d52cb2011-03-29 13:46:06 +08002684 /*
2685 * we only want to have 32k of ram per block group for keeping
2686 * track of free space, and if we pass 1/2 of that we want to
2687 * start converting things over to using bitmaps
2688 */
Byongho Leeee221842015-12-15 01:42:10 +09002689 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002690}
2691
Chris Masonfa9c0d792009-04-03 09:47:43 -04002692/*
2693 * for a given cluster, put all of its extents back into the free
2694 * space cache. If the block group passed doesn't match the block group
2695 * pointed to by the cluster, someone else raced in and freed the
2696 * cluster already. In that case, we just return without changing anything
2697 */
Anand Jain69b0e092020-06-03 18:10:18 +08002698static void __btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002699 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002700 struct btrfs_free_cluster *cluster)
2701{
Li Zefan34d52cb2011-03-29 13:46:06 +08002702 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002703 struct btrfs_free_space *entry;
2704 struct rb_node *node;
2705
2706 spin_lock(&cluster->lock);
2707 if (cluster->block_group != block_group)
2708 goto out;
2709
Josef Bacik96303082009-07-13 21:29:25 -04002710 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002711 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002712 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002713
Chris Masonfa9c0d792009-04-03 09:47:43 -04002714 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002715 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002716 bool bitmap;
2717
Chris Masonfa9c0d792009-04-03 09:47:43 -04002718 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2719 node = rb_next(&entry->offset_index);
2720 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002721 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002722
2723 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002724 if (!bitmap) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002725 /* Merging treats extents as if they were new */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002726 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002727 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002728 ctl->discardable_bytes[BTRFS_STAT_CURR] -=
2729 entry->bytes;
2730 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002731
Li Zefan34d52cb2011-03-29 13:46:06 +08002732 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002733 steal_from_bitmap(ctl, entry, false);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002734
2735 /* As we insert directly, update these statistics */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002736 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002737 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002738 ctl->discardable_bytes[BTRFS_STAT_CURR] +=
2739 entry->bytes;
2740 }
Filipe Manana20005522014-08-29 13:35:13 +01002741 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002742 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002743 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002744 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002745 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002746
Chris Masonfa9c0d792009-04-03 09:47:43 -04002747out:
2748 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002749 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002750}
2751
Eric Sandeen48a3b632013-04-25 20:41:01 +00002752static void __btrfs_remove_free_space_cache_locked(
2753 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002754{
2755 struct btrfs_free_space *info;
2756 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002757
Li Zefan581bb052011-04-20 10:06:11 +08002758 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2759 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002760 if (!info->bitmap) {
2761 unlink_free_space(ctl, info);
2762 kmem_cache_free(btrfs_free_space_cachep, info);
2763 } else {
2764 free_bitmap(ctl, info);
2765 }
David Sterba351810c2015-01-08 15:20:54 +01002766
2767 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002768 }
Chris Mason09655372011-05-21 09:27:38 -04002769}
2770
2771void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2772{
2773 spin_lock(&ctl->tree_lock);
2774 __btrfs_remove_free_space_cache_locked(ctl);
Dennis Zhou27f0afc2020-01-02 16:26:45 -05002775 if (ctl->private)
Josef Bacik66b53ba2020-10-23 09:58:07 -04002776 btrfs_discard_update_discardable(ctl->private);
Li Zefan581bb052011-04-20 10:06:11 +08002777 spin_unlock(&ctl->tree_lock);
2778}
2779
David Sterba32da53862019-10-29 19:20:18 +01002780void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002781{
Li Zefan34d52cb2011-03-29 13:46:06 +08002782 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002783 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002784 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002785
Li Zefan34d52cb2011-03-29 13:46:06 +08002786 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002787 while ((head = block_group->cluster_list.next) !=
2788 &block_group->cluster_list) {
2789 cluster = list_entry(head, struct btrfs_free_cluster,
2790 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002791
2792 WARN_ON(cluster->block_group != block_group);
2793 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002794
2795 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002796 }
Chris Mason09655372011-05-21 09:27:38 -04002797 __btrfs_remove_free_space_cache_locked(ctl);
Josef Bacik66b53ba2020-10-23 09:58:07 -04002798 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002799 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002800
Josef Bacik0f9dd462008-09-23 13:14:11 -04002801}
2802
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08002803/**
2804 * btrfs_is_free_space_trimmed - see if everything is trimmed
2805 * @block_group: block_group of interest
2806 *
2807 * Walk @block_group's free space rb_tree to determine if everything is trimmed.
2808 */
2809bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
2810{
2811 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2812 struct btrfs_free_space *info;
2813 struct rb_node *node;
2814 bool ret = true;
2815
2816 spin_lock(&ctl->tree_lock);
2817 node = rb_first(&ctl->free_space_offset);
2818
2819 while (node) {
2820 info = rb_entry(node, struct btrfs_free_space, offset_index);
2821
2822 if (!btrfs_free_space_trimmed(info)) {
2823 ret = false;
2824 break;
2825 }
2826
2827 node = rb_next(node);
2828 }
2829
2830 spin_unlock(&ctl->tree_lock);
2831 return ret;
2832}
2833
David Sterba32da53862019-10-29 19:20:18 +01002834u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002835 u64 offset, u64 bytes, u64 empty_size,
2836 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002837{
Li Zefan34d52cb2011-03-29 13:46:06 +08002838 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002839 struct btrfs_discard_ctl *discard_ctl =
2840 &block_group->fs_info->discard_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002841 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002842 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002843 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002844 u64 align_gap = 0;
2845 u64 align_gap_len = 0;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002846 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002847
Li Zefan34d52cb2011-03-29 13:46:06 +08002848 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002849 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002850 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002851 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002852 goto out;
2853
2854 ret = offset;
2855 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002856 bitmap_clear_bits(ctl, entry, offset, bytes);
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002857
2858 if (!btrfs_free_space_trimmed(entry))
2859 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2860
Li Zefanedf6e2d2010-11-09 14:50:07 +08002861 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002862 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002863 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002864 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002865 align_gap_len = offset - entry->offset;
2866 align_gap = entry->offset;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002867 align_gap_trim_state = entry->trim_state;
David Woodhouse53b381b2013-01-29 18:40:14 -05002868
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002869 if (!btrfs_free_space_trimmed(entry))
2870 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2871
David Woodhouse53b381b2013-01-29 18:40:14 -05002872 entry->offset = offset + bytes;
2873 WARN_ON(entry->bytes < bytes + align_gap_len);
2874
2875 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002876 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002877 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002878 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002879 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002880 }
Josef Bacik96303082009-07-13 21:29:25 -04002881out:
Josef Bacik66b53ba2020-10-23 09:58:07 -04002882 btrfs_discard_update_discardable(block_group);
Li Zefan34d52cb2011-03-29 13:46:06 +08002883 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002884
David Woodhouse53b381b2013-01-29 18:40:14 -05002885 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002886 __btrfs_add_free_space(block_group->fs_info, ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002887 align_gap, align_gap_len,
2888 align_gap_trim_state);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002889 return ret;
2890}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002891
2892/*
2893 * given a cluster, put all of its extents back into the free space
2894 * cache. If a block group is passed, this function will only free
2895 * a cluster that belongs to the passed block group.
2896 *
2897 * Otherwise, it'll get a reference on the block group pointed to by the
2898 * cluster and remove the cluster from it.
2899 */
Anand Jain69b0e092020-06-03 18:10:18 +08002900void btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002901 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002902 struct btrfs_free_cluster *cluster)
2903{
Li Zefan34d52cb2011-03-29 13:46:06 +08002904 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002905
2906 /* first, get a safe pointer to the block group */
2907 spin_lock(&cluster->lock);
2908 if (!block_group) {
2909 block_group = cluster->block_group;
2910 if (!block_group) {
2911 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08002912 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002913 }
2914 } else if (cluster->block_group != block_group) {
2915 /* someone else has already freed it don't redo their work */
2916 spin_unlock(&cluster->lock);
Anand Jain69b0e092020-06-03 18:10:18 +08002917 return;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002918 }
Anand Jainb5790d52020-06-03 18:10:20 +08002919 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002920 spin_unlock(&cluster->lock);
2921
Li Zefan34d52cb2011-03-29 13:46:06 +08002922 ctl = block_group->free_space_ctl;
2923
Chris Masonfa9c0d792009-04-03 09:47:43 -04002924 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002925 spin_lock(&ctl->tree_lock);
Anand Jain69b0e092020-06-03 18:10:18 +08002926 __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002927 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002928
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08002929 btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
2930
Chris Masonfa9c0d792009-04-03 09:47:43 -04002931 /* finally drop our ref */
2932 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002933}
2934
David Sterba32da53862019-10-29 19:20:18 +01002935static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04002936 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002937 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002938 u64 bytes, u64 min_start,
2939 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002940{
Li Zefan34d52cb2011-03-29 13:46:06 +08002941 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002942 int err;
2943 u64 search_start = cluster->window_start;
2944 u64 search_bytes = bytes;
2945 u64 ret = 0;
2946
Josef Bacik96303082009-07-13 21:29:25 -04002947 search_start = min_start;
2948 search_bytes = bytes;
2949
Josef Bacik0584f712015-10-02 16:12:23 -04002950 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002951 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002952 *max_extent_size = max(get_max_extent_size(entry),
2953 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002954 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002955 }
Josef Bacik96303082009-07-13 21:29:25 -04002956
2957 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002958 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002959
2960 return ret;
2961}
2962
Chris Masonfa9c0d792009-04-03 09:47:43 -04002963/*
2964 * given a cluster, try to allocate 'bytes' from it, returns 0
2965 * if it couldn't find anything suitably large, or a logical disk offset
2966 * if things worked out
2967 */
David Sterba32da53862019-10-29 19:20:18 +01002968u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002969 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002970 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002971{
Li Zefan34d52cb2011-03-29 13:46:06 +08002972 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002973 struct btrfs_discard_ctl *discard_ctl =
2974 &block_group->fs_info->discard_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002975 struct btrfs_free_space *entry = NULL;
2976 struct rb_node *node;
2977 u64 ret = 0;
2978
2979 spin_lock(&cluster->lock);
2980 if (bytes > cluster->max_size)
2981 goto out;
2982
2983 if (cluster->block_group != block_group)
2984 goto out;
2985
2986 node = rb_first(&cluster->root);
2987 if (!node)
2988 goto out;
2989
2990 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302991 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002992 if (entry->bytes < bytes)
2993 *max_extent_size = max(get_max_extent_size(entry),
2994 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002995
Josef Bacik4e69b592011-03-21 10:11:24 -04002996 if (entry->bytes < bytes ||
2997 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002998 node = rb_next(&entry->offset_index);
2999 if (!node)
3000 break;
3001 entry = rb_entry(node, struct btrfs_free_space,
3002 offset_index);
3003 continue;
3004 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003005
Josef Bacik4e69b592011-03-21 10:11:24 -04003006 if (entry->bitmap) {
3007 ret = btrfs_alloc_from_bitmap(block_group,
3008 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003009 cluster->window_start,
3010 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003011 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003012 node = rb_next(&entry->offset_index);
3013 if (!node)
3014 break;
3015 entry = rb_entry(node, struct btrfs_free_space,
3016 offset_index);
3017 continue;
3018 }
Josef Bacik9b230622012-01-26 15:01:12 -05003019 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003020 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04003021 ret = entry->offset;
3022
3023 entry->offset += bytes;
3024 entry->bytes -= bytes;
3025 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003026
Li Zefan5e71b5d2010-11-09 14:55:34 +08003027 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003028 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003029 break;
3030 }
3031out:
3032 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04003033
Li Zefan5e71b5d2010-11-09 14:55:34 +08003034 if (!ret)
3035 return 0;
3036
Li Zefan34d52cb2011-03-29 13:46:06 +08003037 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003038
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003039 if (!btrfs_free_space_trimmed(entry))
3040 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3041
Li Zefan34d52cb2011-03-29 13:46:06 +08003042 ctl->free_space -= bytes;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003043 if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
3044 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08003045 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003046 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003047 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003048 kmem_cache_free(btrfs_free_space_bitmap_cachep,
3049 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08003050 ctl->total_bitmaps--;
David Sterbafa598b02020-12-03 17:18:38 +01003051 recalculate_thresholds(ctl);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003052 } else if (!btrfs_free_space_trimmed(entry)) {
3053 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003054 }
Josef Bacikdc89e982011-01-28 17:05:48 -05003055 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003056 }
3057
Li Zefan34d52cb2011-03-29 13:46:06 +08003058 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003059
Chris Masonfa9c0d792009-04-03 09:47:43 -04003060 return ret;
3061}
3062
David Sterba32da53862019-10-29 19:20:18 +01003063static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003064 struct btrfs_free_space *entry,
3065 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003066 u64 offset, u64 bytes,
3067 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04003068{
Li Zefan34d52cb2011-03-29 13:46:06 +08003069 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003070 unsigned long next_zero;
3071 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003072 unsigned long want_bits;
3073 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003074 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04003075 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003076 unsigned long start = 0;
3077 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003078 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04003079
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003080 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04003081 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003082 want_bits = bytes_to_bits(bytes, ctl->unit);
3083 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04003084
Josef Bacikcef40482015-10-02 16:09:42 -04003085 /*
3086 * Don't bother looking for a cluster in this bitmap if it's heavily
3087 * fragmented.
3088 */
3089 if (entry->max_extent_size &&
3090 entry->max_extent_size < cont1_bytes)
3091 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04003092again:
3093 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06003094 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04003095 next_zero = find_next_zero_bit(entry->bitmap,
3096 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003097 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04003098 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04003099 if (found_bits > max_bits)
3100 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003101 break;
3102 }
Josef Bacikcef40482015-10-02 16:09:42 -04003103 if (next_zero - i > max_bits)
3104 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04003105 i = next_zero;
3106 }
3107
Josef Bacikcef40482015-10-02 16:09:42 -04003108 if (!found_bits) {
3109 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04003110 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04003111 }
Josef Bacik96303082009-07-13 21:29:25 -04003112
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003113 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04003114 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05003115 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003116 }
3117
3118 total_found += found_bits;
3119
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003120 if (cluster->max_size < found_bits * ctl->unit)
3121 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04003122
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003123 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
3124 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04003125 goto again;
3126 }
3127
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003128 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08003129 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003130 ret = tree_insert_offset(&cluster->root, entry->offset,
3131 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04003132 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04003133
Josef Bacik3f7de032011-11-10 08:29:20 -05003134 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003135 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04003136 return 0;
3137}
3138
Chris Masonfa9c0d792009-04-03 09:47:43 -04003139/*
Josef Bacik4e69b592011-03-21 10:11:24 -04003140 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003141 * Try to find a cluster with at least bytes total bytes, at least one
3142 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04003143 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003144static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003145setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003146 struct btrfs_free_cluster *cluster,
3147 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003148 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003149{
Li Zefan34d52cb2011-03-29 13:46:06 +08003150 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04003151 struct btrfs_free_space *first = NULL;
3152 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003153 struct btrfs_free_space *last;
3154 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04003155 u64 window_free;
3156 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003157 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003158
Li Zefan34d52cb2011-03-29 13:46:06 +08003159 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04003160 if (!entry)
3161 return -ENOSPC;
3162
3163 /*
3164 * We don't want bitmaps, so just move along until we find a normal
3165 * extent entry.
3166 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003167 while (entry->bitmap || entry->bytes < min_bytes) {
3168 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04003169 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003170 node = rb_next(&entry->offset_index);
3171 if (!node)
3172 return -ENOSPC;
3173 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3174 }
3175
Josef Bacik4e69b592011-03-21 10:11:24 -04003176 window_free = entry->bytes;
3177 max_extent = entry->bytes;
3178 first = entry;
3179 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04003180
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003181 for (node = rb_next(&entry->offset_index); node;
3182 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003183 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3184
Josef Bacik86d4a772011-05-25 13:03:16 -04003185 if (entry->bitmap) {
3186 if (list_empty(&entry->list))
3187 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003188 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04003189 }
3190
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003191 if (entry->bytes < min_bytes)
3192 continue;
3193
3194 last = entry;
3195 window_free += entry->bytes;
3196 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04003197 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003198 }
3199
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003200 if (window_free < bytes || max_extent < cont1_bytes)
3201 return -ENOSPC;
3202
Josef Bacik4e69b592011-03-21 10:11:24 -04003203 cluster->window_start = first->offset;
3204
3205 node = &first->offset_index;
3206
3207 /*
3208 * now we've found our entries, pull them out of the free space
3209 * cache and put them into the cluster rbtree
3210 */
3211 do {
3212 int ret;
3213
3214 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3215 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003216 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003217 continue;
3218
Li Zefan34d52cb2011-03-29 13:46:06 +08003219 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003220 ret = tree_insert_offset(&cluster->root, entry->offset,
3221 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05003222 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04003223 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04003224 } while (node && entry != last);
3225
3226 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003227 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04003228 return 0;
3229}
3230
3231/*
3232 * This specifically looks for bitmaps that may work in the cluster, we assume
3233 * that we have already failed to find extents that will work.
3234 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003235static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003236setup_cluster_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003237 struct btrfs_free_cluster *cluster,
3238 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003239 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003240{
Li Zefan34d52cb2011-03-29 13:46:06 +08003241 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003242 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003243 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003244 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003245
Li Zefan34d52cb2011-03-29 13:46:06 +08003246 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003247 return -ENOSPC;
3248
Josef Bacik86d4a772011-05-25 13:03:16 -04003249 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003250 * The bitmap that covers offset won't be in the list unless offset
3251 * is just its start offset.
3252 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003253 if (!list_empty(bitmaps))
3254 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3255
3256 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003257 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3258 if (entry && list_empty(&entry->list))
3259 list_add(&entry->list, bitmaps);
3260 }
3261
Josef Bacik86d4a772011-05-25 13:03:16 -04003262 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003263 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003264 continue;
3265 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003266 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003267 if (!ret)
3268 return 0;
3269 }
3270
3271 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003272 * The bitmaps list has all the bitmaps that record free space
3273 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003274 */
Li Zefan52621cb2011-11-20 07:33:38 -05003275 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003276}
3277
3278/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003279 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003280 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003281 * We might not find them all in one contiguous area.
3282 *
3283 * returns zero and sets up cluster if things worked out, otherwise
3284 * it returns -enospc
3285 */
David Sterba32da53862019-10-29 19:20:18 +01003286int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003287 struct btrfs_free_cluster *cluster,
3288 u64 offset, u64 bytes, u64 empty_size)
3289{
David Sterba2ceeae22019-03-20 13:53:49 +01003290 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003291 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003292 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003293 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003294 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003295 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003296 int ret;
3297
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003298 /*
3299 * Choose the minimum extent size we'll require for this
3300 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3301 * For metadata, allow allocates with smaller extents. For
3302 * data, keep it dense.
3303 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003304 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003305 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003306 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003307 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003308 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003309 } else {
3310 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003311 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003312 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003313
Li Zefan34d52cb2011-03-29 13:46:06 +08003314 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003315
3316 /*
3317 * If we know we don't have enough space to make a cluster don't even
3318 * bother doing all the work to try and find one.
3319 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003320 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003321 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003322 return -ENOSPC;
3323 }
3324
Chris Masonfa9c0d792009-04-03 09:47:43 -04003325 spin_lock(&cluster->lock);
3326
3327 /* someone already found a cluster, hooray */
3328 if (cluster->block_group) {
3329 ret = 0;
3330 goto out;
3331 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003332
Josef Bacik3f7de032011-11-10 08:29:20 -05003333 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3334 min_bytes);
3335
Josef Bacik86d4a772011-05-25 13:03:16 -04003336 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003337 bytes + empty_size,
3338 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003339 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003340 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003341 offset, bytes + empty_size,
3342 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003343
3344 /* Clear our temporary list */
3345 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3346 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003347
3348 if (!ret) {
Anand Jainb5790d52020-06-03 18:10:20 +08003349 btrfs_get_block_group(block_group);
Josef Bacik4e69b592011-03-21 10:11:24 -04003350 list_add_tail(&cluster->block_group_list,
3351 &block_group->cluster_list);
3352 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003353 } else {
3354 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003355 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003356out:
3357 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003358 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003359
3360 return ret;
3361}
3362
3363/*
3364 * simple code to zero out a cluster
3365 */
3366void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3367{
3368 spin_lock_init(&cluster->lock);
3369 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003370 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003371 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003372 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003373 INIT_LIST_HEAD(&cluster->block_group_list);
3374 cluster->block_group = NULL;
3375}
3376
David Sterba32da53862019-10-29 19:20:18 +01003377static int do_trimming(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003378 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003379 u64 reserved_start, u64 reserved_bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003380 enum btrfs_trim_state reserved_trim_state,
Filipe Manana55507ce2014-12-01 17:04:09 +00003381 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003382{
3383 struct btrfs_space_info *space_info = block_group->space_info;
3384 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003385 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003386 int ret;
3387 int update = 0;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003388 const u64 end = start + bytes;
3389 const u64 reserved_end = reserved_start + reserved_bytes;
3390 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003391 u64 trimmed = 0;
3392
3393 spin_lock(&space_info->lock);
3394 spin_lock(&block_group->lock);
3395 if (!block_group->ro) {
3396 block_group->reserved += reserved_bytes;
3397 space_info->bytes_reserved += reserved_bytes;
3398 update = 1;
3399 }
3400 spin_unlock(&block_group->lock);
3401 spin_unlock(&space_info->lock);
3402
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003403 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003404 if (!ret) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003405 *total_trimmed += trimmed;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003406 trim_state = BTRFS_TRIM_STATE_TRIMMED;
3407 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003408
Filipe Manana55507ce2014-12-01 17:04:09 +00003409 mutex_lock(&ctl->cache_writeout_mutex);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003410 if (reserved_start < start)
3411 __btrfs_add_free_space(fs_info, ctl, reserved_start,
3412 start - reserved_start,
3413 reserved_trim_state);
3414 if (start + bytes < reserved_start + reserved_bytes)
3415 __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end,
3416 reserved_trim_state);
3417 __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state);
Filipe Manana55507ce2014-12-01 17:04:09 +00003418 list_del(&trim_entry->list);
3419 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003420
3421 if (update) {
3422 spin_lock(&space_info->lock);
3423 spin_lock(&block_group->lock);
3424 if (block_group->ro)
3425 space_info->bytes_readonly += reserved_bytes;
3426 block_group->reserved -= reserved_bytes;
3427 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003428 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003429 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003430 }
3431
3432 return ret;
3433}
3434
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003435/*
3436 * If @async is set, then we will trim 1 region and return.
3437 */
David Sterba32da53862019-10-29 19:20:18 +01003438static int trim_no_bitmap(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003439 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3440 bool async)
Li Dongyangf7039b12011-03-24 10:24:28 +00003441{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003442 struct btrfs_discard_ctl *discard_ctl =
3443 &block_group->fs_info->discard_ctl;
Li Zefan34d52cb2011-03-29 13:46:06 +08003444 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003445 struct btrfs_free_space *entry;
3446 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003447 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003448 u64 extent_start;
3449 u64 extent_bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003450 enum btrfs_trim_state extent_trim_state;
Li Zefan7fe1e642011-12-29 14:47:27 +08003451 u64 bytes;
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003452 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Dongyangf7039b12011-03-24 10:24:28 +00003453
3454 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003455 struct btrfs_trim_range trim_entry;
3456
3457 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003458 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003459
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003460 if (ctl->free_space < minlen)
3461 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003462
Li Zefan34d52cb2011-03-29 13:46:06 +08003463 entry = tree_search_offset(ctl, start, 0, 1);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003464 if (!entry)
3465 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003466
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003467 /* Skip bitmaps and if async, already trimmed entries */
3468 while (entry->bitmap ||
3469 (async && btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003470 node = rb_next(&entry->offset_index);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003471 if (!node)
3472 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003473 entry = rb_entry(node, struct btrfs_free_space,
3474 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003475 }
3476
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003477 if (entry->offset >= end)
3478 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003479
3480 extent_start = entry->offset;
3481 extent_bytes = entry->bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003482 extent_trim_state = entry->trim_state;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003483 if (async) {
3484 start = entry->offset;
3485 bytes = entry->bytes;
3486 if (bytes < minlen) {
3487 spin_unlock(&ctl->tree_lock);
3488 mutex_unlock(&ctl->cache_writeout_mutex);
3489 goto next;
3490 }
3491 unlink_free_space(ctl, entry);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003492 /*
3493 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3494 * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
3495 * X when we come back around. So trim it now.
3496 */
3497 if (max_discard_size &&
3498 bytes >= (max_discard_size +
3499 BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003500 bytes = max_discard_size;
3501 extent_bytes = max_discard_size;
3502 entry->offset += max_discard_size;
3503 entry->bytes -= max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003504 link_free_space(ctl, entry);
3505 } else {
3506 kmem_cache_free(btrfs_free_space_cachep, entry);
3507 }
3508 } else {
3509 start = max(start, extent_start);
3510 bytes = min(extent_start + extent_bytes, end) - start;
3511 if (bytes < minlen) {
3512 spin_unlock(&ctl->tree_lock);
3513 mutex_unlock(&ctl->cache_writeout_mutex);
3514 goto next;
3515 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003516
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003517 unlink_free_space(ctl, entry);
3518 kmem_cache_free(btrfs_free_space_cachep, entry);
3519 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003520
Li Zefan34d52cb2011-03-29 13:46:06 +08003521 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003522 trim_entry.start = extent_start;
3523 trim_entry.bytes = extent_bytes;
3524 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3525 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003526
Li Zefan7fe1e642011-12-29 14:47:27 +08003527 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003528 extent_start, extent_bytes, extent_trim_state,
3529 &trim_entry);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003530 if (ret) {
3531 block_group->discard_cursor = start + bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003532 break;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003533 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003534next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003535 start += bytes;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003536 block_group->discard_cursor = start;
3537 if (async && *total_trimmed)
3538 break;
Li Dongyangf7039b12011-03-24 10:24:28 +00003539
3540 if (fatal_signal_pending(current)) {
3541 ret = -ERESTARTSYS;
3542 break;
3543 }
3544
3545 cond_resched();
3546 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003547
3548 return ret;
3549
3550out_unlock:
3551 block_group->discard_cursor = btrfs_block_group_end(block_group);
3552 spin_unlock(&ctl->tree_lock);
3553 mutex_unlock(&ctl->cache_writeout_mutex);
3554
Li Zefan7fe1e642011-12-29 14:47:27 +08003555 return ret;
3556}
3557
Dennis Zhouda080fe2019-12-13 16:22:13 -08003558/*
3559 * If we break out of trimming a bitmap prematurely, we should reset the
3560 * trimming bit. In a rather contrieved case, it's possible to race here so
3561 * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
3562 *
3563 * start = start of bitmap
3564 * end = near end of bitmap
3565 *
3566 * Thread 1: Thread 2:
3567 * trim_bitmaps(start)
3568 * trim_bitmaps(end)
3569 * end_trimming_bitmap()
3570 * reset_trimming_bitmap()
3571 */
3572static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
3573{
3574 struct btrfs_free_space *entry;
3575
3576 spin_lock(&ctl->tree_lock);
3577 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003578 if (entry) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003579 if (btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003580 ctl->discardable_extents[BTRFS_STAT_CURR] +=
3581 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003582 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
3583 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003584 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003585 }
3586
Dennis Zhouda080fe2019-12-13 16:22:13 -08003587 spin_unlock(&ctl->tree_lock);
3588}
3589
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003590static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
3591 struct btrfs_free_space *entry)
Dennis Zhouda080fe2019-12-13 16:22:13 -08003592{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003593 if (btrfs_free_space_trimming_bitmap(entry)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003594 entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003595 ctl->discardable_extents[BTRFS_STAT_CURR] -=
3596 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003597 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003598 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003599}
3600
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003601/*
3602 * If @async is set, then we will trim 1 region and return.
3603 */
David Sterba32da53862019-10-29 19:20:18 +01003604static int trim_bitmaps(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003605 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003606 u64 maxlen, bool async)
Li Zefan7fe1e642011-12-29 14:47:27 +08003607{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003608 struct btrfs_discard_ctl *discard_ctl =
3609 &block_group->fs_info->discard_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003610 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3611 struct btrfs_free_space *entry;
3612 int ret = 0;
3613 int ret2;
3614 u64 bytes;
3615 u64 offset = offset_to_bitmap(ctl, start);
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003616 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Zefan7fe1e642011-12-29 14:47:27 +08003617
3618 while (offset < end) {
3619 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003620 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003621
Filipe Manana55507ce2014-12-01 17:04:09 +00003622 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003623 spin_lock(&ctl->tree_lock);
3624
3625 if (ctl->free_space < minlen) {
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003626 block_group->discard_cursor =
3627 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003628 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003629 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003630 break;
3631 }
3632
3633 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003634 /*
3635 * Bitmaps are marked trimmed lossily now to prevent constant
3636 * discarding of the same bitmap (the reason why we are bound
3637 * by the filters). So, retrim the block group bitmaps when we
3638 * are preparing to punt to the unused_bgs list. This uses
3639 * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
3640 * which is the only discard index which sets minlen to 0.
3641 */
3642 if (!entry || (async && minlen && start == offset &&
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003643 btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003644 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003645 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003646 next_bitmap = true;
3647 goto next;
3648 }
3649
Dennis Zhouda080fe2019-12-13 16:22:13 -08003650 /*
3651 * Async discard bitmap trimming begins at by setting the start
3652 * to be key.objectid and the offset_to_bitmap() aligns to the
3653 * start of the bitmap. This lets us know we are fully
3654 * scanning the bitmap rather than only some portion of it.
3655 */
3656 if (start == offset)
3657 entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
3658
Li Zefan7fe1e642011-12-29 14:47:27 +08003659 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003660 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003661 if (ret2 || start >= end) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003662 /*
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003663 * We lossily consider a bitmap trimmed if we only skip
3664 * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
Dennis Zhouda080fe2019-12-13 16:22:13 -08003665 */
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003666 if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003667 end_trimming_bitmap(ctl, entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003668 else
3669 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003670 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003671 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003672 next_bitmap = true;
3673 goto next;
3674 }
3675
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003676 /*
3677 * We already trimmed a region, but are using the locking above
3678 * to reset the trim_state.
3679 */
3680 if (async && *total_trimmed) {
3681 spin_unlock(&ctl->tree_lock);
3682 mutex_unlock(&ctl->cache_writeout_mutex);
3683 goto out;
3684 }
3685
Li Zefan7fe1e642011-12-29 14:47:27 +08003686 bytes = min(bytes, end - start);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003687 if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003688 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003689 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003690 goto next;
3691 }
3692
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003693 /*
3694 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3695 * If X < @minlen, we won't trim X when we come back around.
3696 * So trim it now. We differ here from trimming extents as we
3697 * don't keep individual state per bit.
3698 */
3699 if (async &&
3700 max_discard_size &&
3701 bytes > (max_discard_size + minlen))
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003702 bytes = max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003703
Li Zefan7fe1e642011-12-29 14:47:27 +08003704 bitmap_clear_bits(ctl, entry, start, bytes);
3705 if (entry->bytes == 0)
3706 free_bitmap(ctl, entry);
3707
3708 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003709 trim_entry.start = start;
3710 trim_entry.bytes = bytes;
3711 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3712 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003713
3714 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003715 start, bytes, 0, &trim_entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003716 if (ret) {
3717 reset_trimming_bitmap(ctl, offset);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003718 block_group->discard_cursor =
3719 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003720 break;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003721 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003722next:
3723 if (next_bitmap) {
3724 offset += BITS_PER_BITMAP * ctl->unit;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003725 start = offset;
Li Zefan7fe1e642011-12-29 14:47:27 +08003726 } else {
3727 start += bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003728 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003729 block_group->discard_cursor = start;
Li Zefan7fe1e642011-12-29 14:47:27 +08003730
3731 if (fatal_signal_pending(current)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003732 if (start != offset)
3733 reset_trimming_bitmap(ctl, offset);
Li Zefan7fe1e642011-12-29 14:47:27 +08003734 ret = -ERESTARTSYS;
3735 break;
3736 }
3737
3738 cond_resched();
3739 }
3740
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003741 if (offset >= end)
3742 block_group->discard_cursor = end;
3743
3744out:
Li Zefan7fe1e642011-12-29 14:47:27 +08003745 return ret;
3746}
3747
David Sterba32da53862019-10-29 19:20:18 +01003748int btrfs_trim_block_group(struct btrfs_block_group *block_group,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003749 u64 *trimmed, u64 start, u64 end, u64 minlen)
3750{
Dennis Zhouda080fe2019-12-13 16:22:13 -08003751 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003752 int ret;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003753 u64 rem = 0;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003754
3755 *trimmed = 0;
3756
3757 spin_lock(&block_group->lock);
3758 if (block_group->removed) {
3759 spin_unlock(&block_group->lock);
3760 return 0;
3761 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003762 btrfs_freeze_block_group(block_group);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003763 spin_unlock(&block_group->lock);
3764
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003765 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003766 if (ret)
3767 goto out;
3768
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003769 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003770 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
3771 /* If we ended in the middle of a bitmap, reset the trimming flag */
3772 if (rem)
3773 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003774out:
Filipe Manana6b7304a2020-05-08 11:01:47 +01003775 btrfs_unfreeze_block_group(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003776 return ret;
3777}
Li Zefan581bb052011-04-20 10:06:11 +08003778
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003779int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
3780 u64 *trimmed, u64 start, u64 end, u64 minlen,
3781 bool async)
3782{
3783 int ret;
3784
3785 *trimmed = 0;
3786
3787 spin_lock(&block_group->lock);
3788 if (block_group->removed) {
3789 spin_unlock(&block_group->lock);
3790 return 0;
3791 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003792 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003793 spin_unlock(&block_group->lock);
3794
3795 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
Filipe Manana6b7304a2020-05-08 11:01:47 +01003796 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003797
3798 return ret;
3799}
3800
3801int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
3802 u64 *trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003803 u64 maxlen, bool async)
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003804{
3805 int ret;
3806
3807 *trimmed = 0;
3808
3809 spin_lock(&block_group->lock);
3810 if (block_group->removed) {
3811 spin_unlock(&block_group->lock);
3812 return 0;
3813 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003814 btrfs_freeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003815 spin_unlock(&block_group->lock);
3816
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003817 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
3818 async);
3819
Filipe Manana6b7304a2020-05-08 11:01:47 +01003820 btrfs_unfreeze_block_group(block_group);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003821
3822 return ret;
3823}
3824
Boris Burkov94846222020-11-18 15:06:22 -08003825bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info)
3826{
3827 return btrfs_super_cache_generation(fs_info->super_copy);
3828}
3829
Boris Burkov36b216c2020-11-18 15:06:25 -08003830static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info,
3831 struct btrfs_trans_handle *trans)
3832{
3833 struct btrfs_block_group *block_group;
3834 struct rb_node *node;
3835 int ret;
3836
3837 btrfs_info(fs_info, "cleaning free space cache v1");
3838
3839 node = rb_first(&fs_info->block_group_cache_tree);
3840 while (node) {
3841 block_group = rb_entry(node, struct btrfs_block_group, cache_node);
3842 ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
3843 if (ret)
3844 goto out;
3845 node = rb_next(node);
3846 }
3847out:
3848 return ret;
3849}
3850
Boris Burkov94846222020-11-18 15:06:22 -08003851int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active)
3852{
3853 struct btrfs_trans_handle *trans;
3854 int ret;
3855
3856 /*
Boris Burkov36b216c2020-11-18 15:06:25 -08003857 * update_super_roots will appropriately set or unset
3858 * super_copy->cache_generation based on SPACE_CACHE and
3859 * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a
3860 * transaction commit whether we are enabling space cache v1 and don't
3861 * have any other work to do, or are disabling it and removing free
3862 * space inodes.
Boris Burkov94846222020-11-18 15:06:22 -08003863 */
3864 trans = btrfs_start_transaction(fs_info->tree_root, 0);
3865 if (IS_ERR(trans))
3866 return PTR_ERR(trans);
3867
Boris Burkov36b216c2020-11-18 15:06:25 -08003868 if (!active) {
Boris Burkov94846222020-11-18 15:06:22 -08003869 set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
Boris Burkov36b216c2020-11-18 15:06:25 -08003870 ret = cleanup_free_space_cache_v1(fs_info, trans);
3871 if (ret) {
3872 btrfs_abort_transaction(trans, ret);
3873 btrfs_end_transaction(trans);
3874 goto out;
3875 }
3876 }
Boris Burkov94846222020-11-18 15:06:22 -08003877
3878 ret = btrfs_commit_transaction(trans);
Boris Burkov36b216c2020-11-18 15:06:25 -08003879out:
Boris Burkov94846222020-11-18 15:06:22 -08003880 clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
3881
3882 return ret;
3883}
3884
Josef Bacik74255aa2013-03-15 09:47:08 -04003885#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003886/*
3887 * Use this if you need to make a bitmap or extent entry specifically, it
3888 * doesn't do any of the merging that add_free_space does, this acts a lot like
3889 * how the free space cache loading stuff works, so you can get really weird
3890 * configurations.
3891 */
David Sterba32da53862019-10-29 19:20:18 +01003892int test_add_free_space_entry(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04003893 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003894{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003895 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3896 struct btrfs_free_space *info = NULL, *bitmap_info;
3897 void *map = NULL;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003898 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003899 u64 bytes_added;
3900 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003901
Josef Bacikdc11dd52013-08-14 15:05:12 -04003902again:
3903 if (!info) {
3904 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3905 if (!info)
3906 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003907 }
3908
Josef Bacikdc11dd52013-08-14 15:05:12 -04003909 if (!bitmap) {
3910 spin_lock(&ctl->tree_lock);
3911 info->offset = offset;
3912 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003913 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003914 ret = link_free_space(ctl, info);
3915 spin_unlock(&ctl->tree_lock);
3916 if (ret)
3917 kmem_cache_free(btrfs_free_space_cachep, info);
3918 return ret;
3919 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003920
Josef Bacikdc11dd52013-08-14 15:05:12 -04003921 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003922 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003923 if (!map) {
3924 kmem_cache_free(btrfs_free_space_cachep, info);
3925 return -ENOMEM;
3926 }
3927 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003928
Josef Bacikdc11dd52013-08-14 15:05:12 -04003929 spin_lock(&ctl->tree_lock);
3930 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3931 1, 0);
3932 if (!bitmap_info) {
3933 info->bitmap = map;
3934 map = NULL;
3935 add_new_bitmap(ctl, info, offset);
3936 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003937 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003938 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003939
Dennis Zhouda080fe2019-12-13 16:22:13 -08003940 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
3941 trim_state);
Josef Bacikcef40482015-10-02 16:09:42 -04003942
Josef Bacikdc11dd52013-08-14 15:05:12 -04003943 bytes -= bytes_added;
3944 offset += bytes_added;
3945 spin_unlock(&ctl->tree_lock);
3946
3947 if (bytes)
3948 goto again;
3949
Filipe Manana20005522014-08-29 13:35:13 +01003950 if (info)
3951 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00003952 if (map)
3953 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003954 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003955}
3956
3957/*
3958 * Checks to see if the given range is in the free space cache. This is really
3959 * just used to check the absence of space, so if there is free space in the
3960 * range at all we will return 1.
3961 */
David Sterba32da53862019-10-29 19:20:18 +01003962int test_check_exists(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04003963 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003964{
3965 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3966 struct btrfs_free_space *info;
3967 int ret = 0;
3968
3969 spin_lock(&ctl->tree_lock);
3970 info = tree_search_offset(ctl, offset, 0, 0);
3971 if (!info) {
3972 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3973 1, 0);
3974 if (!info)
3975 goto out;
3976 }
3977
3978have_info:
3979 if (info->bitmap) {
3980 u64 bit_off, bit_bytes;
3981 struct rb_node *n;
3982 struct btrfs_free_space *tmp;
3983
3984 bit_off = offset;
3985 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003986 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003987 if (!ret) {
3988 if (bit_off == offset) {
3989 ret = 1;
3990 goto out;
3991 } else if (bit_off > offset &&
3992 offset + bytes > bit_off) {
3993 ret = 1;
3994 goto out;
3995 }
3996 }
3997
3998 n = rb_prev(&info->offset_index);
3999 while (n) {
4000 tmp = rb_entry(n, struct btrfs_free_space,
4001 offset_index);
4002 if (tmp->offset + tmp->bytes < offset)
4003 break;
4004 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004005 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004006 continue;
4007 }
4008 info = tmp;
4009 goto have_info;
4010 }
4011
4012 n = rb_next(&info->offset_index);
4013 while (n) {
4014 tmp = rb_entry(n, struct btrfs_free_space,
4015 offset_index);
4016 if (offset + bytes < tmp->offset)
4017 break;
4018 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004019 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004020 continue;
4021 }
4022 info = tmp;
4023 goto have_info;
4024 }
4025
Filipe Manana20005522014-08-29 13:35:13 +01004026 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004027 goto out;
4028 }
4029
4030 if (info->offset == offset) {
4031 ret = 1;
4032 goto out;
4033 }
4034
4035 if (offset > info->offset && offset < info->offset + info->bytes)
4036 ret = 1;
4037out:
4038 spin_unlock(&ctl->tree_lock);
4039 return ret;
4040}
Josef Bacikdc11dd52013-08-14 15:05:12 -04004041#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */