blob: 4c65305fd4180874a5088446c65953b510b2d03c [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 Bacik9006ad12018-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"
Chris Masonfa9c0d792009-04-03 09:47:43 -040021
Feifei Xu0ef64472016-06-01 19:18:24 +080022#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Byongho Leeee221842015-12-15 01:42:10 +090023#define MAX_CACHE_BYTES_PER_GIG SZ_32K
Josef Bacik96303082009-07-13 21:29:25 -040024
Filipe Manana55507ce2014-12-01 17:04:09 +000025struct btrfs_trim_range {
26 u64 start;
27 u64 bytes;
28 struct list_head list;
29};
30
Li Zefan34d52cb2011-03-29 13:46:06 +080031static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040032 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040033static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
34 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040035static int btrfs_wait_cache_io_root(struct btrfs_root *root,
36 struct btrfs_trans_handle *trans,
37 struct btrfs_io_ctl *io_ctl,
38 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040039
Li Zefan0414efa2011-04-20 10:20:14 +080040static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
41 struct btrfs_path *path,
42 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040044 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040045 struct btrfs_key key;
46 struct btrfs_key location;
47 struct btrfs_disk_key disk_key;
48 struct btrfs_free_space_header *header;
49 struct extent_buffer *leaf;
50 struct inode *inode = NULL;
Josef Bacik9006ad12018-09-28 07:17:49 -040051 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040052 int ret;
53
Josef Bacik0af3d002010-06-21 14:48:16 -040054 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080055 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040056 key.type = 0;
57
58 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
59 if (ret < 0)
60 return ERR_PTR(ret);
61 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020062 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040063 return ERR_PTR(-ENOENT);
64 }
65
66 leaf = path->nodes[0];
67 header = btrfs_item_ptr(leaf, path->slots[0],
68 struct btrfs_free_space_header);
69 btrfs_free_space_key(leaf, header, &disk_key);
70 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020071 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040072
Josef Bacik9006ad12018-09-28 07:17:49 -040073 /*
74 * We are often under a trans handle at this point, so we need to make
75 * sure NOFS is set to keep us from deadlocking.
76 */
77 nofs_flag = memalloc_nofs_save();
Filipe Mananacb38a172018-10-24 10:13:03 +010078 inode = btrfs_iget_path(fs_info->sb, &location, root, NULL, path);
79 btrfs_release_path(path);
Josef Bacik9006ad12018-09-28 07:17:49 -040080 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040081 if (IS_ERR(inode))
82 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040083
Al Viro528c0322012-04-13 11:03:55 -040084 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080085 mapping_gfp_constraint(inode->i_mapping,
86 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000087
Li Zefan0414efa2011-04-20 10:20:14 +080088 return inode;
89}
90
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050091struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +080092 struct btrfs_block_group_cache
93 *block_group, struct btrfs_path *path)
94{
95 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040096 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080097
98 spin_lock(&block_group->lock);
99 if (block_group->inode)
100 inode = igrab(block_group->inode);
101 spin_unlock(&block_group->lock);
102 if (inode)
103 return inode;
104
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500105 inode = __lookup_free_space_inode(fs_info->tree_root, path,
Li Zefan0414efa2011-04-20 10:20:14 +0800106 block_group->key.objectid);
107 if (IS_ERR(inode))
108 return inode;
109
Josef Bacik0af3d002010-06-21 14:48:16 -0400110 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400111 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400112 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400113 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
114 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400115 block_group->disk_cache_state = BTRFS_DC_CLEAR;
116 }
117
Josef Bacik300e4f82011-08-29 14:06:00 -0400118 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400119 block_group->inode = igrab(inode);
120 block_group->iref = 1;
121 }
122 spin_unlock(&block_group->lock);
123
124 return inode;
125}
126
Eric Sandeen48a3b632013-04-25 20:41:01 +0000127static int __create_free_space_inode(struct btrfs_root *root,
128 struct btrfs_trans_handle *trans,
129 struct btrfs_path *path,
130 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400131{
132 struct btrfs_key key;
133 struct btrfs_disk_key disk_key;
134 struct btrfs_free_space_header *header;
135 struct btrfs_inode_item *inode_item;
136 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400137 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400138 int ret;
139
Li Zefan0414efa2011-04-20 10:20:14 +0800140 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400141 if (ret)
142 return ret;
143
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400144 /* We inline crc's for the free disk space cache */
145 if (ino != BTRFS_FREE_INO_OBJECTID)
146 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
147
Josef Bacik0af3d002010-06-21 14:48:16 -0400148 leaf = path->nodes[0];
149 inode_item = btrfs_item_ptr(leaf, path->slots[0],
150 struct btrfs_inode_item);
151 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100152 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400153 sizeof(*inode_item));
154 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
155 btrfs_set_inode_size(leaf, inode_item, 0);
156 btrfs_set_inode_nbytes(leaf, inode_item, 0);
157 btrfs_set_inode_uid(leaf, inode_item, 0);
158 btrfs_set_inode_gid(leaf, inode_item, 0);
159 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400160 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400161 btrfs_set_inode_nlink(leaf, inode_item, 1);
162 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800163 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200165 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400166
167 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800168 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400169 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400170 ret = btrfs_insert_empty_item(trans, root, path, &key,
171 sizeof(struct btrfs_free_space_header));
172 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200173 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400174 return ret;
175 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700176
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 leaf = path->nodes[0];
178 header = btrfs_item_ptr(leaf, path->slots[0],
179 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100180 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400181 btrfs_set_free_space_key(leaf, header, &disk_key);
182 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200183 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400184
185 return 0;
186}
187
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500188int create_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +0800189 struct btrfs_trans_handle *trans,
190 struct btrfs_block_group_cache *block_group,
191 struct btrfs_path *path)
192{
193 int ret;
194 u64 ino;
195
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500196 ret = btrfs_find_free_objectid(fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800197 if (ret < 0)
198 return ret;
199
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500200 return __create_free_space_inode(fs_info->tree_root, trans, path, ino,
Li Zefan0414efa2011-04-20 10:20:14 +0800201 block_group->key.objectid);
202}
203
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400204int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000205 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400206{
Josef Bacikc8174312011-11-02 09:29:35 -0400207 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000208 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400209
210 /* 1 for slack space, 1 for updating the inode */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400211 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
212 btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400213
Miao Xie7b61cd92013-05-13 13:55:09 +0000214 spin_lock(&rsv->lock);
215 if (rsv->reserved < needed_bytes)
216 ret = -ENOSPC;
217 else
218 ret = 0;
219 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000220 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000221}
222
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500223int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Chris Mason1bbc6212015-04-06 12:46:08 -0700224 struct btrfs_block_group_cache *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000225 struct inode *inode)
226{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500227 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000228 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100229 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700230
Chris Mason1bbc6212015-04-06 12:46:08 -0700231 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500232 struct btrfs_path *path = btrfs_alloc_path();
233
234 if (!path) {
235 ret = -ENOMEM;
236 goto fail;
237 }
Filipe Manana35c76642015-04-30 17:47:05 +0100238 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700239 mutex_lock(&trans->transaction->cache_write_mutex);
240 if (!list_empty(&block_group->io_list)) {
241 list_del_init(&block_group->io_list);
242
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400243 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700244 btrfs_put_block_group(block_group);
245 }
246
247 /*
248 * now that we've truncated the cache away, its no longer
249 * setup or written
250 */
251 spin_lock(&block_group->lock);
252 block_group->disk_cache_state = BTRFS_DC_CLEAR;
253 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500254 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700255 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400256
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200257 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700258 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400259
260 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700261 * We skip the throttling logic for free space cache inodes, so we don't
262 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400263 */
264 ret = btrfs_truncate_inode_items(trans, root, inode,
265 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100266 if (ret)
267 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400268
Li Zefan82d59022011-04-20 10:33:24 +0800269 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700270
Chris Mason1bbc6212015-04-06 12:46:08 -0700271fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100272 if (locked)
273 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100274 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400275 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400276
Li Zefan82d59022011-04-20 10:33:24 +0800277 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400278}
279
David Sterba1d480532017-01-23 17:28:19 +0100280static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400281{
282 struct file_ra_state *ra;
283 unsigned long last_index;
284
285 ra = kzalloc(sizeof(*ra), GFP_NOFS);
286 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100287 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400288
289 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300290 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400291
292 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
293
294 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400295}
296
Chris Mason4c6d1d82015-04-06 13:17:20 -0700297static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400298 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400299{
Miao Xie5349d6c2014-06-19 10:42:49 +0800300 int num_pages;
301 int check_crcs = 0;
302
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300303 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800304
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200305 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800306 check_crcs = 1;
307
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400308 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800309 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400310 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800311 return -ENOSPC;
312
Chris Mason4c6d1d82015-04-06 13:17:20 -0700313 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800314
David Sterba31e818f2015-02-20 18:00:26 +0100315 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400316 if (!io_ctl->pages)
317 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800318
319 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400320 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800321 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700322 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800323
Josef Bacika67509c2011-10-05 15:18:58 -0400324 return 0;
325}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900326ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400327
Chris Mason4c6d1d82015-04-06 13:17:20 -0700328static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400329{
330 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700331 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400332}
333
Chris Mason4c6d1d82015-04-06 13:17:20 -0700334static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400335{
336 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400337 io_ctl->cur = NULL;
338 io_ctl->orig = NULL;
339 }
340}
341
Chris Mason4c6d1d82015-04-06 13:17:20 -0700342static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400343{
Josef Bacikb12d6862013-08-26 17:14:08 -0400344 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400345 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700346 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400347 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300348 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400349 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200350 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400351}
352
Chris Mason4c6d1d82015-04-06 13:17:20 -0700353static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400354{
355 int i;
356
357 io_ctl_unmap_page(io_ctl);
358
359 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800360 if (io_ctl->pages[i]) {
361 ClearPageChecked(io_ctl->pages[i]);
362 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300363 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800364 }
Josef Bacika67509c2011-10-05 15:18:58 -0400365 }
366}
367
Chris Mason4c6d1d82015-04-06 13:17:20 -0700368static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400369 int uptodate)
370{
371 struct page *page;
372 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
373 int i;
374
375 for (i = 0; i < io_ctl->num_pages; i++) {
376 page = find_or_create_page(inode->i_mapping, i, mask);
377 if (!page) {
378 io_ctl_drop_pages(io_ctl);
379 return -ENOMEM;
380 }
381 io_ctl->pages[i] = page;
382 if (uptodate && !PageUptodate(page)) {
383 btrfs_readpage(NULL, page);
384 lock_page(page);
Josef Bacik6e3b9062019-09-24 16:50:43 -0400385 if (page->mapping != inode->i_mapping) {
386 btrfs_err(BTRFS_I(inode)->root->fs_info,
387 "free space cache page truncated");
388 io_ctl_drop_pages(io_ctl);
389 return -EIO;
390 }
Josef Bacika67509c2011-10-05 15:18:58 -0400391 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500392 btrfs_err(BTRFS_I(inode)->root->fs_info,
393 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400394 io_ctl_drop_pages(io_ctl);
395 return -EIO;
396 }
397 }
398 }
399
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500400 for (i = 0; i < io_ctl->num_pages; i++) {
401 clear_page_dirty_for_io(io_ctl->pages[i]);
402 set_page_extent_mapped(io_ctl->pages[i]);
403 }
404
Josef Bacika67509c2011-10-05 15:18:58 -0400405 return 0;
406}
407
Chris Mason4c6d1d82015-04-06 13:17:20 -0700408static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400409{
Al Viro528c0322012-04-13 11:03:55 -0400410 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400411
412 io_ctl_map_page(io_ctl, 1);
413
414 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400415 * Skip the csum areas. If we don't check crcs then we just have a
416 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400417 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400418 if (io_ctl->check_crcs) {
419 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
420 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
421 } else {
422 io_ctl->cur += sizeof(u64);
423 io_ctl->size -= sizeof(u64) * 2;
424 }
Josef Bacika67509c2011-10-05 15:18:58 -0400425
426 val = io_ctl->cur;
427 *val = cpu_to_le64(generation);
428 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400429}
430
Chris Mason4c6d1d82015-04-06 13:17:20 -0700431static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400432{
Al Viro528c0322012-04-13 11:03:55 -0400433 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400434
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400435 /*
436 * Skip the crc area. If we don't check crcs then we just have a 64bit
437 * chunk at the front of the first page.
438 */
439 if (io_ctl->check_crcs) {
440 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
441 io_ctl->size -= sizeof(u64) +
442 (sizeof(u32) * io_ctl->num_pages);
443 } else {
444 io_ctl->cur += sizeof(u64);
445 io_ctl->size -= sizeof(u64) * 2;
446 }
Josef Bacika67509c2011-10-05 15:18:58 -0400447
Josef Bacika67509c2011-10-05 15:18:58 -0400448 gen = io_ctl->cur;
449 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400450 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200451 "space cache generation (%llu) does not match inode (%llu)",
452 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400453 io_ctl_unmap_page(io_ctl);
454 return -EIO;
455 }
456 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400457 return 0;
458}
459
Chris Mason4c6d1d82015-04-06 13:17:20 -0700460static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400461{
462 u32 *tmp;
463 u32 crc = ~(u32)0;
464 unsigned offset = 0;
465
466 if (!io_ctl->check_crcs) {
467 io_ctl_unmap_page(io_ctl);
468 return;
469 }
470
471 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800472 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400473
Liu Bob0496682013-03-14 14:57:45 +0000474 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300475 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100476 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400477 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700478 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400479 tmp += index;
480 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400481}
482
Chris Mason4c6d1d82015-04-06 13:17:20 -0700483static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400484{
485 u32 *tmp, val;
486 u32 crc = ~(u32)0;
487 unsigned offset = 0;
488
489 if (!io_ctl->check_crcs) {
490 io_ctl_map_page(io_ctl, 0);
491 return 0;
492 }
493
494 if (index == 0)
495 offset = sizeof(u32) * io_ctl->num_pages;
496
Chris Mason2b108262015-04-06 07:48:20 -0700497 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400498 tmp += index;
499 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400500
501 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000502 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300503 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100504 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400506 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200507 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400508 io_ctl_unmap_page(io_ctl);
509 return -EIO;
510 }
511
Josef Bacika67509c2011-10-05 15:18:58 -0400512 return 0;
513}
514
Chris Mason4c6d1d82015-04-06 13:17:20 -0700515static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400516 void *bitmap)
517{
518 struct btrfs_free_space_entry *entry;
519
520 if (!io_ctl->cur)
521 return -ENOSPC;
522
523 entry = io_ctl->cur;
524 entry->offset = cpu_to_le64(offset);
525 entry->bytes = cpu_to_le64(bytes);
526 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
527 BTRFS_FREE_SPACE_EXTENT;
528 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
529 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
530
531 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
532 return 0;
533
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400534 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400535
536 /* No more pages to map */
537 if (io_ctl->index >= io_ctl->num_pages)
538 return 0;
539
540 /* map the next page */
541 io_ctl_map_page(io_ctl, 1);
542 return 0;
543}
544
Chris Mason4c6d1d82015-04-06 13:17:20 -0700545static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400546{
547 if (!io_ctl->cur)
548 return -ENOSPC;
549
550 /*
551 * If we aren't at the start of the current page, unmap this one and
552 * map the next one if there is any left.
553 */
554 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400555 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400556 if (io_ctl->index >= io_ctl->num_pages)
557 return -ENOSPC;
558 io_ctl_map_page(io_ctl, 0);
559 }
560
David Sterba69d24802018-06-29 10:56:44 +0200561 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400562 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400563 if (io_ctl->index < io_ctl->num_pages)
564 io_ctl_map_page(io_ctl, 0);
565 return 0;
566}
567
Chris Mason4c6d1d82015-04-06 13:17:20 -0700568static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400569{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400570 /*
571 * If we're not on the boundary we know we've modified the page and we
572 * need to crc the page.
573 */
574 if (io_ctl->cur != io_ctl->orig)
575 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
576 else
577 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400578
579 while (io_ctl->index < io_ctl->num_pages) {
580 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400581 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400582 }
583}
584
Chris Mason4c6d1d82015-04-06 13:17:20 -0700585static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400586 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400587{
588 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500589 int ret;
590
591 if (!io_ctl->cur) {
592 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
593 if (ret)
594 return ret;
595 }
Josef Bacika67509c2011-10-05 15:18:58 -0400596
597 e = io_ctl->cur;
598 entry->offset = le64_to_cpu(e->offset);
599 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400600 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400601 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
602 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
603
604 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400605 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400606
607 io_ctl_unmap_page(io_ctl);
608
Josef Bacik2f120c02011-11-10 20:45:05 -0500609 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400610}
611
Chris Mason4c6d1d82015-04-06 13:17:20 -0700612static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400613 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400614{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400615 int ret;
616
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400617 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
618 if (ret)
619 return ret;
620
David Sterba69d24802018-06-29 10:56:44 +0200621 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400622 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400623
624 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400625}
626
Josef Bacikcd023e72012-05-14 10:06:40 -0400627/*
628 * Since we attach pinned extents after the fact we can have contiguous sections
629 * of free space that are split up in entries. This poses a problem with the
630 * tree logging stuff since it could have allocated across what appears to be 2
631 * entries since we would have merged the entries when adding the pinned extents
632 * back to the free space cache. So run through the space cache that we just
633 * loaded and merge contiguous entries. This will make the log replay stuff not
634 * blow up and it will make for nicer allocator behavior.
635 */
636static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
637{
638 struct btrfs_free_space *e, *prev = NULL;
639 struct rb_node *n;
640
641again:
642 spin_lock(&ctl->tree_lock);
643 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
644 e = rb_entry(n, struct btrfs_free_space, offset_index);
645 if (!prev)
646 goto next;
647 if (e->bitmap || prev->bitmap)
648 goto next;
649 if (prev->offset + prev->bytes == e->offset) {
650 unlink_free_space(ctl, prev);
651 unlink_free_space(ctl, e);
652 prev->bytes += e->bytes;
653 kmem_cache_free(btrfs_free_space_cachep, e);
654 link_free_space(ctl, prev);
655 prev = NULL;
656 spin_unlock(&ctl->tree_lock);
657 goto again;
658 }
659next:
660 prev = e;
661 }
662 spin_unlock(&ctl->tree_lock);
663}
664
Eric Sandeen48a3b632013-04-25 20:41:01 +0000665static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
666 struct btrfs_free_space_ctl *ctl,
667 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400668{
David Sterba3ffbd682018-06-29 10:56:42 +0200669 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 struct btrfs_free_space_header *header;
671 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700672 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400673 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400674 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800675 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400676 u64 num_entries;
677 u64 num_bitmaps;
678 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400679 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400680 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400681
Josef Bacik9d66e232010-08-25 16:54:15 -0400682 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800683 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400684 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685
686 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800687 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400688 key.type = 0;
689
690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800691 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400692 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800693 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400694 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400695 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400696 }
697
Li Zefan0414efa2011-04-20 10:20:14 +0800698 ret = -1;
699
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 leaf = path->nodes[0];
701 header = btrfs_item_ptr(leaf, path->slots[0],
702 struct btrfs_free_space_header);
703 num_entries = btrfs_free_space_entries(leaf, header);
704 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
705 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400706 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400707
Miao Xiee570fd22014-06-19 10:42:50 +0800708 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400709 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200710 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800711 offset);
712 return 0;
713 }
714
Josef Bacik9d66e232010-08-25 16:54:15 -0400715 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 btrfs_err(fs_info,
717 "free space inode generation (%llu) did not match free space cache generation (%llu)",
718 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400719 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 }
721
722 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400723 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400724
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400725 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800726 if (ret)
727 return ret;
728
David Sterba1d480532017-01-23 17:28:19 +0100729 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400730
Josef Bacika67509c2011-10-05 15:18:58 -0400731 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
732 if (ret)
733 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400734
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400735 ret = io_ctl_check_crc(&io_ctl, 0);
736 if (ret)
737 goto free_cache;
738
Josef Bacika67509c2011-10-05 15:18:58 -0400739 ret = io_ctl_check_generation(&io_ctl, generation);
740 if (ret)
741 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400742
Josef Bacika67509c2011-10-05 15:18:58 -0400743 while (num_entries) {
744 e = kmem_cache_zalloc(btrfs_free_space_cachep,
745 GFP_NOFS);
746 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400747 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400748
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400749 ret = io_ctl_read_entry(&io_ctl, e, &type);
750 if (ret) {
751 kmem_cache_free(btrfs_free_space_cachep, e);
752 goto free_cache;
753 }
754
Josef Bacika67509c2011-10-05 15:18:58 -0400755 if (!e->bytes) {
756 kmem_cache_free(btrfs_free_space_cachep, e);
757 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400758 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400759
Josef Bacika67509c2011-10-05 15:18:58 -0400760 if (type == BTRFS_FREE_SPACE_EXTENT) {
761 spin_lock(&ctl->tree_lock);
762 ret = link_free_space(ctl, e);
763 spin_unlock(&ctl->tree_lock);
764 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400765 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000766 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500767 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400768 goto free_cache;
769 }
Josef Bacika67509c2011-10-05 15:18:58 -0400770 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400771 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400772 num_bitmaps--;
Christophe Leroy4874c6f2019-08-21 15:05:55 +0000773 e->bitmap = kmem_cache_zalloc(
774 btrfs_free_space_bitmap_cachep, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400775 if (!e->bitmap) {
776 kmem_cache_free(
777 btrfs_free_space_cachep, e);
778 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400779 }
Josef Bacika67509c2011-10-05 15:18:58 -0400780 spin_lock(&ctl->tree_lock);
781 ret = link_free_space(ctl, e);
782 ctl->total_bitmaps++;
783 ctl->op->recalc_thresholds(ctl);
784 spin_unlock(&ctl->tree_lock);
785 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400786 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000787 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400788 kmem_cache_free(btrfs_free_space_cachep, e);
789 goto free_cache;
790 }
791 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400792 }
793
Josef Bacika67509c2011-10-05 15:18:58 -0400794 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400795 }
796
Josef Bacik2f120c02011-11-10 20:45:05 -0500797 io_ctl_unmap_page(&io_ctl);
798
Josef Bacika67509c2011-10-05 15:18:58 -0400799 /*
800 * We add the bitmaps at the end of the entries in order that
801 * the bitmap entries are added to the cache.
802 */
803 list_for_each_entry_safe(e, n, &bitmaps, list) {
804 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400805 ret = io_ctl_read_bitmap(&io_ctl, e);
806 if (ret)
807 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400808 }
809
810 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400811 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400812 ret = 1;
813out:
Josef Bacika67509c2011-10-05 15:18:58 -0400814 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400815 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400816free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400817 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800818 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400819 goto out;
820}
821
Li Zefan0414efa2011-04-20 10:20:14 +0800822int load_free_space_cache(struct btrfs_fs_info *fs_info,
823 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400824{
Li Zefan34d52cb2011-03-29 13:46:06 +0800825 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800826 struct inode *inode;
827 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400828 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800829 bool matched;
830 u64 used = btrfs_block_group_used(&block_group->item);
831
832 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800833 * If this block group has been marked to be cleared for one reason or
834 * another then we can't trust the on disk cache, so just return.
835 */
836 spin_lock(&block_group->lock);
837 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
838 spin_unlock(&block_group->lock);
839 return 0;
840 }
841 spin_unlock(&block_group->lock);
842
843 path = btrfs_alloc_path();
844 if (!path)
845 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400846 path->search_commit_root = 1;
847 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800848
Filipe Mananacb38a172018-10-24 10:13:03 +0100849 /*
850 * We must pass a path with search_commit_root set to btrfs_iget in
851 * order to avoid a deadlock when allocating extents for the tree root.
852 *
853 * When we are COWing an extent buffer from the tree root, when looking
854 * for a free extent, at extent-tree.c:find_free_extent(), we can find
855 * block group without its free space cache loaded. When we find one
856 * we must load its space cache which requires reading its free space
857 * cache's inode item from the root tree. If this inode item is located
858 * in the same leaf that we started COWing before, then we end up in
859 * deadlock on the extent buffer (trying to read lock it when we
860 * previously write locked it).
861 *
862 * It's safe to read the inode item using the commit root because
863 * block groups, once loaded, stay in memory forever (until they are
864 * removed) as well as their space caches once loaded. New block groups
865 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
866 * we will never try to read their inode item while the fs is mounted.
867 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500868 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800869 if (IS_ERR(inode)) {
870 btrfs_free_path(path);
871 return 0;
872 }
873
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400874 /* We may have converted the inode and made the cache invalid. */
875 spin_lock(&block_group->lock);
876 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
877 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900878 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400879 goto out;
880 }
881 spin_unlock(&block_group->lock);
882
Li Zefan0414efa2011-04-20 10:20:14 +0800883 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
884 path, block_group->key.objectid);
885 btrfs_free_path(path);
886 if (ret <= 0)
887 goto out;
888
889 spin_lock(&ctl->tree_lock);
890 matched = (ctl->free_space == (block_group->key.offset - used -
891 block_group->bytes_super));
892 spin_unlock(&ctl->tree_lock);
893
894 if (!matched) {
895 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400896 btrfs_warn(fs_info,
897 "block group %llu has wrong amount of free space",
898 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800899 ret = -1;
900 }
901out:
902 if (ret < 0) {
903 /* This cache is bogus, make sure it gets cleared */
904 spin_lock(&block_group->lock);
905 block_group->disk_cache_state = BTRFS_DC_CLEAR;
906 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800907 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800908
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400909 btrfs_warn(fs_info,
910 "failed to load free space cache for block group %llu, rebuilding it now",
911 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800912 }
913
914 iput(inode);
915 return ret;
916}
917
Chris Masond4452bc2014-05-19 20:47:56 -0700918static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700919int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700920 struct btrfs_free_space_ctl *ctl,
921 struct btrfs_block_group_cache *block_group,
922 int *entries, int *bitmaps,
923 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400924{
Josef Bacikc09544e2011-08-30 10:19:10 -0400925 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700926 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700927 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700928 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000929 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400930
Josef Bacik43be2142011-04-01 14:55:00 +0000931 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700932 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000933 cluster = list_entry(block_group->cluster_list.next,
934 struct btrfs_free_cluster,
935 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700936 }
Josef Bacik43be2142011-04-01 14:55:00 +0000937
Josef Bacikf75b1302011-10-05 10:00:18 -0400938 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700939 cluster_locked = cluster;
940 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400941 node = rb_first(&cluster->root);
942 cluster = NULL;
943 }
944
Josef Bacik0cb59c92010-07-02 12:14:14 -0400945 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400946 while (node) {
947 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400948
Josef Bacika67509c2011-10-05 15:18:58 -0400949 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700950 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000951
Chris Masond4452bc2014-05-19 20:47:56 -0700952 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400953 e->bitmap);
954 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700955 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400956
957 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700958 list_add_tail(&e->list, bitmap_list);
959 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400960 }
961 node = rb_next(node);
962 if (!node && cluster) {
963 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700964 cluster_locked = cluster;
965 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400966 cluster = NULL;
967 }
968 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700969 if (cluster_locked) {
970 spin_unlock(&cluster_locked->lock);
971 cluster_locked = NULL;
972 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000973
974 /*
975 * Make sure we don't miss any range that was removed from our rbtree
976 * because trimming is running. Otherwise after a umount+mount (or crash
977 * after committing the transaction) we would leak free space and get
978 * an inconsistent free space cache report from fsck.
979 */
980 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
981 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
982 trim_entry->bytes, NULL);
983 if (ret)
984 goto fail;
985 *entries += 1;
986 }
987
Chris Masond4452bc2014-05-19 20:47:56 -0700988 return 0;
989fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700990 if (cluster_locked)
991 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700992 return -ENOSPC;
993}
994
995static noinline_for_stack int
996update_cache_item(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
998 struct inode *inode,
999 struct btrfs_path *path, u64 offset,
1000 int entries, int bitmaps)
1001{
1002 struct btrfs_key key;
1003 struct btrfs_free_space_header *header;
1004 struct extent_buffer *leaf;
1005 int ret;
1006
1007 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1008 key.offset = offset;
1009 key.type = 0;
1010
1011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1012 if (ret < 0) {
1013 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001014 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001015 goto fail;
1016 }
1017 leaf = path->nodes[0];
1018 if (ret > 0) {
1019 struct btrfs_key found_key;
1020 ASSERT(path->slots[0]);
1021 path->slots[0]--;
1022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1023 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1024 found_key.offset != offset) {
1025 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1026 inode->i_size - 1,
1027 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001028 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
1049static noinline_for_stack int
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001050write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
Miao Xie5349d6c2014-06-19 10:42:49 +08001051 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001052 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001053 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001054{
1055 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001056 struct extent_io_tree *unpin = NULL;
1057 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001058
Miao Xie5349d6c2014-06-19 10:42:49 +08001059 if (!block_group)
1060 return 0;
1061
Josef Bacika67509c2011-10-05 15:18:58 -04001062 /*
1063 * We want to add any pinned extents to our free space cache
1064 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001065 *
Li Zefandb804f22012-01-10 16:41:01 +08001066 * We shouldn't have switched the pinned extents yet so this is the
1067 * right one
1068 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001069 unpin = fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001070
Miao Xie5349d6c2014-06-19 10:42:49 +08001071 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001072
Miao Xie5349d6c2014-06-19 10:42:49 +08001073 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001074 ret = find_first_extent_bit(unpin, start,
1075 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001076 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001077 if (ret)
1078 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001079
Josef Bacika67509c2011-10-05 15:18:58 -04001080 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001081 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001082 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001083 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001084
Li Zefandb804f22012-01-10 16:41:01 +08001085 extent_start = max(extent_start, start);
1086 extent_end = min(block_group->key.objectid +
1087 block_group->key.offset, extent_end + 1);
1088 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001089
Chris Masond4452bc2014-05-19 20:47:56 -07001090 *entries += 1;
1091 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001092 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001093 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001094
Li Zefandb804f22012-01-10 16:41:01 +08001095 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001096 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001097
Miao Xie5349d6c2014-06-19 10:42:49 +08001098 return 0;
1099}
1100
1101static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001102write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001103{
Geliang Tang7ae16812015-12-18 22:17:00 +08001104 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001105 int ret;
1106
Josef Bacik0cb59c92010-07-02 12:14:14 -04001107 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001108 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001109 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001110 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001111 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001112 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001113 }
1114
Miao Xie5349d6c2014-06-19 10:42:49 +08001115 return 0;
1116}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001117
Miao Xie5349d6c2014-06-19 10:42:49 +08001118static int flush_dirty_cache(struct inode *inode)
1119{
1120 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001121
Josef Bacik0ef8b722013-10-25 16:13:35 -04001122 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001123 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001124 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001125 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001126
Miao Xie5349d6c2014-06-19 10:42:49 +08001127 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001128}
1129
1130static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001131cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001132{
Geliang Tang7ae16812015-12-18 22:17:00 +08001133 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001134
Geliang Tang7ae16812015-12-18 22:17:00 +08001135 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001136 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001137}
1138
1139static void noinline_for_stack
1140cleanup_write_cache_enospc(struct inode *inode,
1141 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001142 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001143{
Chris Masond4452bc2014-05-19 20:47:56 -07001144 io_ctl_drop_pages(io_ctl);
1145 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001146 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001147}
1148
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001149static int __btrfs_wait_cache_io(struct btrfs_root *root,
1150 struct btrfs_trans_handle *trans,
1151 struct btrfs_block_group_cache *block_group,
1152 struct btrfs_io_ctl *io_ctl,
1153 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001154{
1155 int ret;
1156 struct inode *inode = io_ctl->inode;
1157
Chris Mason1bbc6212015-04-06 12:46:08 -07001158 if (!inode)
1159 return 0;
1160
Chris Masonc9dc4c62015-04-04 17:14:42 -07001161 /* Flush the dirty pages in the cache file. */
1162 ret = flush_dirty_cache(inode);
1163 if (ret)
1164 goto out;
1165
1166 /* Update the cache item to tell everyone this cache file is valid. */
1167 ret = update_cache_item(trans, root, inode, path, offset,
1168 io_ctl->entries, io_ctl->bitmaps);
1169out:
1170 io_ctl_free(io_ctl);
1171 if (ret) {
1172 invalidate_inode_pages2(inode->i_mapping);
1173 BTRFS_I(inode)->generation = 0;
1174 if (block_group) {
1175#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001176 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 "failed to write free space cache for block group %llu",
1178 block_group->key.objectid);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001179#endif
1180 }
1181 }
1182 btrfs_update_inode(trans, root, inode);
1183
1184 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001185 /* the dirty list is protected by the dirty_bgs_lock */
1186 spin_lock(&trans->transaction->dirty_bgs_lock);
1187
1188 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001189 spin_lock(&block_group->lock);
1190
1191 /*
1192 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001193 * the dirty list while waiting for IO. Otherwise our
1194 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001195 */
1196 if (!ret && list_empty(&block_group->dirty_list))
1197 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1198 else if (ret)
1199 block_group->disk_cache_state = BTRFS_DC_ERROR;
1200
1201 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001202 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001203 io_ctl->inode = NULL;
1204 iput(inode);
1205 }
1206
1207 return ret;
1208
1209}
1210
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001211static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1212 struct btrfs_trans_handle *trans,
1213 struct btrfs_io_ctl *io_ctl,
1214 struct btrfs_path *path)
1215{
1216 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1217}
1218
1219int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1220 struct btrfs_block_group_cache *block_group,
1221 struct btrfs_path *path)
1222{
1223 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1224 block_group, &block_group->io_ctl,
1225 path, block_group->key.objectid);
1226}
1227
Chris Masond4452bc2014-05-19 20:47:56 -07001228/**
1229 * __btrfs_write_out_cache - write out cached info to an inode
1230 * @root - the root the inode belongs to
1231 * @ctl - the free space cache we are going to write out
1232 * @block_group - the block_group for this cache if it belongs to a block_group
1233 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001234 *
1235 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001236 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001237 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001238 */
1239static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1240 struct btrfs_free_space_ctl *ctl,
1241 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001242 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001243 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001244{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001245 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masond4452bc2014-05-19 20:47:56 -07001246 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001247 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001248 int entries = 0;
1249 int bitmaps = 0;
1250 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001251 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001252
1253 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001254 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001255
Chris Masonc9dc4c62015-04-04 17:14:42 -07001256 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001257 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001258 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001259 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001260
Miao Xiee570fd22014-06-19 10:42:50 +08001261 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1262 down_write(&block_group->data_rwsem);
1263 spin_lock(&block_group->lock);
1264 if (block_group->delalloc_bytes) {
1265 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1266 spin_unlock(&block_group->lock);
1267 up_write(&block_group->data_rwsem);
1268 BTRFS_I(inode)->generation = 0;
1269 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001270 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001271 goto out;
1272 }
1273 spin_unlock(&block_group->lock);
1274 }
1275
Chris Masond4452bc2014-05-19 20:47:56 -07001276 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001277 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1278 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001279 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001280
1281 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001282 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001283
Chris Masonc9dc4c62015-04-04 17:14:42 -07001284 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001285
Filipe Manana55507ce2014-12-01 17:04:09 +00001286 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001287 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001288 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001289 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001290 block_group, &entries, &bitmaps,
1291 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001292 if (ret)
1293 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001294
Miao Xie5349d6c2014-06-19 10:42:49 +08001295 /*
1296 * Some spaces that are freed in the current transaction are pinned,
1297 * they will be added into free space cache after the transaction is
1298 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001299 *
1300 * If this changes while we are working we'll get added back to
1301 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001302 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001303 ret = write_pinned_extent_entries(fs_info, block_group,
1304 io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001305 if (ret)
1306 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001307
Filipe Manana55507ce2014-12-01 17:04:09 +00001308 /*
1309 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1310 * locked while doing it because a concurrent trim can be manipulating
1311 * or freeing the bitmap.
1312 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001313 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001314 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001315 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001316 if (ret)
1317 goto out_nospc;
1318
1319 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001320 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001321
1322 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001323 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1324 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001325 if (ret)
1326 goto out_nospc;
1327
Miao Xiee570fd22014-06-19 10:42:50 +08001328 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1329 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001330 /*
1331 * Release the pages and unlock the extent, we will flush
1332 * them out later
1333 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001334 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001335
1336 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001337 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001338
Chris Masonc9dc4c62015-04-04 17:14:42 -07001339 /*
1340 * at this point the pages are under IO and we're happy,
1341 * The caller is responsible for waiting on them and updating the
1342 * the cache and the inode
1343 */
1344 io_ctl->entries = entries;
1345 io_ctl->bitmaps = bitmaps;
1346
1347 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001348 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001349 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001350
Chris Masonc9dc4c62015-04-04 17:14:42 -07001351 return 0;
1352
Josef Bacik2f356122011-06-10 15:31:13 -04001353out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001354 io_ctl->inode = NULL;
1355 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001356 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001357 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001358 BTRFS_I(inode)->generation = 0;
1359 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001360 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001361 if (must_iput)
1362 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001363 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001364
Chris Masona3bdccc2015-04-24 11:00:00 -07001365out_nospc_locked:
1366 cleanup_bitmap_list(&bitmap_list);
1367 spin_unlock(&ctl->tree_lock);
1368 mutex_unlock(&ctl->cache_writeout_mutex);
1369
Josef Bacika67509c2011-10-05 15:18:58 -04001370out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001371 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001372
Josef Bacikb77000e2017-11-15 16:20:52 -05001373out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001374 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1375 up_write(&block_group->data_rwsem);
1376
Josef Bacika67509c2011-10-05 15:18:58 -04001377 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001378}
1379
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001380int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +08001381 struct btrfs_trans_handle *trans,
1382 struct btrfs_block_group_cache *block_group,
1383 struct btrfs_path *path)
1384{
1385 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1386 struct inode *inode;
1387 int ret = 0;
1388
Li Zefan0414efa2011-04-20 10:20:14 +08001389 spin_lock(&block_group->lock);
1390 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1391 spin_unlock(&block_group->lock);
1392 return 0;
1393 }
1394 spin_unlock(&block_group->lock);
1395
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001396 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001397 if (IS_ERR(inode))
1398 return 0;
1399
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001400 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1401 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001402 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001403#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001404 btrfs_err(fs_info,
1405 "failed to write free space cache for block group %llu",
1406 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001407#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001408 spin_lock(&block_group->lock);
1409 block_group->disk_cache_state = BTRFS_DC_ERROR;
1410 spin_unlock(&block_group->lock);
1411
1412 block_group->io_ctl.inode = NULL;
1413 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001414 }
1415
Chris Masonc9dc4c62015-04-04 17:14:42 -07001416 /*
1417 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1418 * to wait for IO and put the inode
1419 */
1420
Josef Bacik0cb59c92010-07-02 12:14:14 -04001421 return ret;
1422}
1423
Li Zefan34d52cb2011-03-29 13:46:06 +08001424static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001425 u64 offset)
1426{
Josef Bacikb12d6862013-08-26 17:14:08 -04001427 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001428 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001429 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001430}
1431
Li Zefan34d52cb2011-03-29 13:46:06 +08001432static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001433{
Li Zefan34d52cb2011-03-29 13:46:06 +08001434 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001435}
1436
Li Zefan34d52cb2011-03-29 13:46:06 +08001437static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001438 u64 offset)
1439{
1440 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001441 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001442
Li Zefan34d52cb2011-03-29 13:46:06 +08001443 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1444 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001445 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001446 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001447 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001448
1449 return bitmap_start;
1450}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001451
1452static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001453 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001454{
1455 struct rb_node **p = &root->rb_node;
1456 struct rb_node *parent = NULL;
1457 struct btrfs_free_space *info;
1458
1459 while (*p) {
1460 parent = *p;
1461 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1462
Josef Bacik96303082009-07-13 21:29:25 -04001463 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001464 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001465 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001466 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001467 } else {
1468 /*
1469 * we could have a bitmap entry and an extent entry
1470 * share the same offset. If this is the case, we want
1471 * the extent entry to always be found first if we do a
1472 * linear search through the tree, since we want to have
1473 * the quickest allocation time, and allocating from an
1474 * extent is faster than allocating from a bitmap. So
1475 * if we're inserting a bitmap and we find an entry at
1476 * this offset, we want to go right, or after this entry
1477 * logically. If we are inserting an extent and we've
1478 * found a bitmap, we want to go left, or before
1479 * logically.
1480 */
1481 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001482 if (info->bitmap) {
1483 WARN_ON_ONCE(1);
1484 return -EEXIST;
1485 }
Josef Bacik96303082009-07-13 21:29:25 -04001486 p = &(*p)->rb_right;
1487 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001488 if (!info->bitmap) {
1489 WARN_ON_ONCE(1);
1490 return -EEXIST;
1491 }
Josef Bacik96303082009-07-13 21:29:25 -04001492 p = &(*p)->rb_left;
1493 }
1494 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001495 }
1496
1497 rb_link_node(node, parent, p);
1498 rb_insert_color(node, root);
1499
1500 return 0;
1501}
1502
1503/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001504 * searches the tree for the given offset.
1505 *
Josef Bacik96303082009-07-13 21:29:25 -04001506 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1507 * want a section that has at least bytes size and comes at or after the given
1508 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509 */
Josef Bacik96303082009-07-13 21:29:25 -04001510static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001511tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001512 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001513{
Li Zefan34d52cb2011-03-29 13:46:06 +08001514 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001515 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001516
Josef Bacik96303082009-07-13 21:29:25 -04001517 /* find entry that is closest to the 'offset' */
1518 while (1) {
1519 if (!n) {
1520 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001521 break;
1522 }
Josef Bacik96303082009-07-13 21:29:25 -04001523
1524 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1525 prev = entry;
1526
1527 if (offset < entry->offset)
1528 n = n->rb_left;
1529 else if (offset > entry->offset)
1530 n = n->rb_right;
1531 else
1532 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001533 }
1534
Josef Bacik96303082009-07-13 21:29:25 -04001535 if (bitmap_only) {
1536 if (!entry)
1537 return NULL;
1538 if (entry->bitmap)
1539 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001540
Josef Bacik96303082009-07-13 21:29:25 -04001541 /*
1542 * bitmap entry and extent entry may share same offset,
1543 * in that case, bitmap entry comes after extent entry.
1544 */
1545 n = rb_next(n);
1546 if (!n)
1547 return NULL;
1548 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1549 if (entry->offset != offset)
1550 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001551
Josef Bacik96303082009-07-13 21:29:25 -04001552 WARN_ON(!entry->bitmap);
1553 return entry;
1554 } else if (entry) {
1555 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001556 /*
Josef Bacik96303082009-07-13 21:29:25 -04001557 * if previous extent entry covers the offset,
1558 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001559 */
Miao Xiede6c4112012-10-18 08:18:01 +00001560 n = rb_prev(&entry->offset_index);
1561 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001562 prev = rb_entry(n, struct btrfs_free_space,
1563 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001564 if (!prev->bitmap &&
1565 prev->offset + prev->bytes > offset)
1566 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001567 }
Josef Bacik96303082009-07-13 21:29:25 -04001568 }
1569 return entry;
1570 }
1571
1572 if (!prev)
1573 return NULL;
1574
1575 /* find last entry before the 'offset' */
1576 entry = prev;
1577 if (entry->offset > offset) {
1578 n = rb_prev(&entry->offset_index);
1579 if (n) {
1580 entry = rb_entry(n, struct btrfs_free_space,
1581 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001582 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001583 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001584 if (fuzzy)
1585 return entry;
1586 else
1587 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001588 }
1589 }
1590
Josef Bacik96303082009-07-13 21:29:25 -04001591 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001592 n = rb_prev(&entry->offset_index);
1593 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001594 prev = rb_entry(n, struct btrfs_free_space,
1595 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001596 if (!prev->bitmap &&
1597 prev->offset + prev->bytes > offset)
1598 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001599 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001600 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001601 return entry;
1602 } else if (entry->offset + entry->bytes > offset)
1603 return entry;
1604
1605 if (!fuzzy)
1606 return NULL;
1607
1608 while (1) {
1609 if (entry->bitmap) {
1610 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001611 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001612 break;
1613 } else {
1614 if (entry->offset + entry->bytes > offset)
1615 break;
1616 }
1617
1618 n = rb_next(&entry->offset_index);
1619 if (!n)
1620 return NULL;
1621 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1622 }
1623 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001624}
1625
Li Zefanf333adb2010-11-09 14:57:39 +08001626static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001627__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001628 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001629{
Li Zefan34d52cb2011-03-29 13:46:06 +08001630 rb_erase(&info->offset_index, &ctl->free_space_offset);
1631 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001632}
1633
Li Zefan34d52cb2011-03-29 13:46:06 +08001634static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001635 struct btrfs_free_space *info)
1636{
Li Zefan34d52cb2011-03-29 13:46:06 +08001637 __unlink_free_space(ctl, info);
1638 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001639}
1640
Li Zefan34d52cb2011-03-29 13:46:06 +08001641static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001642 struct btrfs_free_space *info)
1643{
1644 int ret = 0;
1645
Josef Bacikb12d6862013-08-26 17:14:08 -04001646 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001648 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001649 if (ret)
1650 return ret;
1651
Li Zefan34d52cb2011-03-29 13:46:06 +08001652 ctl->free_space += info->bytes;
1653 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001654 return ret;
1655}
1656
Li Zefan34d52cb2011-03-29 13:46:06 +08001657static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001658{
Li Zefan34d52cb2011-03-29 13:46:06 +08001659 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001660 u64 max_bytes;
1661 u64 bitmap_bytes;
1662 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001663 u64 size = block_group->key.offset;
Feifei Xu0ef64472016-06-01 19:18:24 +08001664 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1665 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001666
Feifei Xu0ef64472016-06-01 19:18:24 +08001667 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001668
Josef Bacikb12d6862013-08-26 17:14:08 -04001669 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001670
1671 /*
1672 * The goal is to keep the total amount of memory used per 1gb of space
1673 * at or below 32k, so we need to adjust how much memory we allow to be
1674 * used by extent based free space tracking
1675 */
Byongho Leeee221842015-12-15 01:42:10 +09001676 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001677 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1678 else
Byongho Leeee221842015-12-15 01:42:10 +09001679 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001680
Josef Bacik25891f72009-09-11 16:11:20 -04001681 /*
1682 * we want to account for 1 more bitmap than what we have so we can make
1683 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1684 * we add more bitmaps.
1685 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001686 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001687
Josef Bacik25891f72009-09-11 16:11:20 -04001688 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001689 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001690 return;
Josef Bacik96303082009-07-13 21:29:25 -04001691 }
Josef Bacik25891f72009-09-11 16:11:20 -04001692
1693 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001694 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001695 * bytes we can have, or whatever is less than that.
1696 */
1697 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001698 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001699
Li Zefan34d52cb2011-03-29 13:46:06 +08001700 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001701 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001702}
1703
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001704static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1705 struct btrfs_free_space *info,
1706 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001707{
Li Zefanf38b6e72011-03-14 13:40:51 +08001708 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001709
Li Zefan34d52cb2011-03-29 13:46:06 +08001710 start = offset_to_bit(info->offset, ctl->unit, offset);
1711 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001712 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001713
Li Zefanf38b6e72011-03-14 13:40:51 +08001714 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001715
1716 info->bytes -= bytes;
Josef Bacik9aabbb2e2018-09-28 07:18:00 -04001717 if (info->max_extent_size > ctl->unit)
1718 info->max_extent_size = 0;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001719}
1720
1721static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1722 struct btrfs_free_space *info, u64 offset,
1723 u64 bytes)
1724{
1725 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001726 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001727}
1728
Li Zefan34d52cb2011-03-29 13:46:06 +08001729static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001730 struct btrfs_free_space *info, u64 offset,
1731 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001732{
Li Zefanf38b6e72011-03-14 13:40:51 +08001733 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001734
Li Zefan34d52cb2011-03-29 13:46:06 +08001735 start = offset_to_bit(info->offset, ctl->unit, offset);
1736 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001737 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001738
Li Zefanf38b6e72011-03-14 13:40:51 +08001739 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001740
1741 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001742 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001743}
1744
Miao Xiea4820392013-09-09 13:19:42 +08001745/*
1746 * If we can not find suitable extent, we will use bytes to record
1747 * the size of the max extent.
1748 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001749static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001750 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001751 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001752{
1753 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001754 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001755 unsigned long bits, i;
1756 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001757 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001758
Josef Bacikcef40482015-10-02 16:09:42 -04001759 /*
1760 * Skip searching the bitmap if we don't have a contiguous section that
1761 * is large enough for this allocation.
1762 */
Josef Bacik0584f712015-10-02 16:12:23 -04001763 if (for_alloc &&
1764 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001765 bitmap_info->max_extent_size < *bytes) {
1766 *bytes = bitmap_info->max_extent_size;
1767 return -1;
1768 }
1769
Li Zefan34d52cb2011-03-29 13:46:06 +08001770 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001771 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001772 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001773
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001774 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001775 if (for_alloc && bits == 1) {
1776 found_bits = 1;
1777 break;
1778 }
Josef Bacik96303082009-07-13 21:29:25 -04001779 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1780 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001781 extent_bits = next_zero - i;
1782 if (extent_bits >= bits) {
1783 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001784 break;
Miao Xiea4820392013-09-09 13:19:42 +08001785 } else if (extent_bits > max_bits) {
1786 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001787 }
1788 i = next_zero;
1789 }
1790
1791 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001792 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1793 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001794 return 0;
1795 }
1796
Miao Xiea4820392013-09-09 13:19:42 +08001797 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001798 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001799 return -1;
1800}
1801
Josef Bacik4a351e72018-10-12 15:32:33 -04001802static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1803{
1804 if (entry->bitmap)
1805 return entry->max_extent_size;
1806 return entry->bytes;
1807}
1808
Miao Xiea4820392013-09-09 13:19:42 +08001809/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001810static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001811find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001812 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001813{
1814 struct btrfs_free_space *entry;
1815 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001816 u64 tmp;
1817 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001818 int ret;
1819
Li Zefan34d52cb2011-03-29 13:46:06 +08001820 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001821 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001822
Li Zefan34d52cb2011-03-29 13:46:06 +08001823 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001824 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001825 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001826
1827 for (node = &entry->offset_index; node; node = rb_next(node)) {
1828 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001829 if (entry->bytes < *bytes) {
Josef Bacik4a351e72018-10-12 15:32:33 -04001830 *max_extent_size = max(get_max_extent_size(entry),
1831 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001832 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001833 }
Josef Bacik96303082009-07-13 21:29:25 -04001834
David Woodhouse53b381b2013-01-29 18:40:14 -05001835 /* make sure the space returned is big enough
1836 * to match our requested alignment
1837 */
1838 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001839 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001840 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001841 tmp = tmp * align + ctl->start;
1842 align_off = tmp - entry->offset;
1843 } else {
1844 align_off = 0;
1845 tmp = entry->offset;
1846 }
1847
Miao Xiea4820392013-09-09 13:19:42 +08001848 if (entry->bytes < *bytes + align_off) {
Josef Bacik4a351e72018-10-12 15:32:33 -04001849 *max_extent_size = max(get_max_extent_size(entry),
1850 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001851 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001852 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001853
Josef Bacik96303082009-07-13 21:29:25 -04001854 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001855 u64 size = *bytes;
1856
Josef Bacik0584f712015-10-02 16:12:23 -04001857 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001858 if (!ret) {
1859 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001860 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001861 return entry;
Josef Bacik4a351e72018-10-12 15:32:33 -04001862 } else {
1863 *max_extent_size =
1864 max(get_max_extent_size(entry),
1865 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001866 }
Josef Bacik96303082009-07-13 21:29:25 -04001867 continue;
1868 }
1869
David Woodhouse53b381b2013-01-29 18:40:14 -05001870 *offset = tmp;
1871 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001872 return entry;
1873 }
Miao Xiea4820392013-09-09 13:19:42 +08001874out:
Josef Bacik96303082009-07-13 21:29:25 -04001875 return NULL;
1876}
1877
Li Zefan34d52cb2011-03-29 13:46:06 +08001878static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001879 struct btrfs_free_space *info, u64 offset)
1880{
Li Zefan34d52cb2011-03-29 13:46:06 +08001881 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001882 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001883 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001884 link_free_space(ctl, info);
1885 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001886
Li Zefan34d52cb2011-03-29 13:46:06 +08001887 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001888}
1889
Li Zefan34d52cb2011-03-29 13:46:06 +08001890static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001891 struct btrfs_free_space *bitmap_info)
1892{
Li Zefan34d52cb2011-03-29 13:46:06 +08001893 unlink_free_space(ctl, bitmap_info);
Christophe Leroy4874c6f2019-08-21 15:05:55 +00001894 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001895 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001896 ctl->total_bitmaps--;
1897 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001898}
1899
Li Zefan34d52cb2011-03-29 13:46:06 +08001900static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001901 struct btrfs_free_space *bitmap_info,
1902 u64 *offset, u64 *bytes)
1903{
1904 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001905 u64 search_start, search_bytes;
1906 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001907
1908again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001909 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001910
Josef Bacik6606bb92009-07-31 11:03:58 -04001911 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001912 * We need to search for bits in this bitmap. We could only cover some
1913 * of the extent in this bitmap thanks to how we add space, so we need
1914 * to search for as much as it as we can and clear that amount, and then
1915 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001916 */
1917 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001918 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001919 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001920 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1921 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001922 if (ret < 0 || search_start != *offset)
1923 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001924
Josef Bacikbdb7d302012-06-27 15:10:56 -04001925 /* We may have found more bits than what we need */
1926 search_bytes = min(search_bytes, *bytes);
1927
1928 /* Cannot clear past the end of the bitmap */
1929 search_bytes = min(search_bytes, end - search_start + 1);
1930
1931 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1932 *offset += search_bytes;
1933 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001934
1935 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001936 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001937 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001938 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001939
Josef Bacik6606bb92009-07-31 11:03:58 -04001940 /*
1941 * no entry after this bitmap, but we still have bytes to
1942 * remove, so something has gone wrong.
1943 */
1944 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001945 return -EINVAL;
1946
Josef Bacik6606bb92009-07-31 11:03:58 -04001947 bitmap_info = rb_entry(next, struct btrfs_free_space,
1948 offset_index);
1949
1950 /*
1951 * if the next entry isn't a bitmap we need to return to let the
1952 * extent stuff do its work.
1953 */
Josef Bacik96303082009-07-13 21:29:25 -04001954 if (!bitmap_info->bitmap)
1955 return -EAGAIN;
1956
Josef Bacik6606bb92009-07-31 11:03:58 -04001957 /*
1958 * Ok the next item is a bitmap, but it may not actually hold
1959 * the information for the rest of this free space stuff, so
1960 * look for it, and if we don't find it return so we can try
1961 * everything over again.
1962 */
1963 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001964 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001965 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001966 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001967 if (ret < 0 || search_start != *offset)
1968 return -EAGAIN;
1969
Josef Bacik96303082009-07-13 21:29:25 -04001970 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001971 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001972 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001973
1974 return 0;
1975}
1976
Josef Bacik2cdc3422011-05-27 14:07:49 -04001977static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1978 struct btrfs_free_space *info, u64 offset,
1979 u64 bytes)
1980{
1981 u64 bytes_to_set = 0;
1982 u64 end;
1983
1984 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1985
1986 bytes_to_set = min(end - offset, bytes);
1987
1988 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1989
Josef Bacikcef40482015-10-02 16:09:42 -04001990 /*
1991 * We set some bytes, we have no idea what the max extent size is
1992 * anymore.
1993 */
1994 info->max_extent_size = 0;
1995
Josef Bacik2cdc3422011-05-27 14:07:49 -04001996 return bytes_to_set;
1997
1998}
1999
Li Zefan34d52cb2011-03-29 13:46:06 +08002000static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2001 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04002002{
Li Zefan34d52cb2011-03-29 13:46:06 +08002003 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002004 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04002005 bool forced = false;
2006
2007#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002008 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002009 forced = true;
2010#endif
Josef Bacik96303082009-07-13 21:29:25 -04002011
2012 /*
2013 * If we are below the extents threshold then we can add this as an
2014 * extent, and don't have to deal with the bitmap
2015 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002016 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002017 /*
2018 * If this block group has some small extents we don't want to
2019 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002020 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002021 * of cache left then go ahead an dadd them, no sense in adding
2022 * the overhead of a bitmap if we don't have to.
2023 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002024 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002025 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2026 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002027 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002028 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002029 }
2030 }
Josef Bacik96303082009-07-13 21:29:25 -04002031
2032 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002033 * The original block groups from mkfs can be really small, like 8
2034 * megabytes, so don't bother with a bitmap for those entries. However
2035 * some block groups can be smaller than what a bitmap would cover but
2036 * are still large enough that they could overflow the 32k memory limit,
2037 * so allow those block groups to still be allowed to have a bitmap
2038 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002039 */
Josef Bacikdde57402013-02-12 14:07:51 -05002040 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08002041 return false;
2042
2043 return true;
2044}
2045
David Sterba20e55062015-11-19 11:42:28 +01002046static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002047 .recalc_thresholds = recalculate_thresholds,
2048 .use_bitmap = use_bitmap,
2049};
2050
Li Zefan34d52cb2011-03-29 13:46:06 +08002051static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2052 struct btrfs_free_space *info)
2053{
2054 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002055 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002056 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002057 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002058 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002059
2060 bytes = info->bytes;
2061 offset = info->offset;
2062
Li Zefan34d52cb2011-03-29 13:46:06 +08002063 if (!ctl->op->use_bitmap(ctl, info))
2064 return 0;
2065
Josef Bacik2cdc3422011-05-27 14:07:49 -04002066 if (ctl->op == &free_space_op)
2067 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002068again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002069 /*
2070 * Since we link bitmaps right into the cluster we need to see if we
2071 * have a cluster here, and if so and it has our bitmap we need to add
2072 * the free space to that bitmap.
2073 */
2074 if (block_group && !list_empty(&block_group->cluster_list)) {
2075 struct btrfs_free_cluster *cluster;
2076 struct rb_node *node;
2077 struct btrfs_free_space *entry;
2078
2079 cluster = list_entry(block_group->cluster_list.next,
2080 struct btrfs_free_cluster,
2081 block_group_list);
2082 spin_lock(&cluster->lock);
2083 node = rb_first(&cluster->root);
2084 if (!node) {
2085 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002086 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002087 }
2088
2089 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2090 if (!entry->bitmap) {
2091 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002092 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002093 }
2094
2095 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2096 bytes_added = add_bytes_to_bitmap(ctl, entry,
2097 offset, bytes);
2098 bytes -= bytes_added;
2099 offset += bytes_added;
2100 }
2101 spin_unlock(&cluster->lock);
2102 if (!bytes) {
2103 ret = 1;
2104 goto out;
2105 }
2106 }
Chris Mason38e87882011-06-10 16:36:57 -04002107
2108no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002109 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002110 1, 0);
2111 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002112 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002113 goto new_bitmap;
2114 }
2115
Josef Bacik2cdc3422011-05-27 14:07:49 -04002116 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2117 bytes -= bytes_added;
2118 offset += bytes_added;
2119 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002120
2121 if (!bytes) {
2122 ret = 1;
2123 goto out;
2124 } else
2125 goto again;
2126
2127new_bitmap:
2128 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002129 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002130 added = 1;
2131 info = NULL;
2132 goto again;
2133 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002134 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002135
2136 /* no pre-allocated info, allocate a new one */
2137 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002138 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2139 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002140 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002141 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002142 ret = -ENOMEM;
2143 goto out;
2144 }
2145 }
2146
2147 /* allocate the bitmap */
Christophe Leroy4874c6f2019-08-21 15:05:55 +00002148 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2149 GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002150 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002151 if (!info->bitmap) {
2152 ret = -ENOMEM;
2153 goto out;
2154 }
2155 goto again;
2156 }
2157
2158out:
2159 if (info) {
2160 if (info->bitmap)
Christophe Leroy4874c6f2019-08-21 15:05:55 +00002161 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2162 info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002163 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002164 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002165
2166 return ret;
2167}
2168
Chris Mason945d8962011-05-22 12:33:42 -04002169static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002170 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002171{
Josef Bacik627fa9d2020-07-27 10:28:05 -04002172 struct btrfs_free_space *left_info = NULL;
Li Zefan120d66e2010-11-09 14:56:50 +08002173 struct btrfs_free_space *right_info;
2174 bool merged = false;
2175 u64 offset = info->offset;
2176 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002177
Josef Bacik0f9dd462008-09-23 13:14:11 -04002178 /*
2179 * first we want to see if there is free space adjacent to the range we
2180 * are adding, if there is remove that struct and add a new one to
2181 * cover the entire range
2182 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002183 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002184 if (right_info && rb_prev(&right_info->offset_index))
2185 left_info = rb_entry(rb_prev(&right_info->offset_index),
2186 struct btrfs_free_space, offset_index);
Josef Bacik627fa9d2020-07-27 10:28:05 -04002187 else if (!right_info)
Li Zefan34d52cb2011-03-29 13:46:06 +08002188 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002189
Josef Bacik96303082009-07-13 21:29:25 -04002190 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002191 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002192 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002193 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002194 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002195 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002196 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002197 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002198 }
2199
Josef Bacik96303082009-07-13 21:29:25 -04002200 if (left_info && !left_info->bitmap &&
2201 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002202 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002203 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002204 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002205 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002206 info->offset = left_info->offset;
2207 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002208 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002209 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002210 }
2211
Li Zefan120d66e2010-11-09 14:56:50 +08002212 return merged;
2213}
2214
Filipe Manana20005522014-08-29 13:35:13 +01002215static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2216 struct btrfs_free_space *info,
2217 bool update_stat)
2218{
2219 struct btrfs_free_space *bitmap;
2220 unsigned long i;
2221 unsigned long j;
2222 const u64 end = info->offset + info->bytes;
2223 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2224 u64 bytes;
2225
2226 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2227 if (!bitmap)
2228 return false;
2229
2230 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2231 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2232 if (j == i)
2233 return false;
2234 bytes = (j - i) * ctl->unit;
2235 info->bytes += bytes;
2236
2237 if (update_stat)
2238 bitmap_clear_bits(ctl, bitmap, end, bytes);
2239 else
2240 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2241
2242 if (!bitmap->bytes)
2243 free_bitmap(ctl, bitmap);
2244
2245 return true;
2246}
2247
2248static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2249 struct btrfs_free_space *info,
2250 bool update_stat)
2251{
2252 struct btrfs_free_space *bitmap;
2253 u64 bitmap_offset;
2254 unsigned long i;
2255 unsigned long j;
2256 unsigned long prev_j;
2257 u64 bytes;
2258
2259 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2260 /* If we're on a boundary, try the previous logical bitmap. */
2261 if (bitmap_offset == info->offset) {
2262 if (info->offset == 0)
2263 return false;
2264 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2265 }
2266
2267 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2268 if (!bitmap)
2269 return false;
2270
2271 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2272 j = 0;
2273 prev_j = (unsigned long)-1;
2274 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2275 if (j > i)
2276 break;
2277 prev_j = j;
2278 }
2279 if (prev_j == i)
2280 return false;
2281
2282 if (prev_j == (unsigned long)-1)
2283 bytes = (i + 1) * ctl->unit;
2284 else
2285 bytes = (i - prev_j) * ctl->unit;
2286
2287 info->offset -= bytes;
2288 info->bytes += bytes;
2289
2290 if (update_stat)
2291 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2292 else
2293 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2294
2295 if (!bitmap->bytes)
2296 free_bitmap(ctl, bitmap);
2297
2298 return true;
2299}
2300
2301/*
2302 * We prefer always to allocate from extent entries, both for clustered and
2303 * non-clustered allocation requests. So when attempting to add a new extent
2304 * entry, try to see if there's adjacent free space in bitmap entries, and if
2305 * there is, migrate that space from the bitmaps to the extent.
2306 * Like this we get better chances of satisfying space allocation requests
2307 * because we attempt to satisfy them based on a single cache entry, and never
2308 * on 2 or more entries - even if the entries represent a contiguous free space
2309 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2310 * ends).
2311 */
2312static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2313 struct btrfs_free_space *info,
2314 bool update_stat)
2315{
2316 /*
2317 * Only work with disconnected entries, as we can change their offset,
2318 * and must be extent entries.
2319 */
2320 ASSERT(!info->bitmap);
2321 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2322
2323 if (ctl->total_bitmaps > 0) {
2324 bool stole_end;
2325 bool stole_front = false;
2326
2327 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2328 if (ctl->total_bitmaps > 0)
2329 stole_front = steal_from_bitmap_to_front(ctl, info,
2330 update_stat);
2331
2332 if (stole_end || stole_front)
2333 try_merge_free_space(ctl, info, update_stat);
2334 }
2335}
2336
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002337int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2338 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002339 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002340{
2341 struct btrfs_free_space *info;
2342 int ret = 0;
2343
Josef Bacikdc89e982011-01-28 17:05:48 -05002344 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002345 if (!info)
2346 return -ENOMEM;
2347
2348 info->offset = offset;
2349 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002350 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002351
Li Zefan34d52cb2011-03-29 13:46:06 +08002352 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002353
Li Zefan34d52cb2011-03-29 13:46:06 +08002354 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002355 goto link;
2356
2357 /*
2358 * There was no extent directly to the left or right of this new
2359 * extent then we know we're going to have to allocate a new extent, so
2360 * before we do that see if we need to drop this into a bitmap
2361 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002362 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002363 if (ret < 0) {
2364 goto out;
2365 } else if (ret) {
2366 ret = 0;
2367 goto out;
2368 }
2369link:
Filipe Manana20005522014-08-29 13:35:13 +01002370 /*
2371 * Only steal free space from adjacent bitmaps if we're sure we're not
2372 * going to add the new free space to existing bitmap entries - because
2373 * that would mean unnecessary work that would be reverted. Therefore
2374 * attempt to steal space from bitmaps if we're adding an extent entry.
2375 */
2376 steal_from_bitmap(ctl, info, true);
2377
Li Zefan34d52cb2011-03-29 13:46:06 +08002378 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002379 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002380 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002381out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002382 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002383
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002385 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002386 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002387 }
2388
Josef Bacik0f9dd462008-09-23 13:14:11 -04002389 return ret;
2390}
2391
Josef Bacik6226cb02009-04-03 10:14:18 -04002392int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2393 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002394{
Li Zefan34d52cb2011-03-29 13:46:06 +08002395 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002396 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002397 int ret;
2398 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002399
Li Zefan34d52cb2011-03-29 13:46:06 +08002400 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002401
Josef Bacik96303082009-07-13 21:29:25 -04002402again:
Josef Bacikb0175112012-12-18 11:39:19 -05002403 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002404 if (!bytes)
2405 goto out_lock;
2406
Li Zefan34d52cb2011-03-29 13:46:06 +08002407 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002408 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002409 /*
2410 * oops didn't find an extent that matched the space we wanted
2411 * to remove, look for a bitmap instead
2412 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002413 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002414 1, 0);
2415 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002416 /*
2417 * If we found a partial bit of our free space in a
2418 * bitmap but then couldn't find the other part this may
2419 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002420 */
Josef Bacikb0175112012-12-18 11:39:19 -05002421 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002422 goto out_lock;
2423 }
Josef Bacik96303082009-07-13 21:29:25 -04002424 }
2425
Josef Bacikb0175112012-12-18 11:39:19 -05002426 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002427 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002428 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002429 if (offset == info->offset) {
2430 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002431
Josef Bacikbdb7d302012-06-27 15:10:56 -04002432 info->bytes -= to_free;
2433 info->offset += to_free;
2434 if (info->bytes) {
2435 ret = link_free_space(ctl, info);
2436 WARN_ON(ret);
2437 } else {
2438 kmem_cache_free(btrfs_free_space_cachep, info);
2439 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002440
Josef Bacikbdb7d302012-06-27 15:10:56 -04002441 offset += to_free;
2442 bytes -= to_free;
2443 goto again;
2444 } else {
2445 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002446
Josef Bacikbdb7d302012-06-27 15:10:56 -04002447 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002448 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002449 WARN_ON(ret);
2450 if (ret)
2451 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002452
Josef Bacikbdb7d302012-06-27 15:10:56 -04002453 /* Not enough bytes in this entry to satisfy us */
2454 if (old_end < offset + bytes) {
2455 bytes -= old_end - offset;
2456 offset = old_end;
2457 goto again;
2458 } else if (old_end == offset + bytes) {
2459 /* all done */
2460 goto out_lock;
2461 }
2462 spin_unlock(&ctl->tree_lock);
2463
2464 ret = btrfs_add_free_space(block_group, offset + bytes,
2465 old_end - (offset + bytes));
2466 WARN_ON(ret);
2467 goto out;
2468 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002469 }
Josef Bacik96303082009-07-13 21:29:25 -04002470
Li Zefan34d52cb2011-03-29 13:46:06 +08002471 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002472 if (ret == -EAGAIN) {
2473 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002474 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002475 }
Josef Bacik96303082009-07-13 21:29:25 -04002476out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002477 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002478out:
Josef Bacik25179202008-10-29 14:49:05 -04002479 return ret;
2480}
2481
Josef Bacik0f9dd462008-09-23 13:14:11 -04002482void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2483 u64 bytes)
2484{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002485 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002486 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002487 struct btrfs_free_space *info;
2488 struct rb_node *n;
2489 int count = 0;
2490
Filipe Manana0c286e92018-10-22 10:43:06 +01002491 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002492 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002493 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002494 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002495 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002497 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002498 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002499 }
Filipe Manana0c286e92018-10-22 10:43:06 +01002500 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002501 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002502 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002504 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002505}
2506
Li Zefan34d52cb2011-03-29 13:46:06 +08002507void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002510 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002511
Li Zefan34d52cb2011-03-29 13:46:06 +08002512 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002513 ctl->unit = fs_info->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002514 ctl->start = block_group->key.objectid;
2515 ctl->private = block_group;
2516 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002517 INIT_LIST_HEAD(&ctl->trimming_ranges);
2518 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002519
Li Zefan34d52cb2011-03-29 13:46:06 +08002520 /*
2521 * we only want to have 32k of ram per block group for keeping
2522 * track of free space, and if we pass 1/2 of that we want to
2523 * start converting things over to using bitmaps
2524 */
Byongho Leeee221842015-12-15 01:42:10 +09002525 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002526}
2527
Chris Masonfa9c0d792009-04-03 09:47:43 -04002528/*
2529 * for a given cluster, put all of its extents back into the free
2530 * space cache. If the block group passed doesn't match the block group
2531 * pointed to by the cluster, someone else raced in and freed the
2532 * cluster already. In that case, we just return without changing anything
2533 */
2534static int
2535__btrfs_return_cluster_to_free_space(
2536 struct btrfs_block_group_cache *block_group,
2537 struct btrfs_free_cluster *cluster)
2538{
Li Zefan34d52cb2011-03-29 13:46:06 +08002539 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002540 struct btrfs_free_space *entry;
2541 struct rb_node *node;
2542
2543 spin_lock(&cluster->lock);
2544 if (cluster->block_group != block_group)
2545 goto out;
2546
Josef Bacik96303082009-07-13 21:29:25 -04002547 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002548 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002549 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002550
Chris Masonfa9c0d792009-04-03 09:47:43 -04002551 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002552 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002553 bool bitmap;
2554
Chris Masonfa9c0d792009-04-03 09:47:43 -04002555 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2556 node = rb_next(&entry->offset_index);
2557 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002558 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002559
2560 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002561 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002562 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002563 steal_from_bitmap(ctl, entry, false);
2564 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002565 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002566 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002567 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002568 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002569
Chris Masonfa9c0d792009-04-03 09:47:43 -04002570out:
2571 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002572 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002573 return 0;
2574}
2575
Eric Sandeen48a3b632013-04-25 20:41:01 +00002576static void __btrfs_remove_free_space_cache_locked(
2577 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002578{
2579 struct btrfs_free_space *info;
2580 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002581
Li Zefan581bb052011-04-20 10:06:11 +08002582 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2583 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002584 if (!info->bitmap) {
2585 unlink_free_space(ctl, info);
2586 kmem_cache_free(btrfs_free_space_cachep, info);
2587 } else {
2588 free_bitmap(ctl, info);
2589 }
David Sterba351810c2015-01-08 15:20:54 +01002590
2591 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002592 }
Chris Mason09655372011-05-21 09:27:38 -04002593}
2594
2595void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2596{
2597 spin_lock(&ctl->tree_lock);
2598 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002599 spin_unlock(&ctl->tree_lock);
2600}
2601
Josef Bacik0f9dd462008-09-23 13:14:11 -04002602void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2603{
Li Zefan34d52cb2011-03-29 13:46:06 +08002604 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002605 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002606 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002607
Li Zefan34d52cb2011-03-29 13:46:06 +08002608 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002609 while ((head = block_group->cluster_list.next) !=
2610 &block_group->cluster_list) {
2611 cluster = list_entry(head, struct btrfs_free_cluster,
2612 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002613
2614 WARN_ON(cluster->block_group != block_group);
2615 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002616
2617 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002618 }
Chris Mason09655372011-05-21 09:27:38 -04002619 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002620 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002621
Josef Bacik0f9dd462008-09-23 13:14:11 -04002622}
2623
Josef Bacik6226cb02009-04-03 10:14:18 -04002624u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002625 u64 offset, u64 bytes, u64 empty_size,
2626 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002627{
Li Zefan34d52cb2011-03-29 13:46:06 +08002628 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002629 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002630 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002631 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002632 u64 align_gap = 0;
2633 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002634
Li Zefan34d52cb2011-03-29 13:46:06 +08002635 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002636 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002637 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002638 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002639 goto out;
2640
2641 ret = offset;
2642 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002643 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002644 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002645 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002646 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002647 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002648 align_gap_len = offset - entry->offset;
2649 align_gap = entry->offset;
2650
2651 entry->offset = offset + bytes;
2652 WARN_ON(entry->bytes < bytes + align_gap_len);
2653
2654 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002655 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002656 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002657 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002658 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002659 }
Josef Bacik96303082009-07-13 21:29:25 -04002660out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002661 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002662
David Woodhouse53b381b2013-01-29 18:40:14 -05002663 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002664 __btrfs_add_free_space(block_group->fs_info, ctl,
2665 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002666 return ret;
2667}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002668
2669/*
2670 * given a cluster, put all of its extents back into the free space
2671 * cache. If a block group is passed, this function will only free
2672 * a cluster that belongs to the passed block group.
2673 *
2674 * Otherwise, it'll get a reference on the block group pointed to by the
2675 * cluster and remove the cluster from it.
2676 */
2677int btrfs_return_cluster_to_free_space(
2678 struct btrfs_block_group_cache *block_group,
2679 struct btrfs_free_cluster *cluster)
2680{
Li Zefan34d52cb2011-03-29 13:46:06 +08002681 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002682 int ret;
2683
2684 /* first, get a safe pointer to the block group */
2685 spin_lock(&cluster->lock);
2686 if (!block_group) {
2687 block_group = cluster->block_group;
2688 if (!block_group) {
2689 spin_unlock(&cluster->lock);
2690 return 0;
2691 }
2692 } else if (cluster->block_group != block_group) {
2693 /* someone else has already freed it don't redo their work */
2694 spin_unlock(&cluster->lock);
2695 return 0;
2696 }
2697 atomic_inc(&block_group->count);
2698 spin_unlock(&cluster->lock);
2699
Li Zefan34d52cb2011-03-29 13:46:06 +08002700 ctl = block_group->free_space_ctl;
2701
Chris Masonfa9c0d792009-04-03 09:47:43 -04002702 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002703 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002704 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002705 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002706
2707 /* finally drop our ref */
2708 btrfs_put_block_group(block_group);
2709 return ret;
2710}
2711
Josef Bacik96303082009-07-13 21:29:25 -04002712static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2713 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002714 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002715 u64 bytes, u64 min_start,
2716 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002717{
Li Zefan34d52cb2011-03-29 13:46:06 +08002718 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002719 int err;
2720 u64 search_start = cluster->window_start;
2721 u64 search_bytes = bytes;
2722 u64 ret = 0;
2723
Josef Bacik96303082009-07-13 21:29:25 -04002724 search_start = min_start;
2725 search_bytes = bytes;
2726
Josef Bacik0584f712015-10-02 16:12:23 -04002727 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002728 if (err) {
Josef Bacik4a351e72018-10-12 15:32:33 -04002729 *max_extent_size = max(get_max_extent_size(entry),
2730 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002731 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002732 }
Josef Bacik96303082009-07-13 21:29:25 -04002733
2734 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002735 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002736
2737 return ret;
2738}
2739
Chris Masonfa9c0d792009-04-03 09:47:43 -04002740/*
2741 * given a cluster, try to allocate 'bytes' from it, returns 0
2742 * if it couldn't find anything suitably large, or a logical disk offset
2743 * if things worked out
2744 */
2745u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2746 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002747 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002748{
Li Zefan34d52cb2011-03-29 13:46:06 +08002749 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002750 struct btrfs_free_space *entry = NULL;
2751 struct rb_node *node;
2752 u64 ret = 0;
2753
2754 spin_lock(&cluster->lock);
2755 if (bytes > cluster->max_size)
2756 goto out;
2757
2758 if (cluster->block_group != block_group)
2759 goto out;
2760
2761 node = rb_first(&cluster->root);
2762 if (!node)
2763 goto out;
2764
2765 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302766 while (1) {
Josef Bacik4a351e72018-10-12 15:32:33 -04002767 if (entry->bytes < bytes)
2768 *max_extent_size = max(get_max_extent_size(entry),
2769 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002770
Josef Bacik4e69b592011-03-21 10:11:24 -04002771 if (entry->bytes < bytes ||
2772 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002773 node = rb_next(&entry->offset_index);
2774 if (!node)
2775 break;
2776 entry = rb_entry(node, struct btrfs_free_space,
2777 offset_index);
2778 continue;
2779 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002780
Josef Bacik4e69b592011-03-21 10:11:24 -04002781 if (entry->bitmap) {
2782 ret = btrfs_alloc_from_bitmap(block_group,
2783 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002784 cluster->window_start,
2785 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002786 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002787 node = rb_next(&entry->offset_index);
2788 if (!node)
2789 break;
2790 entry = rb_entry(node, struct btrfs_free_space,
2791 offset_index);
2792 continue;
2793 }
Josef Bacik9b230622012-01-26 15:01:12 -05002794 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002795 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002796 ret = entry->offset;
2797
2798 entry->offset += bytes;
2799 entry->bytes -= bytes;
2800 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002801
Li Zefan5e71b5d2010-11-09 14:55:34 +08002802 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002803 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002804 break;
2805 }
2806out:
2807 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002808
Li Zefan5e71b5d2010-11-09 14:55:34 +08002809 if (!ret)
2810 return 0;
2811
Li Zefan34d52cb2011-03-29 13:46:06 +08002812 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002813
Li Zefan34d52cb2011-03-29 13:46:06 +08002814 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002815 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002816 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002817 if (entry->bitmap) {
Christophe Leroy4874c6f2019-08-21 15:05:55 +00002818 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2819 entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002820 ctl->total_bitmaps--;
2821 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002822 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002823 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002824 }
2825
Li Zefan34d52cb2011-03-29 13:46:06 +08002826 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002827
Chris Masonfa9c0d792009-04-03 09:47:43 -04002828 return ret;
2829}
2830
Josef Bacik96303082009-07-13 21:29:25 -04002831static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2832 struct btrfs_free_space *entry,
2833 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002834 u64 offset, u64 bytes,
2835 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002836{
Li Zefan34d52cb2011-03-29 13:46:06 +08002837 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002838 unsigned long next_zero;
2839 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002840 unsigned long want_bits;
2841 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002842 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002843 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002844 unsigned long start = 0;
2845 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002846 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002847
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002848 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002849 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002850 want_bits = bytes_to_bits(bytes, ctl->unit);
2851 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002852
Josef Bacikcef40482015-10-02 16:09:42 -04002853 /*
2854 * Don't bother looking for a cluster in this bitmap if it's heavily
2855 * fragmented.
2856 */
2857 if (entry->max_extent_size &&
2858 entry->max_extent_size < cont1_bytes)
2859 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002860again:
2861 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002862 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002863 next_zero = find_next_zero_bit(entry->bitmap,
2864 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002865 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002866 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002867 if (found_bits > max_bits)
2868 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002869 break;
2870 }
Josef Bacikcef40482015-10-02 16:09:42 -04002871 if (next_zero - i > max_bits)
2872 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002873 i = next_zero;
2874 }
2875
Josef Bacikcef40482015-10-02 16:09:42 -04002876 if (!found_bits) {
2877 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002878 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002879 }
Josef Bacik96303082009-07-13 21:29:25 -04002880
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002881 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002882 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002883 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002884 }
2885
2886 total_found += found_bits;
2887
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002888 if (cluster->max_size < found_bits * ctl->unit)
2889 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002890
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002891 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2892 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002893 goto again;
2894 }
2895
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002896 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002897 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002898 ret = tree_insert_offset(&cluster->root, entry->offset,
2899 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002900 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002901
Josef Bacik3f7de032011-11-10 08:29:20 -05002902 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002903 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002904 return 0;
2905}
2906
Chris Masonfa9c0d792009-04-03 09:47:43 -04002907/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002908 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002909 * Try to find a cluster with at least bytes total bytes, at least one
2910 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002911 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002912static noinline int
2913setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2914 struct btrfs_free_cluster *cluster,
2915 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002916 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002917{
Li Zefan34d52cb2011-03-29 13:46:06 +08002918 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002919 struct btrfs_free_space *first = NULL;
2920 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002921 struct btrfs_free_space *last;
2922 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002923 u64 window_free;
2924 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002925 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002926
Li Zefan34d52cb2011-03-29 13:46:06 +08002927 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002928 if (!entry)
2929 return -ENOSPC;
2930
2931 /*
2932 * We don't want bitmaps, so just move along until we find a normal
2933 * extent entry.
2934 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002935 while (entry->bitmap || entry->bytes < min_bytes) {
2936 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002937 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002938 node = rb_next(&entry->offset_index);
2939 if (!node)
2940 return -ENOSPC;
2941 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2942 }
2943
Josef Bacik4e69b592011-03-21 10:11:24 -04002944 window_free = entry->bytes;
2945 max_extent = entry->bytes;
2946 first = entry;
2947 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002948
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002949 for (node = rb_next(&entry->offset_index); node;
2950 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002951 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2952
Josef Bacik86d4a772011-05-25 13:03:16 -04002953 if (entry->bitmap) {
2954 if (list_empty(&entry->list))
2955 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002956 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002957 }
2958
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002959 if (entry->bytes < min_bytes)
2960 continue;
2961
2962 last = entry;
2963 window_free += entry->bytes;
2964 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002965 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002966 }
2967
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002968 if (window_free < bytes || max_extent < cont1_bytes)
2969 return -ENOSPC;
2970
Josef Bacik4e69b592011-03-21 10:11:24 -04002971 cluster->window_start = first->offset;
2972
2973 node = &first->offset_index;
2974
2975 /*
2976 * now we've found our entries, pull them out of the free space
2977 * cache and put them into the cluster rbtree
2978 */
2979 do {
2980 int ret;
2981
2982 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2983 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002984 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002985 continue;
2986
Li Zefan34d52cb2011-03-29 13:46:06 +08002987 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002988 ret = tree_insert_offset(&cluster->root, entry->offset,
2989 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002990 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002991 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002992 } while (node && entry != last);
2993
2994 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002995 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002996 return 0;
2997}
2998
2999/*
3000 * This specifically looks for bitmaps that may work in the cluster, we assume
3001 * that we have already failed to find extents that will work.
3002 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04003003static noinline int
3004setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3005 struct btrfs_free_cluster *cluster,
3006 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003007 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04003008{
Li Zefan34d52cb2011-03-29 13:46:06 +08003009 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08003010 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003011 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003012 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003013
Li Zefan34d52cb2011-03-29 13:46:06 +08003014 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003015 return -ENOSPC;
3016
Josef Bacik86d4a772011-05-25 13:03:16 -04003017 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003018 * The bitmap that covers offset won't be in the list unless offset
3019 * is just its start offset.
3020 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003021 if (!list_empty(bitmaps))
3022 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3023
3024 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003025 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3026 if (entry && list_empty(&entry->list))
3027 list_add(&entry->list, bitmaps);
3028 }
3029
Josef Bacik86d4a772011-05-25 13:03:16 -04003030 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003031 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003032 continue;
3033 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003034 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003035 if (!ret)
3036 return 0;
3037 }
3038
3039 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003040 * The bitmaps list has all the bitmaps that record free space
3041 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003042 */
Li Zefan52621cb2011-11-20 07:33:38 -05003043 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003044}
3045
3046/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003047 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003048 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003049 * We might not find them all in one contiguous area.
3050 *
3051 * returns zero and sets up cluster if things worked out, otherwise
3052 * it returns -enospc
3053 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003054int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003055 struct btrfs_block_group_cache *block_group,
3056 struct btrfs_free_cluster *cluster,
3057 u64 offset, u64 bytes, u64 empty_size)
3058{
Li Zefan34d52cb2011-03-29 13:46:06 +08003059 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003060 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003061 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003062 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003063 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003064 int ret;
3065
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003066 /*
3067 * Choose the minimum extent size we'll require for this
3068 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3069 * For metadata, allow allocates with smaller extents. For
3070 * data, keep it dense.
3071 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003073 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003074 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003075 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003077 } else {
3078 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003080 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003081
Li Zefan34d52cb2011-03-29 13:46:06 +08003082 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003083
3084 /*
3085 * If we know we don't have enough space to make a cluster don't even
3086 * bother doing all the work to try and find one.
3087 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003088 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003089 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003090 return -ENOSPC;
3091 }
3092
Chris Masonfa9c0d792009-04-03 09:47:43 -04003093 spin_lock(&cluster->lock);
3094
3095 /* someone already found a cluster, hooray */
3096 if (cluster->block_group) {
3097 ret = 0;
3098 goto out;
3099 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003100
Josef Bacik3f7de032011-11-10 08:29:20 -05003101 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3102 min_bytes);
3103
Josef Bacik86d4a772011-05-25 13:03:16 -04003104 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003105 bytes + empty_size,
3106 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003107 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003108 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003109 offset, bytes + empty_size,
3110 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003111
3112 /* Clear our temporary list */
3113 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3114 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003115
3116 if (!ret) {
3117 atomic_inc(&block_group->count);
3118 list_add_tail(&cluster->block_group_list,
3119 &block_group->cluster_list);
3120 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003121 } else {
3122 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003123 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003124out:
3125 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003126 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003127
3128 return ret;
3129}
3130
3131/*
3132 * simple code to zero out a cluster
3133 */
3134void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3135{
3136 spin_lock_init(&cluster->lock);
3137 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003138 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003139 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003140 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003141 INIT_LIST_HEAD(&cluster->block_group_list);
3142 cluster->block_group = NULL;
3143}
3144
Li Zefan7fe1e642011-12-29 14:47:27 +08003145static int do_trimming(struct btrfs_block_group_cache *block_group,
3146 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003147 u64 reserved_start, u64 reserved_bytes,
3148 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003149{
3150 struct btrfs_space_info *space_info = block_group->space_info;
3151 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003152 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003153 int ret;
3154 int update = 0;
3155 u64 trimmed = 0;
3156
3157 spin_lock(&space_info->lock);
3158 spin_lock(&block_group->lock);
3159 if (!block_group->ro) {
3160 block_group->reserved += reserved_bytes;
3161 space_info->bytes_reserved += reserved_bytes;
3162 update = 1;
3163 }
3164 spin_unlock(&block_group->lock);
3165 spin_unlock(&space_info->lock);
3166
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003167 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003168 if (!ret)
3169 *total_trimmed += trimmed;
3170
Filipe Manana55507ce2014-12-01 17:04:09 +00003171 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003172 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003173 list_del(&trim_entry->list);
3174 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003175
3176 if (update) {
3177 spin_lock(&space_info->lock);
3178 spin_lock(&block_group->lock);
3179 if (block_group->ro)
3180 space_info->bytes_readonly += reserved_bytes;
3181 block_group->reserved -= reserved_bytes;
3182 space_info->bytes_reserved -= reserved_bytes;
3183 spin_unlock(&space_info->lock);
3184 spin_unlock(&block_group->lock);
3185 }
3186
3187 return ret;
3188}
3189
3190static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3191 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003192{
Li Zefan34d52cb2011-03-29 13:46:06 +08003193 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003194 struct btrfs_free_space *entry;
3195 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003196 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003197 u64 extent_start;
3198 u64 extent_bytes;
3199 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003200
3201 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003202 struct btrfs_trim_range trim_entry;
3203
3204 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003205 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003206
Li Zefan34d52cb2011-03-29 13:46:06 +08003207 if (ctl->free_space < minlen) {
3208 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003209 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003210 break;
3211 }
3212
Li Zefan34d52cb2011-03-29 13:46:06 +08003213 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003214 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003215 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003216 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003217 break;
3218 }
3219
Li Zefan7fe1e642011-12-29 14:47:27 +08003220 /* skip bitmaps */
3221 while (entry->bitmap) {
3222 node = rb_next(&entry->offset_index);
3223 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003224 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003225 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003226 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003227 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003228 entry = rb_entry(node, struct btrfs_free_space,
3229 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003230 }
3231
Li Zefan7fe1e642011-12-29 14:47:27 +08003232 if (entry->offset >= end) {
3233 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003234 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003235 break;
3236 }
3237
3238 extent_start = entry->offset;
3239 extent_bytes = entry->bytes;
3240 start = max(start, extent_start);
3241 bytes = min(extent_start + extent_bytes, end) - start;
3242 if (bytes < minlen) {
3243 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003244 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003245 goto next;
3246 }
3247
3248 unlink_free_space(ctl, entry);
3249 kmem_cache_free(btrfs_free_space_cachep, entry);
3250
Li Zefan34d52cb2011-03-29 13:46:06 +08003251 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003252 trim_entry.start = extent_start;
3253 trim_entry.bytes = extent_bytes;
3254 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3255 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003256
Li Zefan7fe1e642011-12-29 14:47:27 +08003257 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003258 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003259 if (ret)
3260 break;
3261next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003262 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003263
3264 if (fatal_signal_pending(current)) {
3265 ret = -ERESTARTSYS;
3266 break;
3267 }
3268
3269 cond_resched();
3270 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003271out:
3272 return ret;
3273}
3274
3275static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3276 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3277{
3278 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3279 struct btrfs_free_space *entry;
3280 int ret = 0;
3281 int ret2;
3282 u64 bytes;
3283 u64 offset = offset_to_bitmap(ctl, start);
3284
3285 while (offset < end) {
3286 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003287 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003288
Filipe Manana55507ce2014-12-01 17:04:09 +00003289 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003290 spin_lock(&ctl->tree_lock);
3291
3292 if (ctl->free_space < minlen) {
3293 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003294 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003295 break;
3296 }
3297
3298 entry = tree_search_offset(ctl, offset, 1, 0);
3299 if (!entry) {
3300 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003301 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003302 next_bitmap = true;
3303 goto next;
3304 }
3305
3306 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003307 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003308 if (ret2 || start >= end) {
3309 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003310 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003311 next_bitmap = true;
3312 goto next;
3313 }
3314
3315 bytes = min(bytes, end - start);
3316 if (bytes < minlen) {
3317 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003318 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003319 goto next;
3320 }
3321
3322 bitmap_clear_bits(ctl, entry, start, bytes);
3323 if (entry->bytes == 0)
3324 free_bitmap(ctl, entry);
3325
3326 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003327 trim_entry.start = start;
3328 trim_entry.bytes = bytes;
3329 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3330 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003331
3332 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003333 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003334 if (ret)
3335 break;
3336next:
3337 if (next_bitmap) {
3338 offset += BITS_PER_BITMAP * ctl->unit;
3339 } else {
3340 start += bytes;
3341 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3342 offset += BITS_PER_BITMAP * ctl->unit;
3343 }
3344
3345 if (fatal_signal_pending(current)) {
3346 ret = -ERESTARTSYS;
3347 break;
3348 }
3349
3350 cond_resched();
3351 }
3352
3353 return ret;
3354}
3355
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003356void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003357{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003358 atomic_inc(&cache->trimming);
3359}
Li Zefan7fe1e642011-12-29 14:47:27 +08003360
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003361void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3362{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003363 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003364 struct extent_map_tree *em_tree;
3365 struct extent_map *em;
3366 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003367
Filipe Manana04216822014-11-27 21:14:15 +00003368 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003369 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3370 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003371 spin_unlock(&block_group->lock);
3372
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003373 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003374 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003376 write_lock(&em_tree->lock);
3377 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3378 1);
3379 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003380 /*
3381 * remove_extent_mapping() will delete us from the pinned_chunks
3382 * list, which is protected by the chunk mutex.
3383 */
Filipe Manana04216822014-11-27 21:14:15 +00003384 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 Mason85db36cf2015-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 Mason85db36cf2015-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 Leroy4874c6f2019-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);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003656 if (map)
Christophe Leroy4874c6f2019-08-21 15:05:55 +00003657 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 */