blob: 3283da419200b7c4b2f23bc7e579c435513a8d25 [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"
Chris Masonfa9c0d792009-04-03 09:47:43 -040024
Feifei Xu0ef64472016-06-01 19:18:24 +080025#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Byongho Leeee221842015-12-15 01:42:10 +090026#define MAX_CACHE_BYTES_PER_GIG SZ_32K
Josef Bacik96303082009-07-13 21:29:25 -040027
Filipe Manana55507ce2014-12-01 17:04:09 +000028struct btrfs_trim_range {
29 u64 start;
30 u64 bytes;
31 struct list_head list;
32};
33
Li Zefan34d52cb2011-03-29 13:46:06 +080034static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040035 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040036static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
37 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040038static int btrfs_wait_cache_io_root(struct btrfs_root *root,
39 struct btrfs_trans_handle *trans,
40 struct btrfs_io_ctl *io_ctl,
41 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040042
Li Zefan0414efa2011-04-20 10:20:14 +080043static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
44 struct btrfs_path *path,
45 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040046{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040047 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040048 struct btrfs_key key;
49 struct btrfs_key location;
50 struct btrfs_disk_key disk_key;
51 struct btrfs_free_space_header *header;
52 struct extent_buffer *leaf;
53 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040054 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040055 int ret;
56
Josef Bacik0af3d002010-06-21 14:48:16 -040057 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080058 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040059 key.type = 0;
60
61 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
62 if (ret < 0)
63 return ERR_PTR(ret);
64 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020065 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040066 return ERR_PTR(-ENOENT);
67 }
68
69 leaf = path->nodes[0];
70 header = btrfs_item_ptr(leaf, path->slots[0],
71 struct btrfs_free_space_header);
72 btrfs_free_space_key(leaf, header, &disk_key);
73 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020074 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040075
Josef Bacik84de76a2018-09-28 07:17:49 -040076 /*
77 * We are often under a trans handle at this point, so we need to make
78 * sure NOFS is set to keep us from deadlocking.
79 */
80 nofs_flag = memalloc_nofs_save();
David Sterba4c66e0d2019-10-03 19:09:35 +020081 inode = btrfs_iget_path(fs_info->sb, &location, root, path);
Filipe Manana4222ea72018-10-24 10:13:03 +010082 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040083 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040084 if (IS_ERR(inode))
85 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040086
Al Viro528c0322012-04-13 11:03:55 -040087 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080088 mapping_gfp_constraint(inode->i_mapping,
89 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000090
Li Zefan0414efa2011-04-20 10:20:14 +080091 return inode;
92}
93
David Sterba32da53862019-10-29 19:20:18 +010094struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
David Sterba7949f332019-03-20 13:40:19 +010095 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +080096{
David Sterba7949f332019-03-20 13:40:19 +010097 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +080098 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040099 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800100
101 spin_lock(&block_group->lock);
102 if (block_group->inode)
103 inode = igrab(block_group->inode);
104 spin_unlock(&block_group->lock);
105 if (inode)
106 return inode;
107
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500108 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200109 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800110 if (IS_ERR(inode))
111 return inode;
112
Josef Bacik0af3d002010-06-21 14:48:16 -0400113 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400114 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400115 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400116 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
117 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400118 block_group->disk_cache_state = BTRFS_DC_CLEAR;
119 }
120
Josef Bacik300e4f82011-08-29 14:06:00 -0400121 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400122 block_group->inode = igrab(inode);
123 block_group->iref = 1;
124 }
125 spin_unlock(&block_group->lock);
126
127 return inode;
128}
129
Eric Sandeen48a3b632013-04-25 20:41:01 +0000130static int __create_free_space_inode(struct btrfs_root *root,
131 struct btrfs_trans_handle *trans,
132 struct btrfs_path *path,
133 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400134{
135 struct btrfs_key key;
136 struct btrfs_disk_key disk_key;
137 struct btrfs_free_space_header *header;
138 struct btrfs_inode_item *inode_item;
139 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400140 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400141 int ret;
142
Li Zefan0414efa2011-04-20 10:20:14 +0800143 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400144 if (ret)
145 return ret;
146
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400147 /* We inline crc's for the free disk space cache */
148 if (ino != BTRFS_FREE_INO_OBJECTID)
149 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
150
Josef Bacik0af3d002010-06-21 14:48:16 -0400151 leaf = path->nodes[0];
152 inode_item = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_inode_item);
154 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100155 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400156 sizeof(*inode_item));
157 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
158 btrfs_set_inode_size(leaf, inode_item, 0);
159 btrfs_set_inode_nbytes(leaf, inode_item, 0);
160 btrfs_set_inode_uid(leaf, inode_item, 0);
161 btrfs_set_inode_gid(leaf, inode_item, 0);
162 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400163 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 btrfs_set_inode_nlink(leaf, inode_item, 1);
165 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800166 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400167 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200168 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169
170 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800171 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400172 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400173 ret = btrfs_insert_empty_item(trans, root, path, &key,
174 sizeof(struct btrfs_free_space_header));
175 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200176 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 return ret;
178 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700179
Josef Bacik0af3d002010-06-21 14:48:16 -0400180 leaf = path->nodes[0];
181 header = btrfs_item_ptr(leaf, path->slots[0],
182 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100183 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400184 btrfs_set_free_space_key(leaf, header, &disk_key);
185 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200186 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400187
188 return 0;
189}
190
David Sterba4ca75f12019-03-20 13:42:57 +0100191int create_free_space_inode(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100192 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +0800193 struct btrfs_path *path)
194{
195 int ret;
196 u64 ino;
197
David Sterba4ca75f12019-03-20 13:42:57 +0100198 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800199 if (ret < 0)
200 return ret;
201
David Sterba4ca75f12019-03-20 13:42:57 +0100202 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200203 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800204}
205
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400206int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000207 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400208{
Josef Bacikc8174312011-11-02 09:29:35 -0400209 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000210 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400211
212 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400213 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
214 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400215
Miao Xie7b61cd92013-05-13 13:55:09 +0000216 spin_lock(&rsv->lock);
217 if (rsv->reserved < needed_bytes)
218 ret = -ENOSPC;
219 else
220 ret = 0;
221 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000222 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000223}
224
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500225int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +0100226 struct btrfs_block_group *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000227 struct inode *inode)
228{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500229 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000230 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100231 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700232
Chris Mason1bbc6212015-04-06 12:46:08 -0700233 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500234 struct btrfs_path *path = btrfs_alloc_path();
235
236 if (!path) {
237 ret = -ENOMEM;
238 goto fail;
239 }
Filipe Manana35c76642015-04-30 17:47:05 +0100240 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700241 mutex_lock(&trans->transaction->cache_write_mutex);
242 if (!list_empty(&block_group->io_list)) {
243 list_del_init(&block_group->io_list);
244
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400245 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700246 btrfs_put_block_group(block_group);
247 }
248
249 /*
250 * now that we've truncated the cache away, its no longer
251 * setup or written
252 */
253 spin_lock(&block_group->lock);
254 block_group->disk_cache_state = BTRFS_DC_CLEAR;
255 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500256 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700257 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400258
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200259 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700260 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400261
262 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700263 * We skip the throttling logic for free space cache inodes, so we don't
264 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400265 */
266 ret = btrfs_truncate_inode_items(trans, root, inode,
267 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100268 if (ret)
269 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400270
Li Zefan82d59022011-04-20 10:33:24 +0800271 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700272
Chris Mason1bbc6212015-04-06 12:46:08 -0700273fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100274 if (locked)
275 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100276 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400277 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400278
Li Zefan82d59022011-04-20 10:33:24 +0800279 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400280}
281
David Sterba1d480532017-01-23 17:28:19 +0100282static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400283{
284 struct file_ra_state *ra;
285 unsigned long last_index;
286
287 ra = kzalloc(sizeof(*ra), GFP_NOFS);
288 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100289 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400290
291 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400293
294 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
295
296 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400297}
298
Chris Mason4c6d1d82015-04-06 13:17:20 -0700299static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400300 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400301{
Miao Xie5349d6c2014-06-19 10:42:49 +0800302 int num_pages;
303 int check_crcs = 0;
304
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300305 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800306
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200307 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800308 check_crcs = 1;
309
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400310 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800311 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400312 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800313 return -ENOSPC;
314
Chris Mason4c6d1d82015-04-06 13:17:20 -0700315 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800316
David Sterba31e818f2015-02-20 18:00:26 +0100317 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400318 if (!io_ctl->pages)
319 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800320
321 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400322 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800323 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700324 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800325
Josef Bacika67509c2011-10-05 15:18:58 -0400326 return 0;
327}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900328ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400329
Chris Mason4c6d1d82015-04-06 13:17:20 -0700330static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400331{
332 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700333 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400334}
335
Chris Mason4c6d1d82015-04-06 13:17:20 -0700336static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400337{
338 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400339 io_ctl->cur = NULL;
340 io_ctl->orig = NULL;
341 }
342}
343
Chris Mason4c6d1d82015-04-06 13:17:20 -0700344static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400345{
Josef Bacikb12d6862013-08-26 17:14:08 -0400346 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400347 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700348 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400349 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300350 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400351 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200352 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400353}
354
Chris Mason4c6d1d82015-04-06 13:17:20 -0700355static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400356{
357 int i;
358
359 io_ctl_unmap_page(io_ctl);
360
361 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800362 if (io_ctl->pages[i]) {
363 ClearPageChecked(io_ctl->pages[i]);
364 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300365 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800366 }
Josef Bacika67509c2011-10-05 15:18:58 -0400367 }
368}
369
Chris Mason4c6d1d82015-04-06 13:17:20 -0700370static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400371 int uptodate)
372{
373 struct page *page;
374 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
375 int i;
376
377 for (i = 0; i < io_ctl->num_pages; i++) {
378 page = find_or_create_page(inode->i_mapping, i, mask);
379 if (!page) {
380 io_ctl_drop_pages(io_ctl);
381 return -ENOMEM;
382 }
383 io_ctl->pages[i] = page;
384 if (uptodate && !PageUptodate(page)) {
385 btrfs_readpage(NULL, page);
386 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400387 if (page->mapping != inode->i_mapping) {
388 btrfs_err(BTRFS_I(inode)->root->fs_info,
389 "free space cache page truncated");
390 io_ctl_drop_pages(io_ctl);
391 return -EIO;
392 }
Josef Bacika67509c2011-10-05 15:18:58 -0400393 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500394 btrfs_err(BTRFS_I(inode)->root->fs_info,
395 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400396 io_ctl_drop_pages(io_ctl);
397 return -EIO;
398 }
399 }
400 }
401
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500402 for (i = 0; i < io_ctl->num_pages; i++) {
403 clear_page_dirty_for_io(io_ctl->pages[i]);
404 set_page_extent_mapped(io_ctl->pages[i]);
405 }
406
Josef Bacika67509c2011-10-05 15:18:58 -0400407 return 0;
408}
409
Chris Mason4c6d1d82015-04-06 13:17:20 -0700410static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400411{
Al Viro528c0322012-04-13 11:03:55 -0400412 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400413
414 io_ctl_map_page(io_ctl, 1);
415
416 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400417 * Skip the csum areas. If we don't check crcs then we just have a
418 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400419 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400420 if (io_ctl->check_crcs) {
421 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
422 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
423 } else {
424 io_ctl->cur += sizeof(u64);
425 io_ctl->size -= sizeof(u64) * 2;
426 }
Josef Bacika67509c2011-10-05 15:18:58 -0400427
428 val = io_ctl->cur;
429 *val = cpu_to_le64(generation);
430 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400431}
432
Chris Mason4c6d1d82015-04-06 13:17:20 -0700433static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400434{
Al Viro528c0322012-04-13 11:03:55 -0400435 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400436
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400437 /*
438 * Skip the crc area. If we don't check crcs then we just have a 64bit
439 * chunk at the front of the first page.
440 */
441 if (io_ctl->check_crcs) {
442 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
443 io_ctl->size -= sizeof(u64) +
444 (sizeof(u32) * io_ctl->num_pages);
445 } else {
446 io_ctl->cur += sizeof(u64);
447 io_ctl->size -= sizeof(u64) * 2;
448 }
Josef Bacika67509c2011-10-05 15:18:58 -0400449
Josef Bacika67509c2011-10-05 15:18:58 -0400450 gen = io_ctl->cur;
451 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400452 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200453 "space cache generation (%llu) does not match inode (%llu)",
454 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400455 io_ctl_unmap_page(io_ctl);
456 return -EIO;
457 }
458 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400459 return 0;
460}
461
Chris Mason4c6d1d82015-04-06 13:17:20 -0700462static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400463{
464 u32 *tmp;
465 u32 crc = ~(u32)0;
466 unsigned offset = 0;
467
468 if (!io_ctl->check_crcs) {
469 io_ctl_unmap_page(io_ctl);
470 return;
471 }
472
473 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800474 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400475
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200476 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
477 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400478 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700479 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400480 tmp += index;
481 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400482}
483
Chris Mason4c6d1d82015-04-06 13:17:20 -0700484static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400485{
486 u32 *tmp, val;
487 u32 crc = ~(u32)0;
488 unsigned offset = 0;
489
490 if (!io_ctl->check_crcs) {
491 io_ctl_map_page(io_ctl, 0);
492 return 0;
493 }
494
495 if (index == 0)
496 offset = sizeof(u32) * io_ctl->num_pages;
497
Chris Mason2b108262015-04-06 07:48:20 -0700498 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400499 tmp += index;
500 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400501
502 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200503 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
504 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400506 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200507 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400508 io_ctl_unmap_page(io_ctl);
509 return -EIO;
510 }
511
Josef Bacika67509c2011-10-05 15:18:58 -0400512 return 0;
513}
514
Chris Mason4c6d1d82015-04-06 13:17:20 -0700515static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400516 void *bitmap)
517{
518 struct btrfs_free_space_entry *entry;
519
520 if (!io_ctl->cur)
521 return -ENOSPC;
522
523 entry = io_ctl->cur;
524 entry->offset = cpu_to_le64(offset);
525 entry->bytes = cpu_to_le64(bytes);
526 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
527 BTRFS_FREE_SPACE_EXTENT;
528 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
529 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
530
531 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
532 return 0;
533
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400534 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400535
536 /* No more pages to map */
537 if (io_ctl->index >= io_ctl->num_pages)
538 return 0;
539
540 /* map the next page */
541 io_ctl_map_page(io_ctl, 1);
542 return 0;
543}
544
Chris Mason4c6d1d82015-04-06 13:17:20 -0700545static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400546{
547 if (!io_ctl->cur)
548 return -ENOSPC;
549
550 /*
551 * If we aren't at the start of the current page, unmap this one and
552 * map the next one if there is any left.
553 */
554 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400555 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400556 if (io_ctl->index >= io_ctl->num_pages)
557 return -ENOSPC;
558 io_ctl_map_page(io_ctl, 0);
559 }
560
David Sterba69d24802018-06-29 10:56:44 +0200561 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400562 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400563 if (io_ctl->index < io_ctl->num_pages)
564 io_ctl_map_page(io_ctl, 0);
565 return 0;
566}
567
Chris Mason4c6d1d82015-04-06 13:17:20 -0700568static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400569{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400570 /*
571 * If we're not on the boundary we know we've modified the page and we
572 * need to crc the page.
573 */
574 if (io_ctl->cur != io_ctl->orig)
575 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
576 else
577 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400578
579 while (io_ctl->index < io_ctl->num_pages) {
580 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400581 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400582 }
583}
584
Chris Mason4c6d1d82015-04-06 13:17:20 -0700585static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400586 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400587{
588 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500589 int ret;
590
591 if (!io_ctl->cur) {
592 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
593 if (ret)
594 return ret;
595 }
Josef Bacika67509c2011-10-05 15:18:58 -0400596
597 e = io_ctl->cur;
598 entry->offset = le64_to_cpu(e->offset);
599 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400600 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400601 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
602 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
603
604 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400605 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400606
607 io_ctl_unmap_page(io_ctl);
608
Josef Bacik2f120c02011-11-10 20:45:05 -0500609 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400610}
611
Chris Mason4c6d1d82015-04-06 13:17:20 -0700612static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400613 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400614{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400615 int ret;
616
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400617 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
618 if (ret)
619 return ret;
620
David Sterba69d24802018-06-29 10:56:44 +0200621 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400622 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400623
624 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400625}
626
Josef Bacikcd023e72012-05-14 10:06:40 -0400627/*
628 * Since we attach pinned extents after the fact we can have contiguous sections
629 * of free space that are split up in entries. This poses a problem with the
630 * tree logging stuff since it could have allocated across what appears to be 2
631 * entries since we would have merged the entries when adding the pinned extents
632 * back to the free space cache. So run through the space cache that we just
633 * loaded and merge contiguous entries. This will make the log replay stuff not
634 * blow up and it will make for nicer allocator behavior.
635 */
636static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
637{
638 struct btrfs_free_space *e, *prev = NULL;
639 struct rb_node *n;
640
641again:
642 spin_lock(&ctl->tree_lock);
643 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
644 e = rb_entry(n, struct btrfs_free_space, offset_index);
645 if (!prev)
646 goto next;
647 if (e->bitmap || prev->bitmap)
648 goto next;
649 if (prev->offset + prev->bytes == e->offset) {
650 unlink_free_space(ctl, prev);
651 unlink_free_space(ctl, e);
652 prev->bytes += e->bytes;
653 kmem_cache_free(btrfs_free_space_cachep, e);
654 link_free_space(ctl, prev);
655 prev = NULL;
656 spin_unlock(&ctl->tree_lock);
657 goto again;
658 }
659next:
660 prev = e;
661 }
662 spin_unlock(&ctl->tree_lock);
663}
664
Eric Sandeen48a3b632013-04-25 20:41:01 +0000665static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
666 struct btrfs_free_space_ctl *ctl,
667 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400668{
David Sterba3ffbd682018-06-29 10:56:42 +0200669 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 struct btrfs_free_space_header *header;
671 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700672 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400673 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400674 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800675 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400676 u64 num_entries;
677 u64 num_bitmaps;
678 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400679 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400680 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400681
Josef Bacik9d66e232010-08-25 16:54:15 -0400682 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800683 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400684 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685
686 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800687 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400688 key.type = 0;
689
690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800691 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400692 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800693 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400694 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400695 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400696 }
697
Li Zefan0414efa2011-04-20 10:20:14 +0800698 ret = -1;
699
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 leaf = path->nodes[0];
701 header = btrfs_item_ptr(leaf, path->slots[0],
702 struct btrfs_free_space_header);
703 num_entries = btrfs_free_space_entries(leaf, header);
704 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
705 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400706 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400707
Miao Xiee570fd22014-06-19 10:42:50 +0800708 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400709 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200710 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800711 offset);
712 return 0;
713 }
714
Josef Bacik9d66e232010-08-25 16:54:15 -0400715 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 btrfs_err(fs_info,
717 "free space inode generation (%llu) did not match free space cache generation (%llu)",
718 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400719 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 }
721
722 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400723 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400724
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400725 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800726 if (ret)
727 return ret;
728
David Sterba1d480532017-01-23 17:28:19 +0100729 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400730
Josef Bacika67509c2011-10-05 15:18:58 -0400731 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
732 if (ret)
733 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400734
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400735 ret = io_ctl_check_crc(&io_ctl, 0);
736 if (ret)
737 goto free_cache;
738
Josef Bacika67509c2011-10-05 15:18:58 -0400739 ret = io_ctl_check_generation(&io_ctl, generation);
740 if (ret)
741 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400742
Josef Bacika67509c2011-10-05 15:18:58 -0400743 while (num_entries) {
744 e = kmem_cache_zalloc(btrfs_free_space_cachep,
745 GFP_NOFS);
746 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400747 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400748
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400749 ret = io_ctl_read_entry(&io_ctl, e, &type);
750 if (ret) {
751 kmem_cache_free(btrfs_free_space_cachep, e);
752 goto free_cache;
753 }
754
Josef Bacika67509c2011-10-05 15:18:58 -0400755 if (!e->bytes) {
756 kmem_cache_free(btrfs_free_space_cachep, e);
757 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400758 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400759
Josef Bacika67509c2011-10-05 15:18:58 -0400760 if (type == BTRFS_FREE_SPACE_EXTENT) {
761 spin_lock(&ctl->tree_lock);
762 ret = link_free_space(ctl, e);
763 spin_unlock(&ctl->tree_lock);
764 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400765 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000766 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500767 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400768 goto free_cache;
769 }
Josef Bacika67509c2011-10-05 15:18:58 -0400770 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400771 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400772 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000773 e->bitmap = kmem_cache_zalloc(
774 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400775 if (!e->bitmap) {
776 kmem_cache_free(
777 btrfs_free_space_cachep, e);
778 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400779 }
Josef Bacika67509c2011-10-05 15:18:58 -0400780 spin_lock(&ctl->tree_lock);
781 ret = link_free_space(ctl, e);
782 ctl->total_bitmaps++;
783 ctl->op->recalc_thresholds(ctl);
784 spin_unlock(&ctl->tree_lock);
785 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400786 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000787 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400788 kmem_cache_free(btrfs_free_space_cachep, e);
789 goto free_cache;
790 }
791 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400792 }
793
Josef Bacika67509c2011-10-05 15:18:58 -0400794 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400795 }
796
Josef Bacik2f120c02011-11-10 20:45:05 -0500797 io_ctl_unmap_page(&io_ctl);
798
Josef Bacika67509c2011-10-05 15:18:58 -0400799 /*
800 * We add the bitmaps at the end of the entries in order that
801 * the bitmap entries are added to the cache.
802 */
803 list_for_each_entry_safe(e, n, &bitmaps, list) {
804 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400805 ret = io_ctl_read_bitmap(&io_ctl, e);
806 if (ret)
807 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400808 }
809
810 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400811 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400812 ret = 1;
813out:
Josef Bacika67509c2011-10-05 15:18:58 -0400814 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400815 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400816free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400817 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800818 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400819 goto out;
820}
821
David Sterba32da53862019-10-29 19:20:18 +0100822int load_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400823{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100824 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800825 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800826 struct inode *inode;
827 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400828 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800829 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200830 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800831
832 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800833 * If this block group has been marked to be cleared for one reason or
834 * another then we can't trust the on disk cache, so just return.
835 */
836 spin_lock(&block_group->lock);
837 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
838 spin_unlock(&block_group->lock);
839 return 0;
840 }
841 spin_unlock(&block_group->lock);
842
843 path = btrfs_alloc_path();
844 if (!path)
845 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400846 path->search_commit_root = 1;
847 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800848
Filipe Manana4222ea72018-10-24 10:13:03 +0100849 /*
850 * We must pass a path with search_commit_root set to btrfs_iget in
851 * order to avoid a deadlock when allocating extents for the tree root.
852 *
853 * When we are COWing an extent buffer from the tree root, when looking
854 * for a free extent, at extent-tree.c:find_free_extent(), we can find
855 * block group without its free space cache loaded. When we find one
856 * we must load its space cache which requires reading its free space
857 * cache's inode item from the root tree. If this inode item is located
858 * in the same leaf that we started COWing before, then we end up in
859 * deadlock on the extent buffer (trying to read lock it when we
860 * previously write locked it).
861 *
862 * It's safe to read the inode item using the commit root because
863 * block groups, once loaded, stay in memory forever (until they are
864 * removed) as well as their space caches once loaded. New block groups
865 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
866 * we will never try to read their inode item while the fs is mounted.
867 */
David Sterba7949f332019-03-20 13:40:19 +0100868 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800869 if (IS_ERR(inode)) {
870 btrfs_free_path(path);
871 return 0;
872 }
873
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400874 /* We may have converted the inode and made the cache invalid. */
875 spin_lock(&block_group->lock);
876 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
877 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900878 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400879 goto out;
880 }
881 spin_unlock(&block_group->lock);
882
Li Zefan0414efa2011-04-20 10:20:14 +0800883 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200884 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800885 btrfs_free_path(path);
886 if (ret <= 0)
887 goto out;
888
889 spin_lock(&ctl->tree_lock);
David Sterbab3470b52019-10-23 18:48:22 +0200890 matched = (ctl->free_space == (block_group->length - used -
Li Zefan0414efa2011-04-20 10:20:14 +0800891 block_group->bytes_super));
892 spin_unlock(&ctl->tree_lock);
893
894 if (!matched) {
895 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400896 btrfs_warn(fs_info,
897 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +0200898 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800899 ret = -1;
900 }
901out:
902 if (ret < 0) {
903 /* This cache is bogus, make sure it gets cleared */
904 spin_lock(&block_group->lock);
905 block_group->disk_cache_state = BTRFS_DC_CLEAR;
906 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800907 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800908
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400909 btrfs_warn(fs_info,
910 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +0200911 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800912 }
913
914 iput(inode);
915 return ret;
916}
917
Chris Masond4452bc2014-05-19 20:47:56 -0700918static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700919int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700920 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +0100921 struct btrfs_block_group *block_group,
Chris Masond4452bc2014-05-19 20:47:56 -0700922 int *entries, int *bitmaps,
923 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400924{
Josef Bacikc09544e2011-08-30 10:19:10 -0400925 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700926 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700927 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700928 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000929 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400930
Josef Bacik43be2142011-04-01 14:55:00 +0000931 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700932 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000933 cluster = list_entry(block_group->cluster_list.next,
934 struct btrfs_free_cluster,
935 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700936 }
Josef Bacik43be2142011-04-01 14:55:00 +0000937
Josef Bacikf75b1302011-10-05 10:00:18 -0400938 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700939 cluster_locked = cluster;
940 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400941 node = rb_first(&cluster->root);
942 cluster = NULL;
943 }
944
Josef Bacik0cb59c92010-07-02 12:14:14 -0400945 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400946 while (node) {
947 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400948
Josef Bacika67509c2011-10-05 15:18:58 -0400949 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700950 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000951
Chris Masond4452bc2014-05-19 20:47:56 -0700952 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400953 e->bitmap);
954 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700955 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400956
957 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700958 list_add_tail(&e->list, bitmap_list);
959 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400960 }
961 node = rb_next(node);
962 if (!node && cluster) {
963 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700964 cluster_locked = cluster;
965 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400966 cluster = NULL;
967 }
968 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700969 if (cluster_locked) {
970 spin_unlock(&cluster_locked->lock);
971 cluster_locked = NULL;
972 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000973
974 /*
975 * Make sure we don't miss any range that was removed from our rbtree
976 * because trimming is running. Otherwise after a umount+mount (or crash
977 * after committing the transaction) we would leak free space and get
978 * an inconsistent free space cache report from fsck.
979 */
980 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
981 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
982 trim_entry->bytes, NULL);
983 if (ret)
984 goto fail;
985 *entries += 1;
986 }
987
Chris Masond4452bc2014-05-19 20:47:56 -0700988 return 0;
989fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700990 if (cluster_locked)
991 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700992 return -ENOSPC;
993}
994
995static noinline_for_stack int
996update_cache_item(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
998 struct inode *inode,
999 struct btrfs_path *path, u64 offset,
1000 int entries, int bitmaps)
1001{
1002 struct btrfs_key key;
1003 struct btrfs_free_space_header *header;
1004 struct extent_buffer *leaf;
1005 int ret;
1006
1007 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1008 key.offset = offset;
1009 key.type = 0;
1010
1011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1012 if (ret < 0) {
1013 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001014 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001015 goto fail;
1016 }
1017 leaf = path->nodes[0];
1018 if (ret > 0) {
1019 struct btrfs_key found_key;
1020 ASSERT(path->slots[0]);
1021 path->slots[0]--;
1022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1023 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1024 found_key.offset != offset) {
1025 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001026 inode->i_size - 1, EXTENT_DELALLOC, 0,
1027 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001028 btrfs_release_path(path);
1029 goto fail;
1030 }
1031 }
1032
1033 BTRFS_I(inode)->generation = trans->transid;
1034 header = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_free_space_header);
1036 btrfs_set_free_space_entries(leaf, header, entries);
1037 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1038 btrfs_set_free_space_generation(leaf, header, trans->transid);
1039 btrfs_mark_buffer_dirty(leaf);
1040 btrfs_release_path(path);
1041
1042 return 0;
1043
1044fail:
1045 return -1;
1046}
1047
David Sterba6701bdb2019-03-20 13:49:09 +01001048static noinline_for_stack int write_pinned_extent_entries(
David Sterba32da53862019-10-29 19:20:18 +01001049 struct btrfs_block_group *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001050 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001051 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001052{
1053 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001054 struct extent_io_tree *unpin = NULL;
1055 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001056
Miao Xie5349d6c2014-06-19 10:42:49 +08001057 if (!block_group)
1058 return 0;
1059
Josef Bacika67509c2011-10-05 15:18:58 -04001060 /*
1061 * We want to add any pinned extents to our free space cache
1062 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001063 *
Li Zefandb804f22012-01-10 16:41:01 +08001064 * We shouldn't have switched the pinned extents yet so this is the
1065 * right one
1066 */
David Sterba6701bdb2019-03-20 13:49:09 +01001067 unpin = block_group->fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001068
David Sterbab3470b52019-10-23 18:48:22 +02001069 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001070
David Sterbab3470b52019-10-23 18:48:22 +02001071 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001072 ret = find_first_extent_bit(unpin, start,
1073 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001074 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001075 if (ret)
1076 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001077
Josef Bacika67509c2011-10-05 15:18:58 -04001078 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001079 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001080 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001081
Li Zefandb804f22012-01-10 16:41:01 +08001082 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001083 extent_end = min(block_group->start + block_group->length,
1084 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001085 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001086
Chris Masond4452bc2014-05-19 20:47:56 -07001087 *entries += 1;
1088 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001089 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001090 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001091
Li Zefandb804f22012-01-10 16:41:01 +08001092 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001093 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001094
Miao Xie5349d6c2014-06-19 10:42:49 +08001095 return 0;
1096}
1097
1098static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001099write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001100{
Geliang Tang7ae16812015-12-18 22:17:00 +08001101 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001102 int ret;
1103
Josef Bacik0cb59c92010-07-02 12:14:14 -04001104 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001105 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001106 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001107 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001108 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001109 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001110 }
1111
Miao Xie5349d6c2014-06-19 10:42:49 +08001112 return 0;
1113}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001114
Miao Xie5349d6c2014-06-19 10:42:49 +08001115static int flush_dirty_cache(struct inode *inode)
1116{
1117 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001118
Josef Bacik0ef8b722013-10-25 16:13:35 -04001119 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001120 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001121 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001122 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001123
Miao Xie5349d6c2014-06-19 10:42:49 +08001124 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001125}
1126
1127static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001128cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001129{
Geliang Tang7ae16812015-12-18 22:17:00 +08001130 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001131
Geliang Tang7ae16812015-12-18 22:17:00 +08001132 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001133 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001134}
1135
1136static void noinline_for_stack
1137cleanup_write_cache_enospc(struct inode *inode,
1138 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001139 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001140{
Chris Masond4452bc2014-05-19 20:47:56 -07001141 io_ctl_drop_pages(io_ctl);
1142 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001143 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001144}
1145
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001146static int __btrfs_wait_cache_io(struct btrfs_root *root,
1147 struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001148 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001149 struct btrfs_io_ctl *io_ctl,
1150 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001151{
1152 int ret;
1153 struct inode *inode = io_ctl->inode;
1154
Chris Mason1bbc6212015-04-06 12:46:08 -07001155 if (!inode)
1156 return 0;
1157
Chris Masonc9dc4c62015-04-04 17:14:42 -07001158 /* Flush the dirty pages in the cache file. */
1159 ret = flush_dirty_cache(inode);
1160 if (ret)
1161 goto out;
1162
1163 /* Update the cache item to tell everyone this cache file is valid. */
1164 ret = update_cache_item(trans, root, inode, path, offset,
1165 io_ctl->entries, io_ctl->bitmaps);
1166out:
1167 io_ctl_free(io_ctl);
1168 if (ret) {
1169 invalidate_inode_pages2(inode->i_mapping);
1170 BTRFS_I(inode)->generation = 0;
1171 if (block_group) {
1172#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001173 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001174 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001175 block_group->start);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001176#endif
1177 }
1178 }
1179 btrfs_update_inode(trans, root, inode);
1180
1181 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001182 /* the dirty list is protected by the dirty_bgs_lock */
1183 spin_lock(&trans->transaction->dirty_bgs_lock);
1184
1185 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001186 spin_lock(&block_group->lock);
1187
1188 /*
1189 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001190 * the dirty list while waiting for IO. Otherwise our
1191 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001192 */
1193 if (!ret && list_empty(&block_group->dirty_list))
1194 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1195 else if (ret)
1196 block_group->disk_cache_state = BTRFS_DC_ERROR;
1197
1198 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001199 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001200 io_ctl->inode = NULL;
1201 iput(inode);
1202 }
1203
1204 return ret;
1205
1206}
1207
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001208static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1209 struct btrfs_trans_handle *trans,
1210 struct btrfs_io_ctl *io_ctl,
1211 struct btrfs_path *path)
1212{
1213 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1214}
1215
1216int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001217 struct btrfs_block_group *block_group,
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001218 struct btrfs_path *path)
1219{
1220 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1221 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001222 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001223}
1224
Chris Masond4452bc2014-05-19 20:47:56 -07001225/**
1226 * __btrfs_write_out_cache - write out cached info to an inode
1227 * @root - the root the inode belongs to
1228 * @ctl - the free space cache we are going to write out
1229 * @block_group - the block_group for this cache if it belongs to a block_group
1230 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001231 *
1232 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001233 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001234 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001235 */
1236static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1237 struct btrfs_free_space_ctl *ctl,
David Sterba32da53862019-10-29 19:20:18 +01001238 struct btrfs_block_group *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001239 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001240 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001241{
1242 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001243 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001244 int entries = 0;
1245 int bitmaps = 0;
1246 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001247 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001248
1249 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001250 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001251
Chris Masonc9dc4c62015-04-04 17:14:42 -07001252 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001253 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001254 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001255 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001256
Miao Xiee570fd22014-06-19 10:42:50 +08001257 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1258 down_write(&block_group->data_rwsem);
1259 spin_lock(&block_group->lock);
1260 if (block_group->delalloc_bytes) {
1261 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1262 spin_unlock(&block_group->lock);
1263 up_write(&block_group->data_rwsem);
1264 BTRFS_I(inode)->generation = 0;
1265 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001266 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001267 goto out;
1268 }
1269 spin_unlock(&block_group->lock);
1270 }
1271
Chris Masond4452bc2014-05-19 20:47:56 -07001272 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001273 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1274 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001275 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001276
1277 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001278 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001279
Chris Masonc9dc4c62015-04-04 17:14:42 -07001280 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001281
Filipe Manana55507ce2014-12-01 17:04:09 +00001282 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001283 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001284 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001285 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001286 block_group, &entries, &bitmaps,
1287 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001288 if (ret)
1289 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001290
Miao Xie5349d6c2014-06-19 10:42:49 +08001291 /*
1292 * Some spaces that are freed in the current transaction are pinned,
1293 * they will be added into free space cache after the transaction is
1294 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001295 *
1296 * If this changes while we are working we'll get added back to
1297 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001298 */
David Sterba6701bdb2019-03-20 13:49:09 +01001299 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001300 if (ret)
1301 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001302
Filipe Manana55507ce2014-12-01 17:04:09 +00001303 /*
1304 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1305 * locked while doing it because a concurrent trim can be manipulating
1306 * or freeing the bitmap.
1307 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001308 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001309 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001310 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001311 if (ret)
1312 goto out_nospc;
1313
1314 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001315 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001316
1317 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001318 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1319 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001320 if (ret)
1321 goto out_nospc;
1322
Miao Xiee570fd22014-06-19 10:42:50 +08001323 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1324 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001325 /*
1326 * Release the pages and unlock the extent, we will flush
1327 * them out later
1328 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001329 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001330
1331 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001332 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001333
Chris Masonc9dc4c62015-04-04 17:14:42 -07001334 /*
1335 * at this point the pages are under IO and we're happy,
1336 * The caller is responsible for waiting on them and updating the
1337 * the cache and the inode
1338 */
1339 io_ctl->entries = entries;
1340 io_ctl->bitmaps = bitmaps;
1341
1342 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001343 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001344 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001345
Chris Masonc9dc4c62015-04-04 17:14:42 -07001346 return 0;
1347
Josef Bacik2f356122011-06-10 15:31:13 -04001348out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001349 io_ctl->inode = NULL;
1350 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001351 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001352 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001353 BTRFS_I(inode)->generation = 0;
1354 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001355 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001356 if (must_iput)
1357 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001358 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001359
Chris Masona3bdccc2015-04-24 11:00:00 -07001360out_nospc_locked:
1361 cleanup_bitmap_list(&bitmap_list);
1362 spin_unlock(&ctl->tree_lock);
1363 mutex_unlock(&ctl->cache_writeout_mutex);
1364
Josef Bacika67509c2011-10-05 15:18:58 -04001365out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001366 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001367
Josef Bacikb77000e2017-11-15 16:20:52 -05001368out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001369 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1370 up_write(&block_group->data_rwsem);
1371
Josef Bacika67509c2011-10-05 15:18:58 -04001372 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001373}
1374
David Sterbafe041532019-03-20 13:51:56 +01001375int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
David Sterba32da53862019-10-29 19:20:18 +01001376 struct btrfs_block_group *block_group,
Li Zefan0414efa2011-04-20 10:20:14 +08001377 struct btrfs_path *path)
1378{
David Sterbafe041532019-03-20 13:51:56 +01001379 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001380 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1381 struct inode *inode;
1382 int ret = 0;
1383
Li Zefan0414efa2011-04-20 10:20:14 +08001384 spin_lock(&block_group->lock);
1385 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1386 spin_unlock(&block_group->lock);
1387 return 0;
1388 }
1389 spin_unlock(&block_group->lock);
1390
David Sterba7949f332019-03-20 13:40:19 +01001391 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001392 if (IS_ERR(inode))
1393 return 0;
1394
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001395 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1396 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001397 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001398#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001399 btrfs_err(fs_info,
1400 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001401 block_group->start);
Josef Bacikc09544e2011-08-30 10:19:10 -04001402#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001403 spin_lock(&block_group->lock);
1404 block_group->disk_cache_state = BTRFS_DC_ERROR;
1405 spin_unlock(&block_group->lock);
1406
1407 block_group->io_ctl.inode = NULL;
1408 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001409 }
1410
Chris Masonc9dc4c62015-04-04 17:14:42 -07001411 /*
1412 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1413 * to wait for IO and put the inode
1414 */
1415
Josef Bacik0cb59c92010-07-02 12:14:14 -04001416 return ret;
1417}
1418
Li Zefan34d52cb2011-03-29 13:46:06 +08001419static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001420 u64 offset)
1421{
Josef Bacikb12d6862013-08-26 17:14:08 -04001422 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001423 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001424 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001425}
1426
Li Zefan34d52cb2011-03-29 13:46:06 +08001427static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001428{
Li Zefan34d52cb2011-03-29 13:46:06 +08001429 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001430}
1431
Li Zefan34d52cb2011-03-29 13:46:06 +08001432static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001433 u64 offset)
1434{
1435 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001436 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001437
Li Zefan34d52cb2011-03-29 13:46:06 +08001438 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1439 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001440 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001441 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001442 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001443
1444 return bitmap_start;
1445}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001446
1447static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001448 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001449{
1450 struct rb_node **p = &root->rb_node;
1451 struct rb_node *parent = NULL;
1452 struct btrfs_free_space *info;
1453
1454 while (*p) {
1455 parent = *p;
1456 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1457
Josef Bacik96303082009-07-13 21:29:25 -04001458 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001459 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001460 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001461 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001462 } else {
1463 /*
1464 * we could have a bitmap entry and an extent entry
1465 * share the same offset. If this is the case, we want
1466 * the extent entry to always be found first if we do a
1467 * linear search through the tree, since we want to have
1468 * the quickest allocation time, and allocating from an
1469 * extent is faster than allocating from a bitmap. So
1470 * if we're inserting a bitmap and we find an entry at
1471 * this offset, we want to go right, or after this entry
1472 * logically. If we are inserting an extent and we've
1473 * found a bitmap, we want to go left, or before
1474 * logically.
1475 */
1476 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001477 if (info->bitmap) {
1478 WARN_ON_ONCE(1);
1479 return -EEXIST;
1480 }
Josef Bacik96303082009-07-13 21:29:25 -04001481 p = &(*p)->rb_right;
1482 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001483 if (!info->bitmap) {
1484 WARN_ON_ONCE(1);
1485 return -EEXIST;
1486 }
Josef Bacik96303082009-07-13 21:29:25 -04001487 p = &(*p)->rb_left;
1488 }
1489 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001490 }
1491
1492 rb_link_node(node, parent, p);
1493 rb_insert_color(node, root);
1494
1495 return 0;
1496}
1497
1498/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001499 * searches the tree for the given offset.
1500 *
Josef Bacik96303082009-07-13 21:29:25 -04001501 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1502 * want a section that has at least bytes size and comes at or after the given
1503 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001504 */
Josef Bacik96303082009-07-13 21:29:25 -04001505static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001506tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001507 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001508{
Li Zefan34d52cb2011-03-29 13:46:06 +08001509 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001510 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001511
Josef Bacik96303082009-07-13 21:29:25 -04001512 /* find entry that is closest to the 'offset' */
1513 while (1) {
1514 if (!n) {
1515 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001516 break;
1517 }
Josef Bacik96303082009-07-13 21:29:25 -04001518
1519 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1520 prev = entry;
1521
1522 if (offset < entry->offset)
1523 n = n->rb_left;
1524 else if (offset > entry->offset)
1525 n = n->rb_right;
1526 else
1527 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001528 }
1529
Josef Bacik96303082009-07-13 21:29:25 -04001530 if (bitmap_only) {
1531 if (!entry)
1532 return NULL;
1533 if (entry->bitmap)
1534 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001535
Josef Bacik96303082009-07-13 21:29:25 -04001536 /*
1537 * bitmap entry and extent entry may share same offset,
1538 * in that case, bitmap entry comes after extent entry.
1539 */
1540 n = rb_next(n);
1541 if (!n)
1542 return NULL;
1543 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1544 if (entry->offset != offset)
1545 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001546
Josef Bacik96303082009-07-13 21:29:25 -04001547 WARN_ON(!entry->bitmap);
1548 return entry;
1549 } else if (entry) {
1550 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001551 /*
Josef Bacik96303082009-07-13 21:29:25 -04001552 * if previous extent entry covers the offset,
1553 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001554 */
Miao Xiede6c4112012-10-18 08:18:01 +00001555 n = rb_prev(&entry->offset_index);
1556 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001557 prev = rb_entry(n, struct btrfs_free_space,
1558 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001559 if (!prev->bitmap &&
1560 prev->offset + prev->bytes > offset)
1561 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001562 }
Josef Bacik96303082009-07-13 21:29:25 -04001563 }
1564 return entry;
1565 }
1566
1567 if (!prev)
1568 return NULL;
1569
1570 /* find last entry before the 'offset' */
1571 entry = prev;
1572 if (entry->offset > offset) {
1573 n = rb_prev(&entry->offset_index);
1574 if (n) {
1575 entry = rb_entry(n, struct btrfs_free_space,
1576 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001577 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001578 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001579 if (fuzzy)
1580 return entry;
1581 else
1582 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001583 }
1584 }
1585
Josef Bacik96303082009-07-13 21:29:25 -04001586 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001587 n = rb_prev(&entry->offset_index);
1588 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001589 prev = rb_entry(n, struct btrfs_free_space,
1590 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001591 if (!prev->bitmap &&
1592 prev->offset + prev->bytes > offset)
1593 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001594 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001595 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001596 return entry;
1597 } else if (entry->offset + entry->bytes > offset)
1598 return entry;
1599
1600 if (!fuzzy)
1601 return NULL;
1602
1603 while (1) {
1604 if (entry->bitmap) {
1605 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001606 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001607 break;
1608 } else {
1609 if (entry->offset + entry->bytes > offset)
1610 break;
1611 }
1612
1613 n = rb_next(&entry->offset_index);
1614 if (!n)
1615 return NULL;
1616 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1617 }
1618 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001619}
1620
Li Zefanf333adb2010-11-09 14:57:39 +08001621static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001622__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001623 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001624{
Li Zefan34d52cb2011-03-29 13:46:06 +08001625 rb_erase(&info->offset_index, &ctl->free_space_offset);
1626 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001627}
1628
Li Zefan34d52cb2011-03-29 13:46:06 +08001629static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001630 struct btrfs_free_space *info)
1631{
Li Zefan34d52cb2011-03-29 13:46:06 +08001632 __unlink_free_space(ctl, info);
1633 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001634}
1635
Li Zefan34d52cb2011-03-29 13:46:06 +08001636static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001637 struct btrfs_free_space *info)
1638{
1639 int ret = 0;
1640
Josef Bacikb12d6862013-08-26 17:14:08 -04001641 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001642 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001643 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001644 if (ret)
1645 return ret;
1646
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 ctl->free_space += info->bytes;
1648 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001649 return ret;
1650}
1651
Li Zefan34d52cb2011-03-29 13:46:06 +08001652static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001653{
David Sterba32da53862019-10-29 19:20:18 +01001654 struct btrfs_block_group *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001655 u64 max_bytes;
1656 u64 bitmap_bytes;
1657 u64 extent_bytes;
David Sterbab3470b52019-10-23 18:48:22 +02001658 u64 size = block_group->length;
Feifei Xu0ef64472016-06-01 19:18:24 +08001659 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1660 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001661
Feifei Xu0ef64472016-06-01 19:18:24 +08001662 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001663
Josef Bacikb12d6862013-08-26 17:14:08 -04001664 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001665
1666 /*
1667 * The goal is to keep the total amount of memory used per 1gb of space
1668 * at or below 32k, so we need to adjust how much memory we allow to be
1669 * used by extent based free space tracking
1670 */
Byongho Leeee221842015-12-15 01:42:10 +09001671 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001672 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1673 else
Byongho Leeee221842015-12-15 01:42:10 +09001674 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001675
Josef Bacik25891f72009-09-11 16:11:20 -04001676 /*
1677 * we want to account for 1 more bitmap than what we have so we can make
1678 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1679 * we add more bitmaps.
1680 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001681 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001682
Josef Bacik25891f72009-09-11 16:11:20 -04001683 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001684 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001685 return;
Josef Bacik96303082009-07-13 21:29:25 -04001686 }
Josef Bacik25891f72009-09-11 16:11:20 -04001687
1688 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001689 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001690 * bytes we can have, or whatever is less than that.
1691 */
1692 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001693 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001694
Li Zefan34d52cb2011-03-29 13:46:06 +08001695 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001696 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001697}
1698
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001699static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1700 struct btrfs_free_space *info,
1701 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001702{
Li Zefanf38b6e72011-03-14 13:40:51 +08001703 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001704
Li Zefan34d52cb2011-03-29 13:46:06 +08001705 start = offset_to_bit(info->offset, ctl->unit, offset);
1706 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001707 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001708
Li Zefanf38b6e72011-03-14 13:40:51 +08001709 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001710
1711 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001712 if (info->max_extent_size > ctl->unit)
1713 info->max_extent_size = 0;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001714}
1715
1716static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1717 struct btrfs_free_space *info, u64 offset,
1718 u64 bytes)
1719{
1720 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001721 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001722}
1723
Li Zefan34d52cb2011-03-29 13:46:06 +08001724static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001725 struct btrfs_free_space *info, u64 offset,
1726 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001727{
Li Zefanf38b6e72011-03-14 13:40:51 +08001728 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001729
Li Zefan34d52cb2011-03-29 13:46:06 +08001730 start = offset_to_bit(info->offset, ctl->unit, offset);
1731 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001732 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001733
Li Zefanf38b6e72011-03-14 13:40:51 +08001734 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001735
1736 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001737 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001738}
1739
Miao Xiea4820392013-09-09 13:19:42 +08001740/*
1741 * If we can not find suitable extent, we will use bytes to record
1742 * the size of the max extent.
1743 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001744static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001745 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001746 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001747{
1748 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001749 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001750 unsigned long bits, i;
1751 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001752 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001753
Josef Bacikcef40482015-10-02 16:09:42 -04001754 /*
1755 * Skip searching the bitmap if we don't have a contiguous section that
1756 * is large enough for this allocation.
1757 */
Josef Bacik0584f712015-10-02 16:12:23 -04001758 if (for_alloc &&
1759 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001760 bitmap_info->max_extent_size < *bytes) {
1761 *bytes = bitmap_info->max_extent_size;
1762 return -1;
1763 }
1764
Li Zefan34d52cb2011-03-29 13:46:06 +08001765 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001766 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001767 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001768
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001769 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001770 if (for_alloc && bits == 1) {
1771 found_bits = 1;
1772 break;
1773 }
Josef Bacik96303082009-07-13 21:29:25 -04001774 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1775 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001776 extent_bits = next_zero - i;
1777 if (extent_bits >= bits) {
1778 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001779 break;
Miao Xiea4820392013-09-09 13:19:42 +08001780 } else if (extent_bits > max_bits) {
1781 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001782 }
1783 i = next_zero;
1784 }
1785
1786 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001787 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1788 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001789 return 0;
1790 }
1791
Miao Xiea4820392013-09-09 13:19:42 +08001792 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001793 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001794 return -1;
1795}
1796
Josef Bacikad22cf62018-10-12 15:32:33 -04001797static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1798{
1799 if (entry->bitmap)
1800 return entry->max_extent_size;
1801 return entry->bytes;
1802}
1803
Miao Xiea4820392013-09-09 13:19:42 +08001804/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001805static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001806find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001807 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001808{
1809 struct btrfs_free_space *entry;
1810 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001811 u64 tmp;
1812 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001813 int ret;
1814
Li Zefan34d52cb2011-03-29 13:46:06 +08001815 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001816 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001817
Li Zefan34d52cb2011-03-29 13:46:06 +08001818 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001819 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001820 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001821
1822 for (node = &entry->offset_index; node; node = rb_next(node)) {
1823 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001824 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001825 *max_extent_size = max(get_max_extent_size(entry),
1826 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001827 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001828 }
Josef Bacik96303082009-07-13 21:29:25 -04001829
David Woodhouse53b381b2013-01-29 18:40:14 -05001830 /* make sure the space returned is big enough
1831 * to match our requested alignment
1832 */
1833 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001834 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001835 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001836 tmp = tmp * align + ctl->start;
1837 align_off = tmp - entry->offset;
1838 } else {
1839 align_off = 0;
1840 tmp = entry->offset;
1841 }
1842
Miao Xiea4820392013-09-09 13:19:42 +08001843 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001844 *max_extent_size = max(get_max_extent_size(entry),
1845 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001846 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001847 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001848
Josef Bacik96303082009-07-13 21:29:25 -04001849 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001850 u64 size = *bytes;
1851
Josef Bacik0584f712015-10-02 16:12:23 -04001852 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001853 if (!ret) {
1854 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001855 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001856 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001857 } else {
1858 *max_extent_size =
1859 max(get_max_extent_size(entry),
1860 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001861 }
Josef Bacik96303082009-07-13 21:29:25 -04001862 continue;
1863 }
1864
David Woodhouse53b381b2013-01-29 18:40:14 -05001865 *offset = tmp;
1866 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001867 return entry;
1868 }
Miao Xiea4820392013-09-09 13:19:42 +08001869out:
Josef Bacik96303082009-07-13 21:29:25 -04001870 return NULL;
1871}
1872
Li Zefan34d52cb2011-03-29 13:46:06 +08001873static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001874 struct btrfs_free_space *info, u64 offset)
1875{
Li Zefan34d52cb2011-03-29 13:46:06 +08001876 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001877 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001878 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001879 link_free_space(ctl, info);
1880 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001881
Li Zefan34d52cb2011-03-29 13:46:06 +08001882 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001883}
1884
Li Zefan34d52cb2011-03-29 13:46:06 +08001885static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001886 struct btrfs_free_space *bitmap_info)
1887{
Li Zefan34d52cb2011-03-29 13:46:06 +08001888 unlink_free_space(ctl, bitmap_info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00001889 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001890 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001891 ctl->total_bitmaps--;
1892 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001893}
1894
Li Zefan34d52cb2011-03-29 13:46:06 +08001895static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001896 struct btrfs_free_space *bitmap_info,
1897 u64 *offset, u64 *bytes)
1898{
1899 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001900 u64 search_start, search_bytes;
1901 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001902
1903again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001904 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001905
Josef Bacik6606bb92009-07-31 11:03:58 -04001906 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001907 * We need to search for bits in this bitmap. We could only cover some
1908 * of the extent in this bitmap thanks to how we add space, so we need
1909 * to search for as much as it as we can and clear that amount, and then
1910 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001911 */
1912 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001913 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001914 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001915 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1916 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001917 if (ret < 0 || search_start != *offset)
1918 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001919
Josef Bacikbdb7d302012-06-27 15:10:56 -04001920 /* We may have found more bits than what we need */
1921 search_bytes = min(search_bytes, *bytes);
1922
1923 /* Cannot clear past the end of the bitmap */
1924 search_bytes = min(search_bytes, end - search_start + 1);
1925
1926 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1927 *offset += search_bytes;
1928 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001929
1930 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001931 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001932 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001933 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001934
Josef Bacik6606bb92009-07-31 11:03:58 -04001935 /*
1936 * no entry after this bitmap, but we still have bytes to
1937 * remove, so something has gone wrong.
1938 */
1939 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001940 return -EINVAL;
1941
Josef Bacik6606bb92009-07-31 11:03:58 -04001942 bitmap_info = rb_entry(next, struct btrfs_free_space,
1943 offset_index);
1944
1945 /*
1946 * if the next entry isn't a bitmap we need to return to let the
1947 * extent stuff do its work.
1948 */
Josef Bacik96303082009-07-13 21:29:25 -04001949 if (!bitmap_info->bitmap)
1950 return -EAGAIN;
1951
Josef Bacik6606bb92009-07-31 11:03:58 -04001952 /*
1953 * Ok the next item is a bitmap, but it may not actually hold
1954 * the information for the rest of this free space stuff, so
1955 * look for it, and if we don't find it return so we can try
1956 * everything over again.
1957 */
1958 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001959 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001961 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001962 if (ret < 0 || search_start != *offset)
1963 return -EAGAIN;
1964
Josef Bacik96303082009-07-13 21:29:25 -04001965 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001966 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001967 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001968
1969 return 0;
1970}
1971
Josef Bacik2cdc3422011-05-27 14:07:49 -04001972static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1973 struct btrfs_free_space *info, u64 offset,
1974 u64 bytes)
1975{
1976 u64 bytes_to_set = 0;
1977 u64 end;
1978
1979 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1980
1981 bytes_to_set = min(end - offset, bytes);
1982
1983 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1984
Josef Bacikcef40482015-10-02 16:09:42 -04001985 /*
1986 * We set some bytes, we have no idea what the max extent size is
1987 * anymore.
1988 */
1989 info->max_extent_size = 0;
1990
Josef Bacik2cdc3422011-05-27 14:07:49 -04001991 return bytes_to_set;
1992
1993}
1994
Li Zefan34d52cb2011-03-29 13:46:06 +08001995static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1996 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001997{
David Sterba32da53862019-10-29 19:20:18 +01001998 struct btrfs_block_group *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001999 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002000 bool forced = false;
2001
2002#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002003 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002004 forced = true;
2005#endif
Josef Bacik96303082009-07-13 21:29:25 -04002006
2007 /*
2008 * If we are below the extents threshold then we can add this as an
2009 * extent, and don't have to deal with the bitmap
2010 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002011 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002012 /*
2013 * If this block group has some small extents we don't want to
2014 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002015 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002016 * of cache left then go ahead an dadd them, no sense in adding
2017 * the overhead of a bitmap if we don't have to.
2018 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002020 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2021 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002022 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002023 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002024 }
2025 }
Josef Bacik96303082009-07-13 21:29:25 -04002026
2027 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002028 * The original block groups from mkfs can be really small, like 8
2029 * megabytes, so don't bother with a bitmap for those entries. However
2030 * some block groups can be smaller than what a bitmap would cover but
2031 * are still large enough that they could overflow the 32k memory limit,
2032 * so allow those block groups to still be allowed to have a bitmap
2033 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002034 */
David Sterbab3470b52019-10-23 18:48:22 +02002035 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002036 return false;
2037
2038 return true;
2039}
2040
David Sterba20e55062015-11-19 11:42:28 +01002041static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002042 .recalc_thresholds = recalculate_thresholds,
2043 .use_bitmap = use_bitmap,
2044};
2045
Li Zefan34d52cb2011-03-29 13:46:06 +08002046static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2047 struct btrfs_free_space *info)
2048{
2049 struct btrfs_free_space *bitmap_info;
David Sterba32da53862019-10-29 19:20:18 +01002050 struct btrfs_block_group *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002051 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002052 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002053 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002054
2055 bytes = info->bytes;
2056 offset = info->offset;
2057
Li Zefan34d52cb2011-03-29 13:46:06 +08002058 if (!ctl->op->use_bitmap(ctl, info))
2059 return 0;
2060
Josef Bacik2cdc3422011-05-27 14:07:49 -04002061 if (ctl->op == &free_space_op)
2062 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002063again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002064 /*
2065 * Since we link bitmaps right into the cluster we need to see if we
2066 * have a cluster here, and if so and it has our bitmap we need to add
2067 * the free space to that bitmap.
2068 */
2069 if (block_group && !list_empty(&block_group->cluster_list)) {
2070 struct btrfs_free_cluster *cluster;
2071 struct rb_node *node;
2072 struct btrfs_free_space *entry;
2073
2074 cluster = list_entry(block_group->cluster_list.next,
2075 struct btrfs_free_cluster,
2076 block_group_list);
2077 spin_lock(&cluster->lock);
2078 node = rb_first(&cluster->root);
2079 if (!node) {
2080 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002081 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002082 }
2083
2084 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2085 if (!entry->bitmap) {
2086 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002087 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002088 }
2089
2090 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2091 bytes_added = add_bytes_to_bitmap(ctl, entry,
2092 offset, bytes);
2093 bytes -= bytes_added;
2094 offset += bytes_added;
2095 }
2096 spin_unlock(&cluster->lock);
2097 if (!bytes) {
2098 ret = 1;
2099 goto out;
2100 }
2101 }
Chris Mason38e87882011-06-10 16:36:57 -04002102
2103no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002104 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002105 1, 0);
2106 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002107 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002108 goto new_bitmap;
2109 }
2110
Josef Bacik2cdc3422011-05-27 14:07:49 -04002111 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2112 bytes -= bytes_added;
2113 offset += bytes_added;
2114 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002115
2116 if (!bytes) {
2117 ret = 1;
2118 goto out;
2119 } else
2120 goto again;
2121
2122new_bitmap:
2123 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002124 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002125 added = 1;
2126 info = NULL;
2127 goto again;
2128 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002129 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002130
2131 /* no pre-allocated info, allocate a new one */
2132 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002133 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2134 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002135 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002136 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002137 ret = -ENOMEM;
2138 goto out;
2139 }
2140 }
2141
2142 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002143 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2144 GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002145 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002146 if (!info->bitmap) {
2147 ret = -ENOMEM;
2148 goto out;
2149 }
2150 goto again;
2151 }
2152
2153out:
2154 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002155 if (info->bitmap)
2156 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2157 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002158 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002159 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002160
2161 return ret;
2162}
2163
Chris Mason945d8962011-05-22 12:33:42 -04002164static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002165 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002166{
Li Zefan120d66e2010-11-09 14:56:50 +08002167 struct btrfs_free_space *left_info;
2168 struct btrfs_free_space *right_info;
2169 bool merged = false;
2170 u64 offset = info->offset;
2171 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002172
Josef Bacik0f9dd462008-09-23 13:14:11 -04002173 /*
2174 * first we want to see if there is free space adjacent to the range we
2175 * are adding, if there is remove that struct and add a new one to
2176 * cover the entire range
2177 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002178 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002179 if (right_info && rb_prev(&right_info->offset_index))
2180 left_info = rb_entry(rb_prev(&right_info->offset_index),
2181 struct btrfs_free_space, offset_index);
2182 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002183 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002184
Josef Bacik96303082009-07-13 21:29:25 -04002185 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002186 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002187 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002188 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002189 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002190 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002191 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002192 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002193 }
2194
Josef Bacik96303082009-07-13 21:29:25 -04002195 if (left_info && !left_info->bitmap &&
2196 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002197 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002198 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002199 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002200 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002201 info->offset = left_info->offset;
2202 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002203 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002204 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002205 }
2206
Li Zefan120d66e2010-11-09 14:56:50 +08002207 return merged;
2208}
2209
Filipe Manana20005522014-08-29 13:35:13 +01002210static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2211 struct btrfs_free_space *info,
2212 bool update_stat)
2213{
2214 struct btrfs_free_space *bitmap;
2215 unsigned long i;
2216 unsigned long j;
2217 const u64 end = info->offset + info->bytes;
2218 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2219 u64 bytes;
2220
2221 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2222 if (!bitmap)
2223 return false;
2224
2225 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2226 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2227 if (j == i)
2228 return false;
2229 bytes = (j - i) * ctl->unit;
2230 info->bytes += bytes;
2231
2232 if (update_stat)
2233 bitmap_clear_bits(ctl, bitmap, end, bytes);
2234 else
2235 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2236
2237 if (!bitmap->bytes)
2238 free_bitmap(ctl, bitmap);
2239
2240 return true;
2241}
2242
2243static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2244 struct btrfs_free_space *info,
2245 bool update_stat)
2246{
2247 struct btrfs_free_space *bitmap;
2248 u64 bitmap_offset;
2249 unsigned long i;
2250 unsigned long j;
2251 unsigned long prev_j;
2252 u64 bytes;
2253
2254 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2255 /* If we're on a boundary, try the previous logical bitmap. */
2256 if (bitmap_offset == info->offset) {
2257 if (info->offset == 0)
2258 return false;
2259 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2260 }
2261
2262 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2263 if (!bitmap)
2264 return false;
2265
2266 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2267 j = 0;
2268 prev_j = (unsigned long)-1;
2269 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2270 if (j > i)
2271 break;
2272 prev_j = j;
2273 }
2274 if (prev_j == i)
2275 return false;
2276
2277 if (prev_j == (unsigned long)-1)
2278 bytes = (i + 1) * ctl->unit;
2279 else
2280 bytes = (i - prev_j) * ctl->unit;
2281
2282 info->offset -= bytes;
2283 info->bytes += bytes;
2284
2285 if (update_stat)
2286 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2287 else
2288 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2289
2290 if (!bitmap->bytes)
2291 free_bitmap(ctl, bitmap);
2292
2293 return true;
2294}
2295
2296/*
2297 * We prefer always to allocate from extent entries, both for clustered and
2298 * non-clustered allocation requests. So when attempting to add a new extent
2299 * entry, try to see if there's adjacent free space in bitmap entries, and if
2300 * there is, migrate that space from the bitmaps to the extent.
2301 * Like this we get better chances of satisfying space allocation requests
2302 * because we attempt to satisfy them based on a single cache entry, and never
2303 * on 2 or more entries - even if the entries represent a contiguous free space
2304 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2305 * ends).
2306 */
2307static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2308 struct btrfs_free_space *info,
2309 bool update_stat)
2310{
2311 /*
2312 * Only work with disconnected entries, as we can change their offset,
2313 * and must be extent entries.
2314 */
2315 ASSERT(!info->bitmap);
2316 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2317
2318 if (ctl->total_bitmaps > 0) {
2319 bool stole_end;
2320 bool stole_front = false;
2321
2322 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2323 if (ctl->total_bitmaps > 0)
2324 stole_front = steal_from_bitmap_to_front(ctl, info,
2325 update_stat);
2326
2327 if (stole_end || stole_front)
2328 try_merge_free_space(ctl, info, update_stat);
2329 }
2330}
2331
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002332int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2333 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002334 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002335{
2336 struct btrfs_free_space *info;
2337 int ret = 0;
2338
Josef Bacikdc89e982011-01-28 17:05:48 -05002339 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002340 if (!info)
2341 return -ENOMEM;
2342
2343 info->offset = offset;
2344 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002345 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002346
Li Zefan34d52cb2011-03-29 13:46:06 +08002347 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002348
Li Zefan34d52cb2011-03-29 13:46:06 +08002349 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002350 goto link;
2351
2352 /*
2353 * There was no extent directly to the left or right of this new
2354 * extent then we know we're going to have to allocate a new extent, so
2355 * before we do that see if we need to drop this into a bitmap
2356 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002357 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002358 if (ret < 0) {
2359 goto out;
2360 } else if (ret) {
2361 ret = 0;
2362 goto out;
2363 }
2364link:
Filipe Manana20005522014-08-29 13:35:13 +01002365 /*
2366 * Only steal free space from adjacent bitmaps if we're sure we're not
2367 * going to add the new free space to existing bitmap entries - because
2368 * that would mean unnecessary work that would be reverted. Therefore
2369 * attempt to steal space from bitmaps if we're adding an extent entry.
2370 */
2371 steal_from_bitmap(ctl, info, true);
2372
Li Zefan34d52cb2011-03-29 13:46:06 +08002373 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002374 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002375 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002376out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002377 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002378
Josef Bacik0f9dd462008-09-23 13:14:11 -04002379 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002380 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002381 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002382 }
2383
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384 return ret;
2385}
2386
David Sterba32da53862019-10-29 19:20:18 +01002387int btrfs_add_free_space(struct btrfs_block_group *block_group,
Josef Bacik478b4d92019-06-20 15:37:43 -04002388 u64 bytenr, u64 size)
2389{
2390 return __btrfs_add_free_space(block_group->fs_info,
2391 block_group->free_space_ctl,
2392 bytenr, size);
2393}
2394
David Sterba32da53862019-10-29 19:20:18 +01002395int btrfs_remove_free_space(struct btrfs_block_group *block_group,
Josef Bacik6226cb02009-04-03 10:14:18 -04002396 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002397{
Li Zefan34d52cb2011-03-29 13:46:06 +08002398 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002399 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002400 int ret;
2401 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002402
Li Zefan34d52cb2011-03-29 13:46:06 +08002403 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002404
Josef Bacik96303082009-07-13 21:29:25 -04002405again:
Josef Bacikb0175112012-12-18 11:39:19 -05002406 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002407 if (!bytes)
2408 goto out_lock;
2409
Li Zefan34d52cb2011-03-29 13:46:06 +08002410 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002411 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002412 /*
2413 * oops didn't find an extent that matched the space we wanted
2414 * to remove, look for a bitmap instead
2415 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002416 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002417 1, 0);
2418 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002419 /*
2420 * If we found a partial bit of our free space in a
2421 * bitmap but then couldn't find the other part this may
2422 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002423 */
Josef Bacikb0175112012-12-18 11:39:19 -05002424 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002425 goto out_lock;
2426 }
Josef Bacik96303082009-07-13 21:29:25 -04002427 }
2428
Josef Bacikb0175112012-12-18 11:39:19 -05002429 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002430 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002431 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002432 if (offset == info->offset) {
2433 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002434
Josef Bacikbdb7d302012-06-27 15:10:56 -04002435 info->bytes -= to_free;
2436 info->offset += to_free;
2437 if (info->bytes) {
2438 ret = link_free_space(ctl, info);
2439 WARN_ON(ret);
2440 } else {
2441 kmem_cache_free(btrfs_free_space_cachep, info);
2442 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002443
Josef Bacikbdb7d302012-06-27 15:10:56 -04002444 offset += to_free;
2445 bytes -= to_free;
2446 goto again;
2447 } else {
2448 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002449
Josef Bacikbdb7d302012-06-27 15:10:56 -04002450 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002451 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002452 WARN_ON(ret);
2453 if (ret)
2454 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002455
Josef Bacikbdb7d302012-06-27 15:10:56 -04002456 /* Not enough bytes in this entry to satisfy us */
2457 if (old_end < offset + bytes) {
2458 bytes -= old_end - offset;
2459 offset = old_end;
2460 goto again;
2461 } else if (old_end == offset + bytes) {
2462 /* all done */
2463 goto out_lock;
2464 }
2465 spin_unlock(&ctl->tree_lock);
2466
2467 ret = btrfs_add_free_space(block_group, offset + bytes,
2468 old_end - (offset + bytes));
2469 WARN_ON(ret);
2470 goto out;
2471 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002472 }
Josef Bacik96303082009-07-13 21:29:25 -04002473
Li Zefan34d52cb2011-03-29 13:46:06 +08002474 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002475 if (ret == -EAGAIN) {
2476 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002477 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002478 }
Josef Bacik96303082009-07-13 21:29:25 -04002479out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002480 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002481out:
Josef Bacik25179202008-10-29 14:49:05 -04002482 return ret;
2483}
2484
David Sterba32da53862019-10-29 19:20:18 +01002485void btrfs_dump_free_space(struct btrfs_block_group *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -04002486 u64 bytes)
2487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002489 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002490 struct btrfs_free_space *info;
2491 struct rb_node *n;
2492 int count = 0;
2493
Filipe Manana9084cb62018-10-22 10:43:06 +01002494 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002495 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002496 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002497 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002498 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002500 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002501 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002502 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002503 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002504 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002505 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002507 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002508}
2509
David Sterba32da53862019-10-29 19:20:18 +01002510void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002511{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002512 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002513 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002514
Li Zefan34d52cb2011-03-29 13:46:06 +08002515 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002516 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002517 ctl->start = block_group->start;
Li Zefan34d52cb2011-03-29 13:46:06 +08002518 ctl->private = block_group;
2519 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002520 INIT_LIST_HEAD(&ctl->trimming_ranges);
2521 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002522
Li Zefan34d52cb2011-03-29 13:46:06 +08002523 /*
2524 * we only want to have 32k of ram per block group for keeping
2525 * track of free space, and if we pass 1/2 of that we want to
2526 * start converting things over to using bitmaps
2527 */
Byongho Leeee221842015-12-15 01:42:10 +09002528 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002529}
2530
Chris Masonfa9c0d792009-04-03 09:47:43 -04002531/*
2532 * for a given cluster, put all of its extents back into the free
2533 * space cache. If the block group passed doesn't match the block group
2534 * pointed to by the cluster, someone else raced in and freed the
2535 * cluster already. In that case, we just return without changing anything
2536 */
2537static int
2538__btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002539 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002540 struct btrfs_free_cluster *cluster)
2541{
Li Zefan34d52cb2011-03-29 13:46:06 +08002542 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002543 struct btrfs_free_space *entry;
2544 struct rb_node *node;
2545
2546 spin_lock(&cluster->lock);
2547 if (cluster->block_group != block_group)
2548 goto out;
2549
Josef Bacik96303082009-07-13 21:29:25 -04002550 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002551 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002552 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002553
Chris Masonfa9c0d792009-04-03 09:47:43 -04002554 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002555 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002556 bool bitmap;
2557
Chris Masonfa9c0d792009-04-03 09:47:43 -04002558 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2559 node = rb_next(&entry->offset_index);
2560 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002561 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002562
2563 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002564 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002565 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002566 steal_from_bitmap(ctl, entry, false);
2567 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002568 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002569 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002570 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002571 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002572
Chris Masonfa9c0d792009-04-03 09:47:43 -04002573out:
2574 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002575 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002576 return 0;
2577}
2578
Eric Sandeen48a3b632013-04-25 20:41:01 +00002579static void __btrfs_remove_free_space_cache_locked(
2580 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002581{
2582 struct btrfs_free_space *info;
2583 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002584
Li Zefan581bb052011-04-20 10:06:11 +08002585 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2586 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002587 if (!info->bitmap) {
2588 unlink_free_space(ctl, info);
2589 kmem_cache_free(btrfs_free_space_cachep, info);
2590 } else {
2591 free_bitmap(ctl, info);
2592 }
David Sterba351810c2015-01-08 15:20:54 +01002593
2594 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002595 }
Chris Mason09655372011-05-21 09:27:38 -04002596}
2597
2598void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2599{
2600 spin_lock(&ctl->tree_lock);
2601 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002602 spin_unlock(&ctl->tree_lock);
2603}
2604
David Sterba32da53862019-10-29 19:20:18 +01002605void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002606{
Li Zefan34d52cb2011-03-29 13:46:06 +08002607 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002608 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002609 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002610
Li Zefan34d52cb2011-03-29 13:46:06 +08002611 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002612 while ((head = block_group->cluster_list.next) !=
2613 &block_group->cluster_list) {
2614 cluster = list_entry(head, struct btrfs_free_cluster,
2615 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002616
2617 WARN_ON(cluster->block_group != block_group);
2618 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002619
2620 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002621 }
Chris Mason09655372011-05-21 09:27:38 -04002622 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002623 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002624
Josef Bacik0f9dd462008-09-23 13:14:11 -04002625}
2626
David Sterba32da53862019-10-29 19:20:18 +01002627u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002628 u64 offset, u64 bytes, u64 empty_size,
2629 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002630{
Li Zefan34d52cb2011-03-29 13:46:06 +08002631 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002632 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002633 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002634 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002635 u64 align_gap = 0;
2636 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002637
Li Zefan34d52cb2011-03-29 13:46:06 +08002638 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002639 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002640 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002641 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002642 goto out;
2643
2644 ret = offset;
2645 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002646 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002647 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002648 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002649 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002650 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002651 align_gap_len = offset - entry->offset;
2652 align_gap = entry->offset;
2653
2654 entry->offset = offset + bytes;
2655 WARN_ON(entry->bytes < bytes + align_gap_len);
2656
2657 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002658 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002659 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002660 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002661 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002662 }
Josef Bacik96303082009-07-13 21:29:25 -04002663out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002664 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002665
David Woodhouse53b381b2013-01-29 18:40:14 -05002666 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002667 __btrfs_add_free_space(block_group->fs_info, ctl,
2668 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002669 return ret;
2670}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002671
2672/*
2673 * given a cluster, put all of its extents back into the free space
2674 * cache. If a block group is passed, this function will only free
2675 * a cluster that belongs to the passed block group.
2676 *
2677 * Otherwise, it'll get a reference on the block group pointed to by the
2678 * cluster and remove the cluster from it.
2679 */
2680int btrfs_return_cluster_to_free_space(
David Sterba32da53862019-10-29 19:20:18 +01002681 struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002682 struct btrfs_free_cluster *cluster)
2683{
Li Zefan34d52cb2011-03-29 13:46:06 +08002684 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002685 int ret;
2686
2687 /* first, get a safe pointer to the block group */
2688 spin_lock(&cluster->lock);
2689 if (!block_group) {
2690 block_group = cluster->block_group;
2691 if (!block_group) {
2692 spin_unlock(&cluster->lock);
2693 return 0;
2694 }
2695 } else if (cluster->block_group != block_group) {
2696 /* someone else has already freed it don't redo their work */
2697 spin_unlock(&cluster->lock);
2698 return 0;
2699 }
2700 atomic_inc(&block_group->count);
2701 spin_unlock(&cluster->lock);
2702
Li Zefan34d52cb2011-03-29 13:46:06 +08002703 ctl = block_group->free_space_ctl;
2704
Chris Masonfa9c0d792009-04-03 09:47:43 -04002705 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002706 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002707 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002708 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002709
2710 /* finally drop our ref */
2711 btrfs_put_block_group(block_group);
2712 return ret;
2713}
2714
David Sterba32da53862019-10-29 19:20:18 +01002715static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04002716 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002717 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002718 u64 bytes, u64 min_start,
2719 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002720{
Li Zefan34d52cb2011-03-29 13:46:06 +08002721 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002722 int err;
2723 u64 search_start = cluster->window_start;
2724 u64 search_bytes = bytes;
2725 u64 ret = 0;
2726
Josef Bacik96303082009-07-13 21:29:25 -04002727 search_start = min_start;
2728 search_bytes = bytes;
2729
Josef Bacik0584f712015-10-02 16:12:23 -04002730 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002731 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002732 *max_extent_size = max(get_max_extent_size(entry),
2733 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002734 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002735 }
Josef Bacik96303082009-07-13 21:29:25 -04002736
2737 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002738 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002739
2740 return ret;
2741}
2742
Chris Masonfa9c0d792009-04-03 09:47:43 -04002743/*
2744 * given a cluster, try to allocate 'bytes' from it, returns 0
2745 * if it couldn't find anything suitably large, or a logical disk offset
2746 * if things worked out
2747 */
David Sterba32da53862019-10-29 19:20:18 +01002748u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002749 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002750 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002751{
Li Zefan34d52cb2011-03-29 13:46:06 +08002752 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002753 struct btrfs_free_space *entry = NULL;
2754 struct rb_node *node;
2755 u64 ret = 0;
2756
2757 spin_lock(&cluster->lock);
2758 if (bytes > cluster->max_size)
2759 goto out;
2760
2761 if (cluster->block_group != block_group)
2762 goto out;
2763
2764 node = rb_first(&cluster->root);
2765 if (!node)
2766 goto out;
2767
2768 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302769 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002770 if (entry->bytes < bytes)
2771 *max_extent_size = max(get_max_extent_size(entry),
2772 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002773
Josef Bacik4e69b592011-03-21 10:11:24 -04002774 if (entry->bytes < bytes ||
2775 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002776 node = rb_next(&entry->offset_index);
2777 if (!node)
2778 break;
2779 entry = rb_entry(node, struct btrfs_free_space,
2780 offset_index);
2781 continue;
2782 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002783
Josef Bacik4e69b592011-03-21 10:11:24 -04002784 if (entry->bitmap) {
2785 ret = btrfs_alloc_from_bitmap(block_group,
2786 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002787 cluster->window_start,
2788 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002789 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002790 node = rb_next(&entry->offset_index);
2791 if (!node)
2792 break;
2793 entry = rb_entry(node, struct btrfs_free_space,
2794 offset_index);
2795 continue;
2796 }
Josef Bacik9b230622012-01-26 15:01:12 -05002797 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002798 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002799 ret = entry->offset;
2800
2801 entry->offset += bytes;
2802 entry->bytes -= bytes;
2803 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002804
Li Zefan5e71b5d2010-11-09 14:55:34 +08002805 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002806 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002807 break;
2808 }
2809out:
2810 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002811
Li Zefan5e71b5d2010-11-09 14:55:34 +08002812 if (!ret)
2813 return 0;
2814
Li Zefan34d52cb2011-03-29 13:46:06 +08002815 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002816
Li Zefan34d52cb2011-03-29 13:46:06 +08002817 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002818 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002819 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002820 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002821 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2822 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002823 ctl->total_bitmaps--;
2824 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002825 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002826 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002827 }
2828
Li Zefan34d52cb2011-03-29 13:46:06 +08002829 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002830
Chris Masonfa9c0d792009-04-03 09:47:43 -04002831 return ret;
2832}
2833
David Sterba32da53862019-10-29 19:20:18 +01002834static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
Josef Bacik96303082009-07-13 21:29:25 -04002835 struct btrfs_free_space *entry,
2836 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002837 u64 offset, u64 bytes,
2838 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002839{
Li Zefan34d52cb2011-03-29 13:46:06 +08002840 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002841 unsigned long next_zero;
2842 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002843 unsigned long want_bits;
2844 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002845 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002846 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002847 unsigned long start = 0;
2848 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002849 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002850
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002851 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002852 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002853 want_bits = bytes_to_bits(bytes, ctl->unit);
2854 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002855
Josef Bacikcef40482015-10-02 16:09:42 -04002856 /*
2857 * Don't bother looking for a cluster in this bitmap if it's heavily
2858 * fragmented.
2859 */
2860 if (entry->max_extent_size &&
2861 entry->max_extent_size < cont1_bytes)
2862 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002863again:
2864 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002865 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002866 next_zero = find_next_zero_bit(entry->bitmap,
2867 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002868 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002869 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002870 if (found_bits > max_bits)
2871 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002872 break;
2873 }
Josef Bacikcef40482015-10-02 16:09:42 -04002874 if (next_zero - i > max_bits)
2875 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002876 i = next_zero;
2877 }
2878
Josef Bacikcef40482015-10-02 16:09:42 -04002879 if (!found_bits) {
2880 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002881 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002882 }
Josef Bacik96303082009-07-13 21:29:25 -04002883
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002884 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002885 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002886 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002887 }
2888
2889 total_found += found_bits;
2890
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002891 if (cluster->max_size < found_bits * ctl->unit)
2892 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002893
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002894 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2895 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002896 goto again;
2897 }
2898
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002899 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002900 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002901 ret = tree_insert_offset(&cluster->root, entry->offset,
2902 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002903 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002904
Josef Bacik3f7de032011-11-10 08:29:20 -05002905 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002906 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002907 return 0;
2908}
2909
Chris Masonfa9c0d792009-04-03 09:47:43 -04002910/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002911 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002912 * Try to find a cluster with at least bytes total bytes, at least one
2913 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002914 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002915static noinline int
David Sterba32da53862019-10-29 19:20:18 +01002916setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04002917 struct btrfs_free_cluster *cluster,
2918 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002919 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002920{
Li Zefan34d52cb2011-03-29 13:46:06 +08002921 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002922 struct btrfs_free_space *first = NULL;
2923 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002924 struct btrfs_free_space *last;
2925 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002926 u64 window_free;
2927 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002928 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002929
Li Zefan34d52cb2011-03-29 13:46:06 +08002930 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002931 if (!entry)
2932 return -ENOSPC;
2933
2934 /*
2935 * We don't want bitmaps, so just move along until we find a normal
2936 * extent entry.
2937 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002938 while (entry->bitmap || entry->bytes < min_bytes) {
2939 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002940 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002941 node = rb_next(&entry->offset_index);
2942 if (!node)
2943 return -ENOSPC;
2944 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2945 }
2946
Josef Bacik4e69b592011-03-21 10:11:24 -04002947 window_free = entry->bytes;
2948 max_extent = entry->bytes;
2949 first = entry;
2950 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002951
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002952 for (node = rb_next(&entry->offset_index); node;
2953 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002954 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2955
Josef Bacik86d4a772011-05-25 13:03:16 -04002956 if (entry->bitmap) {
2957 if (list_empty(&entry->list))
2958 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002959 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002960 }
2961
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002962 if (entry->bytes < min_bytes)
2963 continue;
2964
2965 last = entry;
2966 window_free += entry->bytes;
2967 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002968 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002969 }
2970
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002971 if (window_free < bytes || max_extent < cont1_bytes)
2972 return -ENOSPC;
2973
Josef Bacik4e69b592011-03-21 10:11:24 -04002974 cluster->window_start = first->offset;
2975
2976 node = &first->offset_index;
2977
2978 /*
2979 * now we've found our entries, pull them out of the free space
2980 * cache and put them into the cluster rbtree
2981 */
2982 do {
2983 int ret;
2984
2985 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2986 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002987 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002988 continue;
2989
Li Zefan34d52cb2011-03-29 13:46:06 +08002990 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002991 ret = tree_insert_offset(&cluster->root, entry->offset,
2992 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002993 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002994 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002995 } while (node && entry != last);
2996
2997 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002998 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002999 return 0;
3000}
3001
3002/*
3003 * This specifically looks for bitmaps that may work in the cluster, we assume
3004 * that we have already failed to find extents that will work.
3005 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003006static noinline int
David Sterba32da53862019-10-29 19:20:18 +01003007setup_cluster_bitmap(struct btrfs_block_group *block_group,
Josef Bacik3de85bb2011-05-25 13:07:37 -04003008 struct btrfs_free_cluster *cluster,
3009 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003010 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003011{
Li Zefan34d52cb2011-03-29 13:46:06 +08003012 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003013 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003014 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003015 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003016
Li Zefan34d52cb2011-03-29 13:46:06 +08003017 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003018 return -ENOSPC;
3019
Josef Bacik86d4a772011-05-25 13:03:16 -04003020 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003021 * The bitmap that covers offset won't be in the list unless offset
3022 * is just its start offset.
3023 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003024 if (!list_empty(bitmaps))
3025 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3026
3027 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003028 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3029 if (entry && list_empty(&entry->list))
3030 list_add(&entry->list, bitmaps);
3031 }
3032
Josef Bacik86d4a772011-05-25 13:03:16 -04003033 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003034 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003035 continue;
3036 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003037 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003038 if (!ret)
3039 return 0;
3040 }
3041
3042 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003043 * The bitmaps list has all the bitmaps that record free space
3044 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003045 */
Li Zefan52621cb2011-11-20 07:33:38 -05003046 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003047}
3048
3049/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003050 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003051 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003052 * We might not find them all in one contiguous area.
3053 *
3054 * returns zero and sets up cluster if things worked out, otherwise
3055 * it returns -enospc
3056 */
David Sterba32da53862019-10-29 19:20:18 +01003057int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003058 struct btrfs_free_cluster *cluster,
3059 u64 offset, u64 bytes, u64 empty_size)
3060{
David Sterba2ceeae22019-03-20 13:53:49 +01003061 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003062 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003063 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003064 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003065 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003066 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003067 int ret;
3068
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003069 /*
3070 * Choose the minimum extent size we'll require for this
3071 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3072 * For metadata, allow allocates with smaller extents. For
3073 * data, keep it dense.
3074 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003075 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003076 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003077 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003078 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003080 } else {
3081 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003082 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003083 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003084
Li Zefan34d52cb2011-03-29 13:46:06 +08003085 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003086
3087 /*
3088 * If we know we don't have enough space to make a cluster don't even
3089 * bother doing all the work to try and find one.
3090 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003091 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003092 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003093 return -ENOSPC;
3094 }
3095
Chris Masonfa9c0d792009-04-03 09:47:43 -04003096 spin_lock(&cluster->lock);
3097
3098 /* someone already found a cluster, hooray */
3099 if (cluster->block_group) {
3100 ret = 0;
3101 goto out;
3102 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003103
Josef Bacik3f7de032011-11-10 08:29:20 -05003104 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3105 min_bytes);
3106
Josef Bacik86d4a772011-05-25 13:03:16 -04003107 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003108 bytes + empty_size,
3109 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003110 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003111 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003112 offset, bytes + empty_size,
3113 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003114
3115 /* Clear our temporary list */
3116 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3117 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003118
3119 if (!ret) {
3120 atomic_inc(&block_group->count);
3121 list_add_tail(&cluster->block_group_list,
3122 &block_group->cluster_list);
3123 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003124 } else {
3125 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003126 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003127out:
3128 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003129 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003130
3131 return ret;
3132}
3133
3134/*
3135 * simple code to zero out a cluster
3136 */
3137void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3138{
3139 spin_lock_init(&cluster->lock);
3140 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003141 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003142 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003143 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003144 INIT_LIST_HEAD(&cluster->block_group_list);
3145 cluster->block_group = NULL;
3146}
3147
David Sterba32da53862019-10-29 19:20:18 +01003148static int do_trimming(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003149 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003150 u64 reserved_start, u64 reserved_bytes,
3151 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003152{
3153 struct btrfs_space_info *space_info = block_group->space_info;
3154 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003155 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003156 int ret;
3157 int update = 0;
3158 u64 trimmed = 0;
3159
3160 spin_lock(&space_info->lock);
3161 spin_lock(&block_group->lock);
3162 if (!block_group->ro) {
3163 block_group->reserved += reserved_bytes;
3164 space_info->bytes_reserved += reserved_bytes;
3165 update = 1;
3166 }
3167 spin_unlock(&block_group->lock);
3168 spin_unlock(&space_info->lock);
3169
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003170 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003171 if (!ret)
3172 *total_trimmed += trimmed;
3173
Filipe Manana55507ce2014-12-01 17:04:09 +00003174 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003175 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003176 list_del(&trim_entry->list);
3177 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003178
3179 if (update) {
3180 spin_lock(&space_info->lock);
3181 spin_lock(&block_group->lock);
3182 if (block_group->ro)
3183 space_info->bytes_readonly += reserved_bytes;
3184 block_group->reserved -= reserved_bytes;
3185 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003186 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003187 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003188 }
3189
3190 return ret;
3191}
3192
David Sterba32da53862019-10-29 19:20:18 +01003193static int trim_no_bitmap(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003194 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003195{
Li Zefan34d52cb2011-03-29 13:46:06 +08003196 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003197 struct btrfs_free_space *entry;
3198 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003199 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003200 u64 extent_start;
3201 u64 extent_bytes;
3202 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003203
3204 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003205 struct btrfs_trim_range trim_entry;
3206
3207 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003208 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003209
Li Zefan34d52cb2011-03-29 13:46:06 +08003210 if (ctl->free_space < minlen) {
3211 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003212 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003213 break;
3214 }
3215
Li Zefan34d52cb2011-03-29 13:46:06 +08003216 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003217 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003218 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003219 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003220 break;
3221 }
3222
Li Zefan7fe1e642011-12-29 14:47:27 +08003223 /* skip bitmaps */
3224 while (entry->bitmap) {
3225 node = rb_next(&entry->offset_index);
3226 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003227 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003228 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003229 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003230 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003231 entry = rb_entry(node, struct btrfs_free_space,
3232 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003233 }
3234
Li Zefan7fe1e642011-12-29 14:47:27 +08003235 if (entry->offset >= end) {
3236 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003237 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003238 break;
3239 }
3240
3241 extent_start = entry->offset;
3242 extent_bytes = entry->bytes;
3243 start = max(start, extent_start);
3244 bytes = min(extent_start + extent_bytes, end) - start;
3245 if (bytes < minlen) {
3246 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003247 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003248 goto next;
3249 }
3250
3251 unlink_free_space(ctl, entry);
3252 kmem_cache_free(btrfs_free_space_cachep, entry);
3253
Li Zefan34d52cb2011-03-29 13:46:06 +08003254 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003255 trim_entry.start = extent_start;
3256 trim_entry.bytes = extent_bytes;
3257 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3258 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003259
Li Zefan7fe1e642011-12-29 14:47:27 +08003260 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003261 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003262 if (ret)
3263 break;
3264next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003265 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003266
3267 if (fatal_signal_pending(current)) {
3268 ret = -ERESTARTSYS;
3269 break;
3270 }
3271
3272 cond_resched();
3273 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003274out:
3275 return ret;
3276}
3277
David Sterba32da53862019-10-29 19:20:18 +01003278static int trim_bitmaps(struct btrfs_block_group *block_group,
Li Zefan7fe1e642011-12-29 14:47:27 +08003279 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3280{
3281 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3282 struct btrfs_free_space *entry;
3283 int ret = 0;
3284 int ret2;
3285 u64 bytes;
3286 u64 offset = offset_to_bitmap(ctl, start);
3287
3288 while (offset < end) {
3289 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003290 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003291
Filipe Manana55507ce2014-12-01 17:04:09 +00003292 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003293 spin_lock(&ctl->tree_lock);
3294
3295 if (ctl->free_space < minlen) {
3296 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003297 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003298 break;
3299 }
3300
3301 entry = tree_search_offset(ctl, offset, 1, 0);
3302 if (!entry) {
3303 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003304 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003305 next_bitmap = true;
3306 goto next;
3307 }
3308
3309 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003310 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003311 if (ret2 || start >= end) {
3312 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003313 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003314 next_bitmap = true;
3315 goto next;
3316 }
3317
3318 bytes = min(bytes, end - start);
3319 if (bytes < minlen) {
3320 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003321 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003322 goto next;
3323 }
3324
3325 bitmap_clear_bits(ctl, entry, start, bytes);
3326 if (entry->bytes == 0)
3327 free_bitmap(ctl, entry);
3328
3329 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003330 trim_entry.start = start;
3331 trim_entry.bytes = bytes;
3332 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3333 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003334
3335 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003336 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003337 if (ret)
3338 break;
3339next:
3340 if (next_bitmap) {
3341 offset += BITS_PER_BITMAP * ctl->unit;
3342 } else {
3343 start += bytes;
3344 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3345 offset += BITS_PER_BITMAP * ctl->unit;
3346 }
3347
3348 if (fatal_signal_pending(current)) {
3349 ret = -ERESTARTSYS;
3350 break;
3351 }
3352
3353 cond_resched();
3354 }
3355
3356 return ret;
3357}
3358
David Sterba32da53862019-10-29 19:20:18 +01003359void btrfs_get_block_group_trimming(struct btrfs_block_group *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003360{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003361 atomic_inc(&cache->trimming);
3362}
Li Zefan7fe1e642011-12-29 14:47:27 +08003363
David Sterba32da53862019-10-29 19:20:18 +01003364void btrfs_put_block_group_trimming(struct btrfs_block_group *block_group)
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003366 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003367 struct extent_map_tree *em_tree;
3368 struct extent_map *em;
3369 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003370
Filipe Manana04216822014-11-27 21:14:15 +00003371 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003372 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3373 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003374 spin_unlock(&block_group->lock);
3375
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003376 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003377 mutex_lock(&fs_info->chunk_mutex);
David Sterbac8bf1b62019-05-17 11:43:17 +02003378 em_tree = &fs_info->mapping_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003379 write_lock(&em_tree->lock);
David Sterbab3470b52019-10-23 18:48:22 +02003380 em = lookup_extent_mapping(em_tree, block_group->start,
Filipe Manana04216822014-11-27 21:14:15 +00003381 1);
3382 BUG_ON(!em); /* logic error, can't happen */
3383 remove_extent_mapping(em_tree, em);
3384 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003385 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003386
3387 /* once for us and once for the tree */
3388 free_extent_map(em);
3389 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003390
3391 /*
3392 * We've left one free space entry and other tasks trimming
3393 * this block group have left 1 entry each one. Free them.
3394 */
3395 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003396 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003397}
Li Dongyangf7039b12011-03-24 10:24:28 +00003398
David Sterba32da53862019-10-29 19:20:18 +01003399int btrfs_trim_block_group(struct btrfs_block_group *block_group,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003400 u64 *trimmed, u64 start, u64 end, u64 minlen)
3401{
3402 int ret;
3403
3404 *trimmed = 0;
3405
3406 spin_lock(&block_group->lock);
3407 if (block_group->removed) {
3408 spin_unlock(&block_group->lock);
3409 return 0;
3410 }
3411 btrfs_get_block_group_trimming(block_group);
3412 spin_unlock(&block_group->lock);
3413
3414 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3415 if (ret)
3416 goto out;
3417
3418 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3419out:
3420 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003421 return ret;
3422}
Li Zefan581bb052011-04-20 10:06:11 +08003423
3424/*
3425 * Find the left-most item in the cache tree, and then return the
3426 * smallest inode number in the item.
3427 *
3428 * Note: the returned inode number may not be the smallest one in
3429 * the tree, if the left-most item is a bitmap.
3430 */
3431u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3432{
3433 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3434 struct btrfs_free_space *entry = NULL;
3435 u64 ino = 0;
3436
3437 spin_lock(&ctl->tree_lock);
3438
3439 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3440 goto out;
3441
3442 entry = rb_entry(rb_first(&ctl->free_space_offset),
3443 struct btrfs_free_space, offset_index);
3444
3445 if (!entry->bitmap) {
3446 ino = entry->offset;
3447
3448 unlink_free_space(ctl, entry);
3449 entry->offset++;
3450 entry->bytes--;
3451 if (!entry->bytes)
3452 kmem_cache_free(btrfs_free_space_cachep, entry);
3453 else
3454 link_free_space(ctl, entry);
3455 } else {
3456 u64 offset = 0;
3457 u64 count = 1;
3458 int ret;
3459
Josef Bacik0584f712015-10-02 16:12:23 -04003460 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003461 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003462 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003463
3464 ino = offset;
3465 bitmap_clear_bits(ctl, entry, offset, 1);
3466 if (entry->bytes == 0)
3467 free_bitmap(ctl, entry);
3468 }
3469out:
3470 spin_unlock(&ctl->tree_lock);
3471
3472 return ino;
3473}
Li Zefan82d59022011-04-20 10:33:24 +08003474
3475struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3476 struct btrfs_path *path)
3477{
3478 struct inode *inode = NULL;
3479
David Sterba57cdc8d2014-02-05 02:37:48 +01003480 spin_lock(&root->ino_cache_lock);
3481 if (root->ino_cache_inode)
3482 inode = igrab(root->ino_cache_inode);
3483 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003484 if (inode)
3485 return inode;
3486
3487 inode = __lookup_free_space_inode(root, path, 0);
3488 if (IS_ERR(inode))
3489 return inode;
3490
David Sterba57cdc8d2014-02-05 02:37:48 +01003491 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003492 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003493 root->ino_cache_inode = igrab(inode);
3494 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003495
3496 return inode;
3497}
3498
3499int create_free_ino_inode(struct btrfs_root *root,
3500 struct btrfs_trans_handle *trans,
3501 struct btrfs_path *path)
3502{
3503 return __create_free_space_inode(root, trans, path,
3504 BTRFS_FREE_INO_OBJECTID, 0);
3505}
3506
3507int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3508{
3509 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3510 struct btrfs_path *path;
3511 struct inode *inode;
3512 int ret = 0;
3513 u64 root_gen = btrfs_root_generation(&root->root_item);
3514
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003515 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003516 return 0;
3517
Li Zefan82d59022011-04-20 10:33:24 +08003518 /*
3519 * If we're unmounting then just return, since this does a search on the
3520 * normal root and not the commit root and we could deadlock.
3521 */
David Sterba7841cb22011-05-31 18:07:27 +02003522 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003523 return 0;
3524
3525 path = btrfs_alloc_path();
3526 if (!path)
3527 return 0;
3528
3529 inode = lookup_free_ino_inode(root, path);
3530 if (IS_ERR(inode))
3531 goto out;
3532
3533 if (root_gen != BTRFS_I(inode)->generation)
3534 goto out_put;
3535
3536 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3537
3538 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003539 btrfs_err(fs_info,
3540 "failed to load free ino cache for root %llu",
3541 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003542out_put:
3543 iput(inode);
3544out:
3545 btrfs_free_path(path);
3546 return ret;
3547}
3548
3549int btrfs_write_out_ino_cache(struct btrfs_root *root,
3550 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003551 struct btrfs_path *path,
3552 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003555 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003556 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003557 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003558 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003559
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003561 return 0;
3562
Chris Mason85db36c2015-04-23 08:02:49 -07003563 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003564 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003565 if (!ret) {
3566 /*
3567 * At this point writepages() didn't error out, so our metadata
3568 * reservation is released when the writeback finishes, at
3569 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3570 * with or without an error.
3571 */
3572 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003573 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003574 }
Chris Mason85db36c2015-04-23 08:02:49 -07003575
Josef Bacikc09544e2011-08-30 10:19:10 -04003576 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003577 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003578 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003579 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04003580#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003581 btrfs_err(fs_info,
3582 "failed to write free ino cache for root %llu",
3583 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003584#endif
3585 }
Li Zefan82d59022011-04-20 10:33:24 +08003586
Li Zefan82d59022011-04-20 10:33:24 +08003587 return ret;
3588}
Josef Bacik74255aa2013-03-15 09:47:08 -04003589
3590#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003591/*
3592 * Use this if you need to make a bitmap or extent entry specifically, it
3593 * doesn't do any of the merging that add_free_space does, this acts a lot like
3594 * how the free space cache loading stuff works, so you can get really weird
3595 * configurations.
3596 */
David Sterba32da53862019-10-29 19:20:18 +01003597int test_add_free_space_entry(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04003598 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003599{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003600 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3601 struct btrfs_free_space *info = NULL, *bitmap_info;
3602 void *map = NULL;
3603 u64 bytes_added;
3604 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003605
Josef Bacikdc11dd52013-08-14 15:05:12 -04003606again:
3607 if (!info) {
3608 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3609 if (!info)
3610 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003611 }
3612
Josef Bacikdc11dd52013-08-14 15:05:12 -04003613 if (!bitmap) {
3614 spin_lock(&ctl->tree_lock);
3615 info->offset = offset;
3616 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003617 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003618 ret = link_free_space(ctl, info);
3619 spin_unlock(&ctl->tree_lock);
3620 if (ret)
3621 kmem_cache_free(btrfs_free_space_cachep, info);
3622 return ret;
3623 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003624
Josef Bacikdc11dd52013-08-14 15:05:12 -04003625 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003626 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003627 if (!map) {
3628 kmem_cache_free(btrfs_free_space_cachep, info);
3629 return -ENOMEM;
3630 }
3631 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003632
Josef Bacikdc11dd52013-08-14 15:05:12 -04003633 spin_lock(&ctl->tree_lock);
3634 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3635 1, 0);
3636 if (!bitmap_info) {
3637 info->bitmap = map;
3638 map = NULL;
3639 add_new_bitmap(ctl, info, offset);
3640 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003641 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003642 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003643
Josef Bacikdc11dd52013-08-14 15:05:12 -04003644 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003645
Josef Bacikdc11dd52013-08-14 15:05:12 -04003646 bytes -= bytes_added;
3647 offset += bytes_added;
3648 spin_unlock(&ctl->tree_lock);
3649
3650 if (bytes)
3651 goto again;
3652
Filipe Manana20005522014-08-29 13:35:13 +01003653 if (info)
3654 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00003655 if (map)
3656 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003657 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003658}
3659
3660/*
3661 * Checks to see if the given range is in the free space cache. This is really
3662 * just used to check the absence of space, so if there is free space in the
3663 * range at all we will return 1.
3664 */
David Sterba32da53862019-10-29 19:20:18 +01003665int test_check_exists(struct btrfs_block_group *cache,
Josef Bacikdc11dd52013-08-14 15:05:12 -04003666 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003667{
3668 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3669 struct btrfs_free_space *info;
3670 int ret = 0;
3671
3672 spin_lock(&ctl->tree_lock);
3673 info = tree_search_offset(ctl, offset, 0, 0);
3674 if (!info) {
3675 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3676 1, 0);
3677 if (!info)
3678 goto out;
3679 }
3680
3681have_info:
3682 if (info->bitmap) {
3683 u64 bit_off, bit_bytes;
3684 struct rb_node *n;
3685 struct btrfs_free_space *tmp;
3686
3687 bit_off = offset;
3688 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003689 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003690 if (!ret) {
3691 if (bit_off == offset) {
3692 ret = 1;
3693 goto out;
3694 } else if (bit_off > offset &&
3695 offset + bytes > bit_off) {
3696 ret = 1;
3697 goto out;
3698 }
3699 }
3700
3701 n = rb_prev(&info->offset_index);
3702 while (n) {
3703 tmp = rb_entry(n, struct btrfs_free_space,
3704 offset_index);
3705 if (tmp->offset + tmp->bytes < offset)
3706 break;
3707 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003708 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003709 continue;
3710 }
3711 info = tmp;
3712 goto have_info;
3713 }
3714
3715 n = rb_next(&info->offset_index);
3716 while (n) {
3717 tmp = rb_entry(n, struct btrfs_free_space,
3718 offset_index);
3719 if (offset + bytes < tmp->offset)
3720 break;
3721 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003722 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003723 continue;
3724 }
3725 info = tmp;
3726 goto have_info;
3727 }
3728
Filipe Manana20005522014-08-29 13:35:13 +01003729 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003730 goto out;
3731 }
3732
3733 if (info->offset == offset) {
3734 ret = 1;
3735 goto out;
3736 }
3737
3738 if (offset > info->offset && offset < info->offset + info->bytes)
3739 ret = 1;
3740out:
3741 spin_unlock(&ctl->tree_lock);
3742 return ret;
3743}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003744#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */