blob: 279c41c4ba50b8a31422c4e24b4e940f4d5625d4 [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 Sterba7949f332019-03-20 13:40:19 +010094struct inode *lookup_free_space_inode(
95 struct btrfs_block_group_cache *block_group,
96 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +080097{
David Sterba7949f332019-03-20 13:40:19 +010098 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +080099 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400100 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800101
102 spin_lock(&block_group->lock);
103 if (block_group->inode)
104 inode = igrab(block_group->inode);
105 spin_unlock(&block_group->lock);
106 if (inode)
107 return inode;
108
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500109 inode = __lookup_free_space_inode(fs_info->tree_root, path,
David Sterbab3470b52019-10-23 18:48:22 +0200110 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800111 if (IS_ERR(inode))
112 return inode;
113
Josef Bacik0af3d002010-06-21 14:48:16 -0400114 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400115 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400116 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400117 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
118 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400119 block_group->disk_cache_state = BTRFS_DC_CLEAR;
120 }
121
Josef Bacik300e4f82011-08-29 14:06:00 -0400122 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400123 block_group->inode = igrab(inode);
124 block_group->iref = 1;
125 }
126 spin_unlock(&block_group->lock);
127
128 return inode;
129}
130
Eric Sandeen48a3b632013-04-25 20:41:01 +0000131static int __create_free_space_inode(struct btrfs_root *root,
132 struct btrfs_trans_handle *trans,
133 struct btrfs_path *path,
134 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400135{
136 struct btrfs_key key;
137 struct btrfs_disk_key disk_key;
138 struct btrfs_free_space_header *header;
139 struct btrfs_inode_item *inode_item;
140 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400141 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400142 int ret;
143
Li Zefan0414efa2011-04-20 10:20:14 +0800144 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400145 if (ret)
146 return ret;
147
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400148 /* We inline crc's for the free disk space cache */
149 if (ino != BTRFS_FREE_INO_OBJECTID)
150 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
151
Josef Bacik0af3d002010-06-21 14:48:16 -0400152 leaf = path->nodes[0];
153 inode_item = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_inode_item);
155 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100156 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400157 sizeof(*inode_item));
158 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
159 btrfs_set_inode_size(leaf, inode_item, 0);
160 btrfs_set_inode_nbytes(leaf, inode_item, 0);
161 btrfs_set_inode_uid(leaf, inode_item, 0);
162 btrfs_set_inode_gid(leaf, inode_item, 0);
163 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400164 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 btrfs_set_inode_nlink(leaf, inode_item, 1);
166 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800167 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400168 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200169 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400170
171 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800172 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400173 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400174 ret = btrfs_insert_empty_item(trans, root, path, &key,
175 sizeof(struct btrfs_free_space_header));
176 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200177 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 return ret;
179 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700180
Josef Bacik0af3d002010-06-21 14:48:16 -0400181 leaf = path->nodes[0];
182 header = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100184 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400185 btrfs_set_free_space_key(leaf, header, &disk_key);
186 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200187 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400188
189 return 0;
190}
191
David Sterba4ca75f12019-03-20 13:42:57 +0100192int create_free_space_inode(struct btrfs_trans_handle *trans,
Li Zefan0414efa2011-04-20 10:20:14 +0800193 struct btrfs_block_group_cache *block_group,
194 struct btrfs_path *path)
195{
196 int ret;
197 u64 ino;
198
David Sterba4ca75f12019-03-20 13:42:57 +0100199 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800200 if (ret < 0)
201 return ret;
202
David Sterba4ca75f12019-03-20 13:42:57 +0100203 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
David Sterbab3470b52019-10-23 18:48:22 +0200204 ino, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800205}
206
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400207int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000208 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400209{
Josef Bacikc8174312011-11-02 09:29:35 -0400210 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000211 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400212
213 /* 1 for slack space, 1 for updating the inode */
Josef Bacik2bd36e72019-08-22 15:14:33 -0400214 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
215 btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400216
Miao Xie7b61cd92013-05-13 13:55:09 +0000217 spin_lock(&rsv->lock);
218 if (rsv->reserved < needed_bytes)
219 ret = -ENOSPC;
220 else
221 ret = 0;
222 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000223 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000224}
225
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500226int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Chris Mason1bbc6212015-04-06 12:46:08 -0700227 struct btrfs_block_group_cache *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000228 struct inode *inode)
229{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500230 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000231 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100232 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700233
Chris Mason1bbc6212015-04-06 12:46:08 -0700234 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500235 struct btrfs_path *path = btrfs_alloc_path();
236
237 if (!path) {
238 ret = -ENOMEM;
239 goto fail;
240 }
Filipe Manana35c76642015-04-30 17:47:05 +0100241 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700242 mutex_lock(&trans->transaction->cache_write_mutex);
243 if (!list_empty(&block_group->io_list)) {
244 list_del_init(&block_group->io_list);
245
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400246 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700247 btrfs_put_block_group(block_group);
248 }
249
250 /*
251 * now that we've truncated the cache away, its no longer
252 * setup or written
253 */
254 spin_lock(&block_group->lock);
255 block_group->disk_cache_state = BTRFS_DC_CLEAR;
256 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500257 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700258 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400259
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200260 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700261 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400262
263 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700264 * We skip the throttling logic for free space cache inodes, so we don't
265 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400266 */
267 ret = btrfs_truncate_inode_items(trans, root, inode,
268 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100269 if (ret)
270 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400271
Li Zefan82d59022011-04-20 10:33:24 +0800272 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700273
Chris Mason1bbc6212015-04-06 12:46:08 -0700274fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100275 if (locked)
276 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100277 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400278 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400279
Li Zefan82d59022011-04-20 10:33:24 +0800280 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400281}
282
David Sterba1d480532017-01-23 17:28:19 +0100283static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400284{
285 struct file_ra_state *ra;
286 unsigned long last_index;
287
288 ra = kzalloc(sizeof(*ra), GFP_NOFS);
289 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100290 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400291
292 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300293 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400294
295 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
296
297 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400298}
299
Chris Mason4c6d1d82015-04-06 13:17:20 -0700300static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400301 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400302{
Miao Xie5349d6c2014-06-19 10:42:49 +0800303 int num_pages;
304 int check_crcs = 0;
305
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300306 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800307
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200308 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800309 check_crcs = 1;
310
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400311 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800312 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400313 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800314 return -ENOSPC;
315
Chris Mason4c6d1d82015-04-06 13:17:20 -0700316 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800317
David Sterba31e818f2015-02-20 18:00:26 +0100318 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400319 if (!io_ctl->pages)
320 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800321
322 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400323 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800324 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700325 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800326
Josef Bacika67509c2011-10-05 15:18:58 -0400327 return 0;
328}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900329ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400330
Chris Mason4c6d1d82015-04-06 13:17:20 -0700331static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400332{
333 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700334 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400335}
336
Chris Mason4c6d1d82015-04-06 13:17:20 -0700337static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400338{
339 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400340 io_ctl->cur = NULL;
341 io_ctl->orig = NULL;
342 }
343}
344
Chris Mason4c6d1d82015-04-06 13:17:20 -0700345static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400346{
Josef Bacikb12d6862013-08-26 17:14:08 -0400347 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400348 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700349 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400350 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300351 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400352 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200353 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400354}
355
Chris Mason4c6d1d82015-04-06 13:17:20 -0700356static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400357{
358 int i;
359
360 io_ctl_unmap_page(io_ctl);
361
362 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800363 if (io_ctl->pages[i]) {
364 ClearPageChecked(io_ctl->pages[i]);
365 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300366 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800367 }
Josef Bacika67509c2011-10-05 15:18:58 -0400368 }
369}
370
Chris Mason4c6d1d82015-04-06 13:17:20 -0700371static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400372 int uptodate)
373{
374 struct page *page;
375 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
376 int i;
377
378 for (i = 0; i < io_ctl->num_pages; i++) {
379 page = find_or_create_page(inode->i_mapping, i, mask);
380 if (!page) {
381 io_ctl_drop_pages(io_ctl);
382 return -ENOMEM;
383 }
384 io_ctl->pages[i] = page;
385 if (uptodate && !PageUptodate(page)) {
386 btrfs_readpage(NULL, page);
387 lock_page(page);
Josef Bacik37971362019-09-24 16:50:43 -0400388 if (page->mapping != inode->i_mapping) {
389 btrfs_err(BTRFS_I(inode)->root->fs_info,
390 "free space cache page truncated");
391 io_ctl_drop_pages(io_ctl);
392 return -EIO;
393 }
Josef Bacika67509c2011-10-05 15:18:58 -0400394 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500395 btrfs_err(BTRFS_I(inode)->root->fs_info,
396 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400397 io_ctl_drop_pages(io_ctl);
398 return -EIO;
399 }
400 }
401 }
402
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500403 for (i = 0; i < io_ctl->num_pages; i++) {
404 clear_page_dirty_for_io(io_ctl->pages[i]);
405 set_page_extent_mapped(io_ctl->pages[i]);
406 }
407
Josef Bacika67509c2011-10-05 15:18:58 -0400408 return 0;
409}
410
Chris Mason4c6d1d82015-04-06 13:17:20 -0700411static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400412{
Al Viro528c0322012-04-13 11:03:55 -0400413 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400414
415 io_ctl_map_page(io_ctl, 1);
416
417 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400418 * Skip the csum areas. If we don't check crcs then we just have a
419 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400420 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400421 if (io_ctl->check_crcs) {
422 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
423 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
424 } else {
425 io_ctl->cur += sizeof(u64);
426 io_ctl->size -= sizeof(u64) * 2;
427 }
Josef Bacika67509c2011-10-05 15:18:58 -0400428
429 val = io_ctl->cur;
430 *val = cpu_to_le64(generation);
431 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400432}
433
Chris Mason4c6d1d82015-04-06 13:17:20 -0700434static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400435{
Al Viro528c0322012-04-13 11:03:55 -0400436 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400437
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400438 /*
439 * Skip the crc area. If we don't check crcs then we just have a 64bit
440 * chunk at the front of the first page.
441 */
442 if (io_ctl->check_crcs) {
443 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
444 io_ctl->size -= sizeof(u64) +
445 (sizeof(u32) * io_ctl->num_pages);
446 } else {
447 io_ctl->cur += sizeof(u64);
448 io_ctl->size -= sizeof(u64) * 2;
449 }
Josef Bacika67509c2011-10-05 15:18:58 -0400450
Josef Bacika67509c2011-10-05 15:18:58 -0400451 gen = io_ctl->cur;
452 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400453 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200454 "space cache generation (%llu) does not match inode (%llu)",
455 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400456 io_ctl_unmap_page(io_ctl);
457 return -EIO;
458 }
459 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400460 return 0;
461}
462
Chris Mason4c6d1d82015-04-06 13:17:20 -0700463static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400464{
465 u32 *tmp;
466 u32 crc = ~(u32)0;
467 unsigned offset = 0;
468
469 if (!io_ctl->check_crcs) {
470 io_ctl_unmap_page(io_ctl);
471 return;
472 }
473
474 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800475 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400476
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200477 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
478 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400479 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700480 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400481 tmp += index;
482 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400483}
484
Chris Mason4c6d1d82015-04-06 13:17:20 -0700485static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400486{
487 u32 *tmp, val;
488 u32 crc = ~(u32)0;
489 unsigned offset = 0;
490
491 if (!io_ctl->check_crcs) {
492 io_ctl_map_page(io_ctl, 0);
493 return 0;
494 }
495
496 if (index == 0)
497 offset = sizeof(u32) * io_ctl->num_pages;
498
Chris Mason2b108262015-04-06 07:48:20 -0700499 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400500 tmp += index;
501 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400502
503 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200504 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
505 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400506 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400507 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200508 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400509 io_ctl_unmap_page(io_ctl);
510 return -EIO;
511 }
512
Josef Bacika67509c2011-10-05 15:18:58 -0400513 return 0;
514}
515
Chris Mason4c6d1d82015-04-06 13:17:20 -0700516static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400517 void *bitmap)
518{
519 struct btrfs_free_space_entry *entry;
520
521 if (!io_ctl->cur)
522 return -ENOSPC;
523
524 entry = io_ctl->cur;
525 entry->offset = cpu_to_le64(offset);
526 entry->bytes = cpu_to_le64(bytes);
527 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
528 BTRFS_FREE_SPACE_EXTENT;
529 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
530 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
531
532 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
533 return 0;
534
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400535 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400536
537 /* No more pages to map */
538 if (io_ctl->index >= io_ctl->num_pages)
539 return 0;
540
541 /* map the next page */
542 io_ctl_map_page(io_ctl, 1);
543 return 0;
544}
545
Chris Mason4c6d1d82015-04-06 13:17:20 -0700546static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400547{
548 if (!io_ctl->cur)
549 return -ENOSPC;
550
551 /*
552 * If we aren't at the start of the current page, unmap this one and
553 * map the next one if there is any left.
554 */
555 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400557 if (io_ctl->index >= io_ctl->num_pages)
558 return -ENOSPC;
559 io_ctl_map_page(io_ctl, 0);
560 }
561
David Sterba69d24802018-06-29 10:56:44 +0200562 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400563 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400564 if (io_ctl->index < io_ctl->num_pages)
565 io_ctl_map_page(io_ctl, 0);
566 return 0;
567}
568
Chris Mason4c6d1d82015-04-06 13:17:20 -0700569static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400570{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400571 /*
572 * If we're not on the boundary we know we've modified the page and we
573 * need to crc the page.
574 */
575 if (io_ctl->cur != io_ctl->orig)
576 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
577 else
578 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400579
580 while (io_ctl->index < io_ctl->num_pages) {
581 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400582 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400583 }
584}
585
Chris Mason4c6d1d82015-04-06 13:17:20 -0700586static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400587 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400588{
589 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500590 int ret;
591
592 if (!io_ctl->cur) {
593 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
594 if (ret)
595 return ret;
596 }
Josef Bacika67509c2011-10-05 15:18:58 -0400597
598 e = io_ctl->cur;
599 entry->offset = le64_to_cpu(e->offset);
600 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400601 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400602 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
603 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
604
605 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400606 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400607
608 io_ctl_unmap_page(io_ctl);
609
Josef Bacik2f120c02011-11-10 20:45:05 -0500610 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400611}
612
Chris Mason4c6d1d82015-04-06 13:17:20 -0700613static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400614 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400615{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400616 int ret;
617
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400618 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
619 if (ret)
620 return ret;
621
David Sterba69d24802018-06-29 10:56:44 +0200622 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400623 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400624
625 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400626}
627
Josef Bacikcd023e72012-05-14 10:06:40 -0400628/*
629 * Since we attach pinned extents after the fact we can have contiguous sections
630 * of free space that are split up in entries. This poses a problem with the
631 * tree logging stuff since it could have allocated across what appears to be 2
632 * entries since we would have merged the entries when adding the pinned extents
633 * back to the free space cache. So run through the space cache that we just
634 * loaded and merge contiguous entries. This will make the log replay stuff not
635 * blow up and it will make for nicer allocator behavior.
636 */
637static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
638{
639 struct btrfs_free_space *e, *prev = NULL;
640 struct rb_node *n;
641
642again:
643 spin_lock(&ctl->tree_lock);
644 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
645 e = rb_entry(n, struct btrfs_free_space, offset_index);
646 if (!prev)
647 goto next;
648 if (e->bitmap || prev->bitmap)
649 goto next;
650 if (prev->offset + prev->bytes == e->offset) {
651 unlink_free_space(ctl, prev);
652 unlink_free_space(ctl, e);
653 prev->bytes += e->bytes;
654 kmem_cache_free(btrfs_free_space_cachep, e);
655 link_free_space(ctl, prev);
656 prev = NULL;
657 spin_unlock(&ctl->tree_lock);
658 goto again;
659 }
660next:
661 prev = e;
662 }
663 spin_unlock(&ctl->tree_lock);
664}
665
Eric Sandeen48a3b632013-04-25 20:41:01 +0000666static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
667 struct btrfs_free_space_ctl *ctl,
668 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400669{
David Sterba3ffbd682018-06-29 10:56:42 +0200670 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400671 struct btrfs_free_space_header *header;
672 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700673 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400675 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800676 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400677 u64 num_entries;
678 u64 num_bitmaps;
679 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400680 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400681 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400682
Josef Bacik9d66e232010-08-25 16:54:15 -0400683 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800684 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400685 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400686
687 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800688 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400689 key.type = 0;
690
691 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800692 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400693 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800694 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400695 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400696 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400697 }
698
Li Zefan0414efa2011-04-20 10:20:14 +0800699 ret = -1;
700
Josef Bacik9d66e232010-08-25 16:54:15 -0400701 leaf = path->nodes[0];
702 header = btrfs_item_ptr(leaf, path->slots[0],
703 struct btrfs_free_space_header);
704 num_entries = btrfs_free_space_entries(leaf, header);
705 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
706 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400707 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400708
Miao Xiee570fd22014-06-19 10:42:50 +0800709 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200711 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800712 offset);
713 return 0;
714 }
715
Josef Bacik9d66e232010-08-25 16:54:15 -0400716 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400717 btrfs_err(fs_info,
718 "free space inode generation (%llu) did not match free space cache generation (%llu)",
719 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400720 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400721 }
722
723 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400724 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400725
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400726 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800727 if (ret)
728 return ret;
729
David Sterba1d480532017-01-23 17:28:19 +0100730 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400731
Josef Bacika67509c2011-10-05 15:18:58 -0400732 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
733 if (ret)
734 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400735
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400736 ret = io_ctl_check_crc(&io_ctl, 0);
737 if (ret)
738 goto free_cache;
739
Josef Bacika67509c2011-10-05 15:18:58 -0400740 ret = io_ctl_check_generation(&io_ctl, generation);
741 if (ret)
742 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400743
Josef Bacika67509c2011-10-05 15:18:58 -0400744 while (num_entries) {
745 e = kmem_cache_zalloc(btrfs_free_space_cachep,
746 GFP_NOFS);
747 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400748 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400749
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400750 ret = io_ctl_read_entry(&io_ctl, e, &type);
751 if (ret) {
752 kmem_cache_free(btrfs_free_space_cachep, e);
753 goto free_cache;
754 }
755
Josef Bacika67509c2011-10-05 15:18:58 -0400756 if (!e->bytes) {
757 kmem_cache_free(btrfs_free_space_cachep, e);
758 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400759 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400760
Josef Bacika67509c2011-10-05 15:18:58 -0400761 if (type == BTRFS_FREE_SPACE_EXTENT) {
762 spin_lock(&ctl->tree_lock);
763 ret = link_free_space(ctl, e);
764 spin_unlock(&ctl->tree_lock);
765 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000767 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500768 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400769 goto free_cache;
770 }
Josef Bacika67509c2011-10-05 15:18:58 -0400771 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400772 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400773 num_bitmaps--;
Christophe Leroy3acd4852019-08-21 15:05:55 +0000774 e->bitmap = kmem_cache_zalloc(
775 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400776 if (!e->bitmap) {
777 kmem_cache_free(
778 btrfs_free_space_cachep, e);
779 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400780 }
Josef Bacika67509c2011-10-05 15:18:58 -0400781 spin_lock(&ctl->tree_lock);
782 ret = link_free_space(ctl, e);
783 ctl->total_bitmaps++;
784 ctl->op->recalc_thresholds(ctl);
785 spin_unlock(&ctl->tree_lock);
786 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400787 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000788 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400789 kmem_cache_free(btrfs_free_space_cachep, e);
790 goto free_cache;
791 }
792 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400793 }
794
Josef Bacika67509c2011-10-05 15:18:58 -0400795 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400796 }
797
Josef Bacik2f120c02011-11-10 20:45:05 -0500798 io_ctl_unmap_page(&io_ctl);
799
Josef Bacika67509c2011-10-05 15:18:58 -0400800 /*
801 * We add the bitmaps at the end of the entries in order that
802 * the bitmap entries are added to the cache.
803 */
804 list_for_each_entry_safe(e, n, &bitmaps, list) {
805 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400806 ret = io_ctl_read_bitmap(&io_ctl, e);
807 if (ret)
808 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400809 }
810
811 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400812 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400813 ret = 1;
814out:
Josef Bacika67509c2011-10-05 15:18:58 -0400815 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400816 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400817free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400818 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800819 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400820 goto out;
821}
822
David Sterbabb6cb1c2019-03-20 13:47:15 +0100823int load_free_space_cache(struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400824{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100825 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800826 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800827 struct inode *inode;
828 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400829 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800830 bool matched;
David Sterbabf38be62019-10-23 18:48:11 +0200831 u64 used = block_group->used;
Li Zefan0414efa2011-04-20 10:20:14 +0800832
833 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800834 * If this block group has been marked to be cleared for one reason or
835 * another then we can't trust the on disk cache, so just return.
836 */
837 spin_lock(&block_group->lock);
838 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
839 spin_unlock(&block_group->lock);
840 return 0;
841 }
842 spin_unlock(&block_group->lock);
843
844 path = btrfs_alloc_path();
845 if (!path)
846 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400847 path->search_commit_root = 1;
848 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800849
Filipe Manana4222ea72018-10-24 10:13:03 +0100850 /*
851 * We must pass a path with search_commit_root set to btrfs_iget in
852 * order to avoid a deadlock when allocating extents for the tree root.
853 *
854 * When we are COWing an extent buffer from the tree root, when looking
855 * for a free extent, at extent-tree.c:find_free_extent(), we can find
856 * block group without its free space cache loaded. When we find one
857 * we must load its space cache which requires reading its free space
858 * cache's inode item from the root tree. If this inode item is located
859 * in the same leaf that we started COWing before, then we end up in
860 * deadlock on the extent buffer (trying to read lock it when we
861 * previously write locked it).
862 *
863 * It's safe to read the inode item using the commit root because
864 * block groups, once loaded, stay in memory forever (until they are
865 * removed) as well as their space caches once loaded. New block groups
866 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
867 * we will never try to read their inode item while the fs is mounted.
868 */
David Sterba7949f332019-03-20 13:40:19 +0100869 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800870 if (IS_ERR(inode)) {
871 btrfs_free_path(path);
872 return 0;
873 }
874
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400875 /* We may have converted the inode and made the cache invalid. */
876 spin_lock(&block_group->lock);
877 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
878 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900879 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400880 goto out;
881 }
882 spin_unlock(&block_group->lock);
883
Li Zefan0414efa2011-04-20 10:20:14 +0800884 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
David Sterbab3470b52019-10-23 18:48:22 +0200885 path, block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800886 btrfs_free_path(path);
887 if (ret <= 0)
888 goto out;
889
890 spin_lock(&ctl->tree_lock);
David Sterbab3470b52019-10-23 18:48:22 +0200891 matched = (ctl->free_space == (block_group->length - used -
Li Zefan0414efa2011-04-20 10:20:14 +0800892 block_group->bytes_super));
893 spin_unlock(&ctl->tree_lock);
894
895 if (!matched) {
896 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400897 btrfs_warn(fs_info,
898 "block group %llu has wrong amount of free space",
David Sterbab3470b52019-10-23 18:48:22 +0200899 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800900 ret = -1;
901 }
902out:
903 if (ret < 0) {
904 /* This cache is bogus, make sure it gets cleared */
905 spin_lock(&block_group->lock);
906 block_group->disk_cache_state = BTRFS_DC_CLEAR;
907 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800908 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800909
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400910 btrfs_warn(fs_info,
911 "failed to load free space cache for block group %llu, rebuilding it now",
David Sterbab3470b52019-10-23 18:48:22 +0200912 block_group->start);
Li Zefan0414efa2011-04-20 10:20:14 +0800913 }
914
915 iput(inode);
916 return ret;
917}
918
Chris Masond4452bc2014-05-19 20:47:56 -0700919static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700920int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700921 struct btrfs_free_space_ctl *ctl,
922 struct btrfs_block_group_cache *block_group,
923 int *entries, int *bitmaps,
924 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400925{
Josef Bacikc09544e2011-08-30 10:19:10 -0400926 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700927 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700928 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700929 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000930 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400931
Josef Bacik43be2142011-04-01 14:55:00 +0000932 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700933 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000934 cluster = list_entry(block_group->cluster_list.next,
935 struct btrfs_free_cluster,
936 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700937 }
Josef Bacik43be2142011-04-01 14:55:00 +0000938
Josef Bacikf75b1302011-10-05 10:00:18 -0400939 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700940 cluster_locked = cluster;
941 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400942 node = rb_first(&cluster->root);
943 cluster = NULL;
944 }
945
Josef Bacik0cb59c92010-07-02 12:14:14 -0400946 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400947 while (node) {
948 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400949
Josef Bacika67509c2011-10-05 15:18:58 -0400950 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700951 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000952
Chris Masond4452bc2014-05-19 20:47:56 -0700953 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400954 e->bitmap);
955 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700956 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400957
958 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700959 list_add_tail(&e->list, bitmap_list);
960 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400961 }
962 node = rb_next(node);
963 if (!node && cluster) {
964 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700965 cluster_locked = cluster;
966 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400967 cluster = NULL;
968 }
969 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700970 if (cluster_locked) {
971 spin_unlock(&cluster_locked->lock);
972 cluster_locked = NULL;
973 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000974
975 /*
976 * Make sure we don't miss any range that was removed from our rbtree
977 * because trimming is running. Otherwise after a umount+mount (or crash
978 * after committing the transaction) we would leak free space and get
979 * an inconsistent free space cache report from fsck.
980 */
981 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
982 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
983 trim_entry->bytes, NULL);
984 if (ret)
985 goto fail;
986 *entries += 1;
987 }
988
Chris Masond4452bc2014-05-19 20:47:56 -0700989 return 0;
990fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700991 if (cluster_locked)
992 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700993 return -ENOSPC;
994}
995
996static noinline_for_stack int
997update_cache_item(struct btrfs_trans_handle *trans,
998 struct btrfs_root *root,
999 struct inode *inode,
1000 struct btrfs_path *path, u64 offset,
1001 int entries, int bitmaps)
1002{
1003 struct btrfs_key key;
1004 struct btrfs_free_space_header *header;
1005 struct extent_buffer *leaf;
1006 int ret;
1007
1008 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1009 key.offset = offset;
1010 key.type = 0;
1011
1012 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1013 if (ret < 0) {
1014 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001015 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001016 goto fail;
1017 }
1018 leaf = path->nodes[0];
1019 if (ret > 0) {
1020 struct btrfs_key found_key;
1021 ASSERT(path->slots[0]);
1022 path->slots[0]--;
1023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1024 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1025 found_key.offset != offset) {
1026 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
Omar Sandovale1821632019-08-15 14:04:04 -07001027 inode->i_size - 1, EXTENT_DELALLOC, 0,
1028 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001029 btrfs_release_path(path);
1030 goto fail;
1031 }
1032 }
1033
1034 BTRFS_I(inode)->generation = trans->transid;
1035 header = btrfs_item_ptr(leaf, path->slots[0],
1036 struct btrfs_free_space_header);
1037 btrfs_set_free_space_entries(leaf, header, entries);
1038 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1039 btrfs_set_free_space_generation(leaf, header, trans->transid);
1040 btrfs_mark_buffer_dirty(leaf);
1041 btrfs_release_path(path);
1042
1043 return 0;
1044
1045fail:
1046 return -1;
1047}
1048
David Sterba6701bdb2019-03-20 13:49:09 +01001049static noinline_for_stack int write_pinned_extent_entries(
Miao Xie5349d6c2014-06-19 10:42:49 +08001050 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001051 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001052 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001053{
1054 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001055 struct extent_io_tree *unpin = NULL;
1056 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001057
Miao Xie5349d6c2014-06-19 10:42:49 +08001058 if (!block_group)
1059 return 0;
1060
Josef Bacika67509c2011-10-05 15:18:58 -04001061 /*
1062 * We want to add any pinned extents to our free space cache
1063 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001064 *
Li Zefandb804f22012-01-10 16:41:01 +08001065 * We shouldn't have switched the pinned extents yet so this is the
1066 * right one
1067 */
David Sterba6701bdb2019-03-20 13:49:09 +01001068 unpin = block_group->fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001069
David Sterbab3470b52019-10-23 18:48:22 +02001070 start = block_group->start;
Li Zefandb804f22012-01-10 16:41:01 +08001071
David Sterbab3470b52019-10-23 18:48:22 +02001072 while (start < block_group->start + block_group->length) {
Li Zefandb804f22012-01-10 16:41:01 +08001073 ret = find_first_extent_bit(unpin, start,
1074 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001075 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001076 if (ret)
1077 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001078
Josef Bacika67509c2011-10-05 15:18:58 -04001079 /* This pinned extent is out of our range */
David Sterbab3470b52019-10-23 18:48:22 +02001080 if (extent_start >= block_group->start + block_group->length)
Miao Xie5349d6c2014-06-19 10:42:49 +08001081 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001082
Li Zefandb804f22012-01-10 16:41:01 +08001083 extent_start = max(extent_start, start);
David Sterbab3470b52019-10-23 18:48:22 +02001084 extent_end = min(block_group->start + block_group->length,
1085 extent_end + 1);
Li Zefandb804f22012-01-10 16:41:01 +08001086 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001087
Chris Masond4452bc2014-05-19 20:47:56 -07001088 *entries += 1;
1089 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001090 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001091 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001092
Li Zefandb804f22012-01-10 16:41:01 +08001093 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001094 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001095
Miao Xie5349d6c2014-06-19 10:42:49 +08001096 return 0;
1097}
1098
1099static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001100write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001101{
Geliang Tang7ae16812015-12-18 22:17:00 +08001102 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001103 int ret;
1104
Josef Bacik0cb59c92010-07-02 12:14:14 -04001105 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001106 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001107 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001108 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001109 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001110 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001111 }
1112
Miao Xie5349d6c2014-06-19 10:42:49 +08001113 return 0;
1114}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001115
Miao Xie5349d6c2014-06-19 10:42:49 +08001116static int flush_dirty_cache(struct inode *inode)
1117{
1118 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001119
Josef Bacik0ef8b722013-10-25 16:13:35 -04001120 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001121 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001122 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Omar Sandovale1821632019-08-15 14:04:04 -07001123 EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001124
Miao Xie5349d6c2014-06-19 10:42:49 +08001125 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001126}
1127
1128static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001129cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001130{
Geliang Tang7ae16812015-12-18 22:17:00 +08001131 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001132
Geliang Tang7ae16812015-12-18 22:17:00 +08001133 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001134 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001135}
1136
1137static void noinline_for_stack
1138cleanup_write_cache_enospc(struct inode *inode,
1139 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001140 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001141{
Chris Masond4452bc2014-05-19 20:47:56 -07001142 io_ctl_drop_pages(io_ctl);
1143 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001144 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001145}
1146
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001147static int __btrfs_wait_cache_io(struct btrfs_root *root,
1148 struct btrfs_trans_handle *trans,
1149 struct btrfs_block_group_cache *block_group,
1150 struct btrfs_io_ctl *io_ctl,
1151 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001152{
1153 int ret;
1154 struct inode *inode = io_ctl->inode;
1155
Chris Mason1bbc6212015-04-06 12:46:08 -07001156 if (!inode)
1157 return 0;
1158
Chris Masonc9dc4c62015-04-04 17:14:42 -07001159 /* Flush the dirty pages in the cache file. */
1160 ret = flush_dirty_cache(inode);
1161 if (ret)
1162 goto out;
1163
1164 /* Update the cache item to tell everyone this cache file is valid. */
1165 ret = update_cache_item(trans, root, inode, path, offset,
1166 io_ctl->entries, io_ctl->bitmaps);
1167out:
1168 io_ctl_free(io_ctl);
1169 if (ret) {
1170 invalidate_inode_pages2(inode->i_mapping);
1171 BTRFS_I(inode)->generation = 0;
1172 if (block_group) {
1173#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001174 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001175 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001176 block_group->start);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001177#endif
1178 }
1179 }
1180 btrfs_update_inode(trans, root, inode);
1181
1182 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001183 /* the dirty list is protected by the dirty_bgs_lock */
1184 spin_lock(&trans->transaction->dirty_bgs_lock);
1185
1186 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001187 spin_lock(&block_group->lock);
1188
1189 /*
1190 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001191 * the dirty list while waiting for IO. Otherwise our
1192 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001193 */
1194 if (!ret && list_empty(&block_group->dirty_list))
1195 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1196 else if (ret)
1197 block_group->disk_cache_state = BTRFS_DC_ERROR;
1198
1199 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001200 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001201 io_ctl->inode = NULL;
1202 iput(inode);
1203 }
1204
1205 return ret;
1206
1207}
1208
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001209static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1210 struct btrfs_trans_handle *trans,
1211 struct btrfs_io_ctl *io_ctl,
1212 struct btrfs_path *path)
1213{
1214 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1215}
1216
1217int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1218 struct btrfs_block_group_cache *block_group,
1219 struct btrfs_path *path)
1220{
1221 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1222 block_group, &block_group->io_ctl,
David Sterbab3470b52019-10-23 18:48:22 +02001223 path, block_group->start);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001224}
1225
Chris Masond4452bc2014-05-19 20:47:56 -07001226/**
1227 * __btrfs_write_out_cache - write out cached info to an inode
1228 * @root - the root the inode belongs to
1229 * @ctl - the free space cache we are going to write out
1230 * @block_group - the block_group for this cache if it belongs to a block_group
1231 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001232 *
1233 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001234 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001235 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001236 */
1237static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1238 struct btrfs_free_space_ctl *ctl,
1239 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001240 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001241 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001242{
1243 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001244 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001245 int entries = 0;
1246 int bitmaps = 0;
1247 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001248 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001249
1250 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001251 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001252
Chris Masonc9dc4c62015-04-04 17:14:42 -07001253 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001254 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001255 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001256 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001257
Miao Xiee570fd22014-06-19 10:42:50 +08001258 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1259 down_write(&block_group->data_rwsem);
1260 spin_lock(&block_group->lock);
1261 if (block_group->delalloc_bytes) {
1262 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1263 spin_unlock(&block_group->lock);
1264 up_write(&block_group->data_rwsem);
1265 BTRFS_I(inode)->generation = 0;
1266 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001267 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001268 goto out;
1269 }
1270 spin_unlock(&block_group->lock);
1271 }
1272
Chris Masond4452bc2014-05-19 20:47:56 -07001273 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001274 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1275 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001276 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001277
1278 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001279 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001280
Chris Masonc9dc4c62015-04-04 17:14:42 -07001281 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001282
Filipe Manana55507ce2014-12-01 17:04:09 +00001283 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001284 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001285 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001286 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001287 block_group, &entries, &bitmaps,
1288 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001289 if (ret)
1290 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001291
Miao Xie5349d6c2014-06-19 10:42:49 +08001292 /*
1293 * Some spaces that are freed in the current transaction are pinned,
1294 * they will be added into free space cache after the transaction is
1295 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001296 *
1297 * If this changes while we are working we'll get added back to
1298 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001299 */
David Sterba6701bdb2019-03-20 13:49:09 +01001300 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001301 if (ret)
1302 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001303
Filipe Manana55507ce2014-12-01 17:04:09 +00001304 /*
1305 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1306 * locked while doing it because a concurrent trim can be manipulating
1307 * or freeing the bitmap.
1308 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001309 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001310 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001311 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001312 if (ret)
1313 goto out_nospc;
1314
1315 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001316 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001317
1318 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001319 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1320 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001321 if (ret)
1322 goto out_nospc;
1323
Miao Xiee570fd22014-06-19 10:42:50 +08001324 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1325 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001326 /*
1327 * Release the pages and unlock the extent, we will flush
1328 * them out later
1329 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001330 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001331
1332 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001333 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001334
Chris Masonc9dc4c62015-04-04 17:14:42 -07001335 /*
1336 * at this point the pages are under IO and we're happy,
1337 * The caller is responsible for waiting on them and updating the
1338 * the cache and the inode
1339 */
1340 io_ctl->entries = entries;
1341 io_ctl->bitmaps = bitmaps;
1342
1343 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001344 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001345 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001346
Chris Masonc9dc4c62015-04-04 17:14:42 -07001347 return 0;
1348
Josef Bacik2f356122011-06-10 15:31:13 -04001349out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001350 io_ctl->inode = NULL;
1351 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001352 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001353 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001354 BTRFS_I(inode)->generation = 0;
1355 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001356 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001357 if (must_iput)
1358 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001359 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001360
Chris Masona3bdccc2015-04-24 11:00:00 -07001361out_nospc_locked:
1362 cleanup_bitmap_list(&bitmap_list);
1363 spin_unlock(&ctl->tree_lock);
1364 mutex_unlock(&ctl->cache_writeout_mutex);
1365
Josef Bacika67509c2011-10-05 15:18:58 -04001366out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001367 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001368
Josef Bacikb77000e2017-11-15 16:20:52 -05001369out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001370 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1371 up_write(&block_group->data_rwsem);
1372
Josef Bacika67509c2011-10-05 15:18:58 -04001373 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001374}
1375
David Sterbafe041532019-03-20 13:51:56 +01001376int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
Li Zefan0414efa2011-04-20 10:20:14 +08001377 struct btrfs_block_group_cache *block_group,
1378 struct btrfs_path *path)
1379{
David Sterbafe041532019-03-20 13:51:56 +01001380 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001381 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1382 struct inode *inode;
1383 int ret = 0;
1384
Li Zefan0414efa2011-04-20 10:20:14 +08001385 spin_lock(&block_group->lock);
1386 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1387 spin_unlock(&block_group->lock);
1388 return 0;
1389 }
1390 spin_unlock(&block_group->lock);
1391
David Sterba7949f332019-03-20 13:40:19 +01001392 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001393 if (IS_ERR(inode))
1394 return 0;
1395
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001396 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1397 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001398 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001399#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001400 btrfs_err(fs_info,
1401 "failed to write free space cache for block group %llu",
David Sterbab3470b52019-10-23 18:48:22 +02001402 block_group->start);
Josef Bacikc09544e2011-08-30 10:19:10 -04001403#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001404 spin_lock(&block_group->lock);
1405 block_group->disk_cache_state = BTRFS_DC_ERROR;
1406 spin_unlock(&block_group->lock);
1407
1408 block_group->io_ctl.inode = NULL;
1409 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001410 }
1411
Chris Masonc9dc4c62015-04-04 17:14:42 -07001412 /*
1413 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1414 * to wait for IO and put the inode
1415 */
1416
Josef Bacik0cb59c92010-07-02 12:14:14 -04001417 return ret;
1418}
1419
Li Zefan34d52cb2011-03-29 13:46:06 +08001420static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001421 u64 offset)
1422{
Josef Bacikb12d6862013-08-26 17:14:08 -04001423 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001424 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001425 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001426}
1427
Li Zefan34d52cb2011-03-29 13:46:06 +08001428static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001429{
Li Zefan34d52cb2011-03-29 13:46:06 +08001430 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001431}
1432
Li Zefan34d52cb2011-03-29 13:46:06 +08001433static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001434 u64 offset)
1435{
1436 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001437 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001438
Li Zefan34d52cb2011-03-29 13:46:06 +08001439 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1440 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001441 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001442 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001443 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001444
1445 return bitmap_start;
1446}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001447
1448static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001449 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001450{
1451 struct rb_node **p = &root->rb_node;
1452 struct rb_node *parent = NULL;
1453 struct btrfs_free_space *info;
1454
1455 while (*p) {
1456 parent = *p;
1457 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1458
Josef Bacik96303082009-07-13 21:29:25 -04001459 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001460 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001461 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001462 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001463 } else {
1464 /*
1465 * we could have a bitmap entry and an extent entry
1466 * share the same offset. If this is the case, we want
1467 * the extent entry to always be found first if we do a
1468 * linear search through the tree, since we want to have
1469 * the quickest allocation time, and allocating from an
1470 * extent is faster than allocating from a bitmap. So
1471 * if we're inserting a bitmap and we find an entry at
1472 * this offset, we want to go right, or after this entry
1473 * logically. If we are inserting an extent and we've
1474 * found a bitmap, we want to go left, or before
1475 * logically.
1476 */
1477 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001478 if (info->bitmap) {
1479 WARN_ON_ONCE(1);
1480 return -EEXIST;
1481 }
Josef Bacik96303082009-07-13 21:29:25 -04001482 p = &(*p)->rb_right;
1483 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001484 if (!info->bitmap) {
1485 WARN_ON_ONCE(1);
1486 return -EEXIST;
1487 }
Josef Bacik96303082009-07-13 21:29:25 -04001488 p = &(*p)->rb_left;
1489 }
1490 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001491 }
1492
1493 rb_link_node(node, parent, p);
1494 rb_insert_color(node, root);
1495
1496 return 0;
1497}
1498
1499/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001500 * searches the tree for the given offset.
1501 *
Josef Bacik96303082009-07-13 21:29:25 -04001502 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1503 * want a section that has at least bytes size and comes at or after the given
1504 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001505 */
Josef Bacik96303082009-07-13 21:29:25 -04001506static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001507tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001508 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509{
Li Zefan34d52cb2011-03-29 13:46:06 +08001510 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001511 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001512
Josef Bacik96303082009-07-13 21:29:25 -04001513 /* find entry that is closest to the 'offset' */
1514 while (1) {
1515 if (!n) {
1516 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001517 break;
1518 }
Josef Bacik96303082009-07-13 21:29:25 -04001519
1520 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1521 prev = entry;
1522
1523 if (offset < entry->offset)
1524 n = n->rb_left;
1525 else if (offset > entry->offset)
1526 n = n->rb_right;
1527 else
1528 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001529 }
1530
Josef Bacik96303082009-07-13 21:29:25 -04001531 if (bitmap_only) {
1532 if (!entry)
1533 return NULL;
1534 if (entry->bitmap)
1535 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001536
Josef Bacik96303082009-07-13 21:29:25 -04001537 /*
1538 * bitmap entry and extent entry may share same offset,
1539 * in that case, bitmap entry comes after extent entry.
1540 */
1541 n = rb_next(n);
1542 if (!n)
1543 return NULL;
1544 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1545 if (entry->offset != offset)
1546 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001547
Josef Bacik96303082009-07-13 21:29:25 -04001548 WARN_ON(!entry->bitmap);
1549 return entry;
1550 } else if (entry) {
1551 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001552 /*
Josef Bacik96303082009-07-13 21:29:25 -04001553 * if previous extent entry covers the offset,
1554 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001555 */
Miao Xiede6c4112012-10-18 08:18:01 +00001556 n = rb_prev(&entry->offset_index);
1557 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001558 prev = rb_entry(n, struct btrfs_free_space,
1559 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001560 if (!prev->bitmap &&
1561 prev->offset + prev->bytes > offset)
1562 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001563 }
Josef Bacik96303082009-07-13 21:29:25 -04001564 }
1565 return entry;
1566 }
1567
1568 if (!prev)
1569 return NULL;
1570
1571 /* find last entry before the 'offset' */
1572 entry = prev;
1573 if (entry->offset > offset) {
1574 n = rb_prev(&entry->offset_index);
1575 if (n) {
1576 entry = rb_entry(n, struct btrfs_free_space,
1577 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001578 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001579 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001580 if (fuzzy)
1581 return entry;
1582 else
1583 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001584 }
1585 }
1586
Josef Bacik96303082009-07-13 21:29:25 -04001587 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001588 n = rb_prev(&entry->offset_index);
1589 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001590 prev = rb_entry(n, struct btrfs_free_space,
1591 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001592 if (!prev->bitmap &&
1593 prev->offset + prev->bytes > offset)
1594 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001595 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001596 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001597 return entry;
1598 } else if (entry->offset + entry->bytes > offset)
1599 return entry;
1600
1601 if (!fuzzy)
1602 return NULL;
1603
1604 while (1) {
1605 if (entry->bitmap) {
1606 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001607 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001608 break;
1609 } else {
1610 if (entry->offset + entry->bytes > offset)
1611 break;
1612 }
1613
1614 n = rb_next(&entry->offset_index);
1615 if (!n)
1616 return NULL;
1617 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1618 }
1619 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001620}
1621
Li Zefanf333adb2010-11-09 14:57:39 +08001622static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001623__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001624 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001625{
Li Zefan34d52cb2011-03-29 13:46:06 +08001626 rb_erase(&info->offset_index, &ctl->free_space_offset);
1627 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001628}
1629
Li Zefan34d52cb2011-03-29 13:46:06 +08001630static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001631 struct btrfs_free_space *info)
1632{
Li Zefan34d52cb2011-03-29 13:46:06 +08001633 __unlink_free_space(ctl, info);
1634 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001635}
1636
Li Zefan34d52cb2011-03-29 13:46:06 +08001637static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001638 struct btrfs_free_space *info)
1639{
1640 int ret = 0;
1641
Josef Bacikb12d6862013-08-26 17:14:08 -04001642 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001643 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001644 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001645 if (ret)
1646 return ret;
1647
Li Zefan34d52cb2011-03-29 13:46:06 +08001648 ctl->free_space += info->bytes;
1649 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001650 return ret;
1651}
1652
Li Zefan34d52cb2011-03-29 13:46:06 +08001653static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001654{
Li Zefan34d52cb2011-03-29 13:46:06 +08001655 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001656 u64 max_bytes;
1657 u64 bitmap_bytes;
1658 u64 extent_bytes;
David Sterbab3470b52019-10-23 18:48:22 +02001659 u64 size = block_group->length;
Feifei Xu0ef64472016-06-01 19:18:24 +08001660 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1661 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001662
Feifei Xu0ef64472016-06-01 19:18:24 +08001663 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001664
Josef Bacikb12d6862013-08-26 17:14:08 -04001665 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001666
1667 /*
1668 * The goal is to keep the total amount of memory used per 1gb of space
1669 * at or below 32k, so we need to adjust how much memory we allow to be
1670 * used by extent based free space tracking
1671 */
Byongho Leeee221842015-12-15 01:42:10 +09001672 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001673 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1674 else
Byongho Leeee221842015-12-15 01:42:10 +09001675 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001676
Josef Bacik25891f72009-09-11 16:11:20 -04001677 /*
1678 * we want to account for 1 more bitmap than what we have so we can make
1679 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1680 * we add more bitmaps.
1681 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001682 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001683
Josef Bacik25891f72009-09-11 16:11:20 -04001684 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001685 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001686 return;
Josef Bacik96303082009-07-13 21:29:25 -04001687 }
Josef Bacik25891f72009-09-11 16:11:20 -04001688
1689 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001690 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001691 * bytes we can have, or whatever is less than that.
1692 */
1693 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001694 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001695
Li Zefan34d52cb2011-03-29 13:46:06 +08001696 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001697 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001698}
1699
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001700static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1701 struct btrfs_free_space *info,
1702 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001703{
Li Zefanf38b6e72011-03-14 13:40:51 +08001704 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001705
Li Zefan34d52cb2011-03-29 13:46:06 +08001706 start = offset_to_bit(info->offset, ctl->unit, offset);
1707 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001708 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001709
Li Zefanf38b6e72011-03-14 13:40:51 +08001710 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001711
1712 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001713 if (info->max_extent_size > ctl->unit)
1714 info->max_extent_size = 0;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001715}
1716
1717static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1718 struct btrfs_free_space *info, u64 offset,
1719 u64 bytes)
1720{
1721 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001722 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001723}
1724
Li Zefan34d52cb2011-03-29 13:46:06 +08001725static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001726 struct btrfs_free_space *info, u64 offset,
1727 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001728{
Li Zefanf38b6e72011-03-14 13:40:51 +08001729 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001730
Li Zefan34d52cb2011-03-29 13:46:06 +08001731 start = offset_to_bit(info->offset, ctl->unit, offset);
1732 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001733 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001734
Li Zefanf38b6e72011-03-14 13:40:51 +08001735 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001736
1737 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001738 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001739}
1740
Miao Xiea4820392013-09-09 13:19:42 +08001741/*
1742 * If we can not find suitable extent, we will use bytes to record
1743 * the size of the max extent.
1744 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001745static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001746 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001747 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001748{
1749 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001750 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001751 unsigned long bits, i;
1752 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001753 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001754
Josef Bacikcef40482015-10-02 16:09:42 -04001755 /*
1756 * Skip searching the bitmap if we don't have a contiguous section that
1757 * is large enough for this allocation.
1758 */
Josef Bacik0584f712015-10-02 16:12:23 -04001759 if (for_alloc &&
1760 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001761 bitmap_info->max_extent_size < *bytes) {
1762 *bytes = bitmap_info->max_extent_size;
1763 return -1;
1764 }
1765
Li Zefan34d52cb2011-03-29 13:46:06 +08001766 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001767 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001768 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001769
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001770 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001771 if (for_alloc && bits == 1) {
1772 found_bits = 1;
1773 break;
1774 }
Josef Bacik96303082009-07-13 21:29:25 -04001775 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1776 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001777 extent_bits = next_zero - i;
1778 if (extent_bits >= bits) {
1779 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001780 break;
Miao Xiea4820392013-09-09 13:19:42 +08001781 } else if (extent_bits > max_bits) {
1782 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001783 }
1784 i = next_zero;
1785 }
1786
1787 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001788 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1789 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001790 return 0;
1791 }
1792
Miao Xiea4820392013-09-09 13:19:42 +08001793 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001794 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001795 return -1;
1796}
1797
Josef Bacikad22cf62018-10-12 15:32:33 -04001798static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1799{
1800 if (entry->bitmap)
1801 return entry->max_extent_size;
1802 return entry->bytes;
1803}
1804
Miao Xiea4820392013-09-09 13:19:42 +08001805/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001806static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001807find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001808 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001809{
1810 struct btrfs_free_space *entry;
1811 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001812 u64 tmp;
1813 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001814 int ret;
1815
Li Zefan34d52cb2011-03-29 13:46:06 +08001816 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001817 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001818
Li Zefan34d52cb2011-03-29 13:46:06 +08001819 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001820 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001821 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001822
1823 for (node = &entry->offset_index; node; node = rb_next(node)) {
1824 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001825 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001826 *max_extent_size = max(get_max_extent_size(entry),
1827 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001828 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001829 }
Josef Bacik96303082009-07-13 21:29:25 -04001830
David Woodhouse53b381b2013-01-29 18:40:14 -05001831 /* make sure the space returned is big enough
1832 * to match our requested alignment
1833 */
1834 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001835 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001836 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001837 tmp = tmp * align + ctl->start;
1838 align_off = tmp - entry->offset;
1839 } else {
1840 align_off = 0;
1841 tmp = entry->offset;
1842 }
1843
Miao Xiea4820392013-09-09 13:19:42 +08001844 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001845 *max_extent_size = max(get_max_extent_size(entry),
1846 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001847 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001848 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001849
Josef Bacik96303082009-07-13 21:29:25 -04001850 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001851 u64 size = *bytes;
1852
Josef Bacik0584f712015-10-02 16:12:23 -04001853 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001854 if (!ret) {
1855 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001856 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001857 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001858 } else {
1859 *max_extent_size =
1860 max(get_max_extent_size(entry),
1861 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001862 }
Josef Bacik96303082009-07-13 21:29:25 -04001863 continue;
1864 }
1865
David Woodhouse53b381b2013-01-29 18:40:14 -05001866 *offset = tmp;
1867 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001868 return entry;
1869 }
Miao Xiea4820392013-09-09 13:19:42 +08001870out:
Josef Bacik96303082009-07-13 21:29:25 -04001871 return NULL;
1872}
1873
Li Zefan34d52cb2011-03-29 13:46:06 +08001874static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001875 struct btrfs_free_space *info, u64 offset)
1876{
Li Zefan34d52cb2011-03-29 13:46:06 +08001877 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001878 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001879 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001880 link_free_space(ctl, info);
1881 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001882
Li Zefan34d52cb2011-03-29 13:46:06 +08001883 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001884}
1885
Li Zefan34d52cb2011-03-29 13:46:06 +08001886static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001887 struct btrfs_free_space *bitmap_info)
1888{
Li Zefan34d52cb2011-03-29 13:46:06 +08001889 unlink_free_space(ctl, bitmap_info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00001890 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001891 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001892 ctl->total_bitmaps--;
1893 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001894}
1895
Li Zefan34d52cb2011-03-29 13:46:06 +08001896static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001897 struct btrfs_free_space *bitmap_info,
1898 u64 *offset, u64 *bytes)
1899{
1900 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001901 u64 search_start, search_bytes;
1902 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001903
1904again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001905 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001906
Josef Bacik6606bb92009-07-31 11:03:58 -04001907 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001908 * We need to search for bits in this bitmap. We could only cover some
1909 * of the extent in this bitmap thanks to how we add space, so we need
1910 * to search for as much as it as we can and clear that amount, and then
1911 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001912 */
1913 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001914 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001915 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001916 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1917 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001918 if (ret < 0 || search_start != *offset)
1919 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001920
Josef Bacikbdb7d302012-06-27 15:10:56 -04001921 /* We may have found more bits than what we need */
1922 search_bytes = min(search_bytes, *bytes);
1923
1924 /* Cannot clear past the end of the bitmap */
1925 search_bytes = min(search_bytes, end - search_start + 1);
1926
1927 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1928 *offset += search_bytes;
1929 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001930
1931 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001932 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001933 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001934 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001935
Josef Bacik6606bb92009-07-31 11:03:58 -04001936 /*
1937 * no entry after this bitmap, but we still have bytes to
1938 * remove, so something has gone wrong.
1939 */
1940 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001941 return -EINVAL;
1942
Josef Bacik6606bb92009-07-31 11:03:58 -04001943 bitmap_info = rb_entry(next, struct btrfs_free_space,
1944 offset_index);
1945
1946 /*
1947 * if the next entry isn't a bitmap we need to return to let the
1948 * extent stuff do its work.
1949 */
Josef Bacik96303082009-07-13 21:29:25 -04001950 if (!bitmap_info->bitmap)
1951 return -EAGAIN;
1952
Josef Bacik6606bb92009-07-31 11:03:58 -04001953 /*
1954 * Ok the next item is a bitmap, but it may not actually hold
1955 * the information for the rest of this free space stuff, so
1956 * look for it, and if we don't find it return so we can try
1957 * everything over again.
1958 */
1959 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001960 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001961 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001962 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001963 if (ret < 0 || search_start != *offset)
1964 return -EAGAIN;
1965
Josef Bacik96303082009-07-13 21:29:25 -04001966 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001967 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001968 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001969
1970 return 0;
1971}
1972
Josef Bacik2cdc3422011-05-27 14:07:49 -04001973static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1974 struct btrfs_free_space *info, u64 offset,
1975 u64 bytes)
1976{
1977 u64 bytes_to_set = 0;
1978 u64 end;
1979
1980 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1981
1982 bytes_to_set = min(end - offset, bytes);
1983
1984 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1985
Josef Bacikcef40482015-10-02 16:09:42 -04001986 /*
1987 * We set some bytes, we have no idea what the max extent size is
1988 * anymore.
1989 */
1990 info->max_extent_size = 0;
1991
Josef Bacik2cdc3422011-05-27 14:07:49 -04001992 return bytes_to_set;
1993
1994}
1995
Li Zefan34d52cb2011-03-29 13:46:06 +08001996static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1997 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001998{
Li Zefan34d52cb2011-03-29 13:46:06 +08001999 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002001 bool forced = false;
2002
2003#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002004 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002005 forced = true;
2006#endif
Josef Bacik96303082009-07-13 21:29:25 -04002007
2008 /*
2009 * If we are below the extents threshold then we can add this as an
2010 * extent, and don't have to deal with the bitmap
2011 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002012 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002013 /*
2014 * If this block group has some small extents we don't want to
2015 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002016 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002017 * of cache left then go ahead an dadd them, no sense in adding
2018 * the overhead of a bitmap if we don't have to.
2019 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002021 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2022 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002023 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002024 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002025 }
2026 }
Josef Bacik96303082009-07-13 21:29:25 -04002027
2028 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002029 * The original block groups from mkfs can be really small, like 8
2030 * megabytes, so don't bother with a bitmap for those entries. However
2031 * some block groups can be smaller than what a bitmap would cover but
2032 * are still large enough that they could overflow the 32k memory limit,
2033 * so allow those block groups to still be allowed to have a bitmap
2034 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002035 */
David Sterbab3470b52019-10-23 18:48:22 +02002036 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
Li Zefan34d52cb2011-03-29 13:46:06 +08002037 return false;
2038
2039 return true;
2040}
2041
David Sterba20e55062015-11-19 11:42:28 +01002042static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002043 .recalc_thresholds = recalculate_thresholds,
2044 .use_bitmap = use_bitmap,
2045};
2046
Li Zefan34d52cb2011-03-29 13:46:06 +08002047static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2048 struct btrfs_free_space *info)
2049{
2050 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002051 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002052 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002053 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002054 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002055
2056 bytes = info->bytes;
2057 offset = info->offset;
2058
Li Zefan34d52cb2011-03-29 13:46:06 +08002059 if (!ctl->op->use_bitmap(ctl, info))
2060 return 0;
2061
Josef Bacik2cdc3422011-05-27 14:07:49 -04002062 if (ctl->op == &free_space_op)
2063 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002064again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002065 /*
2066 * Since we link bitmaps right into the cluster we need to see if we
2067 * have a cluster here, and if so and it has our bitmap we need to add
2068 * the free space to that bitmap.
2069 */
2070 if (block_group && !list_empty(&block_group->cluster_list)) {
2071 struct btrfs_free_cluster *cluster;
2072 struct rb_node *node;
2073 struct btrfs_free_space *entry;
2074
2075 cluster = list_entry(block_group->cluster_list.next,
2076 struct btrfs_free_cluster,
2077 block_group_list);
2078 spin_lock(&cluster->lock);
2079 node = rb_first(&cluster->root);
2080 if (!node) {
2081 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002082 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002083 }
2084
2085 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2086 if (!entry->bitmap) {
2087 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002088 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002089 }
2090
2091 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2092 bytes_added = add_bytes_to_bitmap(ctl, entry,
2093 offset, bytes);
2094 bytes -= bytes_added;
2095 offset += bytes_added;
2096 }
2097 spin_unlock(&cluster->lock);
2098 if (!bytes) {
2099 ret = 1;
2100 goto out;
2101 }
2102 }
Chris Mason38e87882011-06-10 16:36:57 -04002103
2104no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002105 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002106 1, 0);
2107 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002108 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002109 goto new_bitmap;
2110 }
2111
Josef Bacik2cdc3422011-05-27 14:07:49 -04002112 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2113 bytes -= bytes_added;
2114 offset += bytes_added;
2115 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002116
2117 if (!bytes) {
2118 ret = 1;
2119 goto out;
2120 } else
2121 goto again;
2122
2123new_bitmap:
2124 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002125 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002126 added = 1;
2127 info = NULL;
2128 goto again;
2129 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002130 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002131
2132 /* no pre-allocated info, allocate a new one */
2133 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002134 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2135 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002136 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002137 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002138 ret = -ENOMEM;
2139 goto out;
2140 }
2141 }
2142
2143 /* allocate the bitmap */
Christophe Leroy3acd4852019-08-21 15:05:55 +00002144 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2145 GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002146 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002147 if (!info->bitmap) {
2148 ret = -ENOMEM;
2149 goto out;
2150 }
2151 goto again;
2152 }
2153
2154out:
2155 if (info) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002156 if (info->bitmap)
2157 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2158 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002159 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002160 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002161
2162 return ret;
2163}
2164
Chris Mason945d8962011-05-22 12:33:42 -04002165static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002166 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002167{
Li Zefan120d66e2010-11-09 14:56:50 +08002168 struct btrfs_free_space *left_info;
2169 struct btrfs_free_space *right_info;
2170 bool merged = false;
2171 u64 offset = info->offset;
2172 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002173
Josef Bacik0f9dd462008-09-23 13:14:11 -04002174 /*
2175 * first we want to see if there is free space adjacent to the range we
2176 * are adding, if there is remove that struct and add a new one to
2177 * cover the entire range
2178 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002179 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002180 if (right_info && rb_prev(&right_info->offset_index))
2181 left_info = rb_entry(rb_prev(&right_info->offset_index),
2182 struct btrfs_free_space, offset_index);
2183 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002184 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002185
Josef Bacik96303082009-07-13 21:29:25 -04002186 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002187 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002188 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002189 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002190 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002191 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002192 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002193 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002194 }
2195
Josef Bacik96303082009-07-13 21:29:25 -04002196 if (left_info && !left_info->bitmap &&
2197 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002198 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002199 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002200 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002201 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002202 info->offset = left_info->offset;
2203 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002204 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002205 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002206 }
2207
Li Zefan120d66e2010-11-09 14:56:50 +08002208 return merged;
2209}
2210
Filipe Manana20005522014-08-29 13:35:13 +01002211static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2212 struct btrfs_free_space *info,
2213 bool update_stat)
2214{
2215 struct btrfs_free_space *bitmap;
2216 unsigned long i;
2217 unsigned long j;
2218 const u64 end = info->offset + info->bytes;
2219 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2220 u64 bytes;
2221
2222 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2223 if (!bitmap)
2224 return false;
2225
2226 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2227 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2228 if (j == i)
2229 return false;
2230 bytes = (j - i) * ctl->unit;
2231 info->bytes += bytes;
2232
2233 if (update_stat)
2234 bitmap_clear_bits(ctl, bitmap, end, bytes);
2235 else
2236 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2237
2238 if (!bitmap->bytes)
2239 free_bitmap(ctl, bitmap);
2240
2241 return true;
2242}
2243
2244static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2245 struct btrfs_free_space *info,
2246 bool update_stat)
2247{
2248 struct btrfs_free_space *bitmap;
2249 u64 bitmap_offset;
2250 unsigned long i;
2251 unsigned long j;
2252 unsigned long prev_j;
2253 u64 bytes;
2254
2255 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2256 /* If we're on a boundary, try the previous logical bitmap. */
2257 if (bitmap_offset == info->offset) {
2258 if (info->offset == 0)
2259 return false;
2260 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2261 }
2262
2263 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2264 if (!bitmap)
2265 return false;
2266
2267 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2268 j = 0;
2269 prev_j = (unsigned long)-1;
2270 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2271 if (j > i)
2272 break;
2273 prev_j = j;
2274 }
2275 if (prev_j == i)
2276 return false;
2277
2278 if (prev_j == (unsigned long)-1)
2279 bytes = (i + 1) * ctl->unit;
2280 else
2281 bytes = (i - prev_j) * ctl->unit;
2282
2283 info->offset -= bytes;
2284 info->bytes += bytes;
2285
2286 if (update_stat)
2287 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2288 else
2289 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2290
2291 if (!bitmap->bytes)
2292 free_bitmap(ctl, bitmap);
2293
2294 return true;
2295}
2296
2297/*
2298 * We prefer always to allocate from extent entries, both for clustered and
2299 * non-clustered allocation requests. So when attempting to add a new extent
2300 * entry, try to see if there's adjacent free space in bitmap entries, and if
2301 * there is, migrate that space from the bitmaps to the extent.
2302 * Like this we get better chances of satisfying space allocation requests
2303 * because we attempt to satisfy them based on a single cache entry, and never
2304 * on 2 or more entries - even if the entries represent a contiguous free space
2305 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2306 * ends).
2307 */
2308static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2309 struct btrfs_free_space *info,
2310 bool update_stat)
2311{
2312 /*
2313 * Only work with disconnected entries, as we can change their offset,
2314 * and must be extent entries.
2315 */
2316 ASSERT(!info->bitmap);
2317 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2318
2319 if (ctl->total_bitmaps > 0) {
2320 bool stole_end;
2321 bool stole_front = false;
2322
2323 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2324 if (ctl->total_bitmaps > 0)
2325 stole_front = steal_from_bitmap_to_front(ctl, info,
2326 update_stat);
2327
2328 if (stole_end || stole_front)
2329 try_merge_free_space(ctl, info, update_stat);
2330 }
2331}
2332
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002333int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2334 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002335 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002336{
2337 struct btrfs_free_space *info;
2338 int ret = 0;
2339
Josef Bacikdc89e982011-01-28 17:05:48 -05002340 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002341 if (!info)
2342 return -ENOMEM;
2343
2344 info->offset = offset;
2345 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002346 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002347
Li Zefan34d52cb2011-03-29 13:46:06 +08002348 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002349
Li Zefan34d52cb2011-03-29 13:46:06 +08002350 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002351 goto link;
2352
2353 /*
2354 * There was no extent directly to the left or right of this new
2355 * extent then we know we're going to have to allocate a new extent, so
2356 * before we do that see if we need to drop this into a bitmap
2357 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002358 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002359 if (ret < 0) {
2360 goto out;
2361 } else if (ret) {
2362 ret = 0;
2363 goto out;
2364 }
2365link:
Filipe Manana20005522014-08-29 13:35:13 +01002366 /*
2367 * Only steal free space from adjacent bitmaps if we're sure we're not
2368 * going to add the new free space to existing bitmap entries - because
2369 * that would mean unnecessary work that would be reverted. Therefore
2370 * attempt to steal space from bitmaps if we're adding an extent entry.
2371 */
2372 steal_from_bitmap(ctl, info, true);
2373
Li Zefan34d52cb2011-03-29 13:46:06 +08002374 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002375 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002376 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002377out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002378 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002379
Josef Bacik0f9dd462008-09-23 13:14:11 -04002380 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002381 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002382 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002383 }
2384
Josef Bacik0f9dd462008-09-23 13:14:11 -04002385 return ret;
2386}
2387
Josef Bacik478b4d92019-06-20 15:37:43 -04002388int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
2389 u64 bytenr, u64 size)
2390{
2391 return __btrfs_add_free_space(block_group->fs_info,
2392 block_group->free_space_ctl,
2393 bytenr, size);
2394}
2395
Josef Bacik6226cb02009-04-03 10:14:18 -04002396int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2397 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002398{
Li Zefan34d52cb2011-03-29 13:46:06 +08002399 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002400 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002401 int ret;
2402 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002403
Li Zefan34d52cb2011-03-29 13:46:06 +08002404 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002405
Josef Bacik96303082009-07-13 21:29:25 -04002406again:
Josef Bacikb0175112012-12-18 11:39:19 -05002407 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002408 if (!bytes)
2409 goto out_lock;
2410
Li Zefan34d52cb2011-03-29 13:46:06 +08002411 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002412 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002413 /*
2414 * oops didn't find an extent that matched the space we wanted
2415 * to remove, look for a bitmap instead
2416 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002417 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002418 1, 0);
2419 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002420 /*
2421 * If we found a partial bit of our free space in a
2422 * bitmap but then couldn't find the other part this may
2423 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002424 */
Josef Bacikb0175112012-12-18 11:39:19 -05002425 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002426 goto out_lock;
2427 }
Josef Bacik96303082009-07-13 21:29:25 -04002428 }
2429
Josef Bacikb0175112012-12-18 11:39:19 -05002430 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002431 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002432 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002433 if (offset == info->offset) {
2434 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002435
Josef Bacikbdb7d302012-06-27 15:10:56 -04002436 info->bytes -= to_free;
2437 info->offset += to_free;
2438 if (info->bytes) {
2439 ret = link_free_space(ctl, info);
2440 WARN_ON(ret);
2441 } else {
2442 kmem_cache_free(btrfs_free_space_cachep, info);
2443 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002444
Josef Bacikbdb7d302012-06-27 15:10:56 -04002445 offset += to_free;
2446 bytes -= to_free;
2447 goto again;
2448 } else {
2449 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002450
Josef Bacikbdb7d302012-06-27 15:10:56 -04002451 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002452 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002453 WARN_ON(ret);
2454 if (ret)
2455 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002456
Josef Bacikbdb7d302012-06-27 15:10:56 -04002457 /* Not enough bytes in this entry to satisfy us */
2458 if (old_end < offset + bytes) {
2459 bytes -= old_end - offset;
2460 offset = old_end;
2461 goto again;
2462 } else if (old_end == offset + bytes) {
2463 /* all done */
2464 goto out_lock;
2465 }
2466 spin_unlock(&ctl->tree_lock);
2467
2468 ret = btrfs_add_free_space(block_group, offset + bytes,
2469 old_end - (offset + bytes));
2470 WARN_ON(ret);
2471 goto out;
2472 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002473 }
Josef Bacik96303082009-07-13 21:29:25 -04002474
Li Zefan34d52cb2011-03-29 13:46:06 +08002475 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002476 if (ret == -EAGAIN) {
2477 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002478 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002479 }
Josef Bacik96303082009-07-13 21:29:25 -04002480out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002481 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002482out:
Josef Bacik25179202008-10-29 14:49:05 -04002483 return ret;
2484}
2485
Josef Bacik0f9dd462008-09-23 13:14:11 -04002486void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2487 u64 bytes)
2488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002489 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002490 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002491 struct btrfs_free_space *info;
2492 struct rb_node *n;
2493 int count = 0;
2494
Filipe Manana9084cb62018-10-22 10:43:06 +01002495 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002496 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002497 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002498 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002499 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002500 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002501 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002502 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002503 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002504 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002505 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002506 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002508 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002509}
2510
Li Zefan34d52cb2011-03-29 13:46:06 +08002511void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002512{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002513 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002514 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002515
Li Zefan34d52cb2011-03-29 13:46:06 +08002516 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 ctl->unit = fs_info->sectorsize;
David Sterbab3470b52019-10-23 18:48:22 +02002518 ctl->start = block_group->start;
Li Zefan34d52cb2011-03-29 13:46:06 +08002519 ctl->private = block_group;
2520 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002521 INIT_LIST_HEAD(&ctl->trimming_ranges);
2522 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002523
Li Zefan34d52cb2011-03-29 13:46:06 +08002524 /*
2525 * we only want to have 32k of ram per block group for keeping
2526 * track of free space, and if we pass 1/2 of that we want to
2527 * start converting things over to using bitmaps
2528 */
Byongho Leeee221842015-12-15 01:42:10 +09002529 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002530}
2531
Chris Masonfa9c0d792009-04-03 09:47:43 -04002532/*
2533 * for a given cluster, put all of its extents back into the free
2534 * space cache. If the block group passed doesn't match the block group
2535 * pointed to by the cluster, someone else raced in and freed the
2536 * cluster already. In that case, we just return without changing anything
2537 */
2538static int
2539__btrfs_return_cluster_to_free_space(
2540 struct btrfs_block_group_cache *block_group,
2541 struct btrfs_free_cluster *cluster)
2542{
Li Zefan34d52cb2011-03-29 13:46:06 +08002543 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002544 struct btrfs_free_space *entry;
2545 struct rb_node *node;
2546
2547 spin_lock(&cluster->lock);
2548 if (cluster->block_group != block_group)
2549 goto out;
2550
Josef Bacik96303082009-07-13 21:29:25 -04002551 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002552 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002553 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002554
Chris Masonfa9c0d792009-04-03 09:47:43 -04002555 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002556 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002557 bool bitmap;
2558
Chris Masonfa9c0d792009-04-03 09:47:43 -04002559 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2560 node = rb_next(&entry->offset_index);
2561 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002562 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002563
2564 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002565 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002566 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002567 steal_from_bitmap(ctl, entry, false);
2568 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002569 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002570 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002571 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002572 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002573
Chris Masonfa9c0d792009-04-03 09:47:43 -04002574out:
2575 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002576 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002577 return 0;
2578}
2579
Eric Sandeen48a3b632013-04-25 20:41:01 +00002580static void __btrfs_remove_free_space_cache_locked(
2581 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002582{
2583 struct btrfs_free_space *info;
2584 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002585
Li Zefan581bb052011-04-20 10:06:11 +08002586 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2587 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002588 if (!info->bitmap) {
2589 unlink_free_space(ctl, info);
2590 kmem_cache_free(btrfs_free_space_cachep, info);
2591 } else {
2592 free_bitmap(ctl, info);
2593 }
David Sterba351810c2015-01-08 15:20:54 +01002594
2595 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002596 }
Chris Mason09655372011-05-21 09:27:38 -04002597}
2598
2599void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2600{
2601 spin_lock(&ctl->tree_lock);
2602 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002603 spin_unlock(&ctl->tree_lock);
2604}
2605
Josef Bacik0f9dd462008-09-23 13:14:11 -04002606void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2607{
Li Zefan34d52cb2011-03-29 13:46:06 +08002608 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002609 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002610 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002611
Li Zefan34d52cb2011-03-29 13:46:06 +08002612 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002613 while ((head = block_group->cluster_list.next) !=
2614 &block_group->cluster_list) {
2615 cluster = list_entry(head, struct btrfs_free_cluster,
2616 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002617
2618 WARN_ON(cluster->block_group != block_group);
2619 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002620
2621 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002622 }
Chris Mason09655372011-05-21 09:27:38 -04002623 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002624 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002625
Josef Bacik0f9dd462008-09-23 13:14:11 -04002626}
2627
Josef Bacik6226cb02009-04-03 10:14:18 -04002628u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002629 u64 offset, u64 bytes, u64 empty_size,
2630 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002631{
Li Zefan34d52cb2011-03-29 13:46:06 +08002632 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002633 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002634 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002635 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002636 u64 align_gap = 0;
2637 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002638
Li Zefan34d52cb2011-03-29 13:46:06 +08002639 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002640 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002641 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002642 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002643 goto out;
2644
2645 ret = offset;
2646 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002647 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002648 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002649 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002650 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002651 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002652 align_gap_len = offset - entry->offset;
2653 align_gap = entry->offset;
2654
2655 entry->offset = offset + bytes;
2656 WARN_ON(entry->bytes < bytes + align_gap_len);
2657
2658 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002659 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002660 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002661 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002662 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002663 }
Josef Bacik96303082009-07-13 21:29:25 -04002664out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002665 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002666
David Woodhouse53b381b2013-01-29 18:40:14 -05002667 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002668 __btrfs_add_free_space(block_group->fs_info, ctl,
2669 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002670 return ret;
2671}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002672
2673/*
2674 * given a cluster, put all of its extents back into the free space
2675 * cache. If a block group is passed, this function will only free
2676 * a cluster that belongs to the passed block group.
2677 *
2678 * Otherwise, it'll get a reference on the block group pointed to by the
2679 * cluster and remove the cluster from it.
2680 */
2681int btrfs_return_cluster_to_free_space(
2682 struct btrfs_block_group_cache *block_group,
2683 struct btrfs_free_cluster *cluster)
2684{
Li Zefan34d52cb2011-03-29 13:46:06 +08002685 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002686 int ret;
2687
2688 /* first, get a safe pointer to the block group */
2689 spin_lock(&cluster->lock);
2690 if (!block_group) {
2691 block_group = cluster->block_group;
2692 if (!block_group) {
2693 spin_unlock(&cluster->lock);
2694 return 0;
2695 }
2696 } else if (cluster->block_group != block_group) {
2697 /* someone else has already freed it don't redo their work */
2698 spin_unlock(&cluster->lock);
2699 return 0;
2700 }
2701 atomic_inc(&block_group->count);
2702 spin_unlock(&cluster->lock);
2703
Li Zefan34d52cb2011-03-29 13:46:06 +08002704 ctl = block_group->free_space_ctl;
2705
Chris Masonfa9c0d792009-04-03 09:47:43 -04002706 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002707 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002708 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002709 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002710
2711 /* finally drop our ref */
2712 btrfs_put_block_group(block_group);
2713 return ret;
2714}
2715
Josef Bacik96303082009-07-13 21:29:25 -04002716static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2717 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002718 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002719 u64 bytes, u64 min_start,
2720 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002721{
Li Zefan34d52cb2011-03-29 13:46:06 +08002722 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002723 int err;
2724 u64 search_start = cluster->window_start;
2725 u64 search_bytes = bytes;
2726 u64 ret = 0;
2727
Josef Bacik96303082009-07-13 21:29:25 -04002728 search_start = min_start;
2729 search_bytes = bytes;
2730
Josef Bacik0584f712015-10-02 16:12:23 -04002731 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002732 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002733 *max_extent_size = max(get_max_extent_size(entry),
2734 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002735 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002736 }
Josef Bacik96303082009-07-13 21:29:25 -04002737
2738 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002739 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002740
2741 return ret;
2742}
2743
Chris Masonfa9c0d792009-04-03 09:47:43 -04002744/*
2745 * given a cluster, try to allocate 'bytes' from it, returns 0
2746 * if it couldn't find anything suitably large, or a logical disk offset
2747 * if things worked out
2748 */
2749u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2750 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002751 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002752{
Li Zefan34d52cb2011-03-29 13:46:06 +08002753 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002754 struct btrfs_free_space *entry = NULL;
2755 struct rb_node *node;
2756 u64 ret = 0;
2757
2758 spin_lock(&cluster->lock);
2759 if (bytes > cluster->max_size)
2760 goto out;
2761
2762 if (cluster->block_group != block_group)
2763 goto out;
2764
2765 node = rb_first(&cluster->root);
2766 if (!node)
2767 goto out;
2768
2769 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302770 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002771 if (entry->bytes < bytes)
2772 *max_extent_size = max(get_max_extent_size(entry),
2773 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002774
Josef Bacik4e69b592011-03-21 10:11:24 -04002775 if (entry->bytes < bytes ||
2776 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002777 node = rb_next(&entry->offset_index);
2778 if (!node)
2779 break;
2780 entry = rb_entry(node, struct btrfs_free_space,
2781 offset_index);
2782 continue;
2783 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002784
Josef Bacik4e69b592011-03-21 10:11:24 -04002785 if (entry->bitmap) {
2786 ret = btrfs_alloc_from_bitmap(block_group,
2787 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002788 cluster->window_start,
2789 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002790 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002791 node = rb_next(&entry->offset_index);
2792 if (!node)
2793 break;
2794 entry = rb_entry(node, struct btrfs_free_space,
2795 offset_index);
2796 continue;
2797 }
Josef Bacik9b230622012-01-26 15:01:12 -05002798 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002799 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002800 ret = entry->offset;
2801
2802 entry->offset += bytes;
2803 entry->bytes -= bytes;
2804 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002805
Li Zefan5e71b5d2010-11-09 14:55:34 +08002806 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002807 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002808 break;
2809 }
2810out:
2811 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002812
Li Zefan5e71b5d2010-11-09 14:55:34 +08002813 if (!ret)
2814 return 0;
2815
Li Zefan34d52cb2011-03-29 13:46:06 +08002816 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002817
Li Zefan34d52cb2011-03-29 13:46:06 +08002818 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002819 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002820 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002821 if (entry->bitmap) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00002822 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2823 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002824 ctl->total_bitmaps--;
2825 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002826 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002827 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002828 }
2829
Li Zefan34d52cb2011-03-29 13:46:06 +08002830 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002831
Chris Masonfa9c0d792009-04-03 09:47:43 -04002832 return ret;
2833}
2834
Josef Bacik96303082009-07-13 21:29:25 -04002835static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2836 struct btrfs_free_space *entry,
2837 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002838 u64 offset, u64 bytes,
2839 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002840{
Li Zefan34d52cb2011-03-29 13:46:06 +08002841 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002842 unsigned long next_zero;
2843 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002844 unsigned long want_bits;
2845 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002846 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002847 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002848 unsigned long start = 0;
2849 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002850 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002851
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002852 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002853 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002854 want_bits = bytes_to_bits(bytes, ctl->unit);
2855 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002856
Josef Bacikcef40482015-10-02 16:09:42 -04002857 /*
2858 * Don't bother looking for a cluster in this bitmap if it's heavily
2859 * fragmented.
2860 */
2861 if (entry->max_extent_size &&
2862 entry->max_extent_size < cont1_bytes)
2863 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002864again:
2865 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002866 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002867 next_zero = find_next_zero_bit(entry->bitmap,
2868 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002869 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002870 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002871 if (found_bits > max_bits)
2872 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002873 break;
2874 }
Josef Bacikcef40482015-10-02 16:09:42 -04002875 if (next_zero - i > max_bits)
2876 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002877 i = next_zero;
2878 }
2879
Josef Bacikcef40482015-10-02 16:09:42 -04002880 if (!found_bits) {
2881 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002882 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002883 }
Josef Bacik96303082009-07-13 21:29:25 -04002884
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002885 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002886 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002887 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002888 }
2889
2890 total_found += found_bits;
2891
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002892 if (cluster->max_size < found_bits * ctl->unit)
2893 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002894
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002895 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2896 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002897 goto again;
2898 }
2899
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002900 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002901 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002902 ret = tree_insert_offset(&cluster->root, entry->offset,
2903 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002904 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002905
Josef Bacik3f7de032011-11-10 08:29:20 -05002906 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002907 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002908 return 0;
2909}
2910
Chris Masonfa9c0d792009-04-03 09:47:43 -04002911/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002912 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002913 * Try to find a cluster with at least bytes total bytes, at least one
2914 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002915 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002916static noinline int
2917setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2918 struct btrfs_free_cluster *cluster,
2919 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002920 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002921{
Li Zefan34d52cb2011-03-29 13:46:06 +08002922 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002923 struct btrfs_free_space *first = NULL;
2924 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002925 struct btrfs_free_space *last;
2926 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002927 u64 window_free;
2928 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002929 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002930
Li Zefan34d52cb2011-03-29 13:46:06 +08002931 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002932 if (!entry)
2933 return -ENOSPC;
2934
2935 /*
2936 * We don't want bitmaps, so just move along until we find a normal
2937 * extent entry.
2938 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002939 while (entry->bitmap || entry->bytes < min_bytes) {
2940 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002941 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002942 node = rb_next(&entry->offset_index);
2943 if (!node)
2944 return -ENOSPC;
2945 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2946 }
2947
Josef Bacik4e69b592011-03-21 10:11:24 -04002948 window_free = entry->bytes;
2949 max_extent = entry->bytes;
2950 first = entry;
2951 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002952
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002953 for (node = rb_next(&entry->offset_index); node;
2954 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002955 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2956
Josef Bacik86d4a772011-05-25 13:03:16 -04002957 if (entry->bitmap) {
2958 if (list_empty(&entry->list))
2959 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002960 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002961 }
2962
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002963 if (entry->bytes < min_bytes)
2964 continue;
2965
2966 last = entry;
2967 window_free += entry->bytes;
2968 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002969 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002970 }
2971
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002972 if (window_free < bytes || max_extent < cont1_bytes)
2973 return -ENOSPC;
2974
Josef Bacik4e69b592011-03-21 10:11:24 -04002975 cluster->window_start = first->offset;
2976
2977 node = &first->offset_index;
2978
2979 /*
2980 * now we've found our entries, pull them out of the free space
2981 * cache and put them into the cluster rbtree
2982 */
2983 do {
2984 int ret;
2985
2986 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2987 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002988 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002989 continue;
2990
Li Zefan34d52cb2011-03-29 13:46:06 +08002991 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002992 ret = tree_insert_offset(&cluster->root, entry->offset,
2993 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002994 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002995 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002996 } while (node && entry != last);
2997
2998 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002999 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04003000 return 0;
3001}
3002
3003/*
3004 * This specifically looks for bitmaps that may work in the cluster, we assume
3005 * that we have already failed to find extents that will work.
3006 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003007static noinline int
3008setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3009 struct btrfs_free_cluster *cluster,
3010 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003011 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003012{
Li Zefan34d52cb2011-03-29 13:46:06 +08003013 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003014 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003015 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003016 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003017
Li Zefan34d52cb2011-03-29 13:46:06 +08003018 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003019 return -ENOSPC;
3020
Josef Bacik86d4a772011-05-25 13:03:16 -04003021 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003022 * The bitmap that covers offset won't be in the list unless offset
3023 * is just its start offset.
3024 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003025 if (!list_empty(bitmaps))
3026 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3027
3028 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003029 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3030 if (entry && list_empty(&entry->list))
3031 list_add(&entry->list, bitmaps);
3032 }
3033
Josef Bacik86d4a772011-05-25 13:03:16 -04003034 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003035 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003036 continue;
3037 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003038 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003039 if (!ret)
3040 return 0;
3041 }
3042
3043 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003044 * The bitmaps list has all the bitmaps that record free space
3045 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003046 */
Li Zefan52621cb2011-11-20 07:33:38 -05003047 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003048}
3049
3050/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003051 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003052 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003053 * We might not find them all in one contiguous area.
3054 *
3055 * returns zero and sets up cluster if things worked out, otherwise
3056 * it returns -enospc
3057 */
David Sterba2ceeae22019-03-20 13:53:49 +01003058int btrfs_find_space_cluster(struct btrfs_block_group_cache *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003059 struct btrfs_free_cluster *cluster,
3060 u64 offset, u64 bytes, u64 empty_size)
3061{
David Sterba2ceeae22019-03-20 13:53:49 +01003062 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003063 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003064 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003065 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003066 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003067 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003068 int ret;
3069
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003070 /*
3071 * Choose the minimum extent size we'll require for this
3072 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3073 * For metadata, allow allocates with smaller extents. For
3074 * data, keep it dense.
3075 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003077 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003078 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003079 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003081 } else {
3082 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003083 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003084 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003085
Li Zefan34d52cb2011-03-29 13:46:06 +08003086 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003087
3088 /*
3089 * If we know we don't have enough space to make a cluster don't even
3090 * bother doing all the work to try and find one.
3091 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003092 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003093 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003094 return -ENOSPC;
3095 }
3096
Chris Masonfa9c0d792009-04-03 09:47:43 -04003097 spin_lock(&cluster->lock);
3098
3099 /* someone already found a cluster, hooray */
3100 if (cluster->block_group) {
3101 ret = 0;
3102 goto out;
3103 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003104
Josef Bacik3f7de032011-11-10 08:29:20 -05003105 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3106 min_bytes);
3107
Josef Bacik86d4a772011-05-25 13:03:16 -04003108 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003109 bytes + empty_size,
3110 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003111 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003112 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003113 offset, bytes + empty_size,
3114 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003115
3116 /* Clear our temporary list */
3117 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3118 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003119
3120 if (!ret) {
3121 atomic_inc(&block_group->count);
3122 list_add_tail(&cluster->block_group_list,
3123 &block_group->cluster_list);
3124 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003125 } else {
3126 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003127 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003128out:
3129 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003130 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003131
3132 return ret;
3133}
3134
3135/*
3136 * simple code to zero out a cluster
3137 */
3138void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3139{
3140 spin_lock_init(&cluster->lock);
3141 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003142 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003143 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003144 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003145 INIT_LIST_HEAD(&cluster->block_group_list);
3146 cluster->block_group = NULL;
3147}
3148
Li Zefan7fe1e642011-12-29 14:47:27 +08003149static int do_trimming(struct btrfs_block_group_cache *block_group,
3150 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003151 u64 reserved_start, u64 reserved_bytes,
3152 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003153{
3154 struct btrfs_space_info *space_info = block_group->space_info;
3155 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003156 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003157 int ret;
3158 int update = 0;
3159 u64 trimmed = 0;
3160
3161 spin_lock(&space_info->lock);
3162 spin_lock(&block_group->lock);
3163 if (!block_group->ro) {
3164 block_group->reserved += reserved_bytes;
3165 space_info->bytes_reserved += reserved_bytes;
3166 update = 1;
3167 }
3168 spin_unlock(&block_group->lock);
3169 spin_unlock(&space_info->lock);
3170
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003171 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003172 if (!ret)
3173 *total_trimmed += trimmed;
3174
Filipe Manana55507ce2014-12-01 17:04:09 +00003175 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003176 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003177 list_del(&trim_entry->list);
3178 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003179
3180 if (update) {
3181 spin_lock(&space_info->lock);
3182 spin_lock(&block_group->lock);
3183 if (block_group->ro)
3184 space_info->bytes_readonly += reserved_bytes;
3185 block_group->reserved -= reserved_bytes;
3186 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003187 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003188 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003189 }
3190
3191 return ret;
3192}
3193
3194static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3195 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003196{
Li Zefan34d52cb2011-03-29 13:46:06 +08003197 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003198 struct btrfs_free_space *entry;
3199 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003200 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003201 u64 extent_start;
3202 u64 extent_bytes;
3203 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003204
3205 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003206 struct btrfs_trim_range trim_entry;
3207
3208 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003209 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003210
Li Zefan34d52cb2011-03-29 13:46:06 +08003211 if (ctl->free_space < minlen) {
3212 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003213 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003214 break;
3215 }
3216
Li Zefan34d52cb2011-03-29 13:46:06 +08003217 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003218 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003219 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003220 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003221 break;
3222 }
3223
Li Zefan7fe1e642011-12-29 14:47:27 +08003224 /* skip bitmaps */
3225 while (entry->bitmap) {
3226 node = rb_next(&entry->offset_index);
3227 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003228 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003229 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003230 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003231 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003232 entry = rb_entry(node, struct btrfs_free_space,
3233 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003234 }
3235
Li Zefan7fe1e642011-12-29 14:47:27 +08003236 if (entry->offset >= end) {
3237 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003238 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003239 break;
3240 }
3241
3242 extent_start = entry->offset;
3243 extent_bytes = entry->bytes;
3244 start = max(start, extent_start);
3245 bytes = min(extent_start + extent_bytes, end) - start;
3246 if (bytes < minlen) {
3247 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003248 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003249 goto next;
3250 }
3251
3252 unlink_free_space(ctl, entry);
3253 kmem_cache_free(btrfs_free_space_cachep, entry);
3254
Li Zefan34d52cb2011-03-29 13:46:06 +08003255 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003256 trim_entry.start = extent_start;
3257 trim_entry.bytes = extent_bytes;
3258 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3259 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003260
Li Zefan7fe1e642011-12-29 14:47:27 +08003261 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003262 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003263 if (ret)
3264 break;
3265next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003266 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003267
3268 if (fatal_signal_pending(current)) {
3269 ret = -ERESTARTSYS;
3270 break;
3271 }
3272
3273 cond_resched();
3274 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003275out:
3276 return ret;
3277}
3278
3279static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3280 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3281{
3282 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3283 struct btrfs_free_space *entry;
3284 int ret = 0;
3285 int ret2;
3286 u64 bytes;
3287 u64 offset = offset_to_bitmap(ctl, start);
3288
3289 while (offset < end) {
3290 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003291 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003292
Filipe Manana55507ce2014-12-01 17:04:09 +00003293 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003294 spin_lock(&ctl->tree_lock);
3295
3296 if (ctl->free_space < minlen) {
3297 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003298 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003299 break;
3300 }
3301
3302 entry = tree_search_offset(ctl, offset, 1, 0);
3303 if (!entry) {
3304 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003305 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003306 next_bitmap = true;
3307 goto next;
3308 }
3309
3310 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003311 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003312 if (ret2 || start >= end) {
3313 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003314 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003315 next_bitmap = true;
3316 goto next;
3317 }
3318
3319 bytes = min(bytes, end - start);
3320 if (bytes < minlen) {
3321 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003322 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003323 goto next;
3324 }
3325
3326 bitmap_clear_bits(ctl, entry, start, bytes);
3327 if (entry->bytes == 0)
3328 free_bitmap(ctl, entry);
3329
3330 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003331 trim_entry.start = start;
3332 trim_entry.bytes = bytes;
3333 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3334 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003335
3336 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003337 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003338 if (ret)
3339 break;
3340next:
3341 if (next_bitmap) {
3342 offset += BITS_PER_BITMAP * ctl->unit;
3343 } else {
3344 start += bytes;
3345 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3346 offset += BITS_PER_BITMAP * ctl->unit;
3347 }
3348
3349 if (fatal_signal_pending(current)) {
3350 ret = -ERESTARTSYS;
3351 break;
3352 }
3353
3354 cond_resched();
3355 }
3356
3357 return ret;
3358}
3359
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003360void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003361{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003362 atomic_inc(&cache->trimming);
3363}
Li Zefan7fe1e642011-12-29 14:47:27 +08003364
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003365void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003367 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003368 struct extent_map_tree *em_tree;
3369 struct extent_map *em;
3370 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003371
Filipe Manana04216822014-11-27 21:14:15 +00003372 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003373 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3374 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003375 spin_unlock(&block_group->lock);
3376
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003377 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003378 mutex_lock(&fs_info->chunk_mutex);
David Sterbac8bf1b62019-05-17 11:43:17 +02003379 em_tree = &fs_info->mapping_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003380 write_lock(&em_tree->lock);
David Sterbab3470b52019-10-23 18:48:22 +02003381 em = lookup_extent_mapping(em_tree, block_group->start,
Filipe Manana04216822014-11-27 21:14:15 +00003382 1);
3383 BUG_ON(!em); /* logic error, can't happen */
3384 remove_extent_mapping(em_tree, em);
3385 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003386 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003387
3388 /* once for us and once for the tree */
3389 free_extent_map(em);
3390 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003391
3392 /*
3393 * We've left one free space entry and other tasks trimming
3394 * this block group have left 1 entry each one. Free them.
3395 */
3396 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003397 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003398}
Li Dongyangf7039b12011-03-24 10:24:28 +00003399
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003400int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3401 u64 *trimmed, u64 start, u64 end, u64 minlen)
3402{
3403 int ret;
3404
3405 *trimmed = 0;
3406
3407 spin_lock(&block_group->lock);
3408 if (block_group->removed) {
3409 spin_unlock(&block_group->lock);
3410 return 0;
3411 }
3412 btrfs_get_block_group_trimming(block_group);
3413 spin_unlock(&block_group->lock);
3414
3415 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3416 if (ret)
3417 goto out;
3418
3419 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3420out:
3421 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003422 return ret;
3423}
Li Zefan581bb052011-04-20 10:06:11 +08003424
3425/*
3426 * Find the left-most item in the cache tree, and then return the
3427 * smallest inode number in the item.
3428 *
3429 * Note: the returned inode number may not be the smallest one in
3430 * the tree, if the left-most item is a bitmap.
3431 */
3432u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3433{
3434 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3435 struct btrfs_free_space *entry = NULL;
3436 u64 ino = 0;
3437
3438 spin_lock(&ctl->tree_lock);
3439
3440 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3441 goto out;
3442
3443 entry = rb_entry(rb_first(&ctl->free_space_offset),
3444 struct btrfs_free_space, offset_index);
3445
3446 if (!entry->bitmap) {
3447 ino = entry->offset;
3448
3449 unlink_free_space(ctl, entry);
3450 entry->offset++;
3451 entry->bytes--;
3452 if (!entry->bytes)
3453 kmem_cache_free(btrfs_free_space_cachep, entry);
3454 else
3455 link_free_space(ctl, entry);
3456 } else {
3457 u64 offset = 0;
3458 u64 count = 1;
3459 int ret;
3460
Josef Bacik0584f712015-10-02 16:12:23 -04003461 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003462 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003463 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003464
3465 ino = offset;
3466 bitmap_clear_bits(ctl, entry, offset, 1);
3467 if (entry->bytes == 0)
3468 free_bitmap(ctl, entry);
3469 }
3470out:
3471 spin_unlock(&ctl->tree_lock);
3472
3473 return ino;
3474}
Li Zefan82d59022011-04-20 10:33:24 +08003475
3476struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3477 struct btrfs_path *path)
3478{
3479 struct inode *inode = NULL;
3480
David Sterba57cdc8d2014-02-05 02:37:48 +01003481 spin_lock(&root->ino_cache_lock);
3482 if (root->ino_cache_inode)
3483 inode = igrab(root->ino_cache_inode);
3484 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003485 if (inode)
3486 return inode;
3487
3488 inode = __lookup_free_space_inode(root, path, 0);
3489 if (IS_ERR(inode))
3490 return inode;
3491
David Sterba57cdc8d2014-02-05 02:37:48 +01003492 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003493 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003494 root->ino_cache_inode = igrab(inode);
3495 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003496
3497 return inode;
3498}
3499
3500int create_free_ino_inode(struct btrfs_root *root,
3501 struct btrfs_trans_handle *trans,
3502 struct btrfs_path *path)
3503{
3504 return __create_free_space_inode(root, trans, path,
3505 BTRFS_FREE_INO_OBJECTID, 0);
3506}
3507
3508int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3509{
3510 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3511 struct btrfs_path *path;
3512 struct inode *inode;
3513 int ret = 0;
3514 u64 root_gen = btrfs_root_generation(&root->root_item);
3515
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003516 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003517 return 0;
3518
Li Zefan82d59022011-04-20 10:33:24 +08003519 /*
3520 * If we're unmounting then just return, since this does a search on the
3521 * normal root and not the commit root and we could deadlock.
3522 */
David Sterba7841cb22011-05-31 18:07:27 +02003523 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003524 return 0;
3525
3526 path = btrfs_alloc_path();
3527 if (!path)
3528 return 0;
3529
3530 inode = lookup_free_ino_inode(root, path);
3531 if (IS_ERR(inode))
3532 goto out;
3533
3534 if (root_gen != BTRFS_I(inode)->generation)
3535 goto out_put;
3536
3537 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3538
3539 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003540 btrfs_err(fs_info,
3541 "failed to load free ino cache for root %llu",
3542 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003543out_put:
3544 iput(inode);
3545out:
3546 btrfs_free_path(path);
3547 return ret;
3548}
3549
3550int btrfs_write_out_ino_cache(struct btrfs_root *root,
3551 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003552 struct btrfs_path *path,
3553 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003554{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003556 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003557 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003558 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003559 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003561 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003562 return 0;
3563
Chris Mason85db36c2015-04-23 08:02:49 -07003564 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003565 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003566 if (!ret) {
3567 /*
3568 * At this point writepages() didn't error out, so our metadata
3569 * reservation is released when the writeback finishes, at
3570 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3571 * with or without an error.
3572 */
3573 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003574 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003575 }
Chris Mason85db36c2015-04-23 08:02:49 -07003576
Josef Bacikc09544e2011-08-30 10:19:10 -04003577 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003578 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003579 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003580 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04003581#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003582 btrfs_err(fs_info,
3583 "failed to write free ino cache for root %llu",
3584 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003585#endif
3586 }
Li Zefan82d59022011-04-20 10:33:24 +08003587
Li Zefan82d59022011-04-20 10:33:24 +08003588 return ret;
3589}
Josef Bacik74255aa2013-03-15 09:47:08 -04003590
3591#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003592/*
3593 * Use this if you need to make a bitmap or extent entry specifically, it
3594 * doesn't do any of the merging that add_free_space does, this acts a lot like
3595 * how the free space cache loading stuff works, so you can get really weird
3596 * configurations.
3597 */
3598int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3599 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003600{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003601 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3602 struct btrfs_free_space *info = NULL, *bitmap_info;
3603 void *map = NULL;
3604 u64 bytes_added;
3605 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003606
Josef Bacikdc11dd52013-08-14 15:05:12 -04003607again:
3608 if (!info) {
3609 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3610 if (!info)
3611 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003612 }
3613
Josef Bacikdc11dd52013-08-14 15:05:12 -04003614 if (!bitmap) {
3615 spin_lock(&ctl->tree_lock);
3616 info->offset = offset;
3617 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003618 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003619 ret = link_free_space(ctl, info);
3620 spin_unlock(&ctl->tree_lock);
3621 if (ret)
3622 kmem_cache_free(btrfs_free_space_cachep, info);
3623 return ret;
3624 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003625
Josef Bacikdc11dd52013-08-14 15:05:12 -04003626 if (!map) {
Christophe Leroy3acd4852019-08-21 15:05:55 +00003627 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003628 if (!map) {
3629 kmem_cache_free(btrfs_free_space_cachep, info);
3630 return -ENOMEM;
3631 }
3632 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003633
Josef Bacikdc11dd52013-08-14 15:05:12 -04003634 spin_lock(&ctl->tree_lock);
3635 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3636 1, 0);
3637 if (!bitmap_info) {
3638 info->bitmap = map;
3639 map = NULL;
3640 add_new_bitmap(ctl, info, offset);
3641 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003642 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003643 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003644
Josef Bacikdc11dd52013-08-14 15:05:12 -04003645 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003646
Josef Bacikdc11dd52013-08-14 15:05:12 -04003647 bytes -= bytes_added;
3648 offset += bytes_added;
3649 spin_unlock(&ctl->tree_lock);
3650
3651 if (bytes)
3652 goto again;
3653
Filipe Manana20005522014-08-29 13:35:13 +01003654 if (info)
3655 kmem_cache_free(btrfs_free_space_cachep, info);
Christophe Leroy3acd4852019-08-21 15:05:55 +00003656 if (map)
3657 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003658 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003659}
3660
3661/*
3662 * Checks to see if the given range is in the free space cache. This is really
3663 * just used to check the absence of space, so if there is free space in the
3664 * range at all we will return 1.
3665 */
Josef Bacikdc11dd52013-08-14 15:05:12 -04003666int test_check_exists(struct btrfs_block_group_cache *cache,
3667 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003668{
3669 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3670 struct btrfs_free_space *info;
3671 int ret = 0;
3672
3673 spin_lock(&ctl->tree_lock);
3674 info = tree_search_offset(ctl, offset, 0, 0);
3675 if (!info) {
3676 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3677 1, 0);
3678 if (!info)
3679 goto out;
3680 }
3681
3682have_info:
3683 if (info->bitmap) {
3684 u64 bit_off, bit_bytes;
3685 struct rb_node *n;
3686 struct btrfs_free_space *tmp;
3687
3688 bit_off = offset;
3689 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003690 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003691 if (!ret) {
3692 if (bit_off == offset) {
3693 ret = 1;
3694 goto out;
3695 } else if (bit_off > offset &&
3696 offset + bytes > bit_off) {
3697 ret = 1;
3698 goto out;
3699 }
3700 }
3701
3702 n = rb_prev(&info->offset_index);
3703 while (n) {
3704 tmp = rb_entry(n, struct btrfs_free_space,
3705 offset_index);
3706 if (tmp->offset + tmp->bytes < offset)
3707 break;
3708 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003709 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003710 continue;
3711 }
3712 info = tmp;
3713 goto have_info;
3714 }
3715
3716 n = rb_next(&info->offset_index);
3717 while (n) {
3718 tmp = rb_entry(n, struct btrfs_free_space,
3719 offset_index);
3720 if (offset + bytes < tmp->offset)
3721 break;
3722 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003723 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003724 continue;
3725 }
3726 info = tmp;
3727 goto have_info;
3728 }
3729
Filipe Manana20005522014-08-29 13:35:13 +01003730 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003731 goto out;
3732 }
3733
3734 if (info->offset == offset) {
3735 ret = 1;
3736 goto out;
3737 }
3738
3739 if (offset > info->offset && offset < info->offset + info->bytes)
3740 ret = 1;
3741out:
3742 spin_unlock(&ctl->tree_lock);
3743 return ret;
3744}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003745#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */