blob: 7813640c12aaee5bafb56caa9c941b1c002ac31b [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"
Li Zefan581bb052011-04-20 10:06:11 +080019#include "inode-map.h"
Filipe Manana04216822014-11-27 21:14:15 +000020#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040021#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040022#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040023#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080024#include "discard.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025
Feifei Xu0ef64472016-06-01 19:18:24 +080026#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Dennis Zhou5d90c5c2020-01-02 16:26:43 -050027#define MAX_CACHE_BYTES_PER_GIG SZ_64K
28#define FORCE_EXTENT_THRESHOLD SZ_1M
Josef Bacik96303082009-07-13 21:29:25 -040029
Filipe Manana55507ce2014-12-01 17:04:09 +000030struct btrfs_trim_range {
31 u64 start;
32 u64 bytes;
33 struct list_head list;
34};
35
Dennis Zhoudfb79dd2019-12-13 16:22:20 -080036static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl,
37 struct btrfs_free_space *bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +080038static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040039 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040040static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
41 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040042static int btrfs_wait_cache_io_root(struct btrfs_root *root,
43 struct btrfs_trans_handle *trans,
44 struct btrfs_io_ctl *io_ctl,
45 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040046
Li Zefan0414efa2011-04-20 10:20:14 +080047static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
48 struct btrfs_path *path,
49 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040051 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040052 struct btrfs_key key;
53 struct btrfs_key location;
54 struct btrfs_disk_key disk_key;
55 struct btrfs_free_space_header *header;
56 struct extent_buffer *leaf;
57 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040058 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040059 int ret;
60
Josef Bacik0af3d002010-06-21 14:48:16 -040061 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080062 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040063 key.type = 0;
64
65 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
66 if (ret < 0)
67 return ERR_PTR(ret);
68 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020069 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040070 return ERR_PTR(-ENOENT);
71 }
72
73 leaf = path->nodes[0];
74 header = btrfs_item_ptr(leaf, path->slots[0],
75 struct btrfs_free_space_header);
76 btrfs_free_space_key(leaf, header, &disk_key);
77 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020078 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040079
Josef Bacik84de76a2018-09-28 07:17:49 -040080 /*
81 * We are often under a trans handle at this point, so we need to make
82 * sure NOFS is set to keep us from deadlocking.
83 */
84 nofs_flag = memalloc_nofs_save();
David Sterba4c66e0d2019-10-03 19:09:35 +020085 inode = btrfs_iget_path(fs_info->sb, &location, root, path);
Filipe Manana4222ea72018-10-24 10:13:03 +010086 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040087 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040088 if (IS_ERR(inode))
89 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040090
Al Viro528c0322012-04-13 11:03:55 -040091 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080092 mapping_gfp_constraint(inode->i_mapping,
93 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000094
Li Zefan0414efa2011-04-20 10:20:14 +080095 return inode;
96}
97
David Sterba32da53862019-10-29 19:20:18 +010098struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
David Sterba7949f332019-03-20 13:40:19 +010099 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +0800100{
David Sterba7949f332019-03-20 13:40:19 +0100101 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +0800102 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400103 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800104
105 spin_lock(&block_group->lock);
106 if (block_group->inode)
107 inode = igrab(block_group->inode);
108 spin_unlock(&block_group->lock);
109 if (inode)
110 return inode;
111
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500112 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200113 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800114 if (IS_ERR(inode))
115 return inode;
116
Josef Bacik0af3d002010-06-21 14:48:16 -0400117 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400118 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400119 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400120 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
121 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400122 block_group->disk_cache_state = BTRFS_DC_CLEAR;
123 }
124
Josef Bacik300e4f82011-08-29 14:06:00 -0400125 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400126 block_group->inode = igrab(inode);
127 block_group->iref = 1;
128 }
129 spin_unlock(&block_group->lock);
130
131 return inode;
132}
133
Eric Sandeen48a3b632013-04-25 20:41:01 +0000134static int __create_free_space_inode(struct btrfs_root *root,
135 struct btrfs_trans_handle *trans,
136 struct btrfs_path *path,
137 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400138{
139 struct btrfs_key key;
140 struct btrfs_disk_key disk_key;
141 struct btrfs_free_space_header *header;
142 struct btrfs_inode_item *inode_item;
143 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400144 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400145 int ret;
146
Li Zefan0414efa2011-04-20 10:20:14 +0800147 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400148 if (ret)
149 return ret;
150
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400151 /* We inline crc's for the free disk space cache */
152 if (ino != BTRFS_FREE_INO_OBJECTID)
153 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
154
Josef Bacik0af3d002010-06-21 14:48:16 -0400155 leaf = path->nodes[0];
156 inode_item = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_inode_item);
158 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100159 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400160 sizeof(*inode_item));
161 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
162 btrfs_set_inode_size(leaf, inode_item, 0);
163 btrfs_set_inode_nbytes(leaf, inode_item, 0);
164 btrfs_set_inode_uid(leaf, inode_item, 0);
165 btrfs_set_inode_gid(leaf, inode_item, 0);
166 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400167 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400168 btrfs_set_inode_nlink(leaf, inode_item, 1);
169 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800170 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400171 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200172 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400173
174 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800175 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400176 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 ret = btrfs_insert_empty_item(trans, root, path, &key,
178 sizeof(struct btrfs_free_space_header));
179 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200180 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400181 return ret;
182 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700183
Josef Bacik0af3d002010-06-21 14:48:16 -0400184 leaf = path->nodes[0];
185 header = btrfs_item_ptr(leaf, path->slots[0],
186 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100187 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400188 btrfs_set_free_space_key(leaf, header, &disk_key);
189 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200190 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400191
192 return 0;
193}
194
David Sterba4ca75f12019-03-20 13:42:57 +0100195int create_free_space_inode(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100196 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +0800197 struct btrfs_path *path)
198{
199 int ret;
200 u64 ino;
201
David Sterba4ca75f12019-03-20 13:42:57 +0100202 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800203 if (ret < 0)
204 return ret;
205
David Sterba4ca75f12019-03-20 13:42:57 +0100206 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200207 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800208}
209
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400210int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000211 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400212{
Josef Bacikc8174312011-11-02 09:29:35 -0400213 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000214 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400215
216 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400217 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
218 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400219
Miao Xie7b61cd92013-05-13 13:55:09 +0000220 spin_lock(&rsv->lock);
221 if (rsv->reserved < needed_bytes)
222 ret = -ENOSPC;
223 else
224 ret = 0;
225 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000226 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000227}
228
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500229int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100230 struct btrfs_block_group *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000231 struct inode *inode)
232{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500233 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000234 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100235 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700236
Chris Mason1bbc6212015-04-06 12:46:08 -0700237 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500238 struct btrfs_path *path = btrfs_alloc_path();
239
240 if (!path) {
241 ret = -ENOMEM;
242 goto fail;
243 }
Filipe Manana35c76642015-04-30 17:47:05 +0100244 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700245 mutex_lock(&trans->transaction->cache_write_mutex);
246 if (!list_empty(&block_group->io_list)) {
247 list_del_init(&block_group->io_list);
248
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400249 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700250 btrfs_put_block_group(block_group);
251 }
252
253 /*
254 * now that we've truncated the cache away, its no longer
255 * setup or written
256 */
257 spin_lock(&block_group->lock);
258 block_group->disk_cache_state = BTRFS_DC_CLEAR;
259 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500260 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700261 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400262
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200263 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700264 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400265
266 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700267 * We skip the throttling logic for free space cache inodes, so we don't
268 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400269 */
270 ret = btrfs_truncate_inode_items(trans, root, inode,
271 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100272 if (ret)
273 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400274
Li Zefan82d59022011-04-20 10:33:24 +0800275 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700276
Chris Mason1bbc6212015-04-06 12:46:08 -0700277fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100278 if (locked)
279 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100280 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400281 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400282
Li Zefan82d59022011-04-20 10:33:24 +0800283 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400284}
285
David Sterba1d480532017-01-23 17:28:19 +0100286static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400287{
288 struct file_ra_state *ra;
289 unsigned long last_index;
290
291 ra = kzalloc(sizeof(*ra), GFP_NOFS);
292 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100293 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400294
295 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300296 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400297
298 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
299
300 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400301}
302
Chris Mason4c6d1d82015-04-06 13:17:20 -0700303static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400304 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400305{
Miao Xie5349d6c2014-06-19 10:42:49 +0800306 int num_pages;
307 int check_crcs = 0;
308
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300309 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800310
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200311 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800312 check_crcs = 1;
313
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400314 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800315 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400316 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800317 return -ENOSPC;
318
Chris Mason4c6d1d82015-04-06 13:17:20 -0700319 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800320
David Sterba31e818f2015-02-20 18:00:26 +0100321 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400322 if (!io_ctl->pages)
323 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800324
325 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400326 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800327 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700328 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800329
Josef Bacika67509c2011-10-05 15:18:58 -0400330 return 0;
331}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900332ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400333
Chris Mason4c6d1d82015-04-06 13:17:20 -0700334static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400335{
336 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700337 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400338}
339
Chris Mason4c6d1d82015-04-06 13:17:20 -0700340static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400341{
342 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400343 io_ctl->cur = NULL;
344 io_ctl->orig = NULL;
345 }
346}
347
Chris Mason4c6d1d82015-04-06 13:17:20 -0700348static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400349{
Josef Bacikb12d6862013-08-26 17:14:08 -0400350 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400351 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700352 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400353 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300354 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400355 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200356 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400357}
358
Chris Mason4c6d1d82015-04-06 13:17:20 -0700359static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400360{
361 int i;
362
363 io_ctl_unmap_page(io_ctl);
364
365 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800366 if (io_ctl->pages[i]) {
367 ClearPageChecked(io_ctl->pages[i]);
368 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300369 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800370 }
Josef Bacika67509c2011-10-05 15:18:58 -0400371 }
372}
373
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900374static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, int uptodate)
Josef Bacika67509c2011-10-05 15:18:58 -0400375{
376 struct page *page;
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900377 struct inode *inode = io_ctl->inode;
Josef Bacika67509c2011-10-05 15:18:58 -0400378 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
379 int i;
380
381 for (i = 0; i < io_ctl->num_pages; i++) {
382 page = find_or_create_page(inode->i_mapping, i, mask);
383 if (!page) {
384 io_ctl_drop_pages(io_ctl);
385 return -ENOMEM;
386 }
387 io_ctl->pages[i] = page;
388 if (uptodate && !PageUptodate(page)) {
389 btrfs_readpage(NULL, page);
390 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400391 if (page->mapping != inode->i_mapping) {
392 btrfs_err(BTRFS_I(inode)->root->fs_info,
393 "free space cache page truncated");
394 io_ctl_drop_pages(io_ctl);
395 return -EIO;
396 }
Josef Bacika67509c2011-10-05 15:18:58 -0400397 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500398 btrfs_err(BTRFS_I(inode)->root->fs_info,
399 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400400 io_ctl_drop_pages(io_ctl);
401 return -EIO;
402 }
403 }
404 }
405
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500406 for (i = 0; i < io_ctl->num_pages; i++) {
407 clear_page_dirty_for_io(io_ctl->pages[i]);
408 set_page_extent_mapped(io_ctl->pages[i]);
409 }
410
Josef Bacika67509c2011-10-05 15:18:58 -0400411 return 0;
412}
413
Chris Mason4c6d1d82015-04-06 13:17:20 -0700414static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400415{
Al Viro528c0322012-04-13 11:03:55 -0400416 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400417
418 io_ctl_map_page(io_ctl, 1);
419
420 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400421 * Skip the csum areas. If we don't check crcs then we just have a
422 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400423 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400424 if (io_ctl->check_crcs) {
425 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
426 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
427 } else {
428 io_ctl->cur += sizeof(u64);
429 io_ctl->size -= sizeof(u64) * 2;
430 }
Josef Bacika67509c2011-10-05 15:18:58 -0400431
432 val = io_ctl->cur;
433 *val = cpu_to_le64(generation);
434 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400435}
436
Chris Mason4c6d1d82015-04-06 13:17:20 -0700437static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400438{
Al Viro528c0322012-04-13 11:03:55 -0400439 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400440
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400441 /*
442 * Skip the crc area. If we don't check crcs then we just have a 64bit
443 * chunk at the front of the first page.
444 */
445 if (io_ctl->check_crcs) {
446 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
447 io_ctl->size -= sizeof(u64) +
448 (sizeof(u32) * io_ctl->num_pages);
449 } else {
450 io_ctl->cur += sizeof(u64);
451 io_ctl->size -= sizeof(u64) * 2;
452 }
Josef Bacika67509c2011-10-05 15:18:58 -0400453
Josef Bacika67509c2011-10-05 15:18:58 -0400454 gen = io_ctl->cur;
455 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400456 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200457 "space cache generation (%llu) does not match inode (%llu)",
458 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400459 io_ctl_unmap_page(io_ctl);
460 return -EIO;
461 }
462 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400463 return 0;
464}
465
Chris Mason4c6d1d82015-04-06 13:17:20 -0700466static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400467{
468 u32 *tmp;
469 u32 crc = ~(u32)0;
470 unsigned offset = 0;
471
472 if (!io_ctl->check_crcs) {
473 io_ctl_unmap_page(io_ctl);
474 return;
475 }
476
477 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800478 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400479
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200480 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
481 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400482 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700483 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400484 tmp += index;
485 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400486}
487
Chris Mason4c6d1d82015-04-06 13:17:20 -0700488static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400489{
490 u32 *tmp, val;
491 u32 crc = ~(u32)0;
492 unsigned offset = 0;
493
494 if (!io_ctl->check_crcs) {
495 io_ctl_map_page(io_ctl, 0);
496 return 0;
497 }
498
499 if (index == 0)
500 offset = sizeof(u32) * io_ctl->num_pages;
501
Chris Mason2b108262015-04-06 07:48:20 -0700502 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400503 tmp += index;
504 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505
506 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200507 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
508 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400509 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400510 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200511 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400512 io_ctl_unmap_page(io_ctl);
513 return -EIO;
514 }
515
Josef Bacika67509c2011-10-05 15:18:58 -0400516 return 0;
517}
518
Chris Mason4c6d1d82015-04-06 13:17:20 -0700519static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400520 void *bitmap)
521{
522 struct btrfs_free_space_entry *entry;
523
524 if (!io_ctl->cur)
525 return -ENOSPC;
526
527 entry = io_ctl->cur;
528 entry->offset = cpu_to_le64(offset);
529 entry->bytes = cpu_to_le64(bytes);
530 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
531 BTRFS_FREE_SPACE_EXTENT;
532 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
533 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
534
535 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
536 return 0;
537
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400538 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400539
540 /* No more pages to map */
541 if (io_ctl->index >= io_ctl->num_pages)
542 return 0;
543
544 /* map the next page */
545 io_ctl_map_page(io_ctl, 1);
546 return 0;
547}
548
Chris Mason4c6d1d82015-04-06 13:17:20 -0700549static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400550{
551 if (!io_ctl->cur)
552 return -ENOSPC;
553
554 /*
555 * If we aren't at the start of the current page, unmap this one and
556 * map the next one if there is any left.
557 */
558 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400559 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400560 if (io_ctl->index >= io_ctl->num_pages)
561 return -ENOSPC;
562 io_ctl_map_page(io_ctl, 0);
563 }
564
David Sterba69d24802018-06-29 10:56:44 +0200565 copy_page(io_ctl->cur, bitmap);
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 if (io_ctl->index < io_ctl->num_pages)
568 io_ctl_map_page(io_ctl, 0);
569 return 0;
570}
571
Chris Mason4c6d1d82015-04-06 13:17:20 -0700572static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400573{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400574 /*
575 * If we're not on the boundary we know we've modified the page and we
576 * need to crc the page.
577 */
578 if (io_ctl->cur != io_ctl->orig)
579 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
580 else
581 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400582
583 while (io_ctl->index < io_ctl->num_pages) {
584 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400585 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400586 }
587}
588
Chris Mason4c6d1d82015-04-06 13:17:20 -0700589static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400590 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400591{
592 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500593 int ret;
594
595 if (!io_ctl->cur) {
596 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
597 if (ret)
598 return ret;
599 }
Josef Bacika67509c2011-10-05 15:18:58 -0400600
601 e = io_ctl->cur;
602 entry->offset = le64_to_cpu(e->offset);
603 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400604 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400605 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
606 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
607
608 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400609 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400610
611 io_ctl_unmap_page(io_ctl);
612
Josef Bacik2f120c02011-11-10 20:45:05 -0500613 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400614}
615
Chris Mason4c6d1d82015-04-06 13:17:20 -0700616static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400617 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400618{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400619 int ret;
620
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400621 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
622 if (ret)
623 return ret;
624
David Sterba69d24802018-06-29 10:56:44 +0200625 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400626 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400627
628 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400629}
630
Josef Bacikcd023e72012-05-14 10:06:40 -0400631/*
632 * Since we attach pinned extents after the fact we can have contiguous sections
633 * of free space that are split up in entries. This poses a problem with the
634 * tree logging stuff since it could have allocated across what appears to be 2
635 * entries since we would have merged the entries when adding the pinned extents
636 * back to the free space cache. So run through the space cache that we just
637 * loaded and merge contiguous entries. This will make the log replay stuff not
638 * blow up and it will make for nicer allocator behavior.
639 */
640static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
641{
642 struct btrfs_free_space *e, *prev = NULL;
643 struct rb_node *n;
644
645again:
646 spin_lock(&ctl->tree_lock);
647 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
648 e = rb_entry(n, struct btrfs_free_space, offset_index);
649 if (!prev)
650 goto next;
651 if (e->bitmap || prev->bitmap)
652 goto next;
653 if (prev->offset + prev->bytes == e->offset) {
654 unlink_free_space(ctl, prev);
655 unlink_free_space(ctl, e);
656 prev->bytes += e->bytes;
657 kmem_cache_free(btrfs_free_space_cachep, e);
658 link_free_space(ctl, prev);
659 prev = NULL;
660 spin_unlock(&ctl->tree_lock);
661 goto again;
662 }
663next:
664 prev = e;
665 }
666 spin_unlock(&ctl->tree_lock);
667}
668
Eric Sandeen48a3b632013-04-25 20:41:01 +0000669static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
670 struct btrfs_free_space_ctl *ctl,
671 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400672{
David Sterba3ffbd682018-06-29 10:56:42 +0200673 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 struct btrfs_free_space_header *header;
675 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700676 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400677 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400678 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800679 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400680 u64 num_entries;
681 u64 num_bitmaps;
682 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400683 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400684 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685
Josef Bacik9d66e232010-08-25 16:54:15 -0400686 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800687 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400688 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400689
690 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800691 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400692 key.type = 0;
693
694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800695 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400696 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800697 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400698 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400699 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 }
701
Li Zefan0414efa2011-04-20 10:20:14 +0800702 ret = -1;
703
Josef Bacik9d66e232010-08-25 16:54:15 -0400704 leaf = path->nodes[0];
705 header = btrfs_item_ptr(leaf, path->slots[0],
706 struct btrfs_free_space_header);
707 num_entries = btrfs_free_space_entries(leaf, header);
708 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
709 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400710 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400711
Miao Xiee570fd22014-06-19 10:42:50 +0800712 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200714 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800715 offset);
716 return 0;
717 }
718
Josef Bacik9d66e232010-08-25 16:54:15 -0400719 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400720 btrfs_err(fs_info,
721 "free space inode generation (%llu) did not match free space cache generation (%llu)",
722 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400723 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400724 }
725
726 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400727 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400728
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400729 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800730 if (ret)
731 return ret;
732
David Sterba1d480532017-01-23 17:28:19 +0100733 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400734
Johannes Thumshirn831fa142020-02-12 00:10:19 +0900735 ret = io_ctl_prepare_pages(&io_ctl, 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400736 if (ret)
737 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400738
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400739 ret = io_ctl_check_crc(&io_ctl, 0);
740 if (ret)
741 goto free_cache;
742
Josef Bacika67509c2011-10-05 15:18:58 -0400743 ret = io_ctl_check_generation(&io_ctl, generation);
744 if (ret)
745 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400746
Josef Bacika67509c2011-10-05 15:18:58 -0400747 while (num_entries) {
748 e = kmem_cache_zalloc(btrfs_free_space_cachep,
749 GFP_NOFS);
750 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400751 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400752
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400753 ret = io_ctl_read_entry(&io_ctl, e, &type);
754 if (ret) {
755 kmem_cache_free(btrfs_free_space_cachep, e);
756 goto free_cache;
757 }
758
Dennis Zhoua7ccb252019-12-13 16:22:12 -0800759 /*
760 * Sync discard ensures that the free space cache is always
761 * trimmed. So when reading this in, the state should reflect
Dennis Zhoub0643e52019-12-13 16:22:14 -0800762 * that. We also do this for async as a stop gap for lack of
763 * persistence.
Dennis Zhoua7ccb252019-12-13 16:22:12 -0800764 */
Dennis Zhoub0643e52019-12-13 16:22:14 -0800765 if (btrfs_test_opt(fs_info, DISCARD_SYNC) ||
766 btrfs_test_opt(fs_info, DISCARD_ASYNC))
Dennis Zhoua7ccb252019-12-13 16:22:12 -0800767 e->trim_state = BTRFS_TRIM_STATE_TRIMMED;
768
Josef Bacika67509c2011-10-05 15:18:58 -0400769 if (!e->bytes) {
770 kmem_cache_free(btrfs_free_space_cachep, e);
771 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400772 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400773
Josef Bacika67509c2011-10-05 15:18:58 -0400774 if (type == BTRFS_FREE_SPACE_EXTENT) {
775 spin_lock(&ctl->tree_lock);
776 ret = link_free_space(ctl, e);
777 spin_unlock(&ctl->tree_lock);
778 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400779 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000780 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500781 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400782 goto free_cache;
783 }
Josef Bacika67509c2011-10-05 15:18:58 -0400784 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400785 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400786 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000787 e->bitmap = kmem_cache_zalloc(
788 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400789 if (!e->bitmap) {
790 kmem_cache_free(
791 btrfs_free_space_cachep, e);
792 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400793 }
Josef Bacika67509c2011-10-05 15:18:58 -0400794 spin_lock(&ctl->tree_lock);
795 ret = link_free_space(ctl, e);
796 ctl->total_bitmaps++;
797 ctl->op->recalc_thresholds(ctl);
798 spin_unlock(&ctl->tree_lock);
799 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400800 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000801 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400802 kmem_cache_free(btrfs_free_space_cachep, e);
803 goto free_cache;
804 }
805 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400806 }
807
Josef Bacika67509c2011-10-05 15:18:58 -0400808 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400809 }
810
Josef Bacik2f120c02011-11-10 20:45:05 -0500811 io_ctl_unmap_page(&io_ctl);
812
Josef Bacika67509c2011-10-05 15:18:58 -0400813 /*
814 * We add the bitmaps at the end of the entries in order that
815 * the bitmap entries are added to the cache.
816 */
817 list_for_each_entry_safe(e, n, &bitmaps, list) {
818 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400819 ret = io_ctl_read_bitmap(&io_ctl, e);
820 if (ret)
821 goto free_cache;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800822 e->bitmap_extents = count_bitmap_extents(ctl, e);
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800823 if (!btrfs_free_space_trimmed(e)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800824 ctl->discardable_extents[BTRFS_STAT_CURR] +=
825 e->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800826 ctl->discardable_bytes[BTRFS_STAT_CURR] += e->bytes;
827 }
Josef Bacika67509c2011-10-05 15:18:58 -0400828 }
829
830 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400831 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400832 ret = 1;
833out:
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800834 btrfs_discard_update_discardable(ctl->private, ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400835 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400836 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400837free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400838 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800839 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400840 goto out;
841}
842
David Sterba32da53862019-10-29 19:20:18 +0100843int load_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400844{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100845 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800846 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800847 struct inode *inode;
848 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400849 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800850 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200851 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800852
853 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800854 * If this block group has been marked to be cleared for one reason or
855 * another then we can't trust the on disk cache, so just return.
856 */
857 spin_lock(&block_group->lock);
858 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
859 spin_unlock(&block_group->lock);
860 return 0;
861 }
862 spin_unlock(&block_group->lock);
863
864 path = btrfs_alloc_path();
865 if (!path)
866 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400867 path->search_commit_root = 1;
868 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800869
Filipe Manana4222ea72018-10-24 10:13:03 +0100870 /*
871 * We must pass a path with search_commit_root set to btrfs_iget in
872 * order to avoid a deadlock when allocating extents for the tree root.
873 *
874 * When we are COWing an extent buffer from the tree root, when looking
875 * for a free extent, at extent-tree.c:find_free_extent(), we can find
876 * block group without its free space cache loaded. When we find one
877 * we must load its space cache which requires reading its free space
878 * cache's inode item from the root tree. If this inode item is located
879 * in the same leaf that we started COWing before, then we end up in
880 * deadlock on the extent buffer (trying to read lock it when we
881 * previously write locked it).
882 *
883 * It's safe to read the inode item using the commit root because
884 * block groups, once loaded, stay in memory forever (until they are
885 * removed) as well as their space caches once loaded. New block groups
886 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
887 * we will never try to read their inode item while the fs is mounted.
888 */
David Sterba7949f332019-03-20 13:40:19 +0100889 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800890 if (IS_ERR(inode)) {
891 btrfs_free_path(path);
892 return 0;
893 }
894
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400895 /* We may have converted the inode and made the cache invalid. */
896 spin_lock(&block_group->lock);
897 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
898 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900899 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400900 goto out;
901 }
902 spin_unlock(&block_group->lock);
903
Li Zefan0414efa2011-04-20 10:20:14 +0800904 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200905 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800906 btrfs_free_path(path);
907 if (ret <= 0)
908 goto out;
909
910 spin_lock(&ctl->tree_lock);
David Sterbab3470b52019-10-23 18:48:22 +0200911 matched = (ctl->free_space == (block_group->length - used -
Li Zefan0414efa2011-04-20 10:20:14 +0800912 block_group->bytes_super));
913 spin_unlock(&ctl->tree_lock);
914
915 if (!matched) {
916 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400917 btrfs_warn(fs_info,
918 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +0200919 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800920 ret = -1;
921 }
922out:
923 if (ret < 0) {
924 /* This cache is bogus, make sure it gets cleared */
925 spin_lock(&block_group->lock);
926 block_group->disk_cache_state = BTRFS_DC_CLEAR;
927 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800928 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800929
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400930 btrfs_warn(fs_info,
931 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +0200932 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800933 }
934
935 iput(inode);
936 return ret;
937}
938
Chris Masond4452bc2014-05-19 20:47:56 -0700939static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700940int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700941 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +0100942 struct btrfs_block_group *block_group,
Chris Masond4452bc2014-05-19 20:47:56 -0700943 int *entries, int *bitmaps,
944 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400945{
Josef Bacikc09544e2011-08-30 10:19:10 -0400946 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700947 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700948 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700949 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000950 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400951
Josef Bacik43be2142011-04-01 14:55:00 +0000952 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700953 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000954 cluster = list_entry(block_group->cluster_list.next,
955 struct btrfs_free_cluster,
956 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700957 }
Josef Bacik43be2142011-04-01 14:55:00 +0000958
Josef Bacikf75b1302011-10-05 10:00:18 -0400959 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700960 cluster_locked = cluster;
961 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400962 node = rb_first(&cluster->root);
963 cluster = NULL;
964 }
965
Josef Bacik0cb59c92010-07-02 12:14:14 -0400966 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400967 while (node) {
968 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400969
Josef Bacika67509c2011-10-05 15:18:58 -0400970 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700971 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000972
Chris Masond4452bc2014-05-19 20:47:56 -0700973 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400974 e->bitmap);
975 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700976 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400977
978 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700979 list_add_tail(&e->list, bitmap_list);
980 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400981 }
982 node = rb_next(node);
983 if (!node && cluster) {
984 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700985 cluster_locked = cluster;
986 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400987 cluster = NULL;
988 }
989 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700990 if (cluster_locked) {
991 spin_unlock(&cluster_locked->lock);
992 cluster_locked = NULL;
993 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000994
995 /*
996 * Make sure we don't miss any range that was removed from our rbtree
997 * because trimming is running. Otherwise after a umount+mount (or crash
998 * after committing the transaction) we would leak free space and get
999 * an inconsistent free space cache report from fsck.
1000 */
1001 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
1002 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
1003 trim_entry->bytes, NULL);
1004 if (ret)
1005 goto fail;
1006 *entries += 1;
1007 }
1008
Chris Masond4452bc2014-05-19 20:47:56 -07001009 return 0;
1010fail:
Chris Mason1bbc6212015-04-06 12:46:08 -07001011 if (cluster_locked)
1012 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -07001013 return -ENOSPC;
1014}
1015
1016static noinline_for_stack int
1017update_cache_item(struct btrfs_trans_handle *trans,
1018 struct btrfs_root *root,
1019 struct inode *inode,
1020 struct btrfs_path *path, u64 offset,
1021 int entries, int bitmaps)
1022{
1023 struct btrfs_key key;
1024 struct btrfs_free_space_header *header;
1025 struct extent_buffer *leaf;
1026 int ret;
1027
1028 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1029 key.offset = offset;
1030 key.type = 0;
1031
1032 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1033 if (ret < 0) {
1034 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001035 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001036 goto fail;
1037 }
1038 leaf = path->nodes[0];
1039 if (ret > 0) {
1040 struct btrfs_key found_key;
1041 ASSERT(path->slots[0]);
1042 path->slots[0]--;
1043 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1044 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1045 found_key.offset != offset) {
1046 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001047 inode->i_size - 1, EXTENT_DELALLOC, 0,
1048 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001049 btrfs_release_path(path);
1050 goto fail;
1051 }
1052 }
1053
1054 BTRFS_I(inode)->generation = trans->transid;
1055 header = btrfs_item_ptr(leaf, path->slots[0],
1056 struct btrfs_free_space_header);
1057 btrfs_set_free_space_entries(leaf, header, entries);
1058 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1059 btrfs_set_free_space_generation(leaf, header, trans->transid);
1060 btrfs_mark_buffer_dirty(leaf);
1061 btrfs_release_path(path);
1062
1063 return 0;
1064
1065fail:
1066 return -1;
1067}
1068
David Sterba6701bdb2019-03-20 13:49:09 +01001069static noinline_for_stack int write_pinned_extent_entries(
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001070 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001071 struct btrfs_block_group *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001072 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001073 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001074{
1075 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001076 struct extent_io_tree *unpin = NULL;
1077 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001078
Miao Xie5349d6c2014-06-19 10:42:49 +08001079 if (!block_group)
1080 return 0;
1081
Josef Bacika67509c2011-10-05 15:18:58 -04001082 /*
1083 * We want to add any pinned extents to our free space cache
1084 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001085 *
Li Zefandb804f22012-01-10 16:41:01 +08001086 * We shouldn't have switched the pinned extents yet so this is the
1087 * right one
1088 */
Nikolay Borisovfe119a62020-01-20 16:09:18 +02001089 unpin = &trans->transaction->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001090
David Sterbab3470b52019-10-23 18:48:22 +02001091 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001092
David Sterbab3470b52019-10-23 18:48:22 +02001093 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001094 ret = find_first_extent_bit(unpin, start,
1095 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001096 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001097 if (ret)
1098 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001099
Josef Bacika67509c2011-10-05 15:18:58 -04001100 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001101 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001102 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001103
Li Zefandb804f22012-01-10 16:41:01 +08001104 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001105 extent_end = min(block_group->start + block_group->length,
1106 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001107 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001108
Chris Masond4452bc2014-05-19 20:47:56 -07001109 *entries += 1;
1110 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001111 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001112 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001113
Li Zefandb804f22012-01-10 16:41:01 +08001114 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001115 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001116
Miao Xie5349d6c2014-06-19 10:42:49 +08001117 return 0;
1118}
1119
1120static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001121write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001122{
Geliang Tang7ae16812015-12-18 22:17:00 +08001123 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001124 int ret;
1125
Josef Bacik0cb59c92010-07-02 12:14:14 -04001126 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001127 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001128 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001129 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001130 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001131 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001132 }
1133
Miao Xie5349d6c2014-06-19 10:42:49 +08001134 return 0;
1135}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001136
Miao Xie5349d6c2014-06-19 10:42:49 +08001137static int flush_dirty_cache(struct inode *inode)
1138{
1139 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001140
Josef Bacik0ef8b722013-10-25 16:13:35 -04001141 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001142 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001143 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001144 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001145
Miao Xie5349d6c2014-06-19 10:42:49 +08001146 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001147}
1148
1149static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001150cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001151{
Geliang Tang7ae16812015-12-18 22:17:00 +08001152 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001153
Geliang Tang7ae16812015-12-18 22:17:00 +08001154 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001155 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001156}
1157
1158static void noinline_for_stack
1159cleanup_write_cache_enospc(struct inode *inode,
1160 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001161 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001162{
Chris Masond4452bc2014-05-19 20:47:56 -07001163 io_ctl_drop_pages(io_ctl);
1164 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001165 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001166}
1167
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001168static int __btrfs_wait_cache_io(struct btrfs_root *root,
1169 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001170 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001171 struct btrfs_io_ctl *io_ctl,
1172 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001173{
1174 int ret;
1175 struct inode *inode = io_ctl->inode;
1176
Chris Mason1bbc6212015-04-06 12:46:08 -07001177 if (!inode)
1178 return 0;
1179
Chris Masonc9dc4c62015-04-04 17:14:42 -07001180 /* Flush the dirty pages in the cache file. */
1181 ret = flush_dirty_cache(inode);
1182 if (ret)
1183 goto out;
1184
1185 /* Update the cache item to tell everyone this cache file is valid. */
1186 ret = update_cache_item(trans, root, inode, path, offset,
1187 io_ctl->entries, io_ctl->bitmaps);
1188out:
1189 io_ctl_free(io_ctl);
1190 if (ret) {
1191 invalidate_inode_pages2(inode->i_mapping);
1192 BTRFS_I(inode)->generation = 0;
1193 if (block_group) {
1194#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001195 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001196 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001197 block_group->start);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001198#endif
1199 }
1200 }
1201 btrfs_update_inode(trans, root, inode);
1202
1203 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001204 /* the dirty list is protected by the dirty_bgs_lock */
1205 spin_lock(&trans->transaction->dirty_bgs_lock);
1206
1207 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001208 spin_lock(&block_group->lock);
1209
1210 /*
1211 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001212 * the dirty list while waiting for IO. Otherwise our
1213 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001214 */
1215 if (!ret && list_empty(&block_group->dirty_list))
1216 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1217 else if (ret)
1218 block_group->disk_cache_state = BTRFS_DC_ERROR;
1219
1220 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001221 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001222 io_ctl->inode = NULL;
1223 iput(inode);
1224 }
1225
1226 return ret;
1227
1228}
1229
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001230static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1231 struct btrfs_trans_handle *trans,
1232 struct btrfs_io_ctl *io_ctl,
1233 struct btrfs_path *path)
1234{
1235 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1236}
1237
1238int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001239 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001240 struct btrfs_path *path)
1241{
1242 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1243 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001244 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001245}
1246
Chris Masond4452bc2014-05-19 20:47:56 -07001247/**
1248 * __btrfs_write_out_cache - write out cached info to an inode
1249 * @root - the root the inode belongs to
1250 * @ctl - the free space cache we are going to write out
1251 * @block_group - the block_group for this cache if it belongs to a block_group
1252 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001253 *
1254 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001255 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001256 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001257 */
1258static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1259 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001260 struct btrfs_block_group *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001261 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001262 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001263{
1264 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001265 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001266 int entries = 0;
1267 int bitmaps = 0;
1268 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001269 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001270
1271 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001272 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001273
Chris Masonc9dc4c62015-04-04 17:14:42 -07001274 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001275 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001276 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001277 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001278
Miao Xiee570fd22014-06-19 10:42:50 +08001279 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1280 down_write(&block_group->data_rwsem);
1281 spin_lock(&block_group->lock);
1282 if (block_group->delalloc_bytes) {
1283 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1284 spin_unlock(&block_group->lock);
1285 up_write(&block_group->data_rwsem);
1286 BTRFS_I(inode)->generation = 0;
1287 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001288 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001289 goto out;
1290 }
1291 spin_unlock(&block_group->lock);
1292 }
1293
Chris Masond4452bc2014-05-19 20:47:56 -07001294 /* Lock all pages first so we can lock the extent safely. */
Johannes Thumshirn831fa142020-02-12 00:10:19 +09001295 ret = io_ctl_prepare_pages(io_ctl, 0);
Omar Sandovalb8605452015-02-24 02:47:06 -08001296 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001297 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001298
1299 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001300 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001301
Chris Masonc9dc4c62015-04-04 17:14:42 -07001302 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001303
Filipe Manana55507ce2014-12-01 17:04:09 +00001304 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001305 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001306 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001307 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001308 block_group, &entries, &bitmaps,
1309 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001310 if (ret)
1311 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001312
Miao Xie5349d6c2014-06-19 10:42:49 +08001313 /*
1314 * Some spaces that are freed in the current transaction are pinned,
1315 * they will be added into free space cache after the transaction is
1316 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001317 *
1318 * If this changes while we are working we'll get added back to
1319 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001320 */
Nikolay Borisov6b45f6412020-01-20 16:09:15 +02001321 ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001322 if (ret)
1323 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001324
Filipe Manana55507ce2014-12-01 17:04:09 +00001325 /*
1326 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1327 * locked while doing it because a concurrent trim can be manipulating
1328 * or freeing the bitmap.
1329 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001330 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001331 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001332 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001333 if (ret)
1334 goto out_nospc;
1335
1336 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001337 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001338
1339 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001340 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1341 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001342 if (ret)
1343 goto out_nospc;
1344
Miao Xiee570fd22014-06-19 10:42:50 +08001345 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1346 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001347 /*
1348 * Release the pages and unlock the extent, we will flush
1349 * them out later
1350 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001351 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001352
1353 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001354 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001355
Chris Masonc9dc4c62015-04-04 17:14:42 -07001356 /*
1357 * at this point the pages are under IO and we're happy,
1358 * The caller is responsible for waiting on them and updating the
1359 * the cache and the inode
1360 */
1361 io_ctl->entries = entries;
1362 io_ctl->bitmaps = bitmaps;
1363
1364 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001365 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001366 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001367
Chris Masonc9dc4c62015-04-04 17:14:42 -07001368 return 0;
1369
Josef Bacik2f356122011-06-10 15:31:13 -04001370out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001371 io_ctl->inode = NULL;
1372 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001373 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001374 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001375 BTRFS_I(inode)->generation = 0;
1376 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001377 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001378 if (must_iput)
1379 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001380 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001381
Chris Masona3bdccc2015-04-24 11:00:00 -07001382out_nospc_locked:
1383 cleanup_bitmap_list(&bitmap_list);
1384 spin_unlock(&ctl->tree_lock);
1385 mutex_unlock(&ctl->cache_writeout_mutex);
1386
Josef Bacika67509c2011-10-05 15:18:58 -04001387out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001388 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001389
Josef Bacikb77000e2017-11-15 16:20:52 -05001390out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001391 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1392 up_write(&block_group->data_rwsem);
1393
Josef Bacika67509c2011-10-05 15:18:58 -04001394 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001395}
1396
David Sterbafe041532019-03-20 13:51:56 +01001397int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001398 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +08001399 struct btrfs_path *path)
1400{
David Sterbafe041532019-03-20 13:51:56 +01001401 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001402 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1403 struct inode *inode;
1404 int ret = 0;
1405
Li Zefan0414efa2011-04-20 10:20:14 +08001406 spin_lock(&block_group->lock);
1407 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1408 spin_unlock(&block_group->lock);
1409 return 0;
1410 }
1411 spin_unlock(&block_group->lock);
1412
David Sterba7949f332019-03-20 13:40:19 +01001413 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001414 if (IS_ERR(inode))
1415 return 0;
1416
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001417 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1418 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001419 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001420#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001421 btrfs_err(fs_info,
1422 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001423 block_group->start);
Josef Bacikc09544e2011-08-30 10:19:10 -04001424#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001425 spin_lock(&block_group->lock);
1426 block_group->disk_cache_state = BTRFS_DC_ERROR;
1427 spin_unlock(&block_group->lock);
1428
1429 block_group->io_ctl.inode = NULL;
1430 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001431 }
1432
Chris Masonc9dc4c62015-04-04 17:14:42 -07001433 /*
1434 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1435 * to wait for IO and put the inode
1436 */
1437
Josef Bacik0cb59c92010-07-02 12:14:14 -04001438 return ret;
1439}
1440
Li Zefan34d52cb2011-03-29 13:46:06 +08001441static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001442 u64 offset)
1443{
Josef Bacikb12d6862013-08-26 17:14:08 -04001444 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001445 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001446 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001447}
1448
Li Zefan34d52cb2011-03-29 13:46:06 +08001449static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001450{
Li Zefan34d52cb2011-03-29 13:46:06 +08001451 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001452}
1453
Li Zefan34d52cb2011-03-29 13:46:06 +08001454static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001455 u64 offset)
1456{
1457 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001458 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001459
Li Zefan34d52cb2011-03-29 13:46:06 +08001460 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1461 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001462 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001463 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001464 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001465
1466 return bitmap_start;
1467}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001468
1469static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001470 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001471{
1472 struct rb_node **p = &root->rb_node;
1473 struct rb_node *parent = NULL;
1474 struct btrfs_free_space *info;
1475
1476 while (*p) {
1477 parent = *p;
1478 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1479
Josef Bacik96303082009-07-13 21:29:25 -04001480 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001481 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001482 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001483 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001484 } else {
1485 /*
1486 * we could have a bitmap entry and an extent entry
1487 * share the same offset. If this is the case, we want
1488 * the extent entry to always be found first if we do a
1489 * linear search through the tree, since we want to have
1490 * the quickest allocation time, and allocating from an
1491 * extent is faster than allocating from a bitmap. So
1492 * if we're inserting a bitmap and we find an entry at
1493 * this offset, we want to go right, or after this entry
1494 * logically. If we are inserting an extent and we've
1495 * found a bitmap, we want to go left, or before
1496 * logically.
1497 */
1498 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001499 if (info->bitmap) {
1500 WARN_ON_ONCE(1);
1501 return -EEXIST;
1502 }
Josef Bacik96303082009-07-13 21:29:25 -04001503 p = &(*p)->rb_right;
1504 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001505 if (!info->bitmap) {
1506 WARN_ON_ONCE(1);
1507 return -EEXIST;
1508 }
Josef Bacik96303082009-07-13 21:29:25 -04001509 p = &(*p)->rb_left;
1510 }
1511 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001512 }
1513
1514 rb_link_node(node, parent, p);
1515 rb_insert_color(node, root);
1516
1517 return 0;
1518}
1519
1520/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001521 * searches the tree for the given offset.
1522 *
Josef Bacik96303082009-07-13 21:29:25 -04001523 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1524 * want a section that has at least bytes size and comes at or after the given
1525 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001526 */
Josef Bacik96303082009-07-13 21:29:25 -04001527static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001528tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001529 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001530{
Li Zefan34d52cb2011-03-29 13:46:06 +08001531 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001532 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001533
Josef Bacik96303082009-07-13 21:29:25 -04001534 /* find entry that is closest to the 'offset' */
1535 while (1) {
1536 if (!n) {
1537 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001538 break;
1539 }
Josef Bacik96303082009-07-13 21:29:25 -04001540
1541 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1542 prev = entry;
1543
1544 if (offset < entry->offset)
1545 n = n->rb_left;
1546 else if (offset > entry->offset)
1547 n = n->rb_right;
1548 else
1549 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001550 }
1551
Josef Bacik96303082009-07-13 21:29:25 -04001552 if (bitmap_only) {
1553 if (!entry)
1554 return NULL;
1555 if (entry->bitmap)
1556 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001557
Josef Bacik96303082009-07-13 21:29:25 -04001558 /*
1559 * bitmap entry and extent entry may share same offset,
1560 * in that case, bitmap entry comes after extent entry.
1561 */
1562 n = rb_next(n);
1563 if (!n)
1564 return NULL;
1565 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1566 if (entry->offset != offset)
1567 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001568
Josef Bacik96303082009-07-13 21:29:25 -04001569 WARN_ON(!entry->bitmap);
1570 return entry;
1571 } else if (entry) {
1572 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001573 /*
Josef Bacik96303082009-07-13 21:29:25 -04001574 * if previous extent entry covers the offset,
1575 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001576 */
Miao Xiede6c4112012-10-18 08:18:01 +00001577 n = rb_prev(&entry->offset_index);
1578 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001579 prev = rb_entry(n, struct btrfs_free_space,
1580 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001581 if (!prev->bitmap &&
1582 prev->offset + prev->bytes > offset)
1583 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001584 }
Josef Bacik96303082009-07-13 21:29:25 -04001585 }
1586 return entry;
1587 }
1588
1589 if (!prev)
1590 return NULL;
1591
1592 /* find last entry before the 'offset' */
1593 entry = prev;
1594 if (entry->offset > offset) {
1595 n = rb_prev(&entry->offset_index);
1596 if (n) {
1597 entry = rb_entry(n, struct btrfs_free_space,
1598 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001599 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001600 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001601 if (fuzzy)
1602 return entry;
1603 else
1604 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001605 }
1606 }
1607
Josef Bacik96303082009-07-13 21:29:25 -04001608 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001609 n = rb_prev(&entry->offset_index);
1610 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001611 prev = rb_entry(n, struct btrfs_free_space,
1612 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001613 if (!prev->bitmap &&
1614 prev->offset + prev->bytes > offset)
1615 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001616 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001617 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001618 return entry;
1619 } else if (entry->offset + entry->bytes > offset)
1620 return entry;
1621
1622 if (!fuzzy)
1623 return NULL;
1624
1625 while (1) {
1626 if (entry->bitmap) {
1627 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001628 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001629 break;
1630 } else {
1631 if (entry->offset + entry->bytes > offset)
1632 break;
1633 }
1634
1635 n = rb_next(&entry->offset_index);
1636 if (!n)
1637 return NULL;
1638 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1639 }
1640 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001641}
1642
Li Zefanf333adb2010-11-09 14:57:39 +08001643static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001644__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001645 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001646{
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 rb_erase(&info->offset_index, &ctl->free_space_offset);
1648 ctl->free_extents--;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001649
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001650 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001651 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001652 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
1653 }
Li Zefanf333adb2010-11-09 14:57:39 +08001654}
1655
Li Zefan34d52cb2011-03-29 13:46:06 +08001656static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001657 struct btrfs_free_space *info)
1658{
Li Zefan34d52cb2011-03-29 13:46:06 +08001659 __unlink_free_space(ctl, info);
1660 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001661}
1662
Li Zefan34d52cb2011-03-29 13:46:06 +08001663static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001664 struct btrfs_free_space *info)
1665{
1666 int ret = 0;
1667
Josef Bacikb12d6862013-08-26 17:14:08 -04001668 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001669 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001670 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001671 if (ret)
1672 return ret;
1673
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001674 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001675 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001676 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
1677 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001678
Li Zefan34d52cb2011-03-29 13:46:06 +08001679 ctl->free_space += info->bytes;
1680 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001681 return ret;
1682}
1683
Li Zefan34d52cb2011-03-29 13:46:06 +08001684static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001685{
David Sterba32da53862019-10-29 19:20:18 +01001686 struct btrfs_block_group *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001687 u64 max_bytes;
1688 u64 bitmap_bytes;
1689 u64 extent_bytes;
David Sterbab3470b52019-10-23 18:48:22 +02001690 u64 size = block_group->length;
Feifei Xu0ef64472016-06-01 19:18:24 +08001691 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1692 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001693
Feifei Xu0ef64472016-06-01 19:18:24 +08001694 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001695
Josef Bacikb12d6862013-08-26 17:14:08 -04001696 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001697
1698 /*
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05001699 * We are trying to keep the total amount of memory used per 1GiB of
1700 * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
1701 * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
1702 * bitmaps, we may end up using more memory than this.
Josef Bacik96303082009-07-13 21:29:25 -04001703 */
Byongho Leeee221842015-12-15 01:42:10 +09001704 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001705 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1706 else
Byongho Leeee221842015-12-15 01:42:10 +09001707 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001708
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05001709 bitmap_bytes = ctl->total_bitmaps * ctl->unit;
Josef Bacik25891f72009-09-11 16:11:20 -04001710
1711 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001712 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001713 * bytes we can have, or whatever is less than that.
1714 */
1715 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001716 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001717
Li Zefan34d52cb2011-03-29 13:46:06 +08001718 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001719 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001720}
1721
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001722static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1723 struct btrfs_free_space *info,
1724 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001725{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001726 unsigned long start, count, end;
1727 int extent_delta = -1;
Josef Bacik96303082009-07-13 21:29:25 -04001728
Li Zefan34d52cb2011-03-29 13:46:06 +08001729 start = offset_to_bit(info->offset, ctl->unit, offset);
1730 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001731 end = start + count;
1732 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001733
Li Zefanf38b6e72011-03-14 13:40:51 +08001734 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001735
1736 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001737 if (info->max_extent_size > ctl->unit)
1738 info->max_extent_size = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001739
1740 if (start && test_bit(start - 1, info->bitmap))
1741 extent_delta++;
1742
1743 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1744 extent_delta++;
1745
1746 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001747 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001748 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001749 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
1750 }
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001751}
1752
1753static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1754 struct btrfs_free_space *info, u64 offset,
1755 u64 bytes)
1756{
1757 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001758 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001759}
1760
Li Zefan34d52cb2011-03-29 13:46:06 +08001761static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001762 struct btrfs_free_space *info, u64 offset,
1763 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001764{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001765 unsigned long start, count, end;
1766 int extent_delta = 1;
Josef Bacik96303082009-07-13 21:29:25 -04001767
Li Zefan34d52cb2011-03-29 13:46:06 +08001768 start = offset_to_bit(info->offset, ctl->unit, offset);
1769 count = bytes_to_bits(bytes, ctl->unit);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001770 end = start + count;
1771 ASSERT(end <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001772
Li Zefanf38b6e72011-03-14 13:40:51 +08001773 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001774
1775 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001776 ctl->free_space += bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001777
1778 if (start && test_bit(start - 1, info->bitmap))
1779 extent_delta--;
1780
1781 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1782 extent_delta--;
1783
1784 info->bitmap_extents += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001785 if (!btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001786 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08001787 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
1788 }
Josef Bacik96303082009-07-13 21:29:25 -04001789}
1790
Miao Xiea4820392013-09-09 13:19:42 +08001791/*
1792 * If we can not find suitable extent, we will use bytes to record
1793 * the size of the max extent.
1794 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001795static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001796 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001797 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001798{
1799 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001800 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001801 unsigned long bits, i;
1802 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001803 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001804
Josef Bacikcef40482015-10-02 16:09:42 -04001805 /*
1806 * Skip searching the bitmap if we don't have a contiguous section that
1807 * is large enough for this allocation.
1808 */
Josef Bacik0584f712015-10-02 16:12:23 -04001809 if (for_alloc &&
1810 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001811 bitmap_info->max_extent_size < *bytes) {
1812 *bytes = bitmap_info->max_extent_size;
1813 return -1;
1814 }
1815
Li Zefan34d52cb2011-03-29 13:46:06 +08001816 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001817 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001818 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001819
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001820 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001821 if (for_alloc && bits == 1) {
1822 found_bits = 1;
1823 break;
1824 }
Josef Bacik96303082009-07-13 21:29:25 -04001825 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1826 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001827 extent_bits = next_zero - i;
1828 if (extent_bits >= bits) {
1829 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001830 break;
Miao Xiea4820392013-09-09 13:19:42 +08001831 } else if (extent_bits > max_bits) {
1832 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001833 }
1834 i = next_zero;
1835 }
1836
1837 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001838 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1839 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001840 return 0;
1841 }
1842
Miao Xiea4820392013-09-09 13:19:42 +08001843 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001844 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001845 return -1;
1846}
1847
Josef Bacikad22cf62018-10-12 15:32:33 -04001848static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1849{
1850 if (entry->bitmap)
1851 return entry->max_extent_size;
1852 return entry->bytes;
1853}
1854
Miao Xiea4820392013-09-09 13:19:42 +08001855/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001856static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001857find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001858 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001859{
1860 struct btrfs_free_space *entry;
1861 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001862 u64 tmp;
1863 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001864 int ret;
1865
Li Zefan34d52cb2011-03-29 13:46:06 +08001866 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001867 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001868
Li Zefan34d52cb2011-03-29 13:46:06 +08001869 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001870 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001871 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001872
1873 for (node = &entry->offset_index; node; node = rb_next(node)) {
1874 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001875 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001876 *max_extent_size = max(get_max_extent_size(entry),
1877 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001878 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001879 }
Josef Bacik96303082009-07-13 21:29:25 -04001880
David Woodhouse53b381b2013-01-29 18:40:14 -05001881 /* make sure the space returned is big enough
1882 * to match our requested alignment
1883 */
1884 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001885 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001886 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001887 tmp = tmp * align + ctl->start;
1888 align_off = tmp - entry->offset;
1889 } else {
1890 align_off = 0;
1891 tmp = entry->offset;
1892 }
1893
Miao Xiea4820392013-09-09 13:19:42 +08001894 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001895 *max_extent_size = max(get_max_extent_size(entry),
1896 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001897 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001898 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001899
Josef Bacik96303082009-07-13 21:29:25 -04001900 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001901 u64 size = *bytes;
1902
Josef Bacik0584f712015-10-02 16:12:23 -04001903 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001904 if (!ret) {
1905 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001906 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001907 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001908 } else {
1909 *max_extent_size =
1910 max(get_max_extent_size(entry),
1911 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001912 }
Josef Bacik96303082009-07-13 21:29:25 -04001913 continue;
1914 }
1915
David Woodhouse53b381b2013-01-29 18:40:14 -05001916 *offset = tmp;
1917 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001918 return entry;
1919 }
Miao Xiea4820392013-09-09 13:19:42 +08001920out:
Josef Bacik96303082009-07-13 21:29:25 -04001921 return NULL;
1922}
1923
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001924static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl,
1925 struct btrfs_free_space *bitmap_info)
1926{
1927 struct btrfs_block_group *block_group = ctl->private;
1928 u64 bytes = bitmap_info->bytes;
1929 unsigned int rs, re;
1930 int count = 0;
1931
1932 if (!block_group || !bytes)
1933 return count;
1934
1935 bitmap_for_each_set_region(bitmap_info->bitmap, rs, re, 0,
1936 BITS_PER_BITMAP) {
1937 bytes -= (rs - re) * ctl->unit;
1938 count++;
1939
1940 if (!bytes)
1941 break;
1942 }
1943
1944 return count;
1945}
1946
Li Zefan34d52cb2011-03-29 13:46:06 +08001947static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001948 struct btrfs_free_space *info, u64 offset)
1949{
Li Zefan34d52cb2011-03-29 13:46:06 +08001950 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001951 info->bytes = 0;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08001952 info->bitmap_extents = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001953 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001954 link_free_space(ctl, info);
1955 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001956
Li Zefan34d52cb2011-03-29 13:46:06 +08001957 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001958}
1959
Li Zefan34d52cb2011-03-29 13:46:06 +08001960static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001961 struct btrfs_free_space *bitmap_info)
1962{
Dennis Zhou27f0afc2020-01-02 16:26:45 -05001963 /*
1964 * Normally when this is called, the bitmap is completely empty. However,
1965 * if we are blowing up the free space cache for one reason or another
1966 * via __btrfs_remove_free_space_cache(), then it may not be freed and
1967 * we may leave stats on the table.
1968 */
1969 if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
1970 ctl->discardable_extents[BTRFS_STAT_CURR] -=
1971 bitmap_info->bitmap_extents;
1972 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
1973
1974 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001975 unlink_free_space(ctl, bitmap_info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00001976 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001977 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001978 ctl->total_bitmaps--;
1979 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001980}
1981
Li Zefan34d52cb2011-03-29 13:46:06 +08001982static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001983 struct btrfs_free_space *bitmap_info,
1984 u64 *offset, u64 *bytes)
1985{
1986 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001987 u64 search_start, search_bytes;
1988 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001989
1990again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001991 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001992
Josef Bacik6606bb92009-07-31 11:03:58 -04001993 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001994 * We need to search for bits in this bitmap. We could only cover some
1995 * of the extent in this bitmap thanks to how we add space, so we need
1996 * to search for as much as it as we can and clear that amount, and then
1997 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001998 */
1999 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002000 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00002001 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04002002 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
2003 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002004 if (ret < 0 || search_start != *offset)
2005 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04002006
Josef Bacikbdb7d302012-06-27 15:10:56 -04002007 /* We may have found more bits than what we need */
2008 search_bytes = min(search_bytes, *bytes);
2009
2010 /* Cannot clear past the end of the bitmap */
2011 search_bytes = min(search_bytes, end - search_start + 1);
2012
2013 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
2014 *offset += search_bytes;
2015 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04002016
2017 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002018 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002019 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002020 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002021
Josef Bacik6606bb92009-07-31 11:03:58 -04002022 /*
2023 * no entry after this bitmap, but we still have bytes to
2024 * remove, so something has gone wrong.
2025 */
2026 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04002027 return -EINVAL;
2028
Josef Bacik6606bb92009-07-31 11:03:58 -04002029 bitmap_info = rb_entry(next, struct btrfs_free_space,
2030 offset_index);
2031
2032 /*
2033 * if the next entry isn't a bitmap we need to return to let the
2034 * extent stuff do its work.
2035 */
Josef Bacik96303082009-07-13 21:29:25 -04002036 if (!bitmap_info->bitmap)
2037 return -EAGAIN;
2038
Josef Bacik6606bb92009-07-31 11:03:58 -04002039 /*
2040 * Ok the next item is a bitmap, but it may not actually hold
2041 * the information for the rest of this free space stuff, so
2042 * look for it, and if we don't find it return so we can try
2043 * everything over again.
2044 */
2045 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002046 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08002047 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04002048 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04002049 if (ret < 0 || search_start != *offset)
2050 return -EAGAIN;
2051
Josef Bacik96303082009-07-13 21:29:25 -04002052 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08002053 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002054 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04002055
2056 return 0;
2057}
2058
Josef Bacik2cdc3422011-05-27 14:07:49 -04002059static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
2060 struct btrfs_free_space *info, u64 offset,
Dennis Zhouda080fe2019-12-13 16:22:13 -08002061 u64 bytes, enum btrfs_trim_state trim_state)
Josef Bacik2cdc3422011-05-27 14:07:49 -04002062{
2063 u64 bytes_to_set = 0;
2064 u64 end;
2065
Dennis Zhouda080fe2019-12-13 16:22:13 -08002066 /*
2067 * This is a tradeoff to make bitmap trim state minimal. We mark the
2068 * whole bitmap untrimmed if at any point we add untrimmed regions.
2069 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002070 if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002071 if (btrfs_free_space_trimmed(info)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002072 ctl->discardable_extents[BTRFS_STAT_CURR] +=
2073 info->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002074 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
2075 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002076 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002077 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08002078
Josef Bacik2cdc3422011-05-27 14:07:49 -04002079 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
2080
2081 bytes_to_set = min(end - offset, bytes);
2082
2083 bitmap_set_bits(ctl, info, offset, bytes_to_set);
2084
Josef Bacikcef40482015-10-02 16:09:42 -04002085 /*
2086 * We set some bytes, we have no idea what the max extent size is
2087 * anymore.
2088 */
2089 info->max_extent_size = 0;
2090
Josef Bacik2cdc3422011-05-27 14:07:49 -04002091 return bytes_to_set;
2092
2093}
2094
Li Zefan34d52cb2011-03-29 13:46:06 +08002095static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2096 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04002097{
David Sterba32da53862019-10-29 19:20:18 +01002098 struct btrfs_block_group *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002099 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002100 bool forced = false;
2101
2102#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002103 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002104 forced = true;
2105#endif
Josef Bacik96303082009-07-13 21:29:25 -04002106
Dennis Zhou5d90c5c2020-01-02 16:26:43 -05002107 /* This is a way to reclaim large regions from the bitmaps. */
2108 if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
2109 return false;
2110
Josef Bacik96303082009-07-13 21:29:25 -04002111 /*
2112 * If we are below the extents threshold then we can add this as an
2113 * extent, and don't have to deal with the bitmap
2114 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002115 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002116 /*
2117 * If this block group has some small extents we don't want to
2118 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002119 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002120 * of cache left then go ahead an dadd them, no sense in adding
2121 * the overhead of a bitmap if we don't have to.
2122 */
Dennis Zhouf9bb6152020-01-02 16:26:44 -05002123 if (info->bytes <= fs_info->sectorsize * 8) {
2124 if (ctl->free_extents * 3 <= ctl->extents_thresh)
Li Zefan34d52cb2011-03-29 13:46:06 +08002125 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002126 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002127 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002128 }
2129 }
Josef Bacik96303082009-07-13 21:29:25 -04002130
2131 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002132 * The original block groups from mkfs can be really small, like 8
2133 * megabytes, so don't bother with a bitmap for those entries. However
2134 * some block groups can be smaller than what a bitmap would cover but
2135 * are still large enough that they could overflow the 32k memory limit,
2136 * so allow those block groups to still be allowed to have a bitmap
2137 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002138 */
David Sterbab3470b52019-10-23 18:48:22 +02002139 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002140 return false;
2141
2142 return true;
2143}
2144
David Sterba20e55062015-11-19 11:42:28 +01002145static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002146 .recalc_thresholds = recalculate_thresholds,
2147 .use_bitmap = use_bitmap,
2148};
2149
Li Zefan34d52cb2011-03-29 13:46:06 +08002150static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2151 struct btrfs_free_space *info)
2152{
2153 struct btrfs_free_space *bitmap_info;
David Sterba32da53862019-10-29 19:20:18 +01002154 struct btrfs_block_group *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002155 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002156 u64 bytes, offset, bytes_added;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002157 enum btrfs_trim_state trim_state;
Li Zefan34d52cb2011-03-29 13:46:06 +08002158 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002159
2160 bytes = info->bytes;
2161 offset = info->offset;
Dennis Zhouda080fe2019-12-13 16:22:13 -08002162 trim_state = info->trim_state;
Josef Bacik96303082009-07-13 21:29:25 -04002163
Li Zefan34d52cb2011-03-29 13:46:06 +08002164 if (!ctl->op->use_bitmap(ctl, info))
2165 return 0;
2166
Josef Bacik2cdc3422011-05-27 14:07:49 -04002167 if (ctl->op == &free_space_op)
2168 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002169again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002170 /*
2171 * Since we link bitmaps right into the cluster we need to see if we
2172 * have a cluster here, and if so and it has our bitmap we need to add
2173 * the free space to that bitmap.
2174 */
2175 if (block_group && !list_empty(&block_group->cluster_list)) {
2176 struct btrfs_free_cluster *cluster;
2177 struct rb_node *node;
2178 struct btrfs_free_space *entry;
2179
2180 cluster = list_entry(block_group->cluster_list.next,
2181 struct btrfs_free_cluster,
2182 block_group_list);
2183 spin_lock(&cluster->lock);
2184 node = rb_first(&cluster->root);
2185 if (!node) {
2186 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002187 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002188 }
2189
2190 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2191 if (!entry->bitmap) {
2192 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002193 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002194 }
2195
2196 if (entry->offset == offset_to_bitmap(ctl, offset)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08002197 bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
2198 bytes, trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002199 bytes -= bytes_added;
2200 offset += bytes_added;
2201 }
2202 spin_unlock(&cluster->lock);
2203 if (!bytes) {
2204 ret = 1;
2205 goto out;
2206 }
2207 }
Chris Mason38e87882011-06-10 16:36:57 -04002208
2209no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002210 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002211 1, 0);
2212 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002213 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002214 goto new_bitmap;
2215 }
2216
Dennis Zhouda080fe2019-12-13 16:22:13 -08002217 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
2218 trim_state);
Josef Bacik2cdc3422011-05-27 14:07:49 -04002219 bytes -= bytes_added;
2220 offset += bytes_added;
2221 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002222
2223 if (!bytes) {
2224 ret = 1;
2225 goto out;
2226 } else
2227 goto again;
2228
2229new_bitmap:
2230 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002231 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002232 added = 1;
2233 info = NULL;
2234 goto again;
2235 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002236 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002237
2238 /* no pre-allocated info, allocate a new one */
2239 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002240 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2241 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002242 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002243 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002244 ret = -ENOMEM;
2245 goto out;
2246 }
2247 }
2248
2249 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002250 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2251 GFP_NOFS);
Dennis Zhouda080fe2019-12-13 16:22:13 -08002252 info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Li Zefan34d52cb2011-03-29 13:46:06 +08002253 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002254 if (!info->bitmap) {
2255 ret = -ENOMEM;
2256 goto out;
2257 }
2258 goto again;
2259 }
2260
2261out:
2262 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002263 if (info->bitmap)
2264 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2265 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002266 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002267 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002268
2269 return ret;
2270}
2271
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002272/*
2273 * Free space merging rules:
2274 * 1) Merge trimmed areas together
2275 * 2) Let untrimmed areas coalesce with trimmed areas
2276 * 3) Always pull neighboring regions from bitmaps
2277 *
2278 * The above rules are for when we merge free space based on btrfs_trim_state.
2279 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2280 * same reason: to promote larger extent regions which makes life easier for
2281 * find_free_extent(). Rule 2 enables coalescing based on the common path
2282 * being returning free space from btrfs_finish_extent_commit(). So when free
2283 * space is trimmed, it will prevent aggregating trimmed new region and
2284 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2285 * and provide find_free_extent() with the largest extents possible hoping for
2286 * the reuse path.
2287 */
Chris Mason945d8962011-05-22 12:33:42 -04002288static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002289 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002290{
Li Zefan120d66e2010-11-09 14:56:50 +08002291 struct btrfs_free_space *left_info;
2292 struct btrfs_free_space *right_info;
2293 bool merged = false;
2294 u64 offset = info->offset;
2295 u64 bytes = info->bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002296 const bool is_trimmed = btrfs_free_space_trimmed(info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002297
Josef Bacik0f9dd462008-09-23 13:14:11 -04002298 /*
2299 * first we want to see if there is free space adjacent to the range we
2300 * are adding, if there is remove that struct and add a new one to
2301 * cover the entire range
2302 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002303 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002304 if (right_info && rb_prev(&right_info->offset_index))
2305 left_info = rb_entry(rb_prev(&right_info->offset_index),
2306 struct btrfs_free_space, offset_index);
2307 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002308 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002309
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002310 /* See try_merge_free_space() comment. */
2311 if (right_info && !right_info->bitmap &&
2312 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
Li Zefanf333adb2010-11-09 14:57:39 +08002313 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002314 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002315 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002316 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002317 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002318 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002319 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002320 }
2321
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002322 /* See try_merge_free_space() comment. */
Josef Bacik96303082009-07-13 21:29:25 -04002323 if (left_info && !left_info->bitmap &&
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002324 left_info->offset + left_info->bytes == offset &&
2325 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
Li Zefanf333adb2010-11-09 14:57:39 +08002326 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002327 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002328 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002329 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002330 info->offset = left_info->offset;
2331 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002332 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002333 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002334 }
2335
Li Zefan120d66e2010-11-09 14:56:50 +08002336 return merged;
2337}
2338
Filipe Manana20005522014-08-29 13:35:13 +01002339static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2340 struct btrfs_free_space *info,
2341 bool update_stat)
2342{
2343 struct btrfs_free_space *bitmap;
2344 unsigned long i;
2345 unsigned long j;
2346 const u64 end = info->offset + info->bytes;
2347 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2348 u64 bytes;
2349
2350 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2351 if (!bitmap)
2352 return false;
2353
2354 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2355 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2356 if (j == i)
2357 return false;
2358 bytes = (j - i) * ctl->unit;
2359 info->bytes += bytes;
2360
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002361 /* See try_merge_free_space() comment. */
2362 if (!btrfs_free_space_trimmed(bitmap))
2363 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2364
Filipe Manana20005522014-08-29 13:35:13 +01002365 if (update_stat)
2366 bitmap_clear_bits(ctl, bitmap, end, bytes);
2367 else
2368 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2369
2370 if (!bitmap->bytes)
2371 free_bitmap(ctl, bitmap);
2372
2373 return true;
2374}
2375
2376static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2377 struct btrfs_free_space *info,
2378 bool update_stat)
2379{
2380 struct btrfs_free_space *bitmap;
2381 u64 bitmap_offset;
2382 unsigned long i;
2383 unsigned long j;
2384 unsigned long prev_j;
2385 u64 bytes;
2386
2387 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2388 /* If we're on a boundary, try the previous logical bitmap. */
2389 if (bitmap_offset == info->offset) {
2390 if (info->offset == 0)
2391 return false;
2392 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2393 }
2394
2395 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2396 if (!bitmap)
2397 return false;
2398
2399 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2400 j = 0;
2401 prev_j = (unsigned long)-1;
2402 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2403 if (j > i)
2404 break;
2405 prev_j = j;
2406 }
2407 if (prev_j == i)
2408 return false;
2409
2410 if (prev_j == (unsigned long)-1)
2411 bytes = (i + 1) * ctl->unit;
2412 else
2413 bytes = (i - prev_j) * ctl->unit;
2414
2415 info->offset -= bytes;
2416 info->bytes += bytes;
2417
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002418 /* See try_merge_free_space() comment. */
2419 if (!btrfs_free_space_trimmed(bitmap))
2420 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2421
Filipe Manana20005522014-08-29 13:35:13 +01002422 if (update_stat)
2423 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2424 else
2425 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2426
2427 if (!bitmap->bytes)
2428 free_bitmap(ctl, bitmap);
2429
2430 return true;
2431}
2432
2433/*
2434 * We prefer always to allocate from extent entries, both for clustered and
2435 * non-clustered allocation requests. So when attempting to add a new extent
2436 * entry, try to see if there's adjacent free space in bitmap entries, and if
2437 * there is, migrate that space from the bitmaps to the extent.
2438 * Like this we get better chances of satisfying space allocation requests
2439 * because we attempt to satisfy them based on a single cache entry, and never
2440 * on 2 or more entries - even if the entries represent a contiguous free space
2441 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2442 * ends).
2443 */
2444static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2445 struct btrfs_free_space *info,
2446 bool update_stat)
2447{
2448 /*
2449 * Only work with disconnected entries, as we can change their offset,
2450 * and must be extent entries.
2451 */
2452 ASSERT(!info->bitmap);
2453 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2454
2455 if (ctl->total_bitmaps > 0) {
2456 bool stole_end;
2457 bool stole_front = false;
2458
2459 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2460 if (ctl->total_bitmaps > 0)
2461 stole_front = steal_from_bitmap_to_front(ctl, info,
2462 update_stat);
2463
2464 if (stole_end || stole_front)
2465 try_merge_free_space(ctl, info, update_stat);
2466 }
2467}
2468
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002469int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2470 struct btrfs_free_space_ctl *ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002471 u64 offset, u64 bytes,
2472 enum btrfs_trim_state trim_state)
Li Zefan120d66e2010-11-09 14:56:50 +08002473{
Dennis Zhoub0643e52019-12-13 16:22:14 -08002474 struct btrfs_block_group *block_group = ctl->private;
Li Zefan120d66e2010-11-09 14:56:50 +08002475 struct btrfs_free_space *info;
2476 int ret = 0;
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002477 u64 filter_bytes = bytes;
Li Zefan120d66e2010-11-09 14:56:50 +08002478
Josef Bacikdc89e982011-01-28 17:05:48 -05002479 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002480 if (!info)
2481 return -ENOMEM;
2482
2483 info->offset = offset;
2484 info->bytes = bytes;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002485 info->trim_state = trim_state;
Filipe Manana20005522014-08-29 13:35:13 +01002486 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002487
Li Zefan34d52cb2011-03-29 13:46:06 +08002488 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002489
Li Zefan34d52cb2011-03-29 13:46:06 +08002490 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002491 goto link;
2492
2493 /*
2494 * There was no extent directly to the left or right of this new
2495 * extent then we know we're going to have to allocate a new extent, so
2496 * before we do that see if we need to drop this into a bitmap
2497 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002498 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002499 if (ret < 0) {
2500 goto out;
2501 } else if (ret) {
2502 ret = 0;
2503 goto out;
2504 }
2505link:
Filipe Manana20005522014-08-29 13:35:13 +01002506 /*
2507 * Only steal free space from adjacent bitmaps if we're sure we're not
2508 * going to add the new free space to existing bitmap entries - because
2509 * that would mean unnecessary work that would be reverted. Therefore
2510 * attempt to steal space from bitmaps if we're adding an extent entry.
2511 */
2512 steal_from_bitmap(ctl, info, true);
2513
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002514 filter_bytes = max(filter_bytes, info->bytes);
2515
Li Zefan34d52cb2011-03-29 13:46:06 +08002516 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002517 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002518 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002519out:
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002520 btrfs_discard_update_discardable(block_group, ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002521 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002522
Josef Bacik0f9dd462008-09-23 13:14:11 -04002523 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002524 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002525 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002526 }
2527
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002528 if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
2529 btrfs_discard_check_filter(block_group, filter_bytes);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002530 btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05002531 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002532
Josef Bacik0f9dd462008-09-23 13:14:11 -04002533 return ret;
2534}
2535
David Sterba32da53862019-10-29 19:20:18 +01002536int btrfs_add_free_space(struct btrfs_block_group *block_group,
Josef Bacik478b4d92019-06-20 15:37:43 -04002537 u64 bytenr, u64 size)
2538{
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002539 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2540
2541 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2542 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2543
Josef Bacik478b4d92019-06-20 15:37:43 -04002544 return __btrfs_add_free_space(block_group->fs_info,
2545 block_group->free_space_ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002546 bytenr, size, trim_state);
Josef Bacik478b4d92019-06-20 15:37:43 -04002547}
2548
Dennis Zhoub0643e52019-12-13 16:22:14 -08002549/*
2550 * This is a subtle distinction because when adding free space back in general,
2551 * we want it to be added as untrimmed for async. But in the case where we add
2552 * it on loading of a block group, we want to consider it trimmed.
2553 */
2554int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
2555 u64 bytenr, u64 size)
2556{
2557 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2558
2559 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
2560 btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
2561 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2562
2563 return __btrfs_add_free_space(block_group->fs_info,
2564 block_group->free_space_ctl,
2565 bytenr, size, trim_state);
2566}
2567
David Sterba32da53862019-10-29 19:20:18 +01002568int btrfs_remove_free_space(struct btrfs_block_group *block_group,
Josef Bacik6226cb02009-04-03 10:14:18 -04002569 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002570{
Li Zefan34d52cb2011-03-29 13:46:06 +08002571 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002572 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002573 int ret;
2574 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002575
Li Zefan34d52cb2011-03-29 13:46:06 +08002576 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002577
Josef Bacik96303082009-07-13 21:29:25 -04002578again:
Josef Bacikb0175112012-12-18 11:39:19 -05002579 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002580 if (!bytes)
2581 goto out_lock;
2582
Li Zefan34d52cb2011-03-29 13:46:06 +08002583 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002584 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002585 /*
2586 * oops didn't find an extent that matched the space we wanted
2587 * to remove, look for a bitmap instead
2588 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002589 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002590 1, 0);
2591 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002592 /*
2593 * If we found a partial bit of our free space in a
2594 * bitmap but then couldn't find the other part this may
2595 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002596 */
Josef Bacikb0175112012-12-18 11:39:19 -05002597 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002598 goto out_lock;
2599 }
Josef Bacik96303082009-07-13 21:29:25 -04002600 }
2601
Josef Bacikb0175112012-12-18 11:39:19 -05002602 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002603 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002604 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002605 if (offset == info->offset) {
2606 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002607
Josef Bacikbdb7d302012-06-27 15:10:56 -04002608 info->bytes -= to_free;
2609 info->offset += to_free;
2610 if (info->bytes) {
2611 ret = link_free_space(ctl, info);
2612 WARN_ON(ret);
2613 } else {
2614 kmem_cache_free(btrfs_free_space_cachep, info);
2615 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002616
Josef Bacikbdb7d302012-06-27 15:10:56 -04002617 offset += to_free;
2618 bytes -= to_free;
2619 goto again;
2620 } else {
2621 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002622
Josef Bacikbdb7d302012-06-27 15:10:56 -04002623 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002624 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002625 WARN_ON(ret);
2626 if (ret)
2627 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002628
Josef Bacikbdb7d302012-06-27 15:10:56 -04002629 /* Not enough bytes in this entry to satisfy us */
2630 if (old_end < offset + bytes) {
2631 bytes -= old_end - offset;
2632 offset = old_end;
2633 goto again;
2634 } else if (old_end == offset + bytes) {
2635 /* all done */
2636 goto out_lock;
2637 }
2638 spin_unlock(&ctl->tree_lock);
2639
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002640 ret = __btrfs_add_free_space(block_group->fs_info, ctl,
2641 offset + bytes,
2642 old_end - (offset + bytes),
2643 info->trim_state);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002644 WARN_ON(ret);
2645 goto out;
2646 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002647 }
Josef Bacik96303082009-07-13 21:29:25 -04002648
Li Zefan34d52cb2011-03-29 13:46:06 +08002649 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002650 if (ret == -EAGAIN) {
2651 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002652 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002653 }
Josef Bacik96303082009-07-13 21:29:25 -04002654out_lock:
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002655 btrfs_discard_update_discardable(block_group, ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002656 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002657out:
Josef Bacik25179202008-10-29 14:49:05 -04002658 return ret;
2659}
2660
David Sterba32da53862019-10-29 19:20:18 +01002661void btrfs_dump_free_space(struct btrfs_block_group *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -04002662 u64 bytes)
2663{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002664 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002665 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002666 struct btrfs_free_space *info;
2667 struct rb_node *n;
2668 int count = 0;
2669
Filipe Manana9084cb62018-10-22 10:43:06 +01002670 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002671 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002672 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002673 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002674 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002676 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002677 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002678 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002679 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002680 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002681 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002682 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002683 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002684}
2685
David Sterba32da53862019-10-29 19:20:18 +01002686void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002687{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002688 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002689 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002690
Li Zefan34d52cb2011-03-29 13:46:06 +08002691 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002692 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002693 ctl->start = block_group->start;
Li Zefan34d52cb2011-03-29 13:46:06 +08002694 ctl->private = block_group;
2695 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002696 INIT_LIST_HEAD(&ctl->trimming_ranges);
2697 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002698
Li Zefan34d52cb2011-03-29 13:46:06 +08002699 /*
2700 * we only want to have 32k of ram per block group for keeping
2701 * track of free space, and if we pass 1/2 of that we want to
2702 * start converting things over to using bitmaps
2703 */
Byongho Leeee221842015-12-15 01:42:10 +09002704 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002705}
2706
Chris Masonfa9c0d792009-04-03 09:47:43 -04002707/*
2708 * for a given cluster, put all of its extents back into the free
2709 * space cache. If the block group passed doesn't match the block group
2710 * pointed to by the cluster, someone else raced in and freed the
2711 * cluster already. In that case, we just return without changing anything
2712 */
2713static int
2714__btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002715 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002716 struct btrfs_free_cluster *cluster)
2717{
Li Zefan34d52cb2011-03-29 13:46:06 +08002718 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002719 struct btrfs_free_space *entry;
2720 struct rb_node *node;
2721
2722 spin_lock(&cluster->lock);
2723 if (cluster->block_group != block_group)
2724 goto out;
2725
Josef Bacik96303082009-07-13 21:29:25 -04002726 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002727 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002728 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002729
Chris Masonfa9c0d792009-04-03 09:47:43 -04002730 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002731 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002732 bool bitmap;
2733
Chris Masonfa9c0d792009-04-03 09:47:43 -04002734 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2735 node = rb_next(&entry->offset_index);
2736 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002737 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002738
2739 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002740 if (!bitmap) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002741 /* Merging treats extents as if they were new */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002742 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002743 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002744 ctl->discardable_bytes[BTRFS_STAT_CURR] -=
2745 entry->bytes;
2746 }
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002747
Li Zefan34d52cb2011-03-29 13:46:06 +08002748 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002749 steal_from_bitmap(ctl, entry, false);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002750
2751 /* As we insert directly, update these statistics */
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002752 if (!btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002753 ctl->discardable_extents[BTRFS_STAT_CURR]++;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08002754 ctl->discardable_bytes[BTRFS_STAT_CURR] +=
2755 entry->bytes;
2756 }
Filipe Manana20005522014-08-29 13:35:13 +01002757 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002758 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002759 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002760 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002761 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002762
Chris Masonfa9c0d792009-04-03 09:47:43 -04002763out:
2764 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002765 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002766 return 0;
2767}
2768
Eric Sandeen48a3b632013-04-25 20:41:01 +00002769static void __btrfs_remove_free_space_cache_locked(
2770 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002771{
2772 struct btrfs_free_space *info;
2773 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002774
Li Zefan581bb052011-04-20 10:06:11 +08002775 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2776 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002777 if (!info->bitmap) {
2778 unlink_free_space(ctl, info);
2779 kmem_cache_free(btrfs_free_space_cachep, info);
2780 } else {
2781 free_bitmap(ctl, info);
2782 }
David Sterba351810c2015-01-08 15:20:54 +01002783
2784 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002785 }
Chris Mason09655372011-05-21 09:27:38 -04002786}
2787
2788void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2789{
2790 spin_lock(&ctl->tree_lock);
2791 __btrfs_remove_free_space_cache_locked(ctl);
Dennis Zhou27f0afc2020-01-02 16:26:45 -05002792 if (ctl->private)
2793 btrfs_discard_update_discardable(ctl->private, ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002794 spin_unlock(&ctl->tree_lock);
2795}
2796
David Sterba32da53862019-10-29 19:20:18 +01002797void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002798{
Li Zefan34d52cb2011-03-29 13:46:06 +08002799 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002800 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002801 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002802
Li Zefan34d52cb2011-03-29 13:46:06 +08002803 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002804 while ((head = block_group->cluster_list.next) !=
2805 &block_group->cluster_list) {
2806 cluster = list_entry(head, struct btrfs_free_cluster,
2807 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002808
2809 WARN_ON(cluster->block_group != block_group);
2810 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002811
2812 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002813 }
Chris Mason09655372011-05-21 09:27:38 -04002814 __btrfs_remove_free_space_cache_locked(ctl);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002815 btrfs_discard_update_discardable(block_group, ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002816 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002817
Josef Bacik0f9dd462008-09-23 13:14:11 -04002818}
2819
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08002820/**
2821 * btrfs_is_free_space_trimmed - see if everything is trimmed
2822 * @block_group: block_group of interest
2823 *
2824 * Walk @block_group's free space rb_tree to determine if everything is trimmed.
2825 */
2826bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
2827{
2828 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2829 struct btrfs_free_space *info;
2830 struct rb_node *node;
2831 bool ret = true;
2832
2833 spin_lock(&ctl->tree_lock);
2834 node = rb_first(&ctl->free_space_offset);
2835
2836 while (node) {
2837 info = rb_entry(node, struct btrfs_free_space, offset_index);
2838
2839 if (!btrfs_free_space_trimmed(info)) {
2840 ret = false;
2841 break;
2842 }
2843
2844 node = rb_next(node);
2845 }
2846
2847 spin_unlock(&ctl->tree_lock);
2848 return ret;
2849}
2850
David Sterba32da53862019-10-29 19:20:18 +01002851u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002852 u64 offset, u64 bytes, u64 empty_size,
2853 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002854{
Li Zefan34d52cb2011-03-29 13:46:06 +08002855 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002856 struct btrfs_discard_ctl *discard_ctl =
2857 &block_group->fs_info->discard_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002858 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002859 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002860 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002861 u64 align_gap = 0;
2862 u64 align_gap_len = 0;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002863 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002864
Li Zefan34d52cb2011-03-29 13:46:06 +08002865 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002866 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002867 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002868 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002869 goto out;
2870
2871 ret = offset;
2872 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002873 bitmap_clear_bits(ctl, entry, offset, bytes);
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002874
2875 if (!btrfs_free_space_trimmed(entry))
2876 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2877
Li Zefanedf6e2d2010-11-09 14:50:07 +08002878 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002879 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002880 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002881 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002882 align_gap_len = offset - entry->offset;
2883 align_gap = entry->offset;
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002884 align_gap_trim_state = entry->trim_state;
David Woodhouse53b381b2013-01-29 18:40:14 -05002885
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002886 if (!btrfs_free_space_trimmed(entry))
2887 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2888
David Woodhouse53b381b2013-01-29 18:40:14 -05002889 entry->offset = offset + bytes;
2890 WARN_ON(entry->bytes < bytes + align_gap_len);
2891
2892 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002893 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002894 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002895 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002896 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002897 }
Josef Bacik96303082009-07-13 21:29:25 -04002898out:
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08002899 btrfs_discard_update_discardable(block_group, ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002900 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002901
David Woodhouse53b381b2013-01-29 18:40:14 -05002902 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002903 __btrfs_add_free_space(block_group->fs_info, ctl,
Dennis Zhoua7ccb252019-12-13 16:22:12 -08002904 align_gap, align_gap_len,
2905 align_gap_trim_state);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002906 return ret;
2907}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002908
2909/*
2910 * given a cluster, put all of its extents back into the free space
2911 * cache. If a block group is passed, this function will only free
2912 * a cluster that belongs to the passed block group.
2913 *
2914 * Otherwise, it'll get a reference on the block group pointed to by the
2915 * cluster and remove the cluster from it.
2916 */
2917int btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002918 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002919 struct btrfs_free_cluster *cluster)
2920{
Li Zefan34d52cb2011-03-29 13:46:06 +08002921 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002922 int ret;
2923
2924 /* first, get a safe pointer to the block group */
2925 spin_lock(&cluster->lock);
2926 if (!block_group) {
2927 block_group = cluster->block_group;
2928 if (!block_group) {
2929 spin_unlock(&cluster->lock);
2930 return 0;
2931 }
2932 } else if (cluster->block_group != block_group) {
2933 /* someone else has already freed it don't redo their work */
2934 spin_unlock(&cluster->lock);
2935 return 0;
2936 }
2937 atomic_inc(&block_group->count);
2938 spin_unlock(&cluster->lock);
2939
Li Zefan34d52cb2011-03-29 13:46:06 +08002940 ctl = block_group->free_space_ctl;
2941
Chris Masonfa9c0d792009-04-03 09:47:43 -04002942 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002943 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002944 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002945 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002946
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08002947 btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
2948
Chris Masonfa9c0d792009-04-03 09:47:43 -04002949 /* finally drop our ref */
2950 btrfs_put_block_group(block_group);
2951 return ret;
2952}
2953
David Sterba32da53862019-10-29 19:20:18 +01002954static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04002955 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002956 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002957 u64 bytes, u64 min_start,
2958 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002959{
Li Zefan34d52cb2011-03-29 13:46:06 +08002960 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002961 int err;
2962 u64 search_start = cluster->window_start;
2963 u64 search_bytes = bytes;
2964 u64 ret = 0;
2965
Josef Bacik96303082009-07-13 21:29:25 -04002966 search_start = min_start;
2967 search_bytes = bytes;
2968
Josef Bacik0584f712015-10-02 16:12:23 -04002969 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002970 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002971 *max_extent_size = max(get_max_extent_size(entry),
2972 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002973 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002974 }
Josef Bacik96303082009-07-13 21:29:25 -04002975
2976 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002977 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002978
2979 return ret;
2980}
2981
Chris Masonfa9c0d792009-04-03 09:47:43 -04002982/*
2983 * given a cluster, try to allocate 'bytes' from it, returns 0
2984 * if it couldn't find anything suitably large, or a logical disk offset
2985 * if things worked out
2986 */
David Sterba32da53862019-10-29 19:20:18 +01002987u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002988 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002989 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002990{
Li Zefan34d52cb2011-03-29 13:46:06 +08002991 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Dennis Zhou9ddf6482020-01-02 16:26:41 -05002992 struct btrfs_discard_ctl *discard_ctl =
2993 &block_group->fs_info->discard_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002994 struct btrfs_free_space *entry = NULL;
2995 struct rb_node *node;
2996 u64 ret = 0;
2997
2998 spin_lock(&cluster->lock);
2999 if (bytes > cluster->max_size)
3000 goto out;
3001
3002 if (cluster->block_group != block_group)
3003 goto out;
3004
3005 node = rb_first(&cluster->root);
3006 if (!node)
3007 goto out;
3008
3009 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303010 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04003011 if (entry->bytes < bytes)
3012 *max_extent_size = max(get_max_extent_size(entry),
3013 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08003014
Josef Bacik4e69b592011-03-21 10:11:24 -04003015 if (entry->bytes < bytes ||
3016 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003017 node = rb_next(&entry->offset_index);
3018 if (!node)
3019 break;
3020 entry = rb_entry(node, struct btrfs_free_space,
3021 offset_index);
3022 continue;
3023 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003024
Josef Bacik4e69b592011-03-21 10:11:24 -04003025 if (entry->bitmap) {
3026 ret = btrfs_alloc_from_bitmap(block_group,
3027 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08003028 cluster->window_start,
3029 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04003030 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003031 node = rb_next(&entry->offset_index);
3032 if (!node)
3033 break;
3034 entry = rb_entry(node, struct btrfs_free_space,
3035 offset_index);
3036 continue;
3037 }
Josef Bacik9b230622012-01-26 15:01:12 -05003038 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003039 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04003040 ret = entry->offset;
3041
3042 entry->offset += bytes;
3043 entry->bytes -= bytes;
3044 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003045
Li Zefan5e71b5d2010-11-09 14:55:34 +08003046 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003047 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003048 break;
3049 }
3050out:
3051 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04003052
Li Zefan5e71b5d2010-11-09 14:55:34 +08003053 if (!ret)
3054 return 0;
3055
Li Zefan34d52cb2011-03-29 13:46:06 +08003056 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003057
Dennis Zhou9ddf6482020-01-02 16:26:41 -05003058 if (!btrfs_free_space_trimmed(entry))
3059 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3060
Li Zefan34d52cb2011-03-29 13:46:06 +08003061 ctl->free_space -= bytes;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003062 if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
3063 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08003064 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003065 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003066 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003067 kmem_cache_free(btrfs_free_space_bitmap_cachep,
3068 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08003069 ctl->total_bitmaps--;
3070 ctl->op->recalc_thresholds(ctl);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003071 } else if (!btrfs_free_space_trimmed(entry)) {
3072 ctl->discardable_extents[BTRFS_STAT_CURR]--;
Josef Bacik4e69b592011-03-21 10:11:24 -04003073 }
Josef Bacikdc89e982011-01-28 17:05:48 -05003074 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003075 }
3076
Li Zefan34d52cb2011-03-29 13:46:06 +08003077 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08003078
Chris Masonfa9c0d792009-04-03 09:47:43 -04003079 return ret;
3080}
3081
David Sterba32da53862019-10-29 19:20:18 +01003082static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04003083 struct btrfs_free_space *entry,
3084 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003085 u64 offset, u64 bytes,
3086 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04003087{
Li Zefan34d52cb2011-03-29 13:46:06 +08003088 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04003089 unsigned long next_zero;
3090 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003091 unsigned long want_bits;
3092 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003093 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04003094 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003095 unsigned long start = 0;
3096 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003097 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04003098
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003099 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04003100 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003101 want_bits = bytes_to_bits(bytes, ctl->unit);
3102 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04003103
Josef Bacikcef40482015-10-02 16:09:42 -04003104 /*
3105 * Don't bother looking for a cluster in this bitmap if it's heavily
3106 * fragmented.
3107 */
3108 if (entry->max_extent_size &&
3109 entry->max_extent_size < cont1_bytes)
3110 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04003111again:
3112 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06003113 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04003114 next_zero = find_next_zero_bit(entry->bitmap,
3115 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003116 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04003117 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04003118 if (found_bits > max_bits)
3119 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04003120 break;
3121 }
Josef Bacikcef40482015-10-02 16:09:42 -04003122 if (next_zero - i > max_bits)
3123 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04003124 i = next_zero;
3125 }
3126
Josef Bacikcef40482015-10-02 16:09:42 -04003127 if (!found_bits) {
3128 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04003129 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04003130 }
Josef Bacik96303082009-07-13 21:29:25 -04003131
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003132 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04003133 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05003134 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04003135 }
3136
3137 total_found += found_bits;
3138
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003139 if (cluster->max_size < found_bits * ctl->unit)
3140 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04003141
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003142 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
3143 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04003144 goto again;
3145 }
3146
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003147 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08003148 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003149 ret = tree_insert_offset(&cluster->root, entry->offset,
3150 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04003151 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04003152
Josef Bacik3f7de032011-11-10 08:29:20 -05003153 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00003154 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04003155 return 0;
3156}
3157
Chris Masonfa9c0d792009-04-03 09:47:43 -04003158/*
Josef Bacik4e69b592011-03-21 10:11:24 -04003159 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003160 * Try to find a cluster with at least bytes total bytes, at least one
3161 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04003162 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003163static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003164setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003165 struct btrfs_free_cluster *cluster,
3166 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003167 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003168{
Li Zefan34d52cb2011-03-29 13:46:06 +08003169 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04003170 struct btrfs_free_space *first = NULL;
3171 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003172 struct btrfs_free_space *last;
3173 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04003174 u64 window_free;
3175 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003176 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04003177
Li Zefan34d52cb2011-03-29 13:46:06 +08003178 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04003179 if (!entry)
3180 return -ENOSPC;
3181
3182 /*
3183 * We don't want bitmaps, so just move along until we find a normal
3184 * extent entry.
3185 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003186 while (entry->bitmap || entry->bytes < min_bytes) {
3187 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04003188 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003189 node = rb_next(&entry->offset_index);
3190 if (!node)
3191 return -ENOSPC;
3192 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3193 }
3194
Josef Bacik4e69b592011-03-21 10:11:24 -04003195 window_free = entry->bytes;
3196 max_extent = entry->bytes;
3197 first = entry;
3198 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04003199
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003200 for (node = rb_next(&entry->offset_index); node;
3201 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04003202 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3203
Josef Bacik86d4a772011-05-25 13:03:16 -04003204 if (entry->bitmap) {
3205 if (list_empty(&entry->list))
3206 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04003207 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04003208 }
3209
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003210 if (entry->bytes < min_bytes)
3211 continue;
3212
3213 last = entry;
3214 window_free += entry->bytes;
3215 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04003216 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04003217 }
3218
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003219 if (window_free < bytes || max_extent < cont1_bytes)
3220 return -ENOSPC;
3221
Josef Bacik4e69b592011-03-21 10:11:24 -04003222 cluster->window_start = first->offset;
3223
3224 node = &first->offset_index;
3225
3226 /*
3227 * now we've found our entries, pull them out of the free space
3228 * cache and put them into the cluster rbtree
3229 */
3230 do {
3231 int ret;
3232
3233 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3234 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003235 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003236 continue;
3237
Li Zefan34d52cb2011-03-29 13:46:06 +08003238 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003239 ret = tree_insert_offset(&cluster->root, entry->offset,
3240 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05003241 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04003242 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04003243 } while (node && entry != last);
3244
3245 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05003246 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04003247 return 0;
3248}
3249
3250/*
3251 * This specifically looks for bitmaps that may work in the cluster, we assume
3252 * that we have already failed to find extents that will work.
3253 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003254static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003255setup_cluster_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003256 struct btrfs_free_cluster *cluster,
3257 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003258 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003259{
Li Zefan34d52cb2011-03-29 13:46:06 +08003260 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003261 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003262 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003263 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003264
Li Zefan34d52cb2011-03-29 13:46:06 +08003265 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003266 return -ENOSPC;
3267
Josef Bacik86d4a772011-05-25 13:03:16 -04003268 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003269 * The bitmap that covers offset won't be in the list unless offset
3270 * is just its start offset.
3271 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003272 if (!list_empty(bitmaps))
3273 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3274
3275 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003276 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3277 if (entry && list_empty(&entry->list))
3278 list_add(&entry->list, bitmaps);
3279 }
3280
Josef Bacik86d4a772011-05-25 13:03:16 -04003281 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003282 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003283 continue;
3284 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003285 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003286 if (!ret)
3287 return 0;
3288 }
3289
3290 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003291 * The bitmaps list has all the bitmaps that record free space
3292 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003293 */
Li Zefan52621cb2011-11-20 07:33:38 -05003294 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003295}
3296
3297/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003298 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003299 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003300 * We might not find them all in one contiguous area.
3301 *
3302 * returns zero and sets up cluster if things worked out, otherwise
3303 * it returns -enospc
3304 */
David Sterba32da53862019-10-29 19:20:18 +01003305int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003306 struct btrfs_free_cluster *cluster,
3307 u64 offset, u64 bytes, u64 empty_size)
3308{
David Sterba2ceeae22019-03-20 13:53:49 +01003309 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003310 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003311 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003312 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003313 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003314 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003315 int ret;
3316
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003317 /*
3318 * Choose the minimum extent size we'll require for this
3319 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3320 * For metadata, allow allocates with smaller extents. For
3321 * data, keep it dense.
3322 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003323 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003324 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003325 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003326 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003327 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003328 } else {
3329 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003330 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003331 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003332
Li Zefan34d52cb2011-03-29 13:46:06 +08003333 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003334
3335 /*
3336 * If we know we don't have enough space to make a cluster don't even
3337 * bother doing all the work to try and find one.
3338 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003339 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003340 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003341 return -ENOSPC;
3342 }
3343
Chris Masonfa9c0d792009-04-03 09:47:43 -04003344 spin_lock(&cluster->lock);
3345
3346 /* someone already found a cluster, hooray */
3347 if (cluster->block_group) {
3348 ret = 0;
3349 goto out;
3350 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003351
Josef Bacik3f7de032011-11-10 08:29:20 -05003352 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3353 min_bytes);
3354
Josef Bacik86d4a772011-05-25 13:03:16 -04003355 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003356 bytes + empty_size,
3357 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003358 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003359 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003360 offset, bytes + empty_size,
3361 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003362
3363 /* Clear our temporary list */
3364 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3365 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003366
3367 if (!ret) {
3368 atomic_inc(&block_group->count);
3369 list_add_tail(&cluster->block_group_list,
3370 &block_group->cluster_list);
3371 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003372 } else {
3373 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003374 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003375out:
3376 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003377 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003378
3379 return ret;
3380}
3381
3382/*
3383 * simple code to zero out a cluster
3384 */
3385void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3386{
3387 spin_lock_init(&cluster->lock);
3388 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003389 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003390 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003391 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003392 INIT_LIST_HEAD(&cluster->block_group_list);
3393 cluster->block_group = NULL;
3394}
3395
David Sterba32da53862019-10-29 19:20:18 +01003396static int do_trimming(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003397 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003398 u64 reserved_start, u64 reserved_bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003399 enum btrfs_trim_state reserved_trim_state,
Filipe Manana55507ce2014-12-01 17:04:09 +00003400 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003401{
3402 struct btrfs_space_info *space_info = block_group->space_info;
3403 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003404 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003405 int ret;
3406 int update = 0;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003407 const u64 end = start + bytes;
3408 const u64 reserved_end = reserved_start + reserved_bytes;
3409 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003410 u64 trimmed = 0;
3411
3412 spin_lock(&space_info->lock);
3413 spin_lock(&block_group->lock);
3414 if (!block_group->ro) {
3415 block_group->reserved += reserved_bytes;
3416 space_info->bytes_reserved += reserved_bytes;
3417 update = 1;
3418 }
3419 spin_unlock(&block_group->lock);
3420 spin_unlock(&space_info->lock);
3421
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003422 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003423 if (!ret) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003424 *total_trimmed += trimmed;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003425 trim_state = BTRFS_TRIM_STATE_TRIMMED;
3426 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003427
Filipe Manana55507ce2014-12-01 17:04:09 +00003428 mutex_lock(&ctl->cache_writeout_mutex);
Dennis Zhoub0643e52019-12-13 16:22:14 -08003429 if (reserved_start < start)
3430 __btrfs_add_free_space(fs_info, ctl, reserved_start,
3431 start - reserved_start,
3432 reserved_trim_state);
3433 if (start + bytes < reserved_start + reserved_bytes)
3434 __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end,
3435 reserved_trim_state);
3436 __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state);
Filipe Manana55507ce2014-12-01 17:04:09 +00003437 list_del(&trim_entry->list);
3438 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003439
3440 if (update) {
3441 spin_lock(&space_info->lock);
3442 spin_lock(&block_group->lock);
3443 if (block_group->ro)
3444 space_info->bytes_readonly += reserved_bytes;
3445 block_group->reserved -= reserved_bytes;
3446 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003447 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003448 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003449 }
3450
3451 return ret;
3452}
3453
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003454/*
3455 * If @async is set, then we will trim 1 region and return.
3456 */
David Sterba32da53862019-10-29 19:20:18 +01003457static int trim_no_bitmap(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003458 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3459 bool async)
Li Dongyangf7039b12011-03-24 10:24:28 +00003460{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003461 struct btrfs_discard_ctl *discard_ctl =
3462 &block_group->fs_info->discard_ctl;
Li Zefan34d52cb2011-03-29 13:46:06 +08003463 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003464 struct btrfs_free_space *entry;
3465 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003466 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003467 u64 extent_start;
3468 u64 extent_bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003469 enum btrfs_trim_state extent_trim_state;
Li Zefan7fe1e642011-12-29 14:47:27 +08003470 u64 bytes;
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003471 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Dongyangf7039b12011-03-24 10:24:28 +00003472
3473 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003474 struct btrfs_trim_range trim_entry;
3475
3476 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003477 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003478
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003479 if (ctl->free_space < minlen)
3480 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003481
Li Zefan34d52cb2011-03-29 13:46:06 +08003482 entry = tree_search_offset(ctl, start, 0, 1);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003483 if (!entry)
3484 goto out_unlock;
Li Dongyangf7039b12011-03-24 10:24:28 +00003485
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003486 /* Skip bitmaps and if async, already trimmed entries */
3487 while (entry->bitmap ||
3488 (async && btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003489 node = rb_next(&entry->offset_index);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003490 if (!node)
3491 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003492 entry = rb_entry(node, struct btrfs_free_space,
3493 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003494 }
3495
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003496 if (entry->offset >= end)
3497 goto out_unlock;
Li Zefan7fe1e642011-12-29 14:47:27 +08003498
3499 extent_start = entry->offset;
3500 extent_bytes = entry->bytes;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003501 extent_trim_state = entry->trim_state;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003502 if (async) {
3503 start = entry->offset;
3504 bytes = entry->bytes;
3505 if (bytes < minlen) {
3506 spin_unlock(&ctl->tree_lock);
3507 mutex_unlock(&ctl->cache_writeout_mutex);
3508 goto next;
3509 }
3510 unlink_free_space(ctl, entry);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003511 /*
3512 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3513 * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
3514 * X when we come back around. So trim it now.
3515 */
3516 if (max_discard_size &&
3517 bytes >= (max_discard_size +
3518 BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003519 bytes = max_discard_size;
3520 extent_bytes = max_discard_size;
3521 entry->offset += max_discard_size;
3522 entry->bytes -= max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003523 link_free_space(ctl, entry);
3524 } else {
3525 kmem_cache_free(btrfs_free_space_cachep, entry);
3526 }
3527 } else {
3528 start = max(start, extent_start);
3529 bytes = min(extent_start + extent_bytes, end) - start;
3530 if (bytes < minlen) {
3531 spin_unlock(&ctl->tree_lock);
3532 mutex_unlock(&ctl->cache_writeout_mutex);
3533 goto next;
3534 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003535
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003536 unlink_free_space(ctl, entry);
3537 kmem_cache_free(btrfs_free_space_cachep, entry);
3538 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003539
Li Zefan34d52cb2011-03-29 13:46:06 +08003540 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003541 trim_entry.start = extent_start;
3542 trim_entry.bytes = extent_bytes;
3543 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3544 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003545
Li Zefan7fe1e642011-12-29 14:47:27 +08003546 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003547 extent_start, extent_bytes, extent_trim_state,
3548 &trim_entry);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003549 if (ret) {
3550 block_group->discard_cursor = start + bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003551 break;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003552 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003553next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003554 start += bytes;
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003555 block_group->discard_cursor = start;
3556 if (async && *total_trimmed)
3557 break;
Li Dongyangf7039b12011-03-24 10:24:28 +00003558
3559 if (fatal_signal_pending(current)) {
3560 ret = -ERESTARTSYS;
3561 break;
3562 }
3563
3564 cond_resched();
3565 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003566
3567 return ret;
3568
3569out_unlock:
3570 block_group->discard_cursor = btrfs_block_group_end(block_group);
3571 spin_unlock(&ctl->tree_lock);
3572 mutex_unlock(&ctl->cache_writeout_mutex);
3573
Li Zefan7fe1e642011-12-29 14:47:27 +08003574 return ret;
3575}
3576
Dennis Zhouda080fe2019-12-13 16:22:13 -08003577/*
3578 * If we break out of trimming a bitmap prematurely, we should reset the
3579 * trimming bit. In a rather contrieved case, it's possible to race here so
3580 * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
3581 *
3582 * start = start of bitmap
3583 * end = near end of bitmap
3584 *
3585 * Thread 1: Thread 2:
3586 * trim_bitmaps(start)
3587 * trim_bitmaps(end)
3588 * end_trimming_bitmap()
3589 * reset_trimming_bitmap()
3590 */
3591static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
3592{
3593 struct btrfs_free_space *entry;
3594
3595 spin_lock(&ctl->tree_lock);
3596 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003597 if (entry) {
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003598 if (btrfs_free_space_trimmed(entry)) {
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003599 ctl->discardable_extents[BTRFS_STAT_CURR] +=
3600 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003601 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
3602 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003603 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003604 }
3605
Dennis Zhouda080fe2019-12-13 16:22:13 -08003606 spin_unlock(&ctl->tree_lock);
3607}
3608
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003609static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
3610 struct btrfs_free_space *entry)
Dennis Zhouda080fe2019-12-13 16:22:13 -08003611{
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003612 if (btrfs_free_space_trimming_bitmap(entry)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003613 entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003614 ctl->discardable_extents[BTRFS_STAT_CURR] -=
3615 entry->bitmap_extents;
Dennis Zhou5dc7c102019-12-13 16:22:21 -08003616 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003617 }
Dennis Zhouda080fe2019-12-13 16:22:13 -08003618}
3619
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003620/*
3621 * If @async is set, then we will trim 1 region and return.
3622 */
David Sterba32da53862019-10-29 19:20:18 +01003623static int trim_bitmaps(struct btrfs_block_group *block_group,
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003624 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003625 u64 maxlen, bool async)
Li Zefan7fe1e642011-12-29 14:47:27 +08003626{
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003627 struct btrfs_discard_ctl *discard_ctl =
3628 &block_group->fs_info->discard_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003629 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3630 struct btrfs_free_space *entry;
3631 int ret = 0;
3632 int ret2;
3633 u64 bytes;
3634 u64 offset = offset_to_bitmap(ctl, start);
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003635 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
Li Zefan7fe1e642011-12-29 14:47:27 +08003636
3637 while (offset < end) {
3638 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003639 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003640
Filipe Manana55507ce2014-12-01 17:04:09 +00003641 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003642 spin_lock(&ctl->tree_lock);
3643
3644 if (ctl->free_space < minlen) {
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003645 block_group->discard_cursor =
3646 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003647 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003648 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003649 break;
3650 }
3651
3652 entry = tree_search_offset(ctl, offset, 1, 0);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003653 /*
3654 * Bitmaps are marked trimmed lossily now to prevent constant
3655 * discarding of the same bitmap (the reason why we are bound
3656 * by the filters). So, retrim the block group bitmaps when we
3657 * are preparing to punt to the unused_bgs list. This uses
3658 * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
3659 * which is the only discard index which sets minlen to 0.
3660 */
3661 if (!entry || (async && minlen && start == offset &&
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003662 btrfs_free_space_trimmed(entry))) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003663 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003664 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003665 next_bitmap = true;
3666 goto next;
3667 }
3668
Dennis Zhouda080fe2019-12-13 16:22:13 -08003669 /*
3670 * Async discard bitmap trimming begins at by setting the start
3671 * to be key.objectid and the offset_to_bitmap() aligns to the
3672 * start of the bitmap. This lets us know we are fully
3673 * scanning the bitmap rather than only some portion of it.
3674 */
3675 if (start == offset)
3676 entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
3677
Li Zefan7fe1e642011-12-29 14:47:27 +08003678 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003679 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003680 if (ret2 || start >= end) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003681 /*
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003682 * We lossily consider a bitmap trimmed if we only skip
3683 * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
Dennis Zhouda080fe2019-12-13 16:22:13 -08003684 */
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003685 if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
Dennis Zhoudfb79dd2019-12-13 16:22:20 -08003686 end_trimming_bitmap(ctl, entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003687 else
3688 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
Li Zefan7fe1e642011-12-29 14:47:27 +08003689 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003690 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003691 next_bitmap = true;
3692 goto next;
3693 }
3694
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003695 /*
3696 * We already trimmed a region, but are using the locking above
3697 * to reset the trim_state.
3698 */
3699 if (async && *total_trimmed) {
3700 spin_unlock(&ctl->tree_lock);
3701 mutex_unlock(&ctl->cache_writeout_mutex);
3702 goto out;
3703 }
3704
Li Zefan7fe1e642011-12-29 14:47:27 +08003705 bytes = min(bytes, end - start);
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003706 if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
Li Zefan7fe1e642011-12-29 14:47:27 +08003707 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003708 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003709 goto next;
3710 }
3711
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003712 /*
3713 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3714 * If X < @minlen, we won't trim X when we come back around.
3715 * So trim it now. We differ here from trimming extents as we
3716 * don't keep individual state per bit.
3717 */
3718 if (async &&
3719 max_discard_size &&
3720 bytes > (max_discard_size + minlen))
Dennis Zhou19b2a2c2020-01-02 16:26:38 -05003721 bytes = max_discard_size;
Dennis Zhou4aa9ad52020-01-02 16:26:37 -05003722
Li Zefan7fe1e642011-12-29 14:47:27 +08003723 bitmap_clear_bits(ctl, entry, start, bytes);
3724 if (entry->bytes == 0)
3725 free_bitmap(ctl, entry);
3726
3727 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003728 trim_entry.start = start;
3729 trim_entry.bytes = bytes;
3730 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3731 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003732
3733 ret = do_trimming(block_group, total_trimmed, start, bytes,
Dennis Zhoub0643e52019-12-13 16:22:14 -08003734 start, bytes, 0, &trim_entry);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003735 if (ret) {
3736 reset_trimming_bitmap(ctl, offset);
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003737 block_group->discard_cursor =
3738 btrfs_block_group_end(block_group);
Li Zefan7fe1e642011-12-29 14:47:27 +08003739 break;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003740 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003741next:
3742 if (next_bitmap) {
3743 offset += BITS_PER_BITMAP * ctl->unit;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003744 start = offset;
Li Zefan7fe1e642011-12-29 14:47:27 +08003745 } else {
3746 start += bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003747 }
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003748 block_group->discard_cursor = start;
Li Zefan7fe1e642011-12-29 14:47:27 +08003749
3750 if (fatal_signal_pending(current)) {
Dennis Zhouda080fe2019-12-13 16:22:13 -08003751 if (start != offset)
3752 reset_trimming_bitmap(ctl, offset);
Li Zefan7fe1e642011-12-29 14:47:27 +08003753 ret = -ERESTARTSYS;
3754 break;
3755 }
3756
3757 cond_resched();
3758 }
3759
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003760 if (offset >= end)
3761 block_group->discard_cursor = end;
3762
3763out:
Li Zefan7fe1e642011-12-29 14:47:27 +08003764 return ret;
3765}
3766
David Sterba32da53862019-10-29 19:20:18 +01003767void btrfs_get_block_group_trimming(struct btrfs_block_group *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003768{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003769 atomic_inc(&cache->trimming);
3770}
Li Zefan7fe1e642011-12-29 14:47:27 +08003771
David Sterba32da53862019-10-29 19:20:18 +01003772void btrfs_put_block_group_trimming(struct btrfs_block_group *block_group)
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003774 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003775 struct extent_map_tree *em_tree;
3776 struct extent_map *em;
3777 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003778
Filipe Manana04216822014-11-27 21:14:15 +00003779 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003780 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3781 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003782 spin_unlock(&block_group->lock);
3783
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003784 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003785 mutex_lock(&fs_info->chunk_mutex);
David Sterbac8bf1b62019-05-17 11:43:17 +02003786 em_tree = &fs_info->mapping_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003787 write_lock(&em_tree->lock);
David Sterbab3470b52019-10-23 18:48:22 +02003788 em = lookup_extent_mapping(em_tree, block_group->start,
Filipe Manana04216822014-11-27 21:14:15 +00003789 1);
3790 BUG_ON(!em); /* logic error, can't happen */
3791 remove_extent_mapping(em_tree, em);
3792 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003793 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003794
3795 /* once for us and once for the tree */
3796 free_extent_map(em);
3797 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003798
3799 /*
3800 * We've left one free space entry and other tasks trimming
3801 * this block group have left 1 entry each one. Free them.
3802 */
3803 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003804 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003805}
Li Dongyangf7039b12011-03-24 10:24:28 +00003806
David Sterba32da53862019-10-29 19:20:18 +01003807int btrfs_trim_block_group(struct btrfs_block_group *block_group,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003808 u64 *trimmed, u64 start, u64 end, u64 minlen)
3809{
Dennis Zhouda080fe2019-12-13 16:22:13 -08003810 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003811 int ret;
Dennis Zhouda080fe2019-12-13 16:22:13 -08003812 u64 rem = 0;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003813
3814 *trimmed = 0;
3815
3816 spin_lock(&block_group->lock);
3817 if (block_group->removed) {
3818 spin_unlock(&block_group->lock);
3819 return 0;
3820 }
3821 btrfs_get_block_group_trimming(block_group);
3822 spin_unlock(&block_group->lock);
3823
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003824 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003825 if (ret)
3826 goto out;
3827
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003828 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
Dennis Zhouda080fe2019-12-13 16:22:13 -08003829 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
3830 /* If we ended in the middle of a bitmap, reset the trimming flag */
3831 if (rem)
3832 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003833out:
3834 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003835 return ret;
3836}
Li Zefan581bb052011-04-20 10:06:11 +08003837
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003838int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
3839 u64 *trimmed, u64 start, u64 end, u64 minlen,
3840 bool async)
3841{
3842 int ret;
3843
3844 *trimmed = 0;
3845
3846 spin_lock(&block_group->lock);
3847 if (block_group->removed) {
3848 spin_unlock(&block_group->lock);
3849 return 0;
3850 }
3851 btrfs_get_block_group_trimming(block_group);
3852 spin_unlock(&block_group->lock);
3853
3854 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
3855 btrfs_put_block_group_trimming(block_group);
3856
3857 return ret;
3858}
3859
3860int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
3861 u64 *trimmed, u64 start, u64 end, u64 minlen,
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003862 u64 maxlen, bool async)
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003863{
3864 int ret;
3865
3866 *trimmed = 0;
3867
3868 spin_lock(&block_group->lock);
3869 if (block_group->removed) {
3870 spin_unlock(&block_group->lock);
3871 return 0;
3872 }
3873 btrfs_get_block_group_trimming(block_group);
3874 spin_unlock(&block_group->lock);
3875
Dennis Zhou7fe6d452020-01-02 16:26:39 -05003876 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
3877 async);
3878
Dennis Zhou2bee7eb2019-12-13 16:22:16 -08003879 btrfs_put_block_group_trimming(block_group);
3880
3881 return ret;
3882}
3883
Li Zefan581bb052011-04-20 10:06:11 +08003884/*
3885 * Find the left-most item in the cache tree, and then return the
3886 * smallest inode number in the item.
3887 *
3888 * Note: the returned inode number may not be the smallest one in
3889 * the tree, if the left-most item is a bitmap.
3890 */
3891u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3892{
3893 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3894 struct btrfs_free_space *entry = NULL;
3895 u64 ino = 0;
3896
3897 spin_lock(&ctl->tree_lock);
3898
3899 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3900 goto out;
3901
3902 entry = rb_entry(rb_first(&ctl->free_space_offset),
3903 struct btrfs_free_space, offset_index);
3904
3905 if (!entry->bitmap) {
3906 ino = entry->offset;
3907
3908 unlink_free_space(ctl, entry);
3909 entry->offset++;
3910 entry->bytes--;
3911 if (!entry->bytes)
3912 kmem_cache_free(btrfs_free_space_cachep, entry);
3913 else
3914 link_free_space(ctl, entry);
3915 } else {
3916 u64 offset = 0;
3917 u64 count = 1;
3918 int ret;
3919
Josef Bacik0584f712015-10-02 16:12:23 -04003920 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003921 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003922 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003923
3924 ino = offset;
3925 bitmap_clear_bits(ctl, entry, offset, 1);
3926 if (entry->bytes == 0)
3927 free_bitmap(ctl, entry);
3928 }
3929out:
3930 spin_unlock(&ctl->tree_lock);
3931
3932 return ino;
3933}
Li Zefan82d59022011-04-20 10:33:24 +08003934
3935struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3936 struct btrfs_path *path)
3937{
3938 struct inode *inode = NULL;
3939
David Sterba57cdc8d2014-02-05 02:37:48 +01003940 spin_lock(&root->ino_cache_lock);
3941 if (root->ino_cache_inode)
3942 inode = igrab(root->ino_cache_inode);
3943 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003944 if (inode)
3945 return inode;
3946
3947 inode = __lookup_free_space_inode(root, path, 0);
3948 if (IS_ERR(inode))
3949 return inode;
3950
David Sterba57cdc8d2014-02-05 02:37:48 +01003951 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003952 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003953 root->ino_cache_inode = igrab(inode);
3954 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003955
3956 return inode;
3957}
3958
3959int create_free_ino_inode(struct btrfs_root *root,
3960 struct btrfs_trans_handle *trans,
3961 struct btrfs_path *path)
3962{
3963 return __create_free_space_inode(root, trans, path,
3964 BTRFS_FREE_INO_OBJECTID, 0);
3965}
3966
3967int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3968{
3969 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3970 struct btrfs_path *path;
3971 struct inode *inode;
3972 int ret = 0;
3973 u64 root_gen = btrfs_root_generation(&root->root_item);
3974
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003975 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003976 return 0;
3977
Li Zefan82d59022011-04-20 10:33:24 +08003978 /*
3979 * If we're unmounting then just return, since this does a search on the
3980 * normal root and not the commit root and we could deadlock.
3981 */
David Sterba7841cb22011-05-31 18:07:27 +02003982 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003983 return 0;
3984
3985 path = btrfs_alloc_path();
3986 if (!path)
3987 return 0;
3988
3989 inode = lookup_free_ino_inode(root, path);
3990 if (IS_ERR(inode))
3991 goto out;
3992
3993 if (root_gen != BTRFS_I(inode)->generation)
3994 goto out_put;
3995
3996 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3997
3998 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003999 btrfs_err(fs_info,
4000 "failed to load free ino cache for root %llu",
4001 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08004002out_put:
4003 iput(inode);
4004out:
4005 btrfs_free_path(path);
4006 return ret;
4007}
4008
4009int btrfs_write_out_ino_cache(struct btrfs_root *root,
4010 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01004011 struct btrfs_path *path,
4012 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08004013{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004014 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08004015 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08004016 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07004017 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01004018 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08004019
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004020 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04004021 return 0;
4022
Chris Mason85db36c2015-04-23 08:02:49 -07004023 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01004024 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01004025 if (!ret) {
4026 /*
4027 * At this point writepages() didn't error out, so our metadata
4028 * reservation is released when the writeback finishes, at
4029 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
4030 * with or without an error.
4031 */
4032 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04004033 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01004034 }
Chris Mason85db36c2015-04-23 08:02:49 -07004035
Josef Bacikc09544e2011-08-30 10:19:10 -04004036 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01004037 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02004038 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08004039 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04004040#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004041 btrfs_err(fs_info,
4042 "failed to write free ino cache for root %llu",
4043 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04004044#endif
4045 }
Li Zefan82d59022011-04-20 10:33:24 +08004046
Li Zefan82d59022011-04-20 10:33:24 +08004047 return ret;
4048}
Josef Bacik74255aa2013-03-15 09:47:08 -04004049
4050#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04004051/*
4052 * Use this if you need to make a bitmap or extent entry specifically, it
4053 * doesn't do any of the merging that add_free_space does, this acts a lot like
4054 * how the free space cache loading stuff works, so you can get really weird
4055 * configurations.
4056 */
David Sterba32da53862019-10-29 19:20:18 +01004057int test_add_free_space_entry(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004058 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04004059{
Josef Bacikdc11dd52013-08-14 15:05:12 -04004060 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4061 struct btrfs_free_space *info = NULL, *bitmap_info;
4062 void *map = NULL;
Dennis Zhouda080fe2019-12-13 16:22:13 -08004063 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004064 u64 bytes_added;
4065 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04004066
Josef Bacikdc11dd52013-08-14 15:05:12 -04004067again:
4068 if (!info) {
4069 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
4070 if (!info)
4071 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04004072 }
4073
Josef Bacikdc11dd52013-08-14 15:05:12 -04004074 if (!bitmap) {
4075 spin_lock(&ctl->tree_lock);
4076 info->offset = offset;
4077 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04004078 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004079 ret = link_free_space(ctl, info);
4080 spin_unlock(&ctl->tree_lock);
4081 if (ret)
4082 kmem_cache_free(btrfs_free_space_cachep, info);
4083 return ret;
4084 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004085
Josef Bacikdc11dd52013-08-14 15:05:12 -04004086 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00004087 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004088 if (!map) {
4089 kmem_cache_free(btrfs_free_space_cachep, info);
4090 return -ENOMEM;
4091 }
4092 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004093
Josef Bacikdc11dd52013-08-14 15:05:12 -04004094 spin_lock(&ctl->tree_lock);
4095 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4096 1, 0);
4097 if (!bitmap_info) {
4098 info->bitmap = map;
4099 map = NULL;
4100 add_new_bitmap(ctl, info, offset);
4101 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01004102 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04004103 }
Josef Bacik74255aa2013-03-15 09:47:08 -04004104
Dennis Zhouda080fe2019-12-13 16:22:13 -08004105 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
4106 trim_state);
Josef Bacikcef40482015-10-02 16:09:42 -04004107
Josef Bacikdc11dd52013-08-14 15:05:12 -04004108 bytes -= bytes_added;
4109 offset += bytes_added;
4110 spin_unlock(&ctl->tree_lock);
4111
4112 if (bytes)
4113 goto again;
4114
Filipe Manana20005522014-08-29 13:35:13 +01004115 if (info)
4116 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00004117 if (map)
4118 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04004119 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004120}
4121
4122/*
4123 * Checks to see if the given range is in the free space cache. This is really
4124 * just used to check the absence of space, so if there is free space in the
4125 * range at all we will return 1.
4126 */
David Sterba32da53862019-10-29 19:20:18 +01004127int test_check_exists(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04004128 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04004129{
4130 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4131 struct btrfs_free_space *info;
4132 int ret = 0;
4133
4134 spin_lock(&ctl->tree_lock);
4135 info = tree_search_offset(ctl, offset, 0, 0);
4136 if (!info) {
4137 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4138 1, 0);
4139 if (!info)
4140 goto out;
4141 }
4142
4143have_info:
4144 if (info->bitmap) {
4145 u64 bit_off, bit_bytes;
4146 struct rb_node *n;
4147 struct btrfs_free_space *tmp;
4148
4149 bit_off = offset;
4150 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04004151 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04004152 if (!ret) {
4153 if (bit_off == offset) {
4154 ret = 1;
4155 goto out;
4156 } else if (bit_off > offset &&
4157 offset + bytes > bit_off) {
4158 ret = 1;
4159 goto out;
4160 }
4161 }
4162
4163 n = rb_prev(&info->offset_index);
4164 while (n) {
4165 tmp = rb_entry(n, struct btrfs_free_space,
4166 offset_index);
4167 if (tmp->offset + tmp->bytes < offset)
4168 break;
4169 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004170 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004171 continue;
4172 }
4173 info = tmp;
4174 goto have_info;
4175 }
4176
4177 n = rb_next(&info->offset_index);
4178 while (n) {
4179 tmp = rb_entry(n, struct btrfs_free_space,
4180 offset_index);
4181 if (offset + bytes < tmp->offset)
4182 break;
4183 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08004184 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04004185 continue;
4186 }
4187 info = tmp;
4188 goto have_info;
4189 }
4190
Filipe Manana20005522014-08-29 13:35:13 +01004191 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04004192 goto out;
4193 }
4194
4195 if (info->offset == offset) {
4196 ret = 1;
4197 goto out;
4198 }
4199
4200 if (offset > info->offset && offset < info->offset + info->bytes)
4201 ret = 1;
4202out:
4203 spin_unlock(&ctl->tree_lock);
4204 return ret;
4205}
Josef Bacikdc11dd52013-08-14 15:05:12 -04004206#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */