David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Red Hat. All rights reserved. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6 | #include <linux/pagemap.h> |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 8 | #include <linux/sched/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10 | #include <linux/math64.h> |
Josef Bacik | 6ab6060 | 2011-08-08 08:24:46 -0400 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 12 | #include <linux/error-injection.h> |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 13 | #include <linux/sched/mm.h> |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 14 | #include "misc.h" |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 15 | #include "ctree.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 16 | #include "free-space-cache.h" |
| 17 | #include "transaction.h" |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 18 | #include "disk-io.h" |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 19 | #include "extent_io.h" |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 20 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 21 | #include "space-info.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 22 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 23 | #include "block-group.h" |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 24 | #include "discard.h" |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 25 | #include "subpage.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 26 | |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 27 | #define BITS_PER_BITMAP (PAGE_SIZE * 8UL) |
Dennis Zhou | 5d90c5c | 2020-01-02 16:26:43 -0500 | [diff] [blame] | 28 | #define MAX_CACHE_BYTES_PER_GIG SZ_64K |
| 29 | #define FORCE_EXTENT_THRESHOLD SZ_1M |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 30 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 31 | struct btrfs_trim_range { |
| 32 | u64 start; |
| 33 | u64 bytes; |
| 34 | struct list_head list; |
| 35 | }; |
| 36 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 37 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 38 | struct btrfs_free_space *info); |
Josef Bacik | cd023e7 | 2012-05-14 10:06:40 -0400 | [diff] [blame] | 39 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
| 40 | struct btrfs_free_space *info); |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 41 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
| 42 | struct btrfs_free_space *bitmap_info, u64 *offset, |
| 43 | u64 *bytes, bool for_alloc); |
| 44 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
| 45 | struct btrfs_free_space *bitmap_info); |
| 46 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 47 | struct btrfs_free_space *info, u64 offset, |
| 48 | u64 bytes); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 49 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 50 | static struct inode *__lookup_free_space_inode(struct btrfs_root *root, |
| 51 | struct btrfs_path *path, |
| 52 | u64 offset) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 53 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 54 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 55 | struct btrfs_key key; |
| 56 | struct btrfs_key location; |
| 57 | struct btrfs_disk_key disk_key; |
| 58 | struct btrfs_free_space_header *header; |
| 59 | struct extent_buffer *leaf; |
| 60 | struct inode *inode = NULL; |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 61 | unsigned nofs_flag; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 62 | int ret; |
| 63 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 64 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 65 | key.offset = offset; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 66 | key.type = 0; |
| 67 | |
| 68 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 69 | if (ret < 0) |
| 70 | return ERR_PTR(ret); |
| 71 | if (ret > 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 72 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 73 | return ERR_PTR(-ENOENT); |
| 74 | } |
| 75 | |
| 76 | leaf = path->nodes[0]; |
| 77 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 78 | struct btrfs_free_space_header); |
| 79 | btrfs_free_space_key(leaf, header, &disk_key); |
| 80 | btrfs_disk_key_to_cpu(&location, &disk_key); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 81 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 82 | |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 83 | /* |
| 84 | * We are often under a trans handle at this point, so we need to make |
| 85 | * sure NOFS is set to keep us from deadlocking. |
| 86 | */ |
| 87 | nofs_flag = memalloc_nofs_save(); |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 88 | inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 89 | btrfs_release_path(path); |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 90 | memalloc_nofs_restore(nofs_flag); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 91 | if (IS_ERR(inode)) |
| 92 | return inode; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 93 | |
Al Viro | 528c032 | 2012-04-13 11:03:55 -0400 | [diff] [blame] | 94 | mapping_set_gfp_mask(inode->i_mapping, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 95 | mapping_gfp_constraint(inode->i_mapping, |
| 96 | ~(__GFP_FS | __GFP_HIGHMEM))); |
Miao Xie | adae52b | 2011-03-31 09:43:23 +0000 | [diff] [blame] | 97 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 98 | return inode; |
| 99 | } |
| 100 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 101 | struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group, |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 102 | struct btrfs_path *path) |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 103 | { |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 104 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 105 | struct inode *inode = NULL; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 106 | u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 107 | |
| 108 | spin_lock(&block_group->lock); |
| 109 | if (block_group->inode) |
| 110 | inode = igrab(block_group->inode); |
| 111 | spin_unlock(&block_group->lock); |
| 112 | if (inode) |
| 113 | return inode; |
| 114 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 115 | inode = __lookup_free_space_inode(fs_info->tree_root, path, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 116 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 117 | if (IS_ERR(inode)) |
| 118 | return inode; |
| 119 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 120 | spin_lock(&block_group->lock); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 121 | if (!((BTRFS_I(inode)->flags & flags) == flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 122 | btrfs_info(fs_info, "Old style space inode found, converting."); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 123 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM | |
| 124 | BTRFS_INODE_NODATACOW; |
Josef Bacik | 2f35612 | 2011-06-10 15:31:13 -0400 | [diff] [blame] | 125 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 126 | } |
| 127 | |
Josef Bacik | 300e4f8 | 2011-08-29 14:06:00 -0400 | [diff] [blame] | 128 | if (!block_group->iref) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 129 | block_group->inode = igrab(inode); |
| 130 | block_group->iref = 1; |
| 131 | } |
| 132 | spin_unlock(&block_group->lock); |
| 133 | |
| 134 | return inode; |
| 135 | } |
| 136 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 137 | static int __create_free_space_inode(struct btrfs_root *root, |
| 138 | struct btrfs_trans_handle *trans, |
| 139 | struct btrfs_path *path, |
| 140 | u64 ino, u64 offset) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 141 | { |
| 142 | struct btrfs_key key; |
| 143 | struct btrfs_disk_key disk_key; |
| 144 | struct btrfs_free_space_header *header; |
| 145 | struct btrfs_inode_item *inode_item; |
| 146 | struct extent_buffer *leaf; |
Nikolay Borisov | f0d1219 | 2020-12-03 10:09:49 +0200 | [diff] [blame] | 147 | /* We inline CRCs for the free disk space cache */ |
| 148 | const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC | |
| 149 | BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 150 | int ret; |
| 151 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 152 | ret = btrfs_insert_empty_inode(trans, root, path, ino); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | leaf = path->nodes[0]; |
| 157 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 158 | struct btrfs_inode_item); |
| 159 | btrfs_item_key(leaf, &disk_key, path->slots[0]); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 160 | memzero_extent_buffer(leaf, (unsigned long)inode_item, |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 161 | sizeof(*inode_item)); |
| 162 | btrfs_set_inode_generation(leaf, inode_item, trans->transid); |
| 163 | btrfs_set_inode_size(leaf, inode_item, 0); |
| 164 | btrfs_set_inode_nbytes(leaf, inode_item, 0); |
| 165 | btrfs_set_inode_uid(leaf, inode_item, 0); |
| 166 | btrfs_set_inode_gid(leaf, inode_item, 0); |
| 167 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 168 | btrfs_set_inode_flags(leaf, inode_item, flags); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 169 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
| 170 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 171 | btrfs_set_inode_block_group(leaf, inode_item, offset); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 172 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 173 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 174 | |
| 175 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 176 | key.offset = offset; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 177 | key.type = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 178 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 179 | sizeof(struct btrfs_free_space_header)); |
| 180 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 181 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 182 | return ret; |
| 183 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 184 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 185 | leaf = path->nodes[0]; |
| 186 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 187 | struct btrfs_free_space_header); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 188 | memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header)); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 189 | btrfs_set_free_space_key(leaf, header, &disk_key); |
| 190 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 191 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
David Sterba | 4ca75f1 | 2019-03-20 13:42:57 +0100 | [diff] [blame] | 196 | int create_free_space_inode(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 197 | struct btrfs_block_group *block_group, |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 198 | struct btrfs_path *path) |
| 199 | { |
| 200 | int ret; |
| 201 | u64 ino; |
| 202 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 203 | ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | |
David Sterba | 4ca75f1 | 2019-03-20 13:42:57 +0100 | [diff] [blame] | 207 | return __create_free_space_inode(trans->fs_info->tree_root, trans, path, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 208 | ino, block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 209 | } |
| 210 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 211 | /* |
| 212 | * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode |
| 213 | * handles lookup, otherwise it takes ownership and iputs the inode. |
| 214 | * Don't reuse an inode pointer after passing it into this function. |
| 215 | */ |
| 216 | int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans, |
| 217 | struct inode *inode, |
| 218 | struct btrfs_block_group *block_group) |
| 219 | { |
| 220 | struct btrfs_path *path; |
| 221 | struct btrfs_key key; |
| 222 | int ret = 0; |
| 223 | |
| 224 | path = btrfs_alloc_path(); |
| 225 | if (!path) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | if (!inode) |
| 229 | inode = lookup_free_space_inode(block_group, path); |
| 230 | if (IS_ERR(inode)) { |
| 231 | if (PTR_ERR(inode) != -ENOENT) |
| 232 | ret = PTR_ERR(inode); |
| 233 | goto out; |
| 234 | } |
| 235 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
| 236 | if (ret) { |
| 237 | btrfs_add_delayed_iput(inode); |
| 238 | goto out; |
| 239 | } |
| 240 | clear_nlink(inode); |
| 241 | /* One for the block groups ref */ |
| 242 | spin_lock(&block_group->lock); |
| 243 | if (block_group->iref) { |
| 244 | block_group->iref = 0; |
| 245 | block_group->inode = NULL; |
| 246 | spin_unlock(&block_group->lock); |
| 247 | iput(inode); |
| 248 | } else { |
| 249 | spin_unlock(&block_group->lock); |
| 250 | } |
| 251 | /* One for the lookup ref */ |
| 252 | btrfs_add_delayed_iput(inode); |
| 253 | |
| 254 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 255 | key.type = 0; |
| 256 | key.offset = block_group->start; |
| 257 | ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path, |
| 258 | -1, 1); |
| 259 | if (ret) { |
| 260 | if (ret > 0) |
| 261 | ret = 0; |
| 262 | goto out; |
| 263 | } |
| 264 | ret = btrfs_del_item(trans, trans->fs_info->tree_root, path); |
| 265 | out: |
| 266 | btrfs_free_path(path); |
| 267 | return ret; |
| 268 | } |
| 269 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 270 | int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info, |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 271 | struct btrfs_block_rsv *rsv) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 272 | { |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 273 | u64 needed_bytes; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 274 | int ret; |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 275 | |
| 276 | /* 1 for slack space, 1 for updating the inode */ |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 277 | needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) + |
| 278 | btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 279 | |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 280 | spin_lock(&rsv->lock); |
| 281 | if (rsv->reserved < needed_bytes) |
| 282 | ret = -ENOSPC; |
| 283 | else |
| 284 | ret = 0; |
| 285 | spin_unlock(&rsv->lock); |
Wei Yongjun | 4b286cd | 2013-05-21 02:39:21 +0000 | [diff] [blame] | 286 | return ret; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 289 | int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 290 | struct btrfs_block_group *block_group, |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 291 | struct inode *inode) |
| 292 | { |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 293 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 294 | int ret = 0; |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 295 | bool locked = false; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 296 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 297 | if (block_group) { |
Jeff Mahoney | 21e75ff | 2017-02-15 16:28:32 -0500 | [diff] [blame] | 298 | struct btrfs_path *path = btrfs_alloc_path(); |
| 299 | |
| 300 | if (!path) { |
| 301 | ret = -ENOMEM; |
| 302 | goto fail; |
| 303 | } |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 304 | locked = true; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 305 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 306 | if (!list_empty(&block_group->io_list)) { |
| 307 | list_del_init(&block_group->io_list); |
| 308 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 309 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 310 | btrfs_put_block_group(block_group); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * now that we've truncated the cache away, its no longer |
| 315 | * setup or written |
| 316 | */ |
| 317 | spin_lock(&block_group->lock); |
| 318 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 319 | spin_unlock(&block_group->lock); |
Jeff Mahoney | 21e75ff | 2017-02-15 16:28:32 -0500 | [diff] [blame] | 320 | btrfs_free_path(path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 321 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 322 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 323 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 324 | truncate_pagecache(inode, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 325 | |
| 326 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 327 | * We skip the throttling logic for free space cache inodes, so we don't |
| 328 | * need to check for -EAGAIN. |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 329 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 330 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 331 | 0, BTRFS_EXTENT_DATA_KEY, NULL); |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 332 | if (ret) |
| 333 | goto fail; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 334 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 335 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 336 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 337 | fail: |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 338 | if (locked) |
| 339 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 340 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 341 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 342 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 343 | return ret; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 344 | } |
| 345 | |
David Sterba | 1d48053 | 2017-01-23 17:28:19 +0100 | [diff] [blame] | 346 | static void readahead_cache(struct inode *inode) |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 347 | { |
Goldwyn Rodrigues | 98caf95 | 2021-07-27 16:17:26 -0500 | [diff] [blame] | 348 | struct file_ra_state ra; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 349 | unsigned long last_index; |
| 350 | |
Goldwyn Rodrigues | 98caf95 | 2021-07-27 16:17:26 -0500 | [diff] [blame] | 351 | file_ra_state_init(&ra, inode->i_mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 352 | last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 353 | |
Goldwyn Rodrigues | 98caf95 | 2021-07-27 16:17:26 -0500 | [diff] [blame] | 354 | page_cache_sync_readahead(inode->i_mapping, &ra, NULL, 0, last_index); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 357 | static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode, |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 358 | int write) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 359 | { |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 360 | int num_pages; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 361 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 362 | num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 363 | |
Zhihui Zhang | 8f6c72a | 2018-07-02 20:00:54 -0400 | [diff] [blame] | 364 | /* Make sure we can fit our crcs and generation into the first page */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 365 | if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 366 | return -ENOSPC; |
| 367 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 368 | memset(io_ctl, 0, sizeof(struct btrfs_io_ctl)); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 369 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 370 | io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 371 | if (!io_ctl->pages) |
| 372 | return -ENOMEM; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 373 | |
| 374 | io_ctl->num_pages = num_pages; |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 375 | io_ctl->fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 376 | io_ctl->inode = inode; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 377 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 378 | return 0; |
| 379 | } |
Masami Hiramatsu | 663faf9 | 2018-01-13 02:55:33 +0900 | [diff] [blame] | 380 | ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 381 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 382 | static void io_ctl_free(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 383 | { |
| 384 | kfree(io_ctl->pages); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 385 | io_ctl->pages = NULL; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 388 | static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 389 | { |
| 390 | if (io_ctl->cur) { |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 391 | io_ctl->cur = NULL; |
| 392 | io_ctl->orig = NULL; |
| 393 | } |
| 394 | } |
| 395 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 396 | static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 397 | { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 398 | ASSERT(io_ctl->index < io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 399 | io_ctl->page = io_ctl->pages[io_ctl->index++]; |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 400 | io_ctl->cur = page_address(io_ctl->page); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 401 | io_ctl->orig = io_ctl->cur; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 402 | io_ctl->size = PAGE_SIZE; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 403 | if (clear) |
David Sterba | 619a974 | 2017-03-29 20:48:44 +0200 | [diff] [blame] | 404 | clear_page(io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 407 | static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 408 | { |
| 409 | int i; |
| 410 | |
| 411 | io_ctl_unmap_page(io_ctl); |
| 412 | |
| 413 | for (i = 0; i < io_ctl->num_pages; i++) { |
Li Zefan | a1ee5a4 | 2012-01-09 14:27:42 +0800 | [diff] [blame] | 414 | if (io_ctl->pages[i]) { |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 415 | btrfs_page_clear_checked(io_ctl->fs_info, |
| 416 | io_ctl->pages[i], |
| 417 | page_offset(io_ctl->pages[i]), |
| 418 | PAGE_SIZE); |
Li Zefan | a1ee5a4 | 2012-01-09 14:27:42 +0800 | [diff] [blame] | 419 | unlock_page(io_ctl->pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 420 | put_page(io_ctl->pages[i]); |
Li Zefan | a1ee5a4 | 2012-01-09 14:27:42 +0800 | [diff] [blame] | 421 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 425 | static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 426 | { |
| 427 | struct page *page; |
Johannes Thumshirn | 831fa14 | 2020-02-12 00:10:19 +0900 | [diff] [blame] | 428 | struct inode *inode = io_ctl->inode; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 429 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
| 430 | int i; |
| 431 | |
| 432 | for (i = 0; i < io_ctl->num_pages; i++) { |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 433 | int ret; |
| 434 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 435 | page = find_or_create_page(inode->i_mapping, i, mask); |
| 436 | if (!page) { |
| 437 | io_ctl_drop_pages(io_ctl); |
| 438 | return -ENOMEM; |
| 439 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 440 | |
| 441 | ret = set_page_extent_mapped(page); |
| 442 | if (ret < 0) { |
| 443 | unlock_page(page); |
| 444 | put_page(page); |
| 445 | io_ctl_drop_pages(io_ctl); |
| 446 | return ret; |
| 447 | } |
| 448 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 449 | io_ctl->pages[i] = page; |
| 450 | if (uptodate && !PageUptodate(page)) { |
| 451 | btrfs_readpage(NULL, page); |
| 452 | lock_page(page); |
Josef Bacik | 3797136 | 2019-09-24 16:50:43 -0400 | [diff] [blame] | 453 | if (page->mapping != inode->i_mapping) { |
| 454 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 455 | "free space cache page truncated"); |
| 456 | io_ctl_drop_pages(io_ctl); |
| 457 | return -EIO; |
| 458 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 459 | if (!PageUptodate(page)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 460 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 461 | "error reading free space cache"); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 462 | io_ctl_drop_pages(io_ctl); |
| 463 | return -EIO; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 468 | for (i = 0; i < io_ctl->num_pages; i++) |
Josef Bacik | f7d61dc | 2011-11-15 09:31:24 -0500 | [diff] [blame] | 469 | clear_page_dirty_for_io(io_ctl->pages[i]); |
Josef Bacik | f7d61dc | 2011-11-15 09:31:24 -0500 | [diff] [blame] | 470 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 474 | static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 475 | { |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 476 | io_ctl_map_page(io_ctl, 1); |
| 477 | |
| 478 | /* |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 479 | * Skip the csum areas. If we don't check crcs then we just have a |
| 480 | * 64bit chunk at the front of the first page. |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 481 | */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 482 | io_ctl->cur += (sizeof(u32) * io_ctl->num_pages); |
| 483 | io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 484 | |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 485 | put_unaligned_le64(generation, io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 486 | io_ctl->cur += sizeof(u64); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 489 | static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 490 | { |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 491 | u64 cache_gen; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 492 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 493 | /* |
| 494 | * Skip the crc area. If we don't check crcs then we just have a 64bit |
| 495 | * chunk at the front of the first page. |
| 496 | */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 497 | io_ctl->cur += sizeof(u32) * io_ctl->num_pages; |
| 498 | io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 499 | |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 500 | cache_gen = get_unaligned_le64(io_ctl->cur); |
| 501 | if (cache_gen != generation) { |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 502 | btrfs_err_rl(io_ctl->fs_info, |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 503 | "space cache generation (%llu) does not match inode (%llu)", |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 504 | cache_gen, generation); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 505 | io_ctl_unmap_page(io_ctl); |
| 506 | return -EIO; |
| 507 | } |
| 508 | io_ctl->cur += sizeof(u64); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 512 | static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 513 | { |
| 514 | u32 *tmp; |
| 515 | u32 crc = ~(u32)0; |
| 516 | unsigned offset = 0; |
| 517 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 518 | if (index == 0) |
Justin P. Mattock | cb54f25 | 2011-11-21 08:43:28 -0800 | [diff] [blame] | 519 | offset = sizeof(u32) * io_ctl->num_pages; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 520 | |
Johannes Thumshirn | 4bb3c2e | 2019-05-22 10:19:00 +0200 | [diff] [blame] | 521 | crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset); |
| 522 | btrfs_crc32c_final(crc, (u8 *)&crc); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 523 | io_ctl_unmap_page(io_ctl); |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 524 | tmp = page_address(io_ctl->pages[0]); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 525 | tmp += index; |
| 526 | *tmp = crc; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 529 | static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 530 | { |
| 531 | u32 *tmp, val; |
| 532 | u32 crc = ~(u32)0; |
| 533 | unsigned offset = 0; |
| 534 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 535 | if (index == 0) |
| 536 | offset = sizeof(u32) * io_ctl->num_pages; |
| 537 | |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 538 | tmp = page_address(io_ctl->pages[0]); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 539 | tmp += index; |
| 540 | val = *tmp; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 541 | |
| 542 | io_ctl_map_page(io_ctl, 0); |
Johannes Thumshirn | 4bb3c2e | 2019-05-22 10:19:00 +0200 | [diff] [blame] | 543 | crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset); |
| 544 | btrfs_crc32c_final(crc, (u8 *)&crc); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 545 | if (val != crc) { |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 546 | btrfs_err_rl(io_ctl->fs_info, |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 547 | "csum mismatch on free space cache"); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 548 | io_ctl_unmap_page(io_ctl); |
| 549 | return -EIO; |
| 550 | } |
| 551 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 555 | static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes, |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 556 | void *bitmap) |
| 557 | { |
| 558 | struct btrfs_free_space_entry *entry; |
| 559 | |
| 560 | if (!io_ctl->cur) |
| 561 | return -ENOSPC; |
| 562 | |
| 563 | entry = io_ctl->cur; |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 564 | put_unaligned_le64(offset, &entry->offset); |
| 565 | put_unaligned_le64(bytes, &entry->bytes); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 566 | entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP : |
| 567 | BTRFS_FREE_SPACE_EXTENT; |
| 568 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); |
| 569 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); |
| 570 | |
| 571 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) |
| 572 | return 0; |
| 573 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 574 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 575 | |
| 576 | /* No more pages to map */ |
| 577 | if (io_ctl->index >= io_ctl->num_pages) |
| 578 | return 0; |
| 579 | |
| 580 | /* map the next page */ |
| 581 | io_ctl_map_page(io_ctl, 1); |
| 582 | return 0; |
| 583 | } |
| 584 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 585 | static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 586 | { |
| 587 | if (!io_ctl->cur) |
| 588 | return -ENOSPC; |
| 589 | |
| 590 | /* |
| 591 | * If we aren't at the start of the current page, unmap this one and |
| 592 | * map the next one if there is any left. |
| 593 | */ |
| 594 | if (io_ctl->cur != io_ctl->orig) { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 595 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 596 | if (io_ctl->index >= io_ctl->num_pages) |
| 597 | return -ENOSPC; |
| 598 | io_ctl_map_page(io_ctl, 0); |
| 599 | } |
| 600 | |
David Sterba | 69d2480 | 2018-06-29 10:56:44 +0200 | [diff] [blame] | 601 | copy_page(io_ctl->cur, bitmap); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 602 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 603 | if (io_ctl->index < io_ctl->num_pages) |
| 604 | io_ctl_map_page(io_ctl, 0); |
| 605 | return 0; |
| 606 | } |
| 607 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 608 | static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 609 | { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 610 | /* |
| 611 | * If we're not on the boundary we know we've modified the page and we |
| 612 | * need to crc the page. |
| 613 | */ |
| 614 | if (io_ctl->cur != io_ctl->orig) |
| 615 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
| 616 | else |
| 617 | io_ctl_unmap_page(io_ctl); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 618 | |
| 619 | while (io_ctl->index < io_ctl->num_pages) { |
| 620 | io_ctl_map_page(io_ctl, 1); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 621 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 625 | static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl, |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 626 | struct btrfs_free_space *entry, u8 *type) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 627 | { |
| 628 | struct btrfs_free_space_entry *e; |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 629 | int ret; |
| 630 | |
| 631 | if (!io_ctl->cur) { |
| 632 | ret = io_ctl_check_crc(io_ctl, io_ctl->index); |
| 633 | if (ret) |
| 634 | return ret; |
| 635 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 636 | |
| 637 | e = io_ctl->cur; |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 638 | entry->offset = get_unaligned_le64(&e->offset); |
| 639 | entry->bytes = get_unaligned_le64(&e->bytes); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 640 | *type = e->type; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 641 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); |
| 642 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); |
| 643 | |
| 644 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 645 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 646 | |
| 647 | io_ctl_unmap_page(io_ctl); |
| 648 | |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 649 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 652 | static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl, |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 653 | struct btrfs_free_space *entry) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 654 | { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 655 | int ret; |
| 656 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 657 | ret = io_ctl_check_crc(io_ctl, io_ctl->index); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | |
David Sterba | 69d2480 | 2018-06-29 10:56:44 +0200 | [diff] [blame] | 661 | copy_page(entry->bitmap, io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 662 | io_ctl_unmap_page(io_ctl); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 663 | |
| 664 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 665 | } |
| 666 | |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 667 | static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) |
| 668 | { |
| 669 | struct btrfs_block_group *block_group = ctl->private; |
| 670 | u64 max_bytes; |
| 671 | u64 bitmap_bytes; |
| 672 | u64 extent_bytes; |
| 673 | u64 size = block_group->length; |
| 674 | u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit; |
| 675 | u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg); |
| 676 | |
| 677 | max_bitmaps = max_t(u64, max_bitmaps, 1); |
| 678 | |
| 679 | ASSERT(ctl->total_bitmaps <= max_bitmaps); |
| 680 | |
| 681 | /* |
| 682 | * We are trying to keep the total amount of memory used per 1GiB of |
| 683 | * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation |
| 684 | * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of |
| 685 | * bitmaps, we may end up using more memory than this. |
| 686 | */ |
| 687 | if (size < SZ_1G) |
| 688 | max_bytes = MAX_CACHE_BYTES_PER_GIG; |
| 689 | else |
| 690 | max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G); |
| 691 | |
| 692 | bitmap_bytes = ctl->total_bitmaps * ctl->unit; |
| 693 | |
| 694 | /* |
| 695 | * we want the extent entry threshold to always be at most 1/2 the max |
| 696 | * bytes we can have, or whatever is less than that. |
| 697 | */ |
| 698 | extent_bytes = max_bytes - bitmap_bytes; |
| 699 | extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1); |
| 700 | |
| 701 | ctl->extents_thresh = |
| 702 | div_u64(extent_bytes, sizeof(struct btrfs_free_space)); |
| 703 | } |
| 704 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 705 | static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, |
| 706 | struct btrfs_free_space_ctl *ctl, |
| 707 | struct btrfs_path *path, u64 offset) |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 708 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 709 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 710 | struct btrfs_free_space_header *header; |
| 711 | struct extent_buffer *leaf; |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 712 | struct btrfs_io_ctl io_ctl; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 713 | struct btrfs_key key; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 714 | struct btrfs_free_space *e, *n; |
Gui Hecheng | b76808f | 2014-12-31 09:51:35 +0800 | [diff] [blame] | 715 | LIST_HEAD(bitmaps); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 716 | u64 num_entries; |
| 717 | u64 num_bitmaps; |
| 718 | u64 generation; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 719 | u8 type; |
Josef Bacik | f6a3982 | 2011-06-06 10:50:35 -0400 | [diff] [blame] | 720 | int ret = 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 721 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 722 | /* Nothing in the space cache, goodbye */ |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 723 | if (!i_size_read(inode)) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 724 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 725 | |
| 726 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 727 | key.offset = offset; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 728 | key.type = 0; |
| 729 | |
| 730 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 731 | if (ret < 0) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 732 | return 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 733 | else if (ret > 0) { |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 734 | btrfs_release_path(path); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 735 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 736 | } |
| 737 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 738 | ret = -1; |
| 739 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 740 | leaf = path->nodes[0]; |
| 741 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 742 | struct btrfs_free_space_header); |
| 743 | num_entries = btrfs_free_space_entries(leaf, header); |
| 744 | num_bitmaps = btrfs_free_space_bitmaps(leaf, header); |
| 745 | generation = btrfs_free_space_generation(leaf, header); |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 746 | btrfs_release_path(path); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 747 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 748 | if (!BTRFS_I(inode)->generation) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 749 | btrfs_info(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 750 | "the free space cache file (%llu) is invalid, skip it", |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 751 | offset); |
| 752 | return 0; |
| 753 | } |
| 754 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 755 | if (BTRFS_I(inode)->generation != generation) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 756 | btrfs_err(fs_info, |
| 757 | "free space inode generation (%llu) did not match free space cache generation (%llu)", |
| 758 | BTRFS_I(inode)->generation, generation); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 759 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if (!num_entries) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 763 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 764 | |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 765 | ret = io_ctl_init(&io_ctl, inode, 0); |
Li Zefan | 706efc6 | 2012-01-09 14:36:28 +0800 | [diff] [blame] | 766 | if (ret) |
| 767 | return ret; |
| 768 | |
David Sterba | 1d48053 | 2017-01-23 17:28:19 +0100 | [diff] [blame] | 769 | readahead_cache(inode); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 770 | |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 771 | ret = io_ctl_prepare_pages(&io_ctl, true); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 772 | if (ret) |
| 773 | goto out; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 774 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 775 | ret = io_ctl_check_crc(&io_ctl, 0); |
| 776 | if (ret) |
| 777 | goto free_cache; |
| 778 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 779 | ret = io_ctl_check_generation(&io_ctl, generation); |
| 780 | if (ret) |
| 781 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 782 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 783 | while (num_entries) { |
| 784 | e = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 785 | GFP_NOFS); |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 786 | if (!e) { |
| 787 | ret = -ENOMEM; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 788 | goto free_cache; |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 789 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 790 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 791 | ret = io_ctl_read_entry(&io_ctl, e, &type); |
| 792 | if (ret) { |
| 793 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 794 | goto free_cache; |
| 795 | } |
| 796 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 797 | if (!e->bytes) { |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 798 | ret = -1; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 799 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 800 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 801 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 802 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 803 | if (type == BTRFS_FREE_SPACE_EXTENT) { |
| 804 | spin_lock(&ctl->tree_lock); |
| 805 | ret = link_free_space(ctl, e); |
| 806 | spin_unlock(&ctl->tree_lock); |
| 807 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 808 | btrfs_err(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 809 | "Duplicate entries in free space cache, dumping"); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 810 | kmem_cache_free(btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 811 | goto free_cache; |
| 812 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 813 | } else { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 814 | ASSERT(num_bitmaps); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 815 | num_bitmaps--; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 816 | e->bitmap = kmem_cache_zalloc( |
| 817 | btrfs_free_space_bitmap_cachep, GFP_NOFS); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 818 | if (!e->bitmap) { |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 819 | ret = -ENOMEM; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 820 | kmem_cache_free( |
| 821 | btrfs_free_space_cachep, e); |
| 822 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 823 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 824 | spin_lock(&ctl->tree_lock); |
| 825 | ret = link_free_space(ctl, e); |
| 826 | ctl->total_bitmaps++; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 827 | recalculate_thresholds(ctl); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 828 | spin_unlock(&ctl->tree_lock); |
| 829 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 830 | btrfs_err(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 831 | "Duplicate entries in free space cache, dumping"); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 832 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 833 | goto free_cache; |
| 834 | } |
| 835 | list_add_tail(&e->list, &bitmaps); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 836 | } |
| 837 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 838 | num_entries--; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 839 | } |
| 840 | |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 841 | io_ctl_unmap_page(&io_ctl); |
| 842 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 843 | /* |
| 844 | * We add the bitmaps at the end of the entries in order that |
| 845 | * the bitmap entries are added to the cache. |
| 846 | */ |
| 847 | list_for_each_entry_safe(e, n, &bitmaps, list) { |
| 848 | list_del_init(&e->list); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 849 | ret = io_ctl_read_bitmap(&io_ctl, e); |
| 850 | if (ret) |
| 851 | goto free_cache; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | io_ctl_drop_pages(&io_ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 855 | ret = 1; |
| 856 | out: |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 857 | io_ctl_free(&io_ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 858 | return ret; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 859 | free_cache: |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 860 | io_ctl_drop_pages(&io_ctl); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 861 | __btrfs_remove_free_space_cache(ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 862 | goto out; |
| 863 | } |
| 864 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 865 | static int copy_free_space_cache(struct btrfs_block_group *block_group, |
| 866 | struct btrfs_free_space_ctl *ctl) |
| 867 | { |
| 868 | struct btrfs_free_space *info; |
| 869 | struct rb_node *n; |
| 870 | int ret = 0; |
| 871 | |
| 872 | while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) { |
| 873 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
| 874 | if (!info->bitmap) { |
| 875 | unlink_free_space(ctl, info); |
| 876 | ret = btrfs_add_free_space(block_group, info->offset, |
| 877 | info->bytes); |
| 878 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 879 | } else { |
| 880 | u64 offset = info->offset; |
| 881 | u64 bytes = ctl->unit; |
| 882 | |
| 883 | while (search_bitmap(ctl, info, &offset, &bytes, |
| 884 | false) == 0) { |
| 885 | ret = btrfs_add_free_space(block_group, offset, |
| 886 | bytes); |
| 887 | if (ret) |
| 888 | break; |
| 889 | bitmap_clear_bits(ctl, info, offset, bytes); |
| 890 | offset = info->offset; |
| 891 | bytes = ctl->unit; |
| 892 | } |
| 893 | free_bitmap(ctl, info); |
| 894 | } |
| 895 | cond_resched(); |
| 896 | } |
| 897 | return ret; |
| 898 | } |
| 899 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 900 | int load_free_space_cache(struct btrfs_block_group *block_group) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 901 | { |
David Sterba | bb6cb1c | 2019-03-20 13:47:15 +0100 | [diff] [blame] | 902 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 903 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 904 | struct btrfs_free_space_ctl tmp_ctl = {}; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 905 | struct inode *inode; |
| 906 | struct btrfs_path *path; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 907 | int ret = 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 908 | bool matched; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 909 | u64 used = block_group->used; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 910 | |
| 911 | /* |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 912 | * Because we could potentially discard our loaded free space, we want |
| 913 | * to load everything into a temporary structure first, and then if it's |
| 914 | * valid copy it all into the actual free space ctl. |
| 915 | */ |
| 916 | btrfs_init_free_space_ctl(block_group, &tmp_ctl); |
| 917 | |
| 918 | /* |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 919 | * If this block group has been marked to be cleared for one reason or |
| 920 | * another then we can't trust the on disk cache, so just return. |
| 921 | */ |
| 922 | spin_lock(&block_group->lock); |
| 923 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
| 924 | spin_unlock(&block_group->lock); |
| 925 | return 0; |
| 926 | } |
| 927 | spin_unlock(&block_group->lock); |
| 928 | |
| 929 | path = btrfs_alloc_path(); |
| 930 | if (!path) |
| 931 | return 0; |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 932 | path->search_commit_root = 1; |
| 933 | path->skip_locking = 1; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 934 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 935 | /* |
| 936 | * We must pass a path with search_commit_root set to btrfs_iget in |
| 937 | * order to avoid a deadlock when allocating extents for the tree root. |
| 938 | * |
| 939 | * When we are COWing an extent buffer from the tree root, when looking |
| 940 | * for a free extent, at extent-tree.c:find_free_extent(), we can find |
| 941 | * block group without its free space cache loaded. When we find one |
| 942 | * we must load its space cache which requires reading its free space |
| 943 | * cache's inode item from the root tree. If this inode item is located |
| 944 | * in the same leaf that we started COWing before, then we end up in |
| 945 | * deadlock on the extent buffer (trying to read lock it when we |
| 946 | * previously write locked it). |
| 947 | * |
| 948 | * It's safe to read the inode item using the commit root because |
| 949 | * block groups, once loaded, stay in memory forever (until they are |
| 950 | * removed) as well as their space caches once loaded. New block groups |
| 951 | * once created get their ->cached field set to BTRFS_CACHE_FINISHED so |
| 952 | * we will never try to read their inode item while the fs is mounted. |
| 953 | */ |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 954 | inode = lookup_free_space_inode(block_group, path); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 955 | if (IS_ERR(inode)) { |
| 956 | btrfs_free_path(path); |
| 957 | return 0; |
| 958 | } |
| 959 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 960 | /* We may have converted the inode and made the cache invalid. */ |
| 961 | spin_lock(&block_group->lock); |
| 962 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
| 963 | spin_unlock(&block_group->lock); |
Tsutomu Itoh | a7e221e | 2012-02-14 17:12:23 +0900 | [diff] [blame] | 964 | btrfs_free_path(path); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 965 | goto out; |
| 966 | } |
| 967 | spin_unlock(&block_group->lock); |
| 968 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 969 | ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 970 | path, block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 971 | btrfs_free_path(path); |
| 972 | if (ret <= 0) |
| 973 | goto out; |
| 974 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 975 | matched = (tmp_ctl.free_space == (block_group->length - used - |
| 976 | block_group->bytes_super)); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 977 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 978 | if (matched) { |
| 979 | ret = copy_free_space_cache(block_group, &tmp_ctl); |
| 980 | /* |
| 981 | * ret == 1 means we successfully loaded the free space cache, |
| 982 | * so we need to re-set it here. |
| 983 | */ |
| 984 | if (ret == 0) |
| 985 | ret = 1; |
| 986 | } else { |
| 987 | __btrfs_remove_free_space_cache(&tmp_ctl); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 988 | btrfs_warn(fs_info, |
| 989 | "block group %llu has wrong amount of free space", |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 990 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 991 | ret = -1; |
| 992 | } |
| 993 | out: |
| 994 | if (ret < 0) { |
| 995 | /* This cache is bogus, make sure it gets cleared */ |
| 996 | spin_lock(&block_group->lock); |
| 997 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 998 | spin_unlock(&block_group->lock); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 999 | ret = 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1000 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1001 | btrfs_warn(fs_info, |
| 1002 | "failed to load free space cache for block group %llu, rebuilding it now", |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1003 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 1006 | spin_lock(&ctl->tree_lock); |
| 1007 | btrfs_discard_update_discardable(block_group); |
| 1008 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1009 | iput(inode); |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1013 | static noinline_for_stack |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1014 | int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1015 | struct btrfs_free_space_ctl *ctl, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1016 | struct btrfs_block_group *block_group, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1017 | int *entries, int *bitmaps, |
| 1018 | struct list_head *bitmap_list) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1019 | { |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 1020 | int ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1021 | struct btrfs_free_cluster *cluster = NULL; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1022 | struct btrfs_free_cluster *cluster_locked = NULL; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1023 | struct rb_node *node = rb_first(&ctl->free_space_offset); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1024 | struct btrfs_trim_range *trim_entry; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1025 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1026 | /* Get the cluster for this block_group if it exists */ |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1027 | if (block_group && !list_empty(&block_group->cluster_list)) { |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1028 | cluster = list_entry(block_group->cluster_list.next, |
| 1029 | struct btrfs_free_cluster, |
| 1030 | block_group_list); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1031 | } |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1032 | |
Josef Bacik | f75b130 | 2011-10-05 10:00:18 -0400 | [diff] [blame] | 1033 | if (!node && cluster) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1034 | cluster_locked = cluster; |
| 1035 | spin_lock(&cluster_locked->lock); |
Josef Bacik | f75b130 | 2011-10-05 10:00:18 -0400 | [diff] [blame] | 1036 | node = rb_first(&cluster->root); |
| 1037 | cluster = NULL; |
| 1038 | } |
| 1039 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1040 | /* Write out the extent entries */ |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1041 | while (node) { |
| 1042 | struct btrfs_free_space *e; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1043 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1044 | e = rb_entry(node, struct btrfs_free_space, offset_index); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1045 | *entries += 1; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1046 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1047 | ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes, |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1048 | e->bitmap); |
| 1049 | if (ret) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1050 | goto fail; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1051 | |
| 1052 | if (e->bitmap) { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1053 | list_add_tail(&e->list, bitmap_list); |
| 1054 | *bitmaps += 1; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1055 | } |
| 1056 | node = rb_next(node); |
| 1057 | if (!node && cluster) { |
| 1058 | node = rb_first(&cluster->root); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1059 | cluster_locked = cluster; |
| 1060 | spin_lock(&cluster_locked->lock); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1061 | cluster = NULL; |
| 1062 | } |
| 1063 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1064 | if (cluster_locked) { |
| 1065 | spin_unlock(&cluster_locked->lock); |
| 1066 | cluster_locked = NULL; |
| 1067 | } |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* |
| 1070 | * Make sure we don't miss any range that was removed from our rbtree |
| 1071 | * because trimming is running. Otherwise after a umount+mount (or crash |
| 1072 | * after committing the transaction) we would leak free space and get |
| 1073 | * an inconsistent free space cache report from fsck. |
| 1074 | */ |
| 1075 | list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) { |
| 1076 | ret = io_ctl_add_entry(io_ctl, trim_entry->start, |
| 1077 | trim_entry->bytes, NULL); |
| 1078 | if (ret) |
| 1079 | goto fail; |
| 1080 | *entries += 1; |
| 1081 | } |
| 1082 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1083 | return 0; |
| 1084 | fail: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1085 | if (cluster_locked) |
| 1086 | spin_unlock(&cluster_locked->lock); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1087 | return -ENOSPC; |
| 1088 | } |
| 1089 | |
| 1090 | static noinline_for_stack int |
| 1091 | update_cache_item(struct btrfs_trans_handle *trans, |
| 1092 | struct btrfs_root *root, |
| 1093 | struct inode *inode, |
| 1094 | struct btrfs_path *path, u64 offset, |
| 1095 | int entries, int bitmaps) |
| 1096 | { |
| 1097 | struct btrfs_key key; |
| 1098 | struct btrfs_free_space_header *header; |
| 1099 | struct extent_buffer *leaf; |
| 1100 | int ret; |
| 1101 | |
| 1102 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 1103 | key.offset = offset; |
| 1104 | key.type = 0; |
| 1105 | |
| 1106 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1107 | if (ret < 0) { |
| 1108 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1109 | EXTENT_DELALLOC, 0, 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1110 | goto fail; |
| 1111 | } |
| 1112 | leaf = path->nodes[0]; |
| 1113 | if (ret > 0) { |
| 1114 | struct btrfs_key found_key; |
| 1115 | ASSERT(path->slots[0]); |
| 1116 | path->slots[0]--; |
| 1117 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1118 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || |
| 1119 | found_key.offset != offset) { |
| 1120 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1121 | inode->i_size - 1, EXTENT_DELALLOC, 0, |
| 1122 | 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1123 | btrfs_release_path(path); |
| 1124 | goto fail; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | BTRFS_I(inode)->generation = trans->transid; |
| 1129 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 1130 | struct btrfs_free_space_header); |
| 1131 | btrfs_set_free_space_entries(leaf, header, entries); |
| 1132 | btrfs_set_free_space_bitmaps(leaf, header, bitmaps); |
| 1133 | btrfs_set_free_space_generation(leaf, header, trans->transid); |
| 1134 | btrfs_mark_buffer_dirty(leaf); |
| 1135 | btrfs_release_path(path); |
| 1136 | |
| 1137 | return 0; |
| 1138 | |
| 1139 | fail: |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
David Sterba | 6701bdb | 2019-03-20 13:49:09 +0100 | [diff] [blame] | 1143 | static noinline_for_stack int write_pinned_extent_entries( |
Nikolay Borisov | 6b45f641 | 2020-01-20 16:09:15 +0200 | [diff] [blame] | 1144 | struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1145 | struct btrfs_block_group *block_group, |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1146 | struct btrfs_io_ctl *io_ctl, |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1147 | int *entries) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1148 | { |
| 1149 | u64 start, extent_start, extent_end, len; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1150 | struct extent_io_tree *unpin = NULL; |
| 1151 | int ret; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1152 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1153 | if (!block_group) |
| 1154 | return 0; |
| 1155 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1156 | /* |
| 1157 | * We want to add any pinned extents to our free space cache |
| 1158 | * so we don't leak the space |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1159 | * |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1160 | * We shouldn't have switched the pinned extents yet so this is the |
| 1161 | * right one |
| 1162 | */ |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 1163 | unpin = &trans->transaction->pinned_extents; |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1164 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1165 | start = block_group->start; |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1166 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1167 | while (start < block_group->start + block_group->length) { |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1168 | ret = find_first_extent_bit(unpin, start, |
| 1169 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1170 | EXTENT_DIRTY, NULL); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1171 | if (ret) |
| 1172 | return 0; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1173 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1174 | /* This pinned extent is out of our range */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1175 | if (extent_start >= block_group->start + block_group->length) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1176 | return 0; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1177 | |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1178 | extent_start = max(extent_start, start); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1179 | extent_end = min(block_group->start + block_group->length, |
| 1180 | extent_end + 1); |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1181 | len = extent_end - extent_start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1182 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1183 | *entries += 1; |
| 1184 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1185 | if (ret) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1186 | return -ENOSPC; |
Josef Bacik | 2f35612 | 2011-06-10 15:31:13 -0400 | [diff] [blame] | 1187 | |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1188 | start = extent_end; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1189 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1190 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | static noinline_for_stack int |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1195 | write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1196 | { |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1197 | struct btrfs_free_space *entry, *next; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1198 | int ret; |
| 1199 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1200 | /* Write out the bitmaps */ |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1201 | list_for_each_entry_safe(entry, next, bitmap_list, list) { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1202 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1203 | if (ret) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1204 | return -ENOSPC; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1205 | list_del_init(&entry->list); |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1208 | return 0; |
| 1209 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1210 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1211 | static int flush_dirty_cache(struct inode *inode) |
| 1212 | { |
| 1213 | int ret; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1214 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1215 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1216 | if (ret) |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1217 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1218 | EXTENT_DELALLOC, 0, 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1219 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1220 | return ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | static void noinline_for_stack |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1224 | cleanup_bitmap_list(struct list_head *bitmap_list) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1225 | { |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1226 | struct btrfs_free_space *entry, *next; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1227 | |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1228 | list_for_each_entry_safe(entry, next, bitmap_list, list) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1229 | list_del_init(&entry->list); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | static void noinline_for_stack |
| 1233 | cleanup_write_cache_enospc(struct inode *inode, |
| 1234 | struct btrfs_io_ctl *io_ctl, |
David Sterba | 7bf1a15 | 2017-02-10 20:23:00 +0100 | [diff] [blame] | 1235 | struct extent_state **cached_state) |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1236 | { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1237 | io_ctl_drop_pages(io_ctl); |
| 1238 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1239 | i_size_read(inode) - 1, cached_state); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1242 | static int __btrfs_wait_cache_io(struct btrfs_root *root, |
| 1243 | struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1244 | struct btrfs_block_group *block_group, |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1245 | struct btrfs_io_ctl *io_ctl, |
| 1246 | struct btrfs_path *path, u64 offset) |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1247 | { |
| 1248 | int ret; |
| 1249 | struct inode *inode = io_ctl->inode; |
| 1250 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1251 | if (!inode) |
| 1252 | return 0; |
| 1253 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1254 | /* Flush the dirty pages in the cache file. */ |
| 1255 | ret = flush_dirty_cache(inode); |
| 1256 | if (ret) |
| 1257 | goto out; |
| 1258 | |
| 1259 | /* Update the cache item to tell everyone this cache file is valid. */ |
| 1260 | ret = update_cache_item(trans, root, inode, path, offset, |
| 1261 | io_ctl->entries, io_ctl->bitmaps); |
| 1262 | out: |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1263 | if (ret) { |
| 1264 | invalidate_inode_pages2(inode->i_mapping); |
| 1265 | BTRFS_I(inode)->generation = 0; |
Filipe Manana | bbcd1f4 | 2020-05-18 17:34:23 +0100 | [diff] [blame] | 1266 | if (block_group) |
| 1267 | btrfs_debug(root->fs_info, |
Filipe Manana | 2e69a7a | 2020-05-18 17:34:11 +0100 | [diff] [blame] | 1268 | "failed to write free space cache for block group %llu error %d", |
| 1269 | block_group->start, ret); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1270 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 1271 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1272 | |
| 1273 | if (block_group) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1274 | /* the dirty list is protected by the dirty_bgs_lock */ |
| 1275 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 1276 | |
| 1277 | /* the disk_cache_state is protected by the block group lock */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1278 | spin_lock(&block_group->lock); |
| 1279 | |
| 1280 | /* |
| 1281 | * only mark this as written if we didn't get put back on |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1282 | * the dirty list while waiting for IO. Otherwise our |
| 1283 | * cache state won't be right, and we won't get written again |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1284 | */ |
| 1285 | if (!ret && list_empty(&block_group->dirty_list)) |
| 1286 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 1287 | else if (ret) |
| 1288 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
| 1289 | |
| 1290 | spin_unlock(&block_group->lock); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1291 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1292 | io_ctl->inode = NULL; |
| 1293 | iput(inode); |
| 1294 | } |
| 1295 | |
| 1296 | return ret; |
| 1297 | |
| 1298 | } |
| 1299 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1300 | int btrfs_wait_cache_io(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1301 | struct btrfs_block_group *block_group, |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1302 | struct btrfs_path *path) |
| 1303 | { |
| 1304 | return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans, |
| 1305 | block_group, &block_group->io_ctl, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1306 | path, block_group->start); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1307 | } |
| 1308 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1309 | /** |
Nikolay Borisov | f092cf3 | 2021-01-22 11:57:56 +0200 | [diff] [blame] | 1310 | * Write out cached info to an inode |
| 1311 | * |
| 1312 | * @root: root the inode belongs to |
| 1313 | * @inode: freespace inode we are writing out |
| 1314 | * @ctl: free space cache we are going to write out |
| 1315 | * @block_group: block_group for this cache if it belongs to a block_group |
| 1316 | * @io_ctl: holds context for the io |
| 1317 | * @trans: the trans handle |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1318 | * |
| 1319 | * This function writes out a free space cache struct to disk for quick recovery |
Geliang Tang | 8cd1e73 | 2015-10-04 17:05:32 +0800 | [diff] [blame] | 1320 | * on mount. This will return 0 if it was successful in writing the cache out, |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1321 | * or an errno if it was not. |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1322 | */ |
| 1323 | static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, |
| 1324 | struct btrfs_free_space_ctl *ctl, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1325 | struct btrfs_block_group *block_group, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1326 | struct btrfs_io_ctl *io_ctl, |
David Sterba | 0e8d931 | 2017-02-10 20:26:24 +0100 | [diff] [blame] | 1327 | struct btrfs_trans_handle *trans) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1328 | { |
| 1329 | struct extent_state *cached_state = NULL; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1330 | LIST_HEAD(bitmap_list); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1331 | int entries = 0; |
| 1332 | int bitmaps = 0; |
| 1333 | int ret; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1334 | int must_iput = 0; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1335 | |
| 1336 | if (!i_size_read(inode)) |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1337 | return -EIO; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1338 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1339 | WARN_ON(io_ctl->pages); |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 1340 | ret = io_ctl_init(io_ctl, inode, 1); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1341 | if (ret) |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1342 | return ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1343 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1344 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) { |
| 1345 | down_write(&block_group->data_rwsem); |
| 1346 | spin_lock(&block_group->lock); |
| 1347 | if (block_group->delalloc_bytes) { |
| 1348 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 1349 | spin_unlock(&block_group->lock); |
| 1350 | up_write(&block_group->data_rwsem); |
| 1351 | BTRFS_I(inode)->generation = 0; |
| 1352 | ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1353 | must_iput = 1; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1354 | goto out; |
| 1355 | } |
| 1356 | spin_unlock(&block_group->lock); |
| 1357 | } |
| 1358 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1359 | /* Lock all pages first so we can lock the extent safely. */ |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 1360 | ret = io_ctl_prepare_pages(io_ctl, false); |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1361 | if (ret) |
Josef Bacik | b77000e | 2017-11-15 16:20:52 -0500 | [diff] [blame] | 1362 | goto out_unlock; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1363 | |
| 1364 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1365 | &cached_state); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1366 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1367 | io_ctl_set_generation(io_ctl, trans->transid); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1368 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1369 | mutex_lock(&ctl->cache_writeout_mutex); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1370 | /* Write out the extent entries in the free space cache */ |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1371 | spin_lock(&ctl->tree_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1372 | ret = write_cache_extent_entries(io_ctl, ctl, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1373 | block_group, &entries, &bitmaps, |
| 1374 | &bitmap_list); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1375 | if (ret) |
| 1376 | goto out_nospc_locked; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1377 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1378 | /* |
| 1379 | * Some spaces that are freed in the current transaction are pinned, |
| 1380 | * they will be added into free space cache after the transaction is |
| 1381 | * committed, we shouldn't lose them. |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1382 | * |
| 1383 | * If this changes while we are working we'll get added back to |
| 1384 | * the dirty list and redo it. No locking needed |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1385 | */ |
Nikolay Borisov | 6b45f641 | 2020-01-20 16:09:15 +0200 | [diff] [blame] | 1386 | ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1387 | if (ret) |
| 1388 | goto out_nospc_locked; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1389 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1390 | /* |
| 1391 | * At last, we write out all the bitmaps and keep cache_writeout_mutex |
| 1392 | * locked while doing it because a concurrent trim can be manipulating |
| 1393 | * or freeing the bitmap. |
| 1394 | */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1395 | ret = write_bitmap_entries(io_ctl, &bitmap_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1396 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1397 | mutex_unlock(&ctl->cache_writeout_mutex); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1398 | if (ret) |
| 1399 | goto out_nospc; |
| 1400 | |
| 1401 | /* Zero out the rest of the pages just to make sure */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1402 | io_ctl_zero_remaining_pages(io_ctl); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1403 | |
| 1404 | /* Everything is written out, now we dirty the pages in the file. */ |
Nikolay Borisov | 088545f | 2020-06-03 08:55:36 +0300 | [diff] [blame] | 1405 | ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages, |
| 1406 | io_ctl->num_pages, 0, i_size_read(inode), |
Goldwyn Rodrigues | aa8c1a4 | 2020-10-14 09:55:45 -0500 | [diff] [blame] | 1407 | &cached_state, false); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1408 | if (ret) |
| 1409 | goto out_nospc; |
| 1410 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1411 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 1412 | up_write(&block_group->data_rwsem); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1413 | /* |
| 1414 | * Release the pages and unlock the extent, we will flush |
| 1415 | * them out later |
| 1416 | */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1417 | io_ctl_drop_pages(io_ctl); |
Filipe Manana | bbc37d6 | 2020-08-14 11:04:09 +0100 | [diff] [blame] | 1418 | io_ctl_free(io_ctl); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1419 | |
| 1420 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1421 | i_size_read(inode) - 1, &cached_state); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1422 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1423 | /* |
| 1424 | * at this point the pages are under IO and we're happy, |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 1425 | * The caller is responsible for waiting on them and updating |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1426 | * the cache and the inode |
| 1427 | */ |
| 1428 | io_ctl->entries = entries; |
| 1429 | io_ctl->bitmaps = bitmaps; |
| 1430 | |
| 1431 | ret = btrfs_fdatawrite_range(inode, 0, (u64)-1); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1432 | if (ret) |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1433 | goto out; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1434 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1435 | return 0; |
| 1436 | |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1437 | out_nospc_locked: |
| 1438 | cleanup_bitmap_list(&bitmap_list); |
| 1439 | spin_unlock(&ctl->tree_lock); |
| 1440 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 1441 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1442 | out_nospc: |
David Sterba | 7bf1a15 | 2017-02-10 20:23:00 +0100 | [diff] [blame] | 1443 | cleanup_write_cache_enospc(inode, io_ctl, &cached_state); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1444 | |
Josef Bacik | b77000e | 2017-11-15 16:20:52 -0500 | [diff] [blame] | 1445 | out_unlock: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1446 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 1447 | up_write(&block_group->data_rwsem); |
| 1448 | |
Johannes Thumshirn | fd8efa8 | 2020-02-12 00:10:22 +0900 | [diff] [blame] | 1449 | out: |
| 1450 | io_ctl->inode = NULL; |
| 1451 | io_ctl_free(io_ctl); |
| 1452 | if (ret) { |
| 1453 | invalidate_inode_pages2(inode->i_mapping); |
| 1454 | BTRFS_I(inode)->generation = 0; |
| 1455 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 1456 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Johannes Thumshirn | fd8efa8 | 2020-02-12 00:10:22 +0900 | [diff] [blame] | 1457 | if (must_iput) |
| 1458 | iput(inode); |
| 1459 | return ret; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1460 | } |
| 1461 | |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 1462 | int btrfs_write_out_cache(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1463 | struct btrfs_block_group *block_group, |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1464 | struct btrfs_path *path) |
| 1465 | { |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 1466 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1467 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 1468 | struct inode *inode; |
| 1469 | int ret = 0; |
| 1470 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1471 | spin_lock(&block_group->lock); |
| 1472 | if (block_group->disk_cache_state < BTRFS_DC_SETUP) { |
| 1473 | spin_unlock(&block_group->lock); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | spin_unlock(&block_group->lock); |
| 1477 | |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 1478 | inode = lookup_free_space_inode(block_group, path); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1479 | if (IS_ERR(inode)) |
| 1480 | return 0; |
| 1481 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 1482 | ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl, |
| 1483 | block_group, &block_group->io_ctl, trans); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 1484 | if (ret) { |
Filipe Manana | bbcd1f4 | 2020-05-18 17:34:23 +0100 | [diff] [blame] | 1485 | btrfs_debug(fs_info, |
Filipe Manana | 2e69a7a | 2020-05-18 17:34:11 +0100 | [diff] [blame] | 1486 | "failed to write free space cache for block group %llu error %d", |
| 1487 | block_group->start, ret); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1488 | spin_lock(&block_group->lock); |
| 1489 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
| 1490 | spin_unlock(&block_group->lock); |
| 1491 | |
| 1492 | block_group->io_ctl.inode = NULL; |
| 1493 | iput(inode); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1496 | /* |
| 1497 | * if ret == 0 the caller is expected to call btrfs_wait_cache_io |
| 1498 | * to wait for IO and put the inode |
| 1499 | */ |
| 1500 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1501 | return ret; |
| 1502 | } |
| 1503 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1504 | static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1505 | u64 offset) |
| 1506 | { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1507 | ASSERT(offset >= bitmap_start); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1508 | offset -= bitmap_start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1509 | return (unsigned long)(div_u64(offset, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1510 | } |
| 1511 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1512 | static inline unsigned long bytes_to_bits(u64 bytes, u32 unit) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1513 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1514 | return (unsigned long)(div_u64(bytes, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1515 | } |
| 1516 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1517 | static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1518 | u64 offset) |
| 1519 | { |
| 1520 | u64 bitmap_start; |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 1521 | u64 bytes_per_bitmap; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1522 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1523 | bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; |
| 1524 | bitmap_start = offset - ctl->start; |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 1525 | bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1526 | bitmap_start *= bytes_per_bitmap; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1527 | bitmap_start += ctl->start; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1528 | |
| 1529 | return bitmap_start; |
| 1530 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1531 | |
| 1532 | static int tree_insert_offset(struct rb_root *root, u64 offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1533 | struct rb_node *node, int bitmap) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1534 | { |
| 1535 | struct rb_node **p = &root->rb_node; |
| 1536 | struct rb_node *parent = NULL; |
| 1537 | struct btrfs_free_space *info; |
| 1538 | |
| 1539 | while (*p) { |
| 1540 | parent = *p; |
| 1541 | info = rb_entry(parent, struct btrfs_free_space, offset_index); |
| 1542 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1543 | if (offset < info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1544 | p = &(*p)->rb_left; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1545 | } else if (offset > info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1546 | p = &(*p)->rb_right; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1547 | } else { |
| 1548 | /* |
| 1549 | * we could have a bitmap entry and an extent entry |
| 1550 | * share the same offset. If this is the case, we want |
| 1551 | * the extent entry to always be found first if we do a |
| 1552 | * linear search through the tree, since we want to have |
| 1553 | * the quickest allocation time, and allocating from an |
| 1554 | * extent is faster than allocating from a bitmap. So |
| 1555 | * if we're inserting a bitmap and we find an entry at |
| 1556 | * this offset, we want to go right, or after this entry |
| 1557 | * logically. If we are inserting an extent and we've |
| 1558 | * found a bitmap, we want to go left, or before |
| 1559 | * logically. |
| 1560 | */ |
| 1561 | if (bitmap) { |
Josef Bacik | 207dde8 | 2011-05-13 14:49:23 -0400 | [diff] [blame] | 1562 | if (info->bitmap) { |
| 1563 | WARN_ON_ONCE(1); |
| 1564 | return -EEXIST; |
| 1565 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1566 | p = &(*p)->rb_right; |
| 1567 | } else { |
Josef Bacik | 207dde8 | 2011-05-13 14:49:23 -0400 | [diff] [blame] | 1568 | if (!info->bitmap) { |
| 1569 | WARN_ON_ONCE(1); |
| 1570 | return -EEXIST; |
| 1571 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1572 | p = &(*p)->rb_left; |
| 1573 | } |
| 1574 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | rb_link_node(node, parent, p); |
| 1578 | rb_insert_color(node, root); |
| 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | /* |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1584 | * This is a little subtle. We *only* have ->max_extent_size set if we actually |
| 1585 | * searched through the bitmap and figured out the largest ->max_extent_size, |
| 1586 | * otherwise it's 0. In the case that it's 0 we don't want to tell the |
| 1587 | * allocator the wrong thing, we want to use the actual real max_extent_size |
| 1588 | * we've found already if it's larger, or we want to use ->bytes. |
| 1589 | * |
| 1590 | * This matters because find_free_space() will skip entries who's ->bytes is |
| 1591 | * less than the required bytes. So if we didn't search down this bitmap, we |
| 1592 | * may pick some previous entry that has a smaller ->max_extent_size than we |
| 1593 | * have. For example, assume we have two entries, one that has |
| 1594 | * ->max_extent_size set to 4K and ->bytes set to 1M. A second entry hasn't set |
| 1595 | * ->max_extent_size yet, has ->bytes set to 8K and it's contiguous. We will |
| 1596 | * call into find_free_space(), and return with max_extent_size == 4K, because |
| 1597 | * that first bitmap entry had ->max_extent_size set, but the second one did |
| 1598 | * not. If instead we returned 8K we'd come in searching for 8K, and find the |
| 1599 | * 8K contiguous range. |
| 1600 | * |
| 1601 | * Consider the other case, we have 2 8K chunks in that second entry and still |
| 1602 | * don't have ->max_extent_size set. We'll return 16K, and the next time the |
| 1603 | * allocator comes in it'll fully search our second bitmap, and this time it'll |
| 1604 | * get an uptodate value of 8K as the maximum chunk size. Then we'll get the |
| 1605 | * right allocation the next loop through. |
| 1606 | */ |
| 1607 | static inline u64 get_max_extent_size(const struct btrfs_free_space *entry) |
| 1608 | { |
| 1609 | if (entry->bitmap && entry->max_extent_size) |
| 1610 | return entry->max_extent_size; |
| 1611 | return entry->bytes; |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | * We want the largest entry to be leftmost, so this is inverted from what you'd |
| 1616 | * normally expect. |
| 1617 | */ |
| 1618 | static bool entry_less(struct rb_node *node, const struct rb_node *parent) |
| 1619 | { |
| 1620 | const struct btrfs_free_space *entry, *exist; |
| 1621 | |
| 1622 | entry = rb_entry(node, struct btrfs_free_space, bytes_index); |
| 1623 | exist = rb_entry(parent, struct btrfs_free_space, bytes_index); |
| 1624 | return get_max_extent_size(exist) < get_max_extent_size(entry); |
| 1625 | } |
| 1626 | |
| 1627 | /* |
Josef Bacik | 70cb074 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 1628 | * searches the tree for the given offset. |
| 1629 | * |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1630 | * fuzzy - If this is set, then we are trying to make an allocation, and we just |
| 1631 | * want a section that has at least bytes size and comes at or after the given |
| 1632 | * offset. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1633 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1634 | static struct btrfs_free_space * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1635 | tree_search_offset(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1636 | u64 offset, int bitmap_only, int fuzzy) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1637 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1638 | struct rb_node *n = ctl->free_space_offset.rb_node; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1639 | struct btrfs_free_space *entry, *prev = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1640 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1641 | /* find entry that is closest to the 'offset' */ |
| 1642 | while (1) { |
| 1643 | if (!n) { |
| 1644 | entry = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1645 | break; |
| 1646 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1647 | |
| 1648 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1649 | prev = entry; |
| 1650 | |
| 1651 | if (offset < entry->offset) |
| 1652 | n = n->rb_left; |
| 1653 | else if (offset > entry->offset) |
| 1654 | n = n->rb_right; |
| 1655 | else |
| 1656 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1657 | } |
| 1658 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1659 | if (bitmap_only) { |
| 1660 | if (!entry) |
| 1661 | return NULL; |
| 1662 | if (entry->bitmap) |
| 1663 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1664 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1665 | /* |
| 1666 | * bitmap entry and extent entry may share same offset, |
| 1667 | * in that case, bitmap entry comes after extent entry. |
| 1668 | */ |
| 1669 | n = rb_next(n); |
| 1670 | if (!n) |
| 1671 | return NULL; |
| 1672 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1673 | if (entry->offset != offset) |
| 1674 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1675 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1676 | WARN_ON(!entry->bitmap); |
| 1677 | return entry; |
| 1678 | } else if (entry) { |
| 1679 | if (entry->bitmap) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1680 | /* |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1681 | * if previous extent entry covers the offset, |
| 1682 | * we should return it instead of the bitmap entry |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1683 | */ |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1684 | n = rb_prev(&entry->offset_index); |
| 1685 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1686 | prev = rb_entry(n, struct btrfs_free_space, |
| 1687 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1688 | if (!prev->bitmap && |
| 1689 | prev->offset + prev->bytes > offset) |
| 1690 | entry = prev; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1691 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1692 | } |
| 1693 | return entry; |
| 1694 | } |
| 1695 | |
| 1696 | if (!prev) |
| 1697 | return NULL; |
| 1698 | |
| 1699 | /* find last entry before the 'offset' */ |
| 1700 | entry = prev; |
| 1701 | if (entry->offset > offset) { |
| 1702 | n = rb_prev(&entry->offset_index); |
| 1703 | if (n) { |
| 1704 | entry = rb_entry(n, struct btrfs_free_space, |
| 1705 | offset_index); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1706 | ASSERT(entry->offset <= offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1707 | } else { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1708 | if (fuzzy) |
| 1709 | return entry; |
| 1710 | else |
| 1711 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1715 | if (entry->bitmap) { |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1716 | n = rb_prev(&entry->offset_index); |
| 1717 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1718 | prev = rb_entry(n, struct btrfs_free_space, |
| 1719 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1720 | if (!prev->bitmap && |
| 1721 | prev->offset + prev->bytes > offset) |
| 1722 | return prev; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1723 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1724 | if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1725 | return entry; |
| 1726 | } else if (entry->offset + entry->bytes > offset) |
| 1727 | return entry; |
| 1728 | |
| 1729 | if (!fuzzy) |
| 1730 | return NULL; |
| 1731 | |
| 1732 | while (1) { |
Nikolay Borisov | 167c0bd | 2021-11-22 17:16:46 +0200 | [diff] [blame^] | 1733 | n = rb_next(&entry->offset_index); |
| 1734 | if (!n) |
| 1735 | return NULL; |
| 1736 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1737 | if (entry->bitmap) { |
| 1738 | if (entry->offset + BITS_PER_BITMAP * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1739 | ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1740 | break; |
| 1741 | } else { |
| 1742 | if (entry->offset + entry->bytes > offset) |
| 1743 | break; |
| 1744 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1745 | } |
| 1746 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1747 | } |
| 1748 | |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1749 | static inline void |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1750 | __unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1751 | struct btrfs_free_space *info) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1752 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1753 | rb_erase(&info->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1754 | rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1755 | ctl->free_extents--; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1756 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1757 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1758 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1759 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes; |
| 1760 | } |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1761 | } |
| 1762 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1763 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1764 | struct btrfs_free_space *info) |
| 1765 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1766 | __unlink_free_space(ctl, info); |
| 1767 | ctl->free_space -= info->bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1768 | } |
| 1769 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1770 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1771 | struct btrfs_free_space *info) |
| 1772 | { |
| 1773 | int ret = 0; |
| 1774 | |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1775 | ASSERT(info->bytes || info->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1776 | ret = tree_insert_offset(&ctl->free_space_offset, info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1777 | &info->offset_index, (info->bitmap != NULL)); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1778 | if (ret) |
| 1779 | return ret; |
| 1780 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1781 | rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); |
| 1782 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1783 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1784 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1785 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 1786 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1787 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1788 | ctl->free_space += info->bytes; |
| 1789 | ctl->free_extents++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1790 | return ret; |
| 1791 | } |
| 1792 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1793 | static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl, |
| 1794 | struct btrfs_free_space *info) |
| 1795 | { |
| 1796 | ASSERT(info->bitmap); |
| 1797 | |
| 1798 | /* |
| 1799 | * If our entry is empty it's because we're on a cluster and we don't |
| 1800 | * want to re-link it into our ctl bytes index. |
| 1801 | */ |
| 1802 | if (RB_EMPTY_NODE(&info->bytes_index)) |
| 1803 | return; |
| 1804 | |
| 1805 | rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); |
| 1806 | rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); |
| 1807 | } |
| 1808 | |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 1809 | static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 1810 | struct btrfs_free_space *info, |
| 1811 | u64 offset, u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1812 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1813 | unsigned long start, count, end; |
| 1814 | int extent_delta = -1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1815 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1816 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1817 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1818 | end = start + count; |
| 1819 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1820 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1821 | bitmap_clear(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1822 | |
| 1823 | info->bytes -= bytes; |
Josef Bacik | 553cceb | 2018-09-28 07:18:00 -0400 | [diff] [blame] | 1824 | if (info->max_extent_size > ctl->unit) |
| 1825 | info->max_extent_size = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1826 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1827 | relink_bitmap_entry(ctl, info); |
| 1828 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1829 | if (start && test_bit(start - 1, info->bitmap)) |
| 1830 | extent_delta++; |
| 1831 | |
| 1832 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1833 | extent_delta++; |
| 1834 | |
| 1835 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1836 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1837 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1838 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
| 1839 | } |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 1843 | struct btrfs_free_space *info, u64 offset, |
| 1844 | u64 bytes) |
| 1845 | { |
| 1846 | __bitmap_clear_bits(ctl, info, offset, bytes); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1847 | ctl->free_space -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1848 | } |
| 1849 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1850 | static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1851 | struct btrfs_free_space *info, u64 offset, |
| 1852 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1853 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1854 | unsigned long start, count, end; |
| 1855 | int extent_delta = 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1856 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1857 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1858 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1859 | end = start + count; |
| 1860 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1861 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1862 | bitmap_set(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1863 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1864 | /* |
| 1865 | * We set some bytes, we have no idea what the max extent size is |
| 1866 | * anymore. |
| 1867 | */ |
| 1868 | info->max_extent_size = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1869 | info->bytes += bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1870 | ctl->free_space += bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1871 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1872 | relink_bitmap_entry(ctl, info); |
| 1873 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1874 | if (start && test_bit(start - 1, info->bitmap)) |
| 1875 | extent_delta--; |
| 1876 | |
| 1877 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1878 | extent_delta--; |
| 1879 | |
| 1880 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1881 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1882 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1883 | ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes; |
| 1884 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1885 | } |
| 1886 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1887 | /* |
| 1888 | * If we can not find suitable extent, we will use bytes to record |
| 1889 | * the size of the max extent. |
| 1890 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1891 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1892 | struct btrfs_free_space *bitmap_info, u64 *offset, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1893 | u64 *bytes, bool for_alloc) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1894 | { |
| 1895 | unsigned long found_bits = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1896 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1897 | unsigned long bits, i; |
| 1898 | unsigned long next_zero; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1899 | unsigned long extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1900 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1901 | /* |
| 1902 | * Skip searching the bitmap if we don't have a contiguous section that |
| 1903 | * is large enough for this allocation. |
| 1904 | */ |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1905 | if (for_alloc && |
| 1906 | bitmap_info->max_extent_size && |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1907 | bitmap_info->max_extent_size < *bytes) { |
| 1908 | *bytes = bitmap_info->max_extent_size; |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1912 | i = offset_to_bit(bitmap_info->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1913 | max_t(u64, *offset, bitmap_info->offset)); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1914 | bits = bytes_to_bits(*bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1915 | |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 1916 | for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1917 | if (for_alloc && bits == 1) { |
| 1918 | found_bits = 1; |
| 1919 | break; |
| 1920 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1921 | next_zero = find_next_zero_bit(bitmap_info->bitmap, |
| 1922 | BITS_PER_BITMAP, i); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1923 | extent_bits = next_zero - i; |
| 1924 | if (extent_bits >= bits) { |
| 1925 | found_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1926 | break; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1927 | } else if (extent_bits > max_bits) { |
| 1928 | max_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1929 | } |
| 1930 | i = next_zero; |
| 1931 | } |
| 1932 | |
| 1933 | if (found_bits) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1934 | *offset = (u64)(i * ctl->unit) + bitmap_info->offset; |
| 1935 | *bytes = (u64)(found_bits) * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1939 | *bytes = (u64)(max_bits) * ctl->unit; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1940 | bitmap_info->max_extent_size = *bytes; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1941 | relink_bitmap_entry(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1942 | return -1; |
| 1943 | } |
| 1944 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1945 | /* Cache the size of the max extent in bytes */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1946 | static struct btrfs_free_space * |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1947 | find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes, |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1948 | unsigned long align, u64 *max_extent_size, bool use_bytes_index) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1949 | { |
| 1950 | struct btrfs_free_space *entry; |
| 1951 | struct rb_node *node; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1952 | u64 tmp; |
| 1953 | u64 align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1954 | int ret; |
| 1955 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1956 | if (!ctl->free_space_offset.rb_node) |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1957 | goto out; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1958 | again: |
| 1959 | if (use_bytes_index) { |
| 1960 | node = rb_first_cached(&ctl->free_space_bytes); |
| 1961 | } else { |
| 1962 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), |
| 1963 | 0, 1); |
| 1964 | if (!entry) |
| 1965 | goto out; |
| 1966 | node = &entry->offset_index; |
| 1967 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1968 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1969 | for (; node; node = rb_next(node)) { |
| 1970 | if (use_bytes_index) |
| 1971 | entry = rb_entry(node, struct btrfs_free_space, |
| 1972 | bytes_index); |
| 1973 | else |
| 1974 | entry = rb_entry(node, struct btrfs_free_space, |
| 1975 | offset_index); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1976 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1977 | /* |
| 1978 | * If we are using the bytes index then all subsequent entries |
| 1979 | * in this tree are going to be < bytes, so simply set the max |
| 1980 | * extent size and exit the loop. |
| 1981 | * |
| 1982 | * If we're using the offset index then we need to keep going |
| 1983 | * through the rest of the tree. |
| 1984 | */ |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1985 | if (entry->bytes < *bytes) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1986 | *max_extent_size = max(get_max_extent_size(entry), |
| 1987 | *max_extent_size); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1988 | if (use_bytes_index) |
| 1989 | break; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1990 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1991 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1992 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1993 | /* make sure the space returned is big enough |
| 1994 | * to match our requested alignment |
| 1995 | */ |
| 1996 | if (*bytes >= align) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1997 | tmp = entry->offset - ctl->start + align - 1; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 1998 | tmp = div64_u64(tmp, align); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1999 | tmp = tmp * align + ctl->start; |
| 2000 | align_off = tmp - entry->offset; |
| 2001 | } else { |
| 2002 | align_off = 0; |
| 2003 | tmp = entry->offset; |
| 2004 | } |
| 2005 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2006 | /* |
| 2007 | * We don't break here if we're using the bytes index because we |
| 2008 | * may have another entry that has the correct alignment that is |
| 2009 | * the right size, so we don't want to miss that possibility. |
| 2010 | * At worst this adds another loop through the logic, but if we |
| 2011 | * broke here we could prematurely ENOSPC. |
| 2012 | */ |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2013 | if (entry->bytes < *bytes + align_off) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 2014 | *max_extent_size = max(get_max_extent_size(entry), |
| 2015 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2016 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2017 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2018 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2019 | if (entry->bitmap) { |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2020 | struct rb_node *old_next = rb_next(node); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2021 | u64 size = *bytes; |
| 2022 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2023 | ret = search_bitmap(ctl, entry, &tmp, &size, true); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2024 | if (!ret) { |
| 2025 | *offset = tmp; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2026 | *bytes = size; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2027 | return entry; |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 2028 | } else { |
| 2029 | *max_extent_size = |
| 2030 | max(get_max_extent_size(entry), |
| 2031 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2032 | } |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2033 | |
| 2034 | /* |
| 2035 | * The bitmap may have gotten re-arranged in the space |
| 2036 | * index here because the max_extent_size may have been |
| 2037 | * updated. Start from the beginning again if this |
| 2038 | * happened. |
| 2039 | */ |
| 2040 | if (use_bytes_index && old_next != rb_next(node)) |
| 2041 | goto again; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2042 | continue; |
| 2043 | } |
| 2044 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2045 | *offset = tmp; |
| 2046 | *bytes = entry->bytes - align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2047 | return entry; |
| 2048 | } |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2049 | out: |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2050 | return NULL; |
| 2051 | } |
| 2052 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2053 | static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2054 | struct btrfs_free_space *info, u64 offset) |
| 2055 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2056 | info->offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | f019f42 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 2057 | info->bytes = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2058 | info->bitmap_extents = 0; |
Alexandre Oliva | f2d0f67 | 2011-11-28 12:04:43 -0200 | [diff] [blame] | 2059 | INIT_LIST_HEAD(&info->list); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2060 | link_free_space(ctl, info); |
| 2061 | ctl->total_bitmaps++; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 2062 | recalculate_thresholds(ctl); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2063 | } |
| 2064 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2065 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2066 | struct btrfs_free_space *bitmap_info) |
| 2067 | { |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 2068 | /* |
| 2069 | * Normally when this is called, the bitmap is completely empty. However, |
| 2070 | * if we are blowing up the free space cache for one reason or another |
| 2071 | * via __btrfs_remove_free_space_cache(), then it may not be freed and |
| 2072 | * we may leave stats on the table. |
| 2073 | */ |
| 2074 | if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) { |
| 2075 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 2076 | bitmap_info->bitmap_extents; |
| 2077 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes; |
| 2078 | |
| 2079 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2080 | unlink_free_space(ctl, bitmap_info); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2081 | kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2082 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2083 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 2084 | recalculate_thresholds(ctl); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2085 | } |
| 2086 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2087 | static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2088 | struct btrfs_free_space *bitmap_info, |
| 2089 | u64 *offset, u64 *bytes) |
| 2090 | { |
| 2091 | u64 end; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2092 | u64 search_start, search_bytes; |
| 2093 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2094 | |
| 2095 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2096 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2097 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2098 | /* |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2099 | * We need to search for bits in this bitmap. We could only cover some |
| 2100 | * of the extent in this bitmap thanks to how we add space, so we need |
| 2101 | * to search for as much as it as we can and clear that amount, and then |
| 2102 | * go searching for the next bit. |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2103 | */ |
| 2104 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2105 | search_bytes = ctl->unit; |
Josef Bacik | 13dbc08 | 2011-02-03 02:39:52 +0000 | [diff] [blame] | 2106 | search_bytes = min(search_bytes, end - search_start + 1); |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2107 | ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes, |
| 2108 | false); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2109 | if (ret < 0 || search_start != *offset) |
| 2110 | return -EINVAL; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2111 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2112 | /* We may have found more bits than what we need */ |
| 2113 | search_bytes = min(search_bytes, *bytes); |
| 2114 | |
| 2115 | /* Cannot clear past the end of the bitmap */ |
| 2116 | search_bytes = min(search_bytes, end - search_start + 1); |
| 2117 | |
| 2118 | bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes); |
| 2119 | *offset += search_bytes; |
| 2120 | *bytes -= search_bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2121 | |
| 2122 | if (*bytes) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2123 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2124 | if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2125 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2126 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2127 | /* |
| 2128 | * no entry after this bitmap, but we still have bytes to |
| 2129 | * remove, so something has gone wrong. |
| 2130 | */ |
| 2131 | if (!next) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2132 | return -EINVAL; |
| 2133 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2134 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
| 2135 | offset_index); |
| 2136 | |
| 2137 | /* |
| 2138 | * if the next entry isn't a bitmap we need to return to let the |
| 2139 | * extent stuff do its work. |
| 2140 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2141 | if (!bitmap_info->bitmap) |
| 2142 | return -EAGAIN; |
| 2143 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2144 | /* |
| 2145 | * Ok the next item is a bitmap, but it may not actually hold |
| 2146 | * the information for the rest of this free space stuff, so |
| 2147 | * look for it, and if we don't find it return so we can try |
| 2148 | * everything over again. |
| 2149 | */ |
| 2150 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2151 | search_bytes = ctl->unit; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2152 | ret = search_bitmap(ctl, bitmap_info, &search_start, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2153 | &search_bytes, false); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2154 | if (ret < 0 || search_start != *offset) |
| 2155 | return -EAGAIN; |
| 2156 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2157 | goto again; |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2158 | } else if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2159 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2164 | static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2165 | struct btrfs_free_space *info, u64 offset, |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2166 | u64 bytes, enum btrfs_trim_state trim_state) |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2167 | { |
| 2168 | u64 bytes_to_set = 0; |
| 2169 | u64 end; |
| 2170 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2171 | /* |
| 2172 | * This is a tradeoff to make bitmap trim state minimal. We mark the |
| 2173 | * whole bitmap untrimmed if at any point we add untrimmed regions. |
| 2174 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2175 | if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2176 | if (btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2177 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 2178 | info->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2179 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 2180 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2181 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2182 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2183 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2184 | end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); |
| 2185 | |
| 2186 | bytes_to_set = min(end - offset, bytes); |
| 2187 | |
| 2188 | bitmap_set_bits(ctl, info, offset, bytes_to_set); |
| 2189 | |
| 2190 | return bytes_to_set; |
| 2191 | |
| 2192 | } |
| 2193 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2194 | static bool use_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2195 | struct btrfs_free_space *info) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2196 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2197 | struct btrfs_block_group *block_group = ctl->private; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2198 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2199 | bool forced = false; |
| 2200 | |
| 2201 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2202 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2203 | forced = true; |
| 2204 | #endif |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2205 | |
Dennis Zhou | 5d90c5c | 2020-01-02 16:26:43 -0500 | [diff] [blame] | 2206 | /* This is a way to reclaim large regions from the bitmaps. */ |
| 2207 | if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD) |
| 2208 | return false; |
| 2209 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2210 | /* |
| 2211 | * If we are below the extents threshold then we can add this as an |
| 2212 | * extent, and don't have to deal with the bitmap |
| 2213 | */ |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2214 | if (!forced && ctl->free_extents < ctl->extents_thresh) { |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2215 | /* |
| 2216 | * If this block group has some small extents we don't want to |
| 2217 | * use up all of our free slots in the cache with them, we want |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2218 | * to reserve them to larger extents, however if we have plenty |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2219 | * of cache left then go ahead an dadd them, no sense in adding |
| 2220 | * the overhead of a bitmap if we don't have to. |
| 2221 | */ |
Dennis Zhou | f9bb615 | 2020-01-02 16:26:44 -0500 | [diff] [blame] | 2222 | if (info->bytes <= fs_info->sectorsize * 8) { |
| 2223 | if (ctl->free_extents * 3 <= ctl->extents_thresh) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2224 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2225 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2226 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2227 | } |
| 2228 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2229 | |
| 2230 | /* |
Josef Bacik | dde5740 | 2013-02-12 14:07:51 -0500 | [diff] [blame] | 2231 | * The original block groups from mkfs can be really small, like 8 |
| 2232 | * megabytes, so don't bother with a bitmap for those entries. However |
| 2233 | * some block groups can be smaller than what a bitmap would cover but |
| 2234 | * are still large enough that they could overflow the 32k memory limit, |
| 2235 | * so allow those block groups to still be allowed to have a bitmap |
| 2236 | * entry. |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2237 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2238 | if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2239 | return false; |
| 2240 | |
| 2241 | return true; |
| 2242 | } |
| 2243 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 2244 | static const struct btrfs_free_space_op free_space_op = { |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2245 | .use_bitmap = use_bitmap, |
| 2246 | }; |
| 2247 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2248 | static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2249 | struct btrfs_free_space *info) |
| 2250 | { |
| 2251 | struct btrfs_free_space *bitmap_info; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2252 | struct btrfs_block_group *block_group = NULL; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2253 | int added = 0; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2254 | u64 bytes, offset, bytes_added; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2255 | enum btrfs_trim_state trim_state; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2256 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2257 | |
| 2258 | bytes = info->bytes; |
| 2259 | offset = info->offset; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2260 | trim_state = info->trim_state; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2261 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2262 | if (!ctl->op->use_bitmap(ctl, info)) |
| 2263 | return 0; |
| 2264 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2265 | if (ctl->op == &free_space_op) |
| 2266 | block_group = ctl->private; |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2267 | again: |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2268 | /* |
| 2269 | * Since we link bitmaps right into the cluster we need to see if we |
| 2270 | * have a cluster here, and if so and it has our bitmap we need to add |
| 2271 | * the free space to that bitmap. |
| 2272 | */ |
| 2273 | if (block_group && !list_empty(&block_group->cluster_list)) { |
| 2274 | struct btrfs_free_cluster *cluster; |
| 2275 | struct rb_node *node; |
| 2276 | struct btrfs_free_space *entry; |
| 2277 | |
| 2278 | cluster = list_entry(block_group->cluster_list.next, |
| 2279 | struct btrfs_free_cluster, |
| 2280 | block_group_list); |
| 2281 | spin_lock(&cluster->lock); |
| 2282 | node = rb_first(&cluster->root); |
| 2283 | if (!node) { |
| 2284 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2285 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2289 | if (!entry->bitmap) { |
| 2290 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2291 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | if (entry->offset == offset_to_bitmap(ctl, offset)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2295 | bytes_added = add_bytes_to_bitmap(ctl, entry, offset, |
| 2296 | bytes, trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2297 | bytes -= bytes_added; |
| 2298 | offset += bytes_added; |
| 2299 | } |
| 2300 | spin_unlock(&cluster->lock); |
| 2301 | if (!bytes) { |
| 2302 | ret = 1; |
| 2303 | goto out; |
| 2304 | } |
| 2305 | } |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2306 | |
| 2307 | no_cluster_bitmap: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2308 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2309 | 1, 0); |
| 2310 | if (!bitmap_info) { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2311 | ASSERT(added == 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2312 | goto new_bitmap; |
| 2313 | } |
| 2314 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2315 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 2316 | trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2317 | bytes -= bytes_added; |
| 2318 | offset += bytes_added; |
| 2319 | added = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2320 | |
| 2321 | if (!bytes) { |
| 2322 | ret = 1; |
| 2323 | goto out; |
| 2324 | } else |
| 2325 | goto again; |
| 2326 | |
| 2327 | new_bitmap: |
| 2328 | if (info && info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2329 | add_new_bitmap(ctl, info, offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2330 | added = 1; |
| 2331 | info = NULL; |
| 2332 | goto again; |
| 2333 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2334 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2335 | |
| 2336 | /* no pre-allocated info, allocate a new one */ |
| 2337 | if (!info) { |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2338 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 2339 | GFP_NOFS); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2340 | if (!info) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2341 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2342 | ret = -ENOMEM; |
| 2343 | goto out; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | /* allocate the bitmap */ |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2348 | info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, |
| 2349 | GFP_NOFS); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2350 | info->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2351 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2352 | if (!info->bitmap) { |
| 2353 | ret = -ENOMEM; |
| 2354 | goto out; |
| 2355 | } |
| 2356 | goto again; |
| 2357 | } |
| 2358 | |
| 2359 | out: |
| 2360 | if (info) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2361 | if (info->bitmap) |
| 2362 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 2363 | info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2364 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2365 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2366 | |
| 2367 | return ret; |
| 2368 | } |
| 2369 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2370 | /* |
| 2371 | * Free space merging rules: |
| 2372 | * 1) Merge trimmed areas together |
| 2373 | * 2) Let untrimmed areas coalesce with trimmed areas |
| 2374 | * 3) Always pull neighboring regions from bitmaps |
| 2375 | * |
| 2376 | * The above rules are for when we merge free space based on btrfs_trim_state. |
| 2377 | * Rules 2 and 3 are subtle because they are suboptimal, but are done for the |
| 2378 | * same reason: to promote larger extent regions which makes life easier for |
| 2379 | * find_free_extent(). Rule 2 enables coalescing based on the common path |
| 2380 | * being returning free space from btrfs_finish_extent_commit(). So when free |
| 2381 | * space is trimmed, it will prevent aggregating trimmed new region and |
| 2382 | * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents |
| 2383 | * and provide find_free_extent() with the largest extents possible hoping for |
| 2384 | * the reuse path. |
| 2385 | */ |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2386 | static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2387 | struct btrfs_free_space *info, bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2388 | { |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2389 | struct btrfs_free_space *left_info = NULL; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2390 | struct btrfs_free_space *right_info; |
| 2391 | bool merged = false; |
| 2392 | u64 offset = info->offset; |
| 2393 | u64 bytes = info->bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2394 | const bool is_trimmed = btrfs_free_space_trimmed(info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2395 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2396 | /* |
| 2397 | * first we want to see if there is free space adjacent to the range we |
| 2398 | * are adding, if there is remove that struct and add a new one to |
| 2399 | * cover the entire range |
| 2400 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2401 | right_info = tree_search_offset(ctl, offset + bytes, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2402 | if (right_info && rb_prev(&right_info->offset_index)) |
| 2403 | left_info = rb_entry(rb_prev(&right_info->offset_index), |
| 2404 | struct btrfs_free_space, offset_index); |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2405 | else if (!right_info) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2406 | left_info = tree_search_offset(ctl, offset - 1, 0, 0); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2407 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2408 | /* See try_merge_free_space() comment. */ |
| 2409 | if (right_info && !right_info->bitmap && |
| 2410 | (!is_trimmed || btrfs_free_space_trimmed(right_info))) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2411 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2412 | unlink_free_space(ctl, right_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2413 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2414 | __unlink_free_space(ctl, right_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2415 | info->bytes += right_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2416 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2417 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2418 | } |
| 2419 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2420 | /* See try_merge_free_space() comment. */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2421 | if (left_info && !left_info->bitmap && |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2422 | left_info->offset + left_info->bytes == offset && |
| 2423 | (!is_trimmed || btrfs_free_space_trimmed(left_info))) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2424 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2425 | unlink_free_space(ctl, left_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2426 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2427 | __unlink_free_space(ctl, left_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2428 | info->offset = left_info->offset; |
| 2429 | info->bytes += left_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2430 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2431 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2432 | } |
| 2433 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2434 | return merged; |
| 2435 | } |
| 2436 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2437 | static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl, |
| 2438 | struct btrfs_free_space *info, |
| 2439 | bool update_stat) |
| 2440 | { |
| 2441 | struct btrfs_free_space *bitmap; |
| 2442 | unsigned long i; |
| 2443 | unsigned long j; |
| 2444 | const u64 end = info->offset + info->bytes; |
| 2445 | const u64 bitmap_offset = offset_to_bitmap(ctl, end); |
| 2446 | u64 bytes; |
| 2447 | |
| 2448 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2449 | if (!bitmap) |
| 2450 | return false; |
| 2451 | |
| 2452 | i = offset_to_bit(bitmap->offset, ctl->unit, end); |
| 2453 | j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i); |
| 2454 | if (j == i) |
| 2455 | return false; |
| 2456 | bytes = (j - i) * ctl->unit; |
| 2457 | info->bytes += bytes; |
| 2458 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2459 | /* See try_merge_free_space() comment. */ |
| 2460 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2461 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2462 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2463 | if (update_stat) |
| 2464 | bitmap_clear_bits(ctl, bitmap, end, bytes); |
| 2465 | else |
| 2466 | __bitmap_clear_bits(ctl, bitmap, end, bytes); |
| 2467 | |
| 2468 | if (!bitmap->bytes) |
| 2469 | free_bitmap(ctl, bitmap); |
| 2470 | |
| 2471 | return true; |
| 2472 | } |
| 2473 | |
| 2474 | static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl, |
| 2475 | struct btrfs_free_space *info, |
| 2476 | bool update_stat) |
| 2477 | { |
| 2478 | struct btrfs_free_space *bitmap; |
| 2479 | u64 bitmap_offset; |
| 2480 | unsigned long i; |
| 2481 | unsigned long j; |
| 2482 | unsigned long prev_j; |
| 2483 | u64 bytes; |
| 2484 | |
| 2485 | bitmap_offset = offset_to_bitmap(ctl, info->offset); |
| 2486 | /* If we're on a boundary, try the previous logical bitmap. */ |
| 2487 | if (bitmap_offset == info->offset) { |
| 2488 | if (info->offset == 0) |
| 2489 | return false; |
| 2490 | bitmap_offset = offset_to_bitmap(ctl, info->offset - 1); |
| 2491 | } |
| 2492 | |
| 2493 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2494 | if (!bitmap) |
| 2495 | return false; |
| 2496 | |
| 2497 | i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1; |
| 2498 | j = 0; |
| 2499 | prev_j = (unsigned long)-1; |
| 2500 | for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) { |
| 2501 | if (j > i) |
| 2502 | break; |
| 2503 | prev_j = j; |
| 2504 | } |
| 2505 | if (prev_j == i) |
| 2506 | return false; |
| 2507 | |
| 2508 | if (prev_j == (unsigned long)-1) |
| 2509 | bytes = (i + 1) * ctl->unit; |
| 2510 | else |
| 2511 | bytes = (i - prev_j) * ctl->unit; |
| 2512 | |
| 2513 | info->offset -= bytes; |
| 2514 | info->bytes += bytes; |
| 2515 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2516 | /* See try_merge_free_space() comment. */ |
| 2517 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2518 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2519 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2520 | if (update_stat) |
| 2521 | bitmap_clear_bits(ctl, bitmap, info->offset, bytes); |
| 2522 | else |
| 2523 | __bitmap_clear_bits(ctl, bitmap, info->offset, bytes); |
| 2524 | |
| 2525 | if (!bitmap->bytes) |
| 2526 | free_bitmap(ctl, bitmap); |
| 2527 | |
| 2528 | return true; |
| 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | * We prefer always to allocate from extent entries, both for clustered and |
| 2533 | * non-clustered allocation requests. So when attempting to add a new extent |
| 2534 | * entry, try to see if there's adjacent free space in bitmap entries, and if |
| 2535 | * there is, migrate that space from the bitmaps to the extent. |
| 2536 | * Like this we get better chances of satisfying space allocation requests |
| 2537 | * because we attempt to satisfy them based on a single cache entry, and never |
| 2538 | * on 2 or more entries - even if the entries represent a contiguous free space |
| 2539 | * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry |
| 2540 | * ends). |
| 2541 | */ |
| 2542 | static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2543 | struct btrfs_free_space *info, |
| 2544 | bool update_stat) |
| 2545 | { |
| 2546 | /* |
| 2547 | * Only work with disconnected entries, as we can change their offset, |
| 2548 | * and must be extent entries. |
| 2549 | */ |
| 2550 | ASSERT(!info->bitmap); |
| 2551 | ASSERT(RB_EMPTY_NODE(&info->offset_index)); |
| 2552 | |
| 2553 | if (ctl->total_bitmaps > 0) { |
| 2554 | bool stole_end; |
| 2555 | bool stole_front = false; |
| 2556 | |
| 2557 | stole_end = steal_from_bitmap_to_end(ctl, info, update_stat); |
| 2558 | if (ctl->total_bitmaps > 0) |
| 2559 | stole_front = steal_from_bitmap_to_front(ctl, info, |
| 2560 | update_stat); |
| 2561 | |
| 2562 | if (stole_end || stole_front) |
| 2563 | try_merge_free_space(ctl, info, update_stat); |
| 2564 | } |
| 2565 | } |
| 2566 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2567 | int __btrfs_add_free_space(struct btrfs_fs_info *fs_info, |
| 2568 | struct btrfs_free_space_ctl *ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2569 | u64 offset, u64 bytes, |
| 2570 | enum btrfs_trim_state trim_state) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2571 | { |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2572 | struct btrfs_block_group *block_group = ctl->private; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2573 | struct btrfs_free_space *info; |
| 2574 | int ret = 0; |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2575 | u64 filter_bytes = bytes; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2576 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2577 | ASSERT(!btrfs_is_zoned(fs_info)); |
| 2578 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2579 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2580 | if (!info) |
| 2581 | return -ENOMEM; |
| 2582 | |
| 2583 | info->offset = offset; |
| 2584 | info->bytes = bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2585 | info->trim_state = trim_state; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2586 | RB_CLEAR_NODE(&info->offset_index); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2587 | RB_CLEAR_NODE(&info->bytes_index); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2588 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2589 | spin_lock(&ctl->tree_lock); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2590 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2591 | if (try_merge_free_space(ctl, info, true)) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2592 | goto link; |
| 2593 | |
| 2594 | /* |
| 2595 | * There was no extent directly to the left or right of this new |
| 2596 | * extent then we know we're going to have to allocate a new extent, so |
| 2597 | * before we do that see if we need to drop this into a bitmap |
| 2598 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2599 | ret = insert_into_bitmap(ctl, info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2600 | if (ret < 0) { |
| 2601 | goto out; |
| 2602 | } else if (ret) { |
| 2603 | ret = 0; |
| 2604 | goto out; |
| 2605 | } |
| 2606 | link: |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2607 | /* |
| 2608 | * Only steal free space from adjacent bitmaps if we're sure we're not |
| 2609 | * going to add the new free space to existing bitmap entries - because |
| 2610 | * that would mean unnecessary work that would be reverted. Therefore |
| 2611 | * attempt to steal space from bitmaps if we're adding an extent entry. |
| 2612 | */ |
| 2613 | steal_from_bitmap(ctl, info, true); |
| 2614 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2615 | filter_bytes = max(filter_bytes, info->bytes); |
| 2616 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2617 | ret = link_free_space(ctl, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2618 | if (ret) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2619 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2620 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2621 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2622 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2623 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2624 | if (ret) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2625 | btrfs_crit(fs_info, "unable to add free space :%d", ret); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2626 | ASSERT(ret != -EEXIST); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2627 | } |
| 2628 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2629 | if (trim_state != BTRFS_TRIM_STATE_TRIMMED) { |
| 2630 | btrfs_discard_check_filter(block_group, filter_bytes); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2631 | btrfs_discard_queue_work(&fs_info->discard_ctl, block_group); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2632 | } |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2633 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2634 | return ret; |
| 2635 | } |
| 2636 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2637 | static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, |
| 2638 | u64 bytenr, u64 size, bool used) |
| 2639 | { |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2640 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2641 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 2642 | u64 offset = bytenr - block_group->start; |
| 2643 | u64 to_free, to_unusable; |
Johannes Thumshirn | 77233c2 | 2021-08-09 20:41:17 +0900 | [diff] [blame] | 2644 | const int bg_reclaim_threshold = READ_ONCE(fs_info->bg_reclaim_threshold); |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2645 | bool initial = (size == block_group->length); |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2646 | u64 reclaimable_unusable; |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2647 | |
| 2648 | WARN_ON(!initial && offset + size > block_group->zone_capacity); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2649 | |
| 2650 | spin_lock(&ctl->tree_lock); |
| 2651 | if (!used) |
| 2652 | to_free = size; |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2653 | else if (initial) |
| 2654 | to_free = block_group->zone_capacity; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2655 | else if (offset >= block_group->alloc_offset) |
| 2656 | to_free = size; |
| 2657 | else if (offset + size <= block_group->alloc_offset) |
| 2658 | to_free = 0; |
| 2659 | else |
| 2660 | to_free = offset + size - block_group->alloc_offset; |
| 2661 | to_unusable = size - to_free; |
| 2662 | |
| 2663 | ctl->free_space += to_free; |
Naohiro Aota | badae9c | 2021-03-03 17:55:48 +0900 | [diff] [blame] | 2664 | /* |
| 2665 | * If the block group is read-only, we should account freed space into |
| 2666 | * bytes_readonly. |
| 2667 | */ |
| 2668 | if (!block_group->ro) |
| 2669 | block_group->zone_unusable += to_unusable; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2670 | spin_unlock(&ctl->tree_lock); |
| 2671 | if (!used) { |
| 2672 | spin_lock(&block_group->lock); |
| 2673 | block_group->alloc_offset -= size; |
| 2674 | spin_unlock(&block_group->lock); |
| 2675 | } |
| 2676 | |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2677 | reclaimable_unusable = block_group->zone_unusable - |
| 2678 | (block_group->length - block_group->zone_capacity); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2679 | /* All the region is now unusable. Mark it as unused and reclaim */ |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2680 | if (block_group->zone_unusable == block_group->length) { |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2681 | btrfs_mark_bg_unused(block_group); |
Johannes Thumshirn | 77233c2 | 2021-08-09 20:41:17 +0900 | [diff] [blame] | 2682 | } else if (bg_reclaim_threshold && |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2683 | reclaimable_unusable >= |
| 2684 | div_factor_fine(block_group->zone_capacity, |
| 2685 | bg_reclaim_threshold)) { |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2686 | btrfs_mark_bg_to_reclaim(block_group); |
| 2687 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2688 | |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2692 | int btrfs_add_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2693 | u64 bytenr, u64 size) |
| 2694 | { |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2695 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2696 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2697 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2698 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2699 | true); |
| 2700 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2701 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC)) |
| 2702 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2703 | |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2704 | return __btrfs_add_free_space(block_group->fs_info, |
| 2705 | block_group->free_space_ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2706 | bytenr, size, trim_state); |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2707 | } |
| 2708 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2709 | int btrfs_add_free_space_unused(struct btrfs_block_group *block_group, |
| 2710 | u64 bytenr, u64 size) |
| 2711 | { |
| 2712 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2713 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2714 | false); |
| 2715 | |
| 2716 | return btrfs_add_free_space(block_group, bytenr, size); |
| 2717 | } |
| 2718 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2719 | /* |
| 2720 | * This is a subtle distinction because when adding free space back in general, |
| 2721 | * we want it to be added as untrimmed for async. But in the case where we add |
| 2722 | * it on loading of a block group, we want to consider it trimmed. |
| 2723 | */ |
| 2724 | int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group, |
| 2725 | u64 bytenr, u64 size) |
| 2726 | { |
| 2727 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2728 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2729 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2730 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2731 | true); |
| 2732 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2733 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) || |
| 2734 | btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC)) |
| 2735 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2736 | |
| 2737 | return __btrfs_add_free_space(block_group->fs_info, |
| 2738 | block_group->free_space_ctl, |
| 2739 | bytenr, size, trim_state); |
| 2740 | } |
| 2741 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2742 | int btrfs_remove_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2743 | u64 offset, u64 bytes) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2744 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2745 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2746 | struct btrfs_free_space *info; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2747 | int ret; |
| 2748 | bool re_search = false; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2749 | |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2750 | if (btrfs_is_zoned(block_group->fs_info)) { |
| 2751 | /* |
| 2752 | * This can happen with conventional zones when replaying log. |
| 2753 | * Since the allocation info of tree-log nodes are not recorded |
| 2754 | * to the extent-tree, calculate_alloc_pointer() failed to |
| 2755 | * advance the allocation pointer after last allocated tree log |
| 2756 | * node blocks. |
| 2757 | * |
| 2758 | * This function is called from |
| 2759 | * btrfs_pin_extent_for_log_replay() when replaying the log. |
| 2760 | * Advance the pointer not to overwrite the tree-log nodes. |
| 2761 | */ |
Naohiro Aota | 0ae79c6 | 2021-08-09 13:13:44 +0900 | [diff] [blame] | 2762 | if (block_group->start + block_group->alloc_offset < |
| 2763 | offset + bytes) { |
| 2764 | block_group->alloc_offset = |
| 2765 | offset + bytes - block_group->start; |
| 2766 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2767 | return 0; |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2768 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2769 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2770 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2771 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2772 | again: |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2773 | ret = 0; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2774 | if (!bytes) |
| 2775 | goto out_lock; |
| 2776 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2777 | info = tree_search_offset(ctl, offset, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2778 | if (!info) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2779 | /* |
| 2780 | * oops didn't find an extent that matched the space we wanted |
| 2781 | * to remove, look for a bitmap instead |
| 2782 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2783 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2784 | 1, 0); |
| 2785 | if (!info) { |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2786 | /* |
| 2787 | * If we found a partial bit of our free space in a |
| 2788 | * bitmap but then couldn't find the other part this may |
| 2789 | * be a problem, so WARN about it. |
Chris Mason | 24a7031 | 2011-11-21 09:39:11 -0500 | [diff] [blame] | 2790 | */ |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2791 | WARN_ON(re_search); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2792 | goto out_lock; |
| 2793 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2794 | } |
| 2795 | |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2796 | re_search = false; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2797 | if (!info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2798 | unlink_free_space(ctl, info); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2799 | if (offset == info->offset) { |
| 2800 | u64 to_free = min(bytes, info->bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2801 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2802 | info->bytes -= to_free; |
| 2803 | info->offset += to_free; |
| 2804 | if (info->bytes) { |
| 2805 | ret = link_free_space(ctl, info); |
| 2806 | WARN_ON(ret); |
| 2807 | } else { |
| 2808 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2809 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2810 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2811 | offset += to_free; |
| 2812 | bytes -= to_free; |
| 2813 | goto again; |
| 2814 | } else { |
| 2815 | u64 old_end = info->bytes + info->offset; |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 2816 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2817 | info->bytes = offset - info->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2818 | ret = link_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2819 | WARN_ON(ret); |
| 2820 | if (ret) |
| 2821 | goto out_lock; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2822 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2823 | /* Not enough bytes in this entry to satisfy us */ |
| 2824 | if (old_end < offset + bytes) { |
| 2825 | bytes -= old_end - offset; |
| 2826 | offset = old_end; |
| 2827 | goto again; |
| 2828 | } else if (old_end == offset + bytes) { |
| 2829 | /* all done */ |
| 2830 | goto out_lock; |
| 2831 | } |
| 2832 | spin_unlock(&ctl->tree_lock); |
| 2833 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2834 | ret = __btrfs_add_free_space(block_group->fs_info, ctl, |
| 2835 | offset + bytes, |
| 2836 | old_end - (offset + bytes), |
| 2837 | info->trim_state); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2838 | WARN_ON(ret); |
| 2839 | goto out; |
| 2840 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2841 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2842 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2843 | ret = remove_from_bitmap(ctl, info, &offset, &bytes); |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2844 | if (ret == -EAGAIN) { |
| 2845 | re_search = true; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2846 | goto again; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2847 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2848 | out_lock: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2849 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2850 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2851 | out: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2852 | return ret; |
| 2853 | } |
| 2854 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2855 | void btrfs_dump_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2856 | u64 bytes) |
| 2857 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2858 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2859 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2860 | struct btrfs_free_space *info; |
| 2861 | struct rb_node *n; |
| 2862 | int count = 0; |
| 2863 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2864 | /* |
| 2865 | * Zoned btrfs does not use free space tree and cluster. Just print |
| 2866 | * out the free space after the allocation offset. |
| 2867 | */ |
| 2868 | if (btrfs_is_zoned(fs_info)) { |
Naohiro Aota | afba2bc | 2021-08-19 21:19:17 +0900 | [diff] [blame] | 2869 | btrfs_info(fs_info, "free space %llu active %d", |
| 2870 | block_group->zone_capacity - block_group->alloc_offset, |
| 2871 | block_group->zone_is_active); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2872 | return; |
| 2873 | } |
| 2874 | |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2875 | spin_lock(&ctl->tree_lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2876 | for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2877 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
Liu Bo | f6175ef | 2012-07-06 03:31:36 -0600 | [diff] [blame] | 2878 | if (info->bytes >= bytes && !block_group->ro) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2879 | count++; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2880 | btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2881 | info->offset, info->bytes, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2882 | (info->bitmap) ? "yes" : "no"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2883 | } |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2884 | spin_unlock(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2885 | btrfs_info(fs_info, "block group has cluster?: %s", |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2886 | list_empty(&block_group->cluster_list) ? "no" : "yes"); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2887 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2888 | "%d blocks of free space at or bigger than bytes is", count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2889 | } |
| 2890 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 2891 | void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group, |
| 2892 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2893 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2894 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2895 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2896 | spin_lock_init(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2897 | ctl->unit = fs_info->sectorsize; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2898 | ctl->start = block_group->start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2899 | ctl->private = block_group; |
| 2900 | ctl->op = &free_space_op; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2901 | ctl->free_space_bytes = RB_ROOT_CACHED; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 2902 | INIT_LIST_HEAD(&ctl->trimming_ranges); |
| 2903 | mutex_init(&ctl->cache_writeout_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2904 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2905 | /* |
| 2906 | * we only want to have 32k of ram per block group for keeping |
| 2907 | * track of free space, and if we pass 1/2 of that we want to |
| 2908 | * start converting things over to using bitmaps |
| 2909 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2910 | ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2911 | } |
| 2912 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2913 | /* |
| 2914 | * for a given cluster, put all of its extents back into the free |
| 2915 | * space cache. If the block group passed doesn't match the block group |
| 2916 | * pointed to by the cluster, someone else raced in and freed the |
| 2917 | * cluster already. In that case, we just return without changing anything |
| 2918 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 2919 | static void __btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2920 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2921 | struct btrfs_free_cluster *cluster) |
| 2922 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2923 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2924 | struct btrfs_free_space *entry; |
| 2925 | struct rb_node *node; |
| 2926 | |
| 2927 | spin_lock(&cluster->lock); |
Josef Bacik | 95c85fb | 2021-01-25 16:42:35 -0500 | [diff] [blame] | 2928 | if (cluster->block_group != block_group) { |
| 2929 | spin_unlock(&cluster->lock); |
| 2930 | return; |
| 2931 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2932 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2933 | cluster->block_group = NULL; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2934 | cluster->window_start = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2935 | list_del_init(&cluster->block_group_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2936 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2937 | node = rb_first(&cluster->root); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2938 | while (node) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2939 | bool bitmap; |
| 2940 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2941 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2942 | node = rb_next(&entry->offset_index); |
| 2943 | rb_erase(&entry->offset_index, &cluster->root); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2944 | RB_CLEAR_NODE(&entry->offset_index); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2945 | |
| 2946 | bitmap = (entry->bitmap != NULL); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2947 | if (!bitmap) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2948 | /* Merging treats extents as if they were new */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2949 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2950 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2951 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= |
| 2952 | entry->bytes; |
| 2953 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2954 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2955 | try_merge_free_space(ctl, entry, false); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2956 | steal_from_bitmap(ctl, entry, false); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2957 | |
| 2958 | /* As we insert directly, update these statistics */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2959 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2960 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2961 | ctl->discardable_bytes[BTRFS_STAT_CURR] += |
| 2962 | entry->bytes; |
| 2963 | } |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2964 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2965 | tree_insert_offset(&ctl->free_space_offset, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2966 | entry->offset, &entry->offset_index, bitmap); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2967 | rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes, |
| 2968 | entry_less); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2969 | } |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 2970 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2971 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2972 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2973 | } |
| 2974 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2975 | static void __btrfs_remove_free_space_cache_locked( |
| 2976 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2977 | { |
| 2978 | struct btrfs_free_space *info; |
| 2979 | struct rb_node *node; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2980 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2981 | while ((node = rb_last(&ctl->free_space_offset)) != NULL) { |
| 2982 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
Josef Bacik | 9b90f51 | 2011-06-24 16:02:51 +0000 | [diff] [blame] | 2983 | if (!info->bitmap) { |
| 2984 | unlink_free_space(ctl, info); |
| 2985 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2986 | } else { |
| 2987 | free_bitmap(ctl, info); |
| 2988 | } |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 2989 | |
| 2990 | cond_resched_lock(&ctl->tree_lock); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2991 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) |
| 2995 | { |
| 2996 | spin_lock(&ctl->tree_lock); |
| 2997 | __btrfs_remove_free_space_cache_locked(ctl); |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 2998 | if (ctl->private) |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2999 | btrfs_discard_update_discardable(ctl->private); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3000 | spin_unlock(&ctl->tree_lock); |
| 3001 | } |
| 3002 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3003 | void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3004 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3005 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3006 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3007 | struct list_head *head; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3008 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3009 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3010 | while ((head = block_group->cluster_list.next) != |
| 3011 | &block_group->cluster_list) { |
| 3012 | cluster = list_entry(head, struct btrfs_free_cluster, |
| 3013 | block_group_list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3014 | |
| 3015 | WARN_ON(cluster->block_group != block_group); |
| 3016 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 3017 | |
| 3018 | cond_resched_lock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3019 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 3020 | __btrfs_remove_free_space_cache_locked(ctl); |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 3021 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3022 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3023 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3024 | } |
| 3025 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 3026 | /** |
| 3027 | * btrfs_is_free_space_trimmed - see if everything is trimmed |
| 3028 | * @block_group: block_group of interest |
| 3029 | * |
| 3030 | * Walk @block_group's free space rb_tree to determine if everything is trimmed. |
| 3031 | */ |
| 3032 | bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group) |
| 3033 | { |
| 3034 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 3035 | struct btrfs_free_space *info; |
| 3036 | struct rb_node *node; |
| 3037 | bool ret = true; |
| 3038 | |
| 3039 | spin_lock(&ctl->tree_lock); |
| 3040 | node = rb_first(&ctl->free_space_offset); |
| 3041 | |
| 3042 | while (node) { |
| 3043 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3044 | |
| 3045 | if (!btrfs_free_space_trimmed(info)) { |
| 3046 | ret = false; |
| 3047 | break; |
| 3048 | } |
| 3049 | |
| 3050 | node = rb_next(node); |
| 3051 | } |
| 3052 | |
| 3053 | spin_unlock(&ctl->tree_lock); |
| 3054 | return ret; |
| 3055 | } |
| 3056 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3057 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3058 | u64 offset, u64 bytes, u64 empty_size, |
| 3059 | u64 *max_extent_size) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3060 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3061 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3062 | struct btrfs_discard_ctl *discard_ctl = |
| 3063 | &block_group->fs_info->discard_ctl; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3064 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3065 | u64 bytes_search = bytes + empty_size; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3066 | u64 ret = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3067 | u64 align_gap = 0; |
| 3068 | u64 align_gap_len = 0; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3069 | enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3070 | bool use_bytes_index = (offset == block_group->start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3071 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3072 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3073 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3074 | spin_lock(&ctl->tree_lock); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3075 | entry = find_free_space(ctl, &offset, &bytes_search, |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3076 | block_group->full_stripe_len, max_extent_size, |
| 3077 | use_bytes_index); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3078 | if (!entry) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3079 | goto out; |
| 3080 | |
| 3081 | ret = offset; |
| 3082 | if (entry->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3083 | bitmap_clear_bits(ctl, entry, offset, bytes); |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3084 | |
| 3085 | if (!btrfs_free_space_trimmed(entry)) |
| 3086 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3087 | |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 3088 | if (!entry->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3089 | free_bitmap(ctl, entry); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3090 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3091 | unlink_free_space(ctl, entry); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3092 | align_gap_len = offset - entry->offset; |
| 3093 | align_gap = entry->offset; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3094 | align_gap_trim_state = entry->trim_state; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3095 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3096 | if (!btrfs_free_space_trimmed(entry)) |
| 3097 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3098 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3099 | entry->offset = offset + bytes; |
| 3100 | WARN_ON(entry->bytes < bytes + align_gap_len); |
| 3101 | |
| 3102 | entry->bytes -= bytes + align_gap_len; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3103 | if (!entry->bytes) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 3104 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3105 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3106 | link_free_space(ctl, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3107 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3108 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 3109 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3110 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3111 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3112 | if (align_gap_len) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 3113 | __btrfs_add_free_space(block_group->fs_info, ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3114 | align_gap, align_gap_len, |
| 3115 | align_gap_trim_state); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3116 | return ret; |
| 3117 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3118 | |
| 3119 | /* |
| 3120 | * given a cluster, put all of its extents back into the free space |
| 3121 | * cache. If a block group is passed, this function will only free |
| 3122 | * a cluster that belongs to the passed block group. |
| 3123 | * |
| 3124 | * Otherwise, it'll get a reference on the block group pointed to by the |
| 3125 | * cluster and remove the cluster from it. |
| 3126 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3127 | void btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3128 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3129 | struct btrfs_free_cluster *cluster) |
| 3130 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3131 | struct btrfs_free_space_ctl *ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3132 | |
| 3133 | /* first, get a safe pointer to the block group */ |
| 3134 | spin_lock(&cluster->lock); |
| 3135 | if (!block_group) { |
| 3136 | block_group = cluster->block_group; |
| 3137 | if (!block_group) { |
| 3138 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3139 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3140 | } |
| 3141 | } else if (cluster->block_group != block_group) { |
| 3142 | /* someone else has already freed it don't redo their work */ |
| 3143 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3144 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3145 | } |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3146 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3147 | spin_unlock(&cluster->lock); |
| 3148 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3149 | ctl = block_group->free_space_ctl; |
| 3150 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3151 | /* now return any extents the cluster had on it */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3152 | spin_lock(&ctl->tree_lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3153 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3154 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3155 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 3156 | btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group); |
| 3157 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3158 | /* finally drop our ref */ |
| 3159 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3160 | } |
| 3161 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3162 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3163 | struct btrfs_free_cluster *cluster, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3164 | struct btrfs_free_space *entry, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3165 | u64 bytes, u64 min_start, |
| 3166 | u64 *max_extent_size) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3167 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3168 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3169 | int err; |
| 3170 | u64 search_start = cluster->window_start; |
| 3171 | u64 search_bytes = bytes; |
| 3172 | u64 ret = 0; |
| 3173 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3174 | search_start = min_start; |
| 3175 | search_bytes = bytes; |
| 3176 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3177 | err = search_bitmap(ctl, entry, &search_start, &search_bytes, true); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3178 | if (err) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3179 | *max_extent_size = max(get_max_extent_size(entry), |
| 3180 | *max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3181 | return 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3182 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3183 | |
| 3184 | ret = search_start; |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 3185 | __bitmap_clear_bits(ctl, entry, ret, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3186 | |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3190 | /* |
| 3191 | * given a cluster, try to allocate 'bytes' from it, returns 0 |
| 3192 | * if it couldn't find anything suitably large, or a logical disk offset |
| 3193 | * if things worked out |
| 3194 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3195 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3196 | struct btrfs_free_cluster *cluster, u64 bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3197 | u64 min_start, u64 *max_extent_size) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3198 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3199 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3200 | struct btrfs_discard_ctl *discard_ctl = |
| 3201 | &block_group->fs_info->discard_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3202 | struct btrfs_free_space *entry = NULL; |
| 3203 | struct rb_node *node; |
| 3204 | u64 ret = 0; |
| 3205 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3206 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3207 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3208 | spin_lock(&cluster->lock); |
| 3209 | if (bytes > cluster->max_size) |
| 3210 | goto out; |
| 3211 | |
| 3212 | if (cluster->block_group != block_group) |
| 3213 | goto out; |
| 3214 | |
| 3215 | node = rb_first(&cluster->root); |
| 3216 | if (!node) |
| 3217 | goto out; |
| 3218 | |
| 3219 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 3220 | while (1) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3221 | if (entry->bytes < bytes) |
| 3222 | *max_extent_size = max(get_max_extent_size(entry), |
| 3223 | *max_extent_size); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3224 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3225 | if (entry->bytes < bytes || |
| 3226 | (!entry->bitmap && entry->offset < min_start)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3227 | node = rb_next(&entry->offset_index); |
| 3228 | if (!node) |
| 3229 | break; |
| 3230 | entry = rb_entry(node, struct btrfs_free_space, |
| 3231 | offset_index); |
| 3232 | continue; |
| 3233 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3234 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3235 | if (entry->bitmap) { |
| 3236 | ret = btrfs_alloc_from_bitmap(block_group, |
| 3237 | cluster, entry, bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3238 | cluster->window_start, |
| 3239 | max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3240 | if (ret == 0) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3241 | node = rb_next(&entry->offset_index); |
| 3242 | if (!node) |
| 3243 | break; |
| 3244 | entry = rb_entry(node, struct btrfs_free_space, |
| 3245 | offset_index); |
| 3246 | continue; |
| 3247 | } |
Josef Bacik | 9b23062 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3248 | cluster->window_start += bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3249 | } else { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3250 | ret = entry->offset; |
| 3251 | |
| 3252 | entry->offset += bytes; |
| 3253 | entry->bytes -= bytes; |
| 3254 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3255 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3256 | break; |
| 3257 | } |
| 3258 | out: |
| 3259 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3260 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3261 | if (!ret) |
| 3262 | return 0; |
| 3263 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3264 | spin_lock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3265 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3266 | if (!btrfs_free_space_trimmed(entry)) |
| 3267 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3268 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3269 | ctl->free_space -= bytes; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3270 | if (!entry->bitmap && !btrfs_free_space_trimmed(entry)) |
| 3271 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3272 | |
| 3273 | spin_lock(&cluster->lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3274 | if (entry->bytes == 0) { |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3275 | rb_erase(&entry->offset_index, &cluster->root); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3276 | ctl->free_extents--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3277 | if (entry->bitmap) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 3278 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 3279 | entry->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3280 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 3281 | recalculate_thresholds(ctl); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3282 | } else if (!btrfs_free_space_trimmed(entry)) { |
| 3283 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3284 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 3285 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3286 | } |
| 3287 | |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3288 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3289 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3290 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3291 | return ret; |
| 3292 | } |
| 3293 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3294 | static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3295 | struct btrfs_free_space *entry, |
| 3296 | struct btrfs_free_cluster *cluster, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3297 | u64 offset, u64 bytes, |
| 3298 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3299 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3300 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3301 | unsigned long next_zero; |
| 3302 | unsigned long i; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3303 | unsigned long want_bits; |
| 3304 | unsigned long min_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3305 | unsigned long found_bits; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3306 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3307 | unsigned long start = 0; |
| 3308 | unsigned long total_found = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3309 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3310 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3311 | i = offset_to_bit(entry->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3312 | max_t(u64, offset, entry->offset)); |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3313 | want_bits = bytes_to_bits(bytes, ctl->unit); |
| 3314 | min_bits = bytes_to_bits(min_bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3315 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3316 | /* |
| 3317 | * Don't bother looking for a cluster in this bitmap if it's heavily |
| 3318 | * fragmented. |
| 3319 | */ |
| 3320 | if (entry->max_extent_size && |
| 3321 | entry->max_extent_size < cont1_bytes) |
| 3322 | return -ENOSPC; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3323 | again: |
| 3324 | found_bits = 0; |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 3325 | for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3326 | next_zero = find_next_zero_bit(entry->bitmap, |
| 3327 | BITS_PER_BITMAP, i); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3328 | if (next_zero - i >= min_bits) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3329 | found_bits = next_zero - i; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3330 | if (found_bits > max_bits) |
| 3331 | max_bits = found_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3332 | break; |
| 3333 | } |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3334 | if (next_zero - i > max_bits) |
| 3335 | max_bits = next_zero - i; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3336 | i = next_zero; |
| 3337 | } |
| 3338 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3339 | if (!found_bits) { |
| 3340 | entry->max_extent_size = (u64)max_bits * ctl->unit; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3341 | return -ENOSPC; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3342 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3343 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3344 | if (!total_found) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3345 | start = i; |
Alexandre Oliva | b78d09b | 2011-11-30 13:43:00 -0500 | [diff] [blame] | 3346 | cluster->max_size = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | total_found += found_bits; |
| 3350 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3351 | if (cluster->max_size < found_bits * ctl->unit) |
| 3352 | cluster->max_size = found_bits * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3353 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3354 | if (total_found < want_bits || cluster->max_size < cont1_bytes) { |
| 3355 | i = next_zero + 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3356 | goto again; |
| 3357 | } |
| 3358 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3359 | cluster->window_start = start * ctl->unit + entry->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3360 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3361 | rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); |
| 3362 | |
| 3363 | /* |
| 3364 | * We need to know if we're currently on the normal space index when we |
| 3365 | * manipulate the bitmap so that we know we need to remove and re-insert |
| 3366 | * it into the space_index tree. Clear the bytes_index node here so the |
| 3367 | * bitmap manipulation helpers know not to mess with the space_index |
| 3368 | * until this bitmap entry is added back into the normal cache. |
| 3369 | */ |
| 3370 | RB_CLEAR_NODE(&entry->bytes_index); |
| 3371 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3372 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3373 | &entry->offset_index, 1); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3374 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3375 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3376 | trace_btrfs_setup_cluster(block_group, cluster, |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3377 | total_found * ctl->unit, 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3378 | return 0; |
| 3379 | } |
| 3380 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3381 | /* |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3382 | * This searches the block group for just extents to fill the cluster with. |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3383 | * Try to find a cluster with at least bytes total bytes, at least one |
| 3384 | * extent of cont1_bytes, and other clusters of at least min_bytes. |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3385 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3386 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3387 | setup_cluster_no_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3388 | struct btrfs_free_cluster *cluster, |
| 3389 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3390 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3391 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3392 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3393 | struct btrfs_free_space *first = NULL; |
| 3394 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3395 | struct btrfs_free_space *last; |
| 3396 | struct rb_node *node; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3397 | u64 window_free; |
| 3398 | u64 max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3399 | u64 total_size = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3400 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3401 | entry = tree_search_offset(ctl, offset, 0, 1); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3402 | if (!entry) |
| 3403 | return -ENOSPC; |
| 3404 | |
| 3405 | /* |
| 3406 | * We don't want bitmaps, so just move along until we find a normal |
| 3407 | * extent entry. |
| 3408 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3409 | while (entry->bitmap || entry->bytes < min_bytes) { |
| 3410 | if (entry->bitmap && list_empty(&entry->list)) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3411 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3412 | node = rb_next(&entry->offset_index); |
| 3413 | if (!node) |
| 3414 | return -ENOSPC; |
| 3415 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3416 | } |
| 3417 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3418 | window_free = entry->bytes; |
| 3419 | max_extent = entry->bytes; |
| 3420 | first = entry; |
| 3421 | last = entry; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3422 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3423 | for (node = rb_next(&entry->offset_index); node; |
| 3424 | node = rb_next(&entry->offset_index)) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3425 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3426 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3427 | if (entry->bitmap) { |
| 3428 | if (list_empty(&entry->list)) |
| 3429 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3430 | continue; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3431 | } |
| 3432 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3433 | if (entry->bytes < min_bytes) |
| 3434 | continue; |
| 3435 | |
| 3436 | last = entry; |
| 3437 | window_free += entry->bytes; |
| 3438 | if (entry->bytes > max_extent) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3439 | max_extent = entry->bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3440 | } |
| 3441 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3442 | if (window_free < bytes || max_extent < cont1_bytes) |
| 3443 | return -ENOSPC; |
| 3444 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3445 | cluster->window_start = first->offset; |
| 3446 | |
| 3447 | node = &first->offset_index; |
| 3448 | |
| 3449 | /* |
| 3450 | * now we've found our entries, pull them out of the free space |
| 3451 | * cache and put them into the cluster rbtree |
| 3452 | */ |
| 3453 | do { |
| 3454 | int ret; |
| 3455 | |
| 3456 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3457 | node = rb_next(&entry->offset_index); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3458 | if (entry->bitmap || entry->bytes < min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3459 | continue; |
| 3460 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3461 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3462 | rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3463 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3464 | &entry->offset_index, 0); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3465 | total_size += entry->bytes; |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3466 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3467 | } while (node && entry != last); |
| 3468 | |
| 3469 | cluster->max_size = max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3470 | trace_btrfs_setup_cluster(block_group, cluster, total_size, 0); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3471 | return 0; |
| 3472 | } |
| 3473 | |
| 3474 | /* |
| 3475 | * This specifically looks for bitmaps that may work in the cluster, we assume |
| 3476 | * that we have already failed to find extents that will work. |
| 3477 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3478 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3479 | setup_cluster_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3480 | struct btrfs_free_cluster *cluster, |
| 3481 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3482 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3483 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3484 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3485 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3486 | int ret = -ENOSPC; |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3487 | u64 bitmap_offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3488 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3489 | if (ctl->total_bitmaps == 0) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3490 | return -ENOSPC; |
| 3491 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3492 | /* |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3493 | * The bitmap that covers offset won't be in the list unless offset |
| 3494 | * is just its start offset. |
| 3495 | */ |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3496 | if (!list_empty(bitmaps)) |
| 3497 | entry = list_first_entry(bitmaps, struct btrfs_free_space, list); |
| 3498 | |
| 3499 | if (!entry || entry->offset != bitmap_offset) { |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3500 | entry = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 3501 | if (entry && list_empty(&entry->list)) |
| 3502 | list_add(&entry->list, bitmaps); |
| 3503 | } |
| 3504 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3505 | list_for_each_entry(entry, bitmaps, list) { |
Josef Bacik | 357b978 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 3506 | if (entry->bytes < bytes) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3507 | continue; |
| 3508 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3509 | bytes, cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3510 | if (!ret) |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | /* |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3515 | * The bitmaps list has all the bitmaps that record free space |
| 3516 | * starting after offset, so no more search is required. |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3517 | */ |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3518 | return -ENOSPC; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | /* |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3522 | * here we try to find a cluster of blocks in a block group. The goal |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3523 | * is to find at least bytes+empty_size. |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3524 | * We might not find them all in one contiguous area. |
| 3525 | * |
| 3526 | * returns zero and sets up cluster if things worked out, otherwise |
| 3527 | * it returns -enospc |
| 3528 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3529 | int btrfs_find_space_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3530 | struct btrfs_free_cluster *cluster, |
| 3531 | u64 offset, u64 bytes, u64 empty_size) |
| 3532 | { |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3533 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3534 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3535 | struct btrfs_free_space *entry, *tmp; |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3536 | LIST_HEAD(bitmaps); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3537 | u64 min_bytes; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3538 | u64 cont1_bytes; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3539 | int ret; |
| 3540 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3541 | /* |
| 3542 | * Choose the minimum extent size we'll require for this |
| 3543 | * cluster. For SSD_SPREAD, don't allow any fragmentation. |
| 3544 | * For metadata, allow allocates with smaller extents. For |
| 3545 | * data, keep it dense. |
| 3546 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3547 | if (btrfs_test_opt(fs_info, SSD_SPREAD)) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3548 | cont1_bytes = min_bytes = bytes + empty_size; |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 3549 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3550 | cont1_bytes = bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3551 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3552 | } else { |
| 3553 | cont1_bytes = max(bytes, (bytes + empty_size) >> 2); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3554 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3555 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3556 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3557 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3558 | |
| 3559 | /* |
| 3560 | * If we know we don't have enough space to make a cluster don't even |
| 3561 | * bother doing all the work to try and find one. |
| 3562 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3563 | if (ctl->free_space < bytes) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3564 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3565 | return -ENOSPC; |
| 3566 | } |
| 3567 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3568 | spin_lock(&cluster->lock); |
| 3569 | |
| 3570 | /* someone already found a cluster, hooray */ |
| 3571 | if (cluster->block_group) { |
| 3572 | ret = 0; |
| 3573 | goto out; |
| 3574 | } |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3575 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3576 | trace_btrfs_find_cluster(block_group, offset, bytes, empty_size, |
| 3577 | min_bytes); |
| 3578 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3579 | ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3580 | bytes + empty_size, |
| 3581 | cont1_bytes, min_bytes); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3582 | if (ret) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3583 | ret = setup_cluster_bitmap(block_group, cluster, &bitmaps, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3584 | offset, bytes + empty_size, |
| 3585 | cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3586 | |
| 3587 | /* Clear our temporary list */ |
| 3588 | list_for_each_entry_safe(entry, tmp, &bitmaps, list) |
| 3589 | list_del_init(&entry->list); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3590 | |
| 3591 | if (!ret) { |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3592 | btrfs_get_block_group(block_group); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3593 | list_add_tail(&cluster->block_group_list, |
| 3594 | &block_group->cluster_list); |
| 3595 | cluster->block_group = block_group; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3596 | } else { |
| 3597 | trace_btrfs_failed_cluster_setup(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3598 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3599 | out: |
| 3600 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3601 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3602 | |
| 3603 | return ret; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * simple code to zero out a cluster |
| 3608 | */ |
| 3609 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) |
| 3610 | { |
| 3611 | spin_lock_init(&cluster->lock); |
| 3612 | spin_lock_init(&cluster->refill_lock); |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 3613 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3614 | cluster->max_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3615 | cluster->fragmented = false; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3616 | INIT_LIST_HEAD(&cluster->block_group_list); |
| 3617 | cluster->block_group = NULL; |
| 3618 | } |
| 3619 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3620 | static int do_trimming(struct btrfs_block_group *block_group, |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3621 | u64 *total_trimmed, u64 start, u64 bytes, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3622 | u64 reserved_start, u64 reserved_bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3623 | enum btrfs_trim_state reserved_trim_state, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3624 | struct btrfs_trim_range *trim_entry) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3625 | { |
| 3626 | struct btrfs_space_info *space_info = block_group->space_info; |
| 3627 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3628 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3629 | int ret; |
| 3630 | int update = 0; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3631 | const u64 end = start + bytes; |
| 3632 | const u64 reserved_end = reserved_start + reserved_bytes; |
| 3633 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3634 | u64 trimmed = 0; |
| 3635 | |
| 3636 | spin_lock(&space_info->lock); |
| 3637 | spin_lock(&block_group->lock); |
| 3638 | if (!block_group->ro) { |
| 3639 | block_group->reserved += reserved_bytes; |
| 3640 | space_info->bytes_reserved += reserved_bytes; |
| 3641 | update = 1; |
| 3642 | } |
| 3643 | spin_unlock(&block_group->lock); |
| 3644 | spin_unlock(&space_info->lock); |
| 3645 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3646 | ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3647 | if (!ret) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3648 | *total_trimmed += trimmed; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3649 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 3650 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3651 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3652 | mutex_lock(&ctl->cache_writeout_mutex); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3653 | if (reserved_start < start) |
| 3654 | __btrfs_add_free_space(fs_info, ctl, reserved_start, |
| 3655 | start - reserved_start, |
| 3656 | reserved_trim_state); |
| 3657 | if (start + bytes < reserved_start + reserved_bytes) |
| 3658 | __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end, |
| 3659 | reserved_trim_state); |
| 3660 | __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3661 | list_del(&trim_entry->list); |
| 3662 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3663 | |
| 3664 | if (update) { |
| 3665 | spin_lock(&space_info->lock); |
| 3666 | spin_lock(&block_group->lock); |
| 3667 | if (block_group->ro) |
| 3668 | space_info->bytes_readonly += reserved_bytes; |
| 3669 | block_group->reserved -= reserved_bytes; |
| 3670 | space_info->bytes_reserved -= reserved_bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3671 | spin_unlock(&block_group->lock); |
Su Yue | 8f63a84 | 2018-11-28 11:21:12 +0800 | [diff] [blame] | 3672 | spin_unlock(&space_info->lock); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | return ret; |
| 3676 | } |
| 3677 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3678 | /* |
| 3679 | * If @async is set, then we will trim 1 region and return. |
| 3680 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3681 | static int trim_no_bitmap(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3682 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
| 3683 | bool async) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3684 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3685 | struct btrfs_discard_ctl *discard_ctl = |
| 3686 | &block_group->fs_info->discard_ctl; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3687 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3688 | struct btrfs_free_space *entry; |
| 3689 | struct rb_node *node; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3690 | int ret = 0; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3691 | u64 extent_start; |
| 3692 | u64 extent_bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3693 | enum btrfs_trim_state extent_trim_state; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3694 | u64 bytes; |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3695 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3696 | |
| 3697 | while (start < end) { |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3698 | struct btrfs_trim_range trim_entry; |
| 3699 | |
| 3700 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3701 | spin_lock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3702 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3703 | if (ctl->free_space < minlen) |
| 3704 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3705 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3706 | entry = tree_search_offset(ctl, start, 0, 1); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3707 | if (!entry) |
| 3708 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3709 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3710 | /* Skip bitmaps and if async, already trimmed entries */ |
| 3711 | while (entry->bitmap || |
| 3712 | (async && btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3713 | node = rb_next(&entry->offset_index); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3714 | if (!node) |
| 3715 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3716 | entry = rb_entry(node, struct btrfs_free_space, |
| 3717 | offset_index); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3720 | if (entry->offset >= end) |
| 3721 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3722 | |
| 3723 | extent_start = entry->offset; |
| 3724 | extent_bytes = entry->bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3725 | extent_trim_state = entry->trim_state; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3726 | if (async) { |
| 3727 | start = entry->offset; |
| 3728 | bytes = entry->bytes; |
| 3729 | if (bytes < minlen) { |
| 3730 | spin_unlock(&ctl->tree_lock); |
| 3731 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3732 | goto next; |
| 3733 | } |
| 3734 | unlink_free_space(ctl, entry); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3735 | /* |
| 3736 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3737 | * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim |
| 3738 | * X when we come back around. So trim it now. |
| 3739 | */ |
| 3740 | if (max_discard_size && |
| 3741 | bytes >= (max_discard_size + |
| 3742 | BTRFS_ASYNC_DISCARD_MIN_FILTER)) { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3743 | bytes = max_discard_size; |
| 3744 | extent_bytes = max_discard_size; |
| 3745 | entry->offset += max_discard_size; |
| 3746 | entry->bytes -= max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3747 | link_free_space(ctl, entry); |
| 3748 | } else { |
| 3749 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3750 | } |
| 3751 | } else { |
| 3752 | start = max(start, extent_start); |
| 3753 | bytes = min(extent_start + extent_bytes, end) - start; |
| 3754 | if (bytes < minlen) { |
| 3755 | spin_unlock(&ctl->tree_lock); |
| 3756 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3757 | goto next; |
| 3758 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3759 | |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3760 | unlink_free_space(ctl, entry); |
| 3761 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3762 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3763 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3764 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3765 | trim_entry.start = extent_start; |
| 3766 | trim_entry.bytes = extent_bytes; |
| 3767 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3768 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3769 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3770 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3771 | extent_start, extent_bytes, extent_trim_state, |
| 3772 | &trim_entry); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3773 | if (ret) { |
| 3774 | block_group->discard_cursor = start + bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3775 | break; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3776 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3777 | next: |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3778 | start += bytes; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3779 | block_group->discard_cursor = start; |
| 3780 | if (async && *total_trimmed) |
| 3781 | break; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3782 | |
| 3783 | if (fatal_signal_pending(current)) { |
| 3784 | ret = -ERESTARTSYS; |
| 3785 | break; |
| 3786 | } |
| 3787 | |
| 3788 | cond_resched(); |
| 3789 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3790 | |
| 3791 | return ret; |
| 3792 | |
| 3793 | out_unlock: |
| 3794 | block_group->discard_cursor = btrfs_block_group_end(block_group); |
| 3795 | spin_unlock(&ctl->tree_lock); |
| 3796 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3797 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3798 | return ret; |
| 3799 | } |
| 3800 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3801 | /* |
| 3802 | * If we break out of trimming a bitmap prematurely, we should reset the |
| 3803 | * trimming bit. In a rather contrieved case, it's possible to race here so |
| 3804 | * reset the state to BTRFS_TRIM_STATE_UNTRIMMED. |
| 3805 | * |
| 3806 | * start = start of bitmap |
| 3807 | * end = near end of bitmap |
| 3808 | * |
| 3809 | * Thread 1: Thread 2: |
| 3810 | * trim_bitmaps(start) |
| 3811 | * trim_bitmaps(end) |
| 3812 | * end_trimming_bitmap() |
| 3813 | * reset_trimming_bitmap() |
| 3814 | */ |
| 3815 | static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset) |
| 3816 | { |
| 3817 | struct btrfs_free_space *entry; |
| 3818 | |
| 3819 | spin_lock(&ctl->tree_lock); |
| 3820 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3821 | if (entry) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3822 | if (btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3823 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 3824 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3825 | ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes; |
| 3826 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3827 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3828 | } |
| 3829 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3830 | spin_unlock(&ctl->tree_lock); |
| 3831 | } |
| 3832 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3833 | static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl, |
| 3834 | struct btrfs_free_space *entry) |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3835 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3836 | if (btrfs_free_space_trimming_bitmap(entry)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3837 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3838 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 3839 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3840 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3841 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3842 | } |
| 3843 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3844 | /* |
| 3845 | * If @async is set, then we will trim 1 region and return. |
| 3846 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3847 | static int trim_bitmaps(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3848 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3849 | u64 maxlen, bool async) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3850 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3851 | struct btrfs_discard_ctl *discard_ctl = |
| 3852 | &block_group->fs_info->discard_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3853 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 3854 | struct btrfs_free_space *entry; |
| 3855 | int ret = 0; |
| 3856 | int ret2; |
| 3857 | u64 bytes; |
| 3858 | u64 offset = offset_to_bitmap(ctl, start); |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3859 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3860 | |
| 3861 | while (offset < end) { |
| 3862 | bool next_bitmap = false; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3863 | struct btrfs_trim_range trim_entry; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3864 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3865 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3866 | spin_lock(&ctl->tree_lock); |
| 3867 | |
| 3868 | if (ctl->free_space < minlen) { |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3869 | block_group->discard_cursor = |
| 3870 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3871 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3872 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3873 | break; |
| 3874 | } |
| 3875 | |
| 3876 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3877 | /* |
| 3878 | * Bitmaps are marked trimmed lossily now to prevent constant |
| 3879 | * discarding of the same bitmap (the reason why we are bound |
| 3880 | * by the filters). So, retrim the block group bitmaps when we |
| 3881 | * are preparing to punt to the unused_bgs list. This uses |
| 3882 | * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED |
| 3883 | * which is the only discard index which sets minlen to 0. |
| 3884 | */ |
| 3885 | if (!entry || (async && minlen && start == offset && |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3886 | btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3887 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3888 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3889 | next_bitmap = true; |
| 3890 | goto next; |
| 3891 | } |
| 3892 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3893 | /* |
| 3894 | * Async discard bitmap trimming begins at by setting the start |
| 3895 | * to be key.objectid and the offset_to_bitmap() aligns to the |
| 3896 | * start of the bitmap. This lets us know we are fully |
| 3897 | * scanning the bitmap rather than only some portion of it. |
| 3898 | */ |
| 3899 | if (start == offset) |
| 3900 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMING; |
| 3901 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3902 | bytes = minlen; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3903 | ret2 = search_bitmap(ctl, entry, &start, &bytes, false); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3904 | if (ret2 || start >= end) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3905 | /* |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3906 | * We lossily consider a bitmap trimmed if we only skip |
| 3907 | * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER. |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3908 | */ |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3909 | if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER) |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3910 | end_trimming_bitmap(ctl, entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3911 | else |
| 3912 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3913 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3914 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3915 | next_bitmap = true; |
| 3916 | goto next; |
| 3917 | } |
| 3918 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3919 | /* |
| 3920 | * We already trimmed a region, but are using the locking above |
| 3921 | * to reset the trim_state. |
| 3922 | */ |
| 3923 | if (async && *total_trimmed) { |
| 3924 | spin_unlock(&ctl->tree_lock); |
| 3925 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3926 | goto out; |
| 3927 | } |
| 3928 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3929 | bytes = min(bytes, end - start); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3930 | if (bytes < minlen || (async && maxlen && bytes > maxlen)) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3931 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3932 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3933 | goto next; |
| 3934 | } |
| 3935 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3936 | /* |
| 3937 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3938 | * If X < @minlen, we won't trim X when we come back around. |
| 3939 | * So trim it now. We differ here from trimming extents as we |
| 3940 | * don't keep individual state per bit. |
| 3941 | */ |
| 3942 | if (async && |
| 3943 | max_discard_size && |
| 3944 | bytes > (max_discard_size + minlen)) |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3945 | bytes = max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3946 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3947 | bitmap_clear_bits(ctl, entry, start, bytes); |
| 3948 | if (entry->bytes == 0) |
| 3949 | free_bitmap(ctl, entry); |
| 3950 | |
| 3951 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3952 | trim_entry.start = start; |
| 3953 | trim_entry.bytes = bytes; |
| 3954 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3955 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3956 | |
| 3957 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3958 | start, bytes, 0, &trim_entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3959 | if (ret) { |
| 3960 | reset_trimming_bitmap(ctl, offset); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3961 | block_group->discard_cursor = |
| 3962 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3963 | break; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3964 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3965 | next: |
| 3966 | if (next_bitmap) { |
| 3967 | offset += BITS_PER_BITMAP * ctl->unit; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3968 | start = offset; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3969 | } else { |
| 3970 | start += bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3971 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3972 | block_group->discard_cursor = start; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3973 | |
| 3974 | if (fatal_signal_pending(current)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3975 | if (start != offset) |
| 3976 | reset_trimming_bitmap(ctl, offset); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3977 | ret = -ERESTARTSYS; |
| 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | cond_resched(); |
| 3982 | } |
| 3983 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3984 | if (offset >= end) |
| 3985 | block_group->discard_cursor = end; |
| 3986 | |
| 3987 | out: |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3988 | return ret; |
| 3989 | } |
| 3990 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3991 | int btrfs_trim_block_group(struct btrfs_block_group *block_group, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3992 | u64 *trimmed, u64 start, u64 end, u64 minlen) |
| 3993 | { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3994 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3995 | int ret; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3996 | u64 rem = 0; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3997 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3998 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3999 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4000 | *trimmed = 0; |
| 4001 | |
| 4002 | spin_lock(&block_group->lock); |
| 4003 | if (block_group->removed) { |
| 4004 | spin_unlock(&block_group->lock); |
| 4005 | return 0; |
| 4006 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4007 | btrfs_freeze_block_group(block_group); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4008 | spin_unlock(&block_group->lock); |
| 4009 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4010 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4011 | if (ret) |
| 4012 | goto out; |
| 4013 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 4014 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4015 | div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem); |
| 4016 | /* If we ended in the middle of a bitmap, reset the trimming flag */ |
| 4017 | if (rem) |
| 4018 | reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end)); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4019 | out: |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4020 | btrfs_unfreeze_block_group(block_group); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 4021 | return ret; |
| 4022 | } |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4023 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4024 | int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group, |
| 4025 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
| 4026 | bool async) |
| 4027 | { |
| 4028 | int ret; |
| 4029 | |
| 4030 | *trimmed = 0; |
| 4031 | |
| 4032 | spin_lock(&block_group->lock); |
| 4033 | if (block_group->removed) { |
| 4034 | spin_unlock(&block_group->lock); |
| 4035 | return 0; |
| 4036 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4037 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4038 | spin_unlock(&block_group->lock); |
| 4039 | |
| 4040 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async); |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4041 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4042 | |
| 4043 | return ret; |
| 4044 | } |
| 4045 | |
| 4046 | int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group, |
| 4047 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 4048 | u64 maxlen, bool async) |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4049 | { |
| 4050 | int ret; |
| 4051 | |
| 4052 | *trimmed = 0; |
| 4053 | |
| 4054 | spin_lock(&block_group->lock); |
| 4055 | if (block_group->removed) { |
| 4056 | spin_unlock(&block_group->lock); |
| 4057 | return 0; |
| 4058 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4059 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4060 | spin_unlock(&block_group->lock); |
| 4061 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 4062 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen, |
| 4063 | async); |
| 4064 | |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4065 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4066 | |
| 4067 | return ret; |
| 4068 | } |
| 4069 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4070 | bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info) |
| 4071 | { |
| 4072 | return btrfs_super_cache_generation(fs_info->super_copy); |
| 4073 | } |
| 4074 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4075 | static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info, |
| 4076 | struct btrfs_trans_handle *trans) |
| 4077 | { |
| 4078 | struct btrfs_block_group *block_group; |
| 4079 | struct rb_node *node; |
Tom Rix | 77364fa | 2021-04-30 11:06:55 -0700 | [diff] [blame] | 4080 | int ret = 0; |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4081 | |
| 4082 | btrfs_info(fs_info, "cleaning free space cache v1"); |
| 4083 | |
| 4084 | node = rb_first(&fs_info->block_group_cache_tree); |
| 4085 | while (node) { |
| 4086 | block_group = rb_entry(node, struct btrfs_block_group, cache_node); |
| 4087 | ret = btrfs_remove_free_space_inode(trans, NULL, block_group); |
| 4088 | if (ret) |
| 4089 | goto out; |
| 4090 | node = rb_next(node); |
| 4091 | } |
| 4092 | out: |
| 4093 | return ret; |
| 4094 | } |
| 4095 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4096 | int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active) |
| 4097 | { |
| 4098 | struct btrfs_trans_handle *trans; |
| 4099 | int ret; |
| 4100 | |
| 4101 | /* |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4102 | * update_super_roots will appropriately set or unset |
| 4103 | * super_copy->cache_generation based on SPACE_CACHE and |
| 4104 | * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a |
| 4105 | * transaction commit whether we are enabling space cache v1 and don't |
| 4106 | * have any other work to do, or are disabling it and removing free |
| 4107 | * space inodes. |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4108 | */ |
| 4109 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
| 4110 | if (IS_ERR(trans)) |
| 4111 | return PTR_ERR(trans); |
| 4112 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4113 | if (!active) { |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4114 | set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4115 | ret = cleanup_free_space_cache_v1(fs_info, trans); |
| 4116 | if (ret) { |
| 4117 | btrfs_abort_transaction(trans, ret); |
| 4118 | btrfs_end_transaction(trans); |
| 4119 | goto out; |
| 4120 | } |
| 4121 | } |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4122 | |
| 4123 | ret = btrfs_commit_transaction(trans); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4124 | out: |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4125 | clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
| 4126 | |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4130 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4131 | /* |
| 4132 | * Use this if you need to make a bitmap or extent entry specifically, it |
| 4133 | * doesn't do any of the merging that add_free_space does, this acts a lot like |
| 4134 | * how the free space cache loading stuff works, so you can get really weird |
| 4135 | * configurations. |
| 4136 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4137 | int test_add_free_space_entry(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4138 | u64 offset, u64 bytes, bool bitmap) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4139 | { |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4140 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4141 | struct btrfs_free_space *info = NULL, *bitmap_info; |
| 4142 | void *map = NULL; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4143 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4144 | u64 bytes_added; |
| 4145 | int ret; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4146 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4147 | again: |
| 4148 | if (!info) { |
| 4149 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
| 4150 | if (!info) |
| 4151 | return -ENOMEM; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4152 | } |
| 4153 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4154 | if (!bitmap) { |
| 4155 | spin_lock(&ctl->tree_lock); |
| 4156 | info->offset = offset; |
| 4157 | info->bytes = bytes; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4158 | info->max_extent_size = 0; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4159 | ret = link_free_space(ctl, info); |
| 4160 | spin_unlock(&ctl->tree_lock); |
| 4161 | if (ret) |
| 4162 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4163 | return ret; |
| 4164 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4165 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4166 | if (!map) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4167 | map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4168 | if (!map) { |
| 4169 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4170 | return -ENOMEM; |
| 4171 | } |
| 4172 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4173 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4174 | spin_lock(&ctl->tree_lock); |
| 4175 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4176 | 1, 0); |
| 4177 | if (!bitmap_info) { |
| 4178 | info->bitmap = map; |
| 4179 | map = NULL; |
| 4180 | add_new_bitmap(ctl, info, offset); |
| 4181 | bitmap_info = info; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4182 | info = NULL; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4183 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4184 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4185 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 4186 | trim_state); |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4187 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4188 | bytes -= bytes_added; |
| 4189 | offset += bytes_added; |
| 4190 | spin_unlock(&ctl->tree_lock); |
| 4191 | |
| 4192 | if (bytes) |
| 4193 | goto again; |
| 4194 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4195 | if (info) |
| 4196 | kmem_cache_free(btrfs_free_space_cachep, info); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4197 | if (map) |
| 4198 | kmem_cache_free(btrfs_free_space_bitmap_cachep, map); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4199 | return 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | /* |
| 4203 | * Checks to see if the given range is in the free space cache. This is really |
| 4204 | * just used to check the absence of space, so if there is free space in the |
| 4205 | * range at all we will return 1. |
| 4206 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4207 | int test_check_exists(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4208 | u64 offset, u64 bytes) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4209 | { |
| 4210 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4211 | struct btrfs_free_space *info; |
| 4212 | int ret = 0; |
| 4213 | |
| 4214 | spin_lock(&ctl->tree_lock); |
| 4215 | info = tree_search_offset(ctl, offset, 0, 0); |
| 4216 | if (!info) { |
| 4217 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4218 | 1, 0); |
| 4219 | if (!info) |
| 4220 | goto out; |
| 4221 | } |
| 4222 | |
| 4223 | have_info: |
| 4224 | if (info->bitmap) { |
| 4225 | u64 bit_off, bit_bytes; |
| 4226 | struct rb_node *n; |
| 4227 | struct btrfs_free_space *tmp; |
| 4228 | |
| 4229 | bit_off = offset; |
| 4230 | bit_bytes = ctl->unit; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 4231 | ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4232 | if (!ret) { |
| 4233 | if (bit_off == offset) { |
| 4234 | ret = 1; |
| 4235 | goto out; |
| 4236 | } else if (bit_off > offset && |
| 4237 | offset + bytes > bit_off) { |
| 4238 | ret = 1; |
| 4239 | goto out; |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | n = rb_prev(&info->offset_index); |
| 4244 | while (n) { |
| 4245 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4246 | offset_index); |
| 4247 | if (tmp->offset + tmp->bytes < offset) |
| 4248 | break; |
| 4249 | if (offset + bytes < tmp->offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4250 | n = rb_prev(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4251 | continue; |
| 4252 | } |
| 4253 | info = tmp; |
| 4254 | goto have_info; |
| 4255 | } |
| 4256 | |
| 4257 | n = rb_next(&info->offset_index); |
| 4258 | while (n) { |
| 4259 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4260 | offset_index); |
| 4261 | if (offset + bytes < tmp->offset) |
| 4262 | break; |
| 4263 | if (tmp->offset + tmp->bytes < offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4264 | n = rb_next(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4265 | continue; |
| 4266 | } |
| 4267 | info = tmp; |
| 4268 | goto have_info; |
| 4269 | } |
| 4270 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4271 | ret = 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4272 | goto out; |
| 4273 | } |
| 4274 | |
| 4275 | if (info->offset == offset) { |
| 4276 | ret = 1; |
| 4277 | goto out; |
| 4278 | } |
| 4279 | |
| 4280 | if (offset > info->offset && offset < info->offset + info->bytes) |
| 4281 | ret = 1; |
| 4282 | out: |
| 4283 | spin_unlock(&ctl->tree_lock); |
| 4284 | return ret; |
| 4285 | } |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4286 | #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */ |