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, |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 40 | struct btrfs_free_space *info, bool update_stat); |
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, |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 48 | u64 bytes, bool update_stats); |
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) { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 875 | unlink_free_space(ctl, info, true); |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 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; |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 889 | bitmap_clear_bits(ctl, info, offset, bytes, true); |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 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; |
Nikolay Borisov | f1a8fc6 | 2021-11-23 09:23:42 +0200 | [diff] [blame] | 1639 | struct btrfs_free_space *entry = NULL, *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' */ |
Nikolay Borisov | f1a8fc6 | 2021-11-23 09:23:42 +0200 | [diff] [blame] | 1642 | while (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1643 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1644 | prev = entry; |
| 1645 | |
| 1646 | if (offset < entry->offset) |
| 1647 | n = n->rb_left; |
| 1648 | else if (offset > entry->offset) |
| 1649 | n = n->rb_right; |
| 1650 | else |
| 1651 | break; |
Nikolay Borisov | f1a8fc6 | 2021-11-23 09:23:42 +0200 | [diff] [blame] | 1652 | |
| 1653 | entry = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1654 | } |
| 1655 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1656 | if (bitmap_only) { |
| 1657 | if (!entry) |
| 1658 | return NULL; |
| 1659 | if (entry->bitmap) |
| 1660 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1661 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1662 | /* |
| 1663 | * bitmap entry and extent entry may share same offset, |
| 1664 | * in that case, bitmap entry comes after extent entry. |
| 1665 | */ |
| 1666 | n = rb_next(n); |
| 1667 | if (!n) |
| 1668 | return NULL; |
| 1669 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1670 | if (entry->offset != offset) |
| 1671 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1672 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1673 | WARN_ON(!entry->bitmap); |
| 1674 | return entry; |
| 1675 | } else if (entry) { |
| 1676 | if (entry->bitmap) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1677 | /* |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1678 | * if previous extent entry covers the offset, |
| 1679 | * we should return it instead of the bitmap entry |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1680 | */ |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1681 | n = rb_prev(&entry->offset_index); |
| 1682 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1683 | prev = rb_entry(n, struct btrfs_free_space, |
| 1684 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1685 | if (!prev->bitmap && |
| 1686 | prev->offset + prev->bytes > offset) |
| 1687 | entry = prev; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1688 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1689 | } |
| 1690 | return entry; |
| 1691 | } |
| 1692 | |
| 1693 | if (!prev) |
| 1694 | return NULL; |
| 1695 | |
| 1696 | /* find last entry before the 'offset' */ |
| 1697 | entry = prev; |
| 1698 | if (entry->offset > offset) { |
| 1699 | n = rb_prev(&entry->offset_index); |
| 1700 | if (n) { |
| 1701 | entry = rb_entry(n, struct btrfs_free_space, |
| 1702 | offset_index); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1703 | ASSERT(entry->offset <= offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1704 | } else { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1705 | if (fuzzy) |
| 1706 | return entry; |
| 1707 | else |
| 1708 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1712 | if (entry->bitmap) { |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1713 | n = rb_prev(&entry->offset_index); |
| 1714 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1715 | prev = rb_entry(n, struct btrfs_free_space, |
| 1716 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1717 | if (!prev->bitmap && |
| 1718 | prev->offset + prev->bytes > offset) |
| 1719 | return prev; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1720 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1721 | if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1722 | return entry; |
| 1723 | } else if (entry->offset + entry->bytes > offset) |
| 1724 | return entry; |
| 1725 | |
| 1726 | if (!fuzzy) |
| 1727 | return NULL; |
| 1728 | |
| 1729 | while (1) { |
Nikolay Borisov | 167c0bd | 2021-11-22 17:16:46 +0200 | [diff] [blame] | 1730 | n = rb_next(&entry->offset_index); |
| 1731 | if (!n) |
| 1732 | return NULL; |
| 1733 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1734 | if (entry->bitmap) { |
| 1735 | if (entry->offset + BITS_PER_BITMAP * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1736 | ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1737 | break; |
| 1738 | } else { |
| 1739 | if (entry->offset + entry->bytes > offset) |
| 1740 | break; |
| 1741 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1742 | } |
| 1743 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 1746 | static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
| 1747 | struct btrfs_free_space *info, |
| 1748 | bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1749 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1750 | rb_erase(&info->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1751 | rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1752 | ctl->free_extents--; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1753 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1754 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1755 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1756 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes; |
| 1757 | } |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1758 | |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 1759 | if (update_stat) |
| 1760 | ctl->free_space -= info->bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1761 | } |
| 1762 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1763 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1764 | struct btrfs_free_space *info) |
| 1765 | { |
| 1766 | int ret = 0; |
| 1767 | |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1768 | ASSERT(info->bytes || info->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1769 | ret = tree_insert_offset(&ctl->free_space_offset, info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1770 | &info->offset_index, (info->bitmap != NULL)); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1771 | if (ret) |
| 1772 | return ret; |
| 1773 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1774 | rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); |
| 1775 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1776 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1777 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1778 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 1779 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1780 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1781 | ctl->free_space += info->bytes; |
| 1782 | ctl->free_extents++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1783 | return ret; |
| 1784 | } |
| 1785 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1786 | static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl, |
| 1787 | struct btrfs_free_space *info) |
| 1788 | { |
| 1789 | ASSERT(info->bitmap); |
| 1790 | |
| 1791 | /* |
| 1792 | * If our entry is empty it's because we're on a cluster and we don't |
| 1793 | * want to re-link it into our ctl bytes index. |
| 1794 | */ |
| 1795 | if (RB_EMPTY_NODE(&info->bytes_index)) |
| 1796 | return; |
| 1797 | |
| 1798 | rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); |
| 1799 | rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); |
| 1800 | } |
| 1801 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 1802 | static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 1803 | struct btrfs_free_space *info, |
| 1804 | u64 offset, u64 bytes, bool update_stat) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1805 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1806 | unsigned long start, count, end; |
| 1807 | int extent_delta = -1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1808 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1809 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1810 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1811 | end = start + count; |
| 1812 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1813 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1814 | bitmap_clear(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1815 | |
| 1816 | info->bytes -= bytes; |
Josef Bacik | 553cceb | 2018-09-28 07:18:00 -0400 | [diff] [blame] | 1817 | if (info->max_extent_size > ctl->unit) |
| 1818 | info->max_extent_size = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1819 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1820 | relink_bitmap_entry(ctl, info); |
| 1821 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1822 | if (start && test_bit(start - 1, info->bitmap)) |
| 1823 | extent_delta++; |
| 1824 | |
| 1825 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1826 | extent_delta++; |
| 1827 | |
| 1828 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1829 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1830 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1831 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
| 1832 | } |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 1833 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 1834 | if (update_stat) |
| 1835 | ctl->free_space -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1836 | } |
| 1837 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1838 | static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1839 | struct btrfs_free_space *info, u64 offset, |
| 1840 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1841 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1842 | unsigned long start, count, end; |
| 1843 | int extent_delta = 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1844 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1845 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1846 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1847 | end = start + count; |
| 1848 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1849 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1850 | bitmap_set(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1851 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1852 | /* |
| 1853 | * We set some bytes, we have no idea what the max extent size is |
| 1854 | * anymore. |
| 1855 | */ |
| 1856 | info->max_extent_size = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1857 | info->bytes += bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1858 | ctl->free_space += bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1859 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1860 | relink_bitmap_entry(ctl, info); |
| 1861 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1862 | if (start && test_bit(start - 1, info->bitmap)) |
| 1863 | extent_delta--; |
| 1864 | |
| 1865 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1866 | extent_delta--; |
| 1867 | |
| 1868 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1869 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1870 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1871 | ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes; |
| 1872 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1873 | } |
| 1874 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1875 | /* |
| 1876 | * If we can not find suitable extent, we will use bytes to record |
| 1877 | * the size of the max extent. |
| 1878 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1879 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1880 | struct btrfs_free_space *bitmap_info, u64 *offset, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1881 | u64 *bytes, bool for_alloc) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1882 | { |
| 1883 | unsigned long found_bits = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1884 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1885 | unsigned long bits, i; |
| 1886 | unsigned long next_zero; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1887 | unsigned long extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1888 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1889 | /* |
| 1890 | * Skip searching the bitmap if we don't have a contiguous section that |
| 1891 | * is large enough for this allocation. |
| 1892 | */ |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1893 | if (for_alloc && |
| 1894 | bitmap_info->max_extent_size && |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1895 | bitmap_info->max_extent_size < *bytes) { |
| 1896 | *bytes = bitmap_info->max_extent_size; |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1900 | i = offset_to_bit(bitmap_info->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1901 | max_t(u64, *offset, bitmap_info->offset)); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1902 | bits = bytes_to_bits(*bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1903 | |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 1904 | for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1905 | if (for_alloc && bits == 1) { |
| 1906 | found_bits = 1; |
| 1907 | break; |
| 1908 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1909 | next_zero = find_next_zero_bit(bitmap_info->bitmap, |
| 1910 | BITS_PER_BITMAP, i); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1911 | extent_bits = next_zero - i; |
| 1912 | if (extent_bits >= bits) { |
| 1913 | found_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1914 | break; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1915 | } else if (extent_bits > max_bits) { |
| 1916 | max_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1917 | } |
| 1918 | i = next_zero; |
| 1919 | } |
| 1920 | |
| 1921 | if (found_bits) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1922 | *offset = (u64)(i * ctl->unit) + bitmap_info->offset; |
| 1923 | *bytes = (u64)(found_bits) * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1924 | return 0; |
| 1925 | } |
| 1926 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1927 | *bytes = (u64)(max_bits) * ctl->unit; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1928 | bitmap_info->max_extent_size = *bytes; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1929 | relink_bitmap_entry(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1930 | return -1; |
| 1931 | } |
| 1932 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1933 | /* Cache the size of the max extent in bytes */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1934 | static struct btrfs_free_space * |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1935 | 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] | 1936 | unsigned long align, u64 *max_extent_size, bool use_bytes_index) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1937 | { |
| 1938 | struct btrfs_free_space *entry; |
| 1939 | struct rb_node *node; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1940 | u64 tmp; |
| 1941 | u64 align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1942 | int ret; |
| 1943 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1944 | if (!ctl->free_space_offset.rb_node) |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1945 | goto out; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1946 | again: |
| 1947 | if (use_bytes_index) { |
| 1948 | node = rb_first_cached(&ctl->free_space_bytes); |
| 1949 | } else { |
| 1950 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), |
| 1951 | 0, 1); |
| 1952 | if (!entry) |
| 1953 | goto out; |
| 1954 | node = &entry->offset_index; |
| 1955 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1956 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1957 | for (; node; node = rb_next(node)) { |
| 1958 | if (use_bytes_index) |
| 1959 | entry = rb_entry(node, struct btrfs_free_space, |
| 1960 | bytes_index); |
| 1961 | else |
| 1962 | entry = rb_entry(node, struct btrfs_free_space, |
| 1963 | offset_index); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1964 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1965 | /* |
| 1966 | * If we are using the bytes index then all subsequent entries |
| 1967 | * in this tree are going to be < bytes, so simply set the max |
| 1968 | * extent size and exit the loop. |
| 1969 | * |
| 1970 | * If we're using the offset index then we need to keep going |
| 1971 | * through the rest of the tree. |
| 1972 | */ |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1973 | if (entry->bytes < *bytes) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1974 | *max_extent_size = max(get_max_extent_size(entry), |
| 1975 | *max_extent_size); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1976 | if (use_bytes_index) |
| 1977 | break; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1978 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1979 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1980 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1981 | /* make sure the space returned is big enough |
| 1982 | * to match our requested alignment |
| 1983 | */ |
| 1984 | if (*bytes >= align) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1985 | tmp = entry->offset - ctl->start + align - 1; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 1986 | tmp = div64_u64(tmp, align); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1987 | tmp = tmp * align + ctl->start; |
| 1988 | align_off = tmp - entry->offset; |
| 1989 | } else { |
| 1990 | align_off = 0; |
| 1991 | tmp = entry->offset; |
| 1992 | } |
| 1993 | |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 1994 | /* |
| 1995 | * We don't break here if we're using the bytes index because we |
| 1996 | * may have another entry that has the correct alignment that is |
| 1997 | * the right size, so we don't want to miss that possibility. |
| 1998 | * At worst this adds another loop through the logic, but if we |
| 1999 | * broke here we could prematurely ENOSPC. |
| 2000 | */ |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2001 | if (entry->bytes < *bytes + align_off) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 2002 | *max_extent_size = max(get_max_extent_size(entry), |
| 2003 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2004 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2005 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2006 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2007 | if (entry->bitmap) { |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2008 | struct rb_node *old_next = rb_next(node); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2009 | u64 size = *bytes; |
| 2010 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2011 | ret = search_bitmap(ctl, entry, &tmp, &size, true); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2012 | if (!ret) { |
| 2013 | *offset = tmp; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2014 | *bytes = size; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2015 | return entry; |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 2016 | } else { |
| 2017 | *max_extent_size = |
| 2018 | max(get_max_extent_size(entry), |
| 2019 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2020 | } |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2021 | |
| 2022 | /* |
| 2023 | * The bitmap may have gotten re-arranged in the space |
| 2024 | * index here because the max_extent_size may have been |
| 2025 | * updated. Start from the beginning again if this |
| 2026 | * happened. |
| 2027 | */ |
| 2028 | if (use_bytes_index && old_next != rb_next(node)) |
| 2029 | goto again; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2030 | continue; |
| 2031 | } |
| 2032 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2033 | *offset = tmp; |
| 2034 | *bytes = entry->bytes - align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2035 | return entry; |
| 2036 | } |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2037 | out: |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2038 | return NULL; |
| 2039 | } |
| 2040 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2041 | static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2042 | struct btrfs_free_space *info, u64 offset) |
| 2043 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2044 | info->offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | f019f42 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 2045 | info->bytes = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2046 | info->bitmap_extents = 0; |
Alexandre Oliva | f2d0f67 | 2011-11-28 12:04:43 -0200 | [diff] [blame] | 2047 | INIT_LIST_HEAD(&info->list); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2048 | link_free_space(ctl, info); |
| 2049 | ctl->total_bitmaps++; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 2050 | recalculate_thresholds(ctl); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2051 | } |
| 2052 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2053 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2054 | struct btrfs_free_space *bitmap_info) |
| 2055 | { |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 2056 | /* |
| 2057 | * Normally when this is called, the bitmap is completely empty. However, |
| 2058 | * if we are blowing up the free space cache for one reason or another |
| 2059 | * via __btrfs_remove_free_space_cache(), then it may not be freed and |
| 2060 | * we may leave stats on the table. |
| 2061 | */ |
| 2062 | if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) { |
| 2063 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 2064 | bitmap_info->bitmap_extents; |
| 2065 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes; |
| 2066 | |
| 2067 | } |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 2068 | unlink_free_space(ctl, bitmap_info, true); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2069 | kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2070 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2071 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 2072 | recalculate_thresholds(ctl); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2075 | static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2076 | struct btrfs_free_space *bitmap_info, |
| 2077 | u64 *offset, u64 *bytes) |
| 2078 | { |
| 2079 | u64 end; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2080 | u64 search_start, search_bytes; |
| 2081 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2082 | |
| 2083 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2084 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2085 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2086 | /* |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2087 | * We need to search for bits in this bitmap. We could only cover some |
| 2088 | * of the extent in this bitmap thanks to how we add space, so we need |
| 2089 | * to search for as much as it as we can and clear that amount, and then |
| 2090 | * go searching for the next bit. |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2091 | */ |
| 2092 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2093 | search_bytes = ctl->unit; |
Josef Bacik | 13dbc08 | 2011-02-03 02:39:52 +0000 | [diff] [blame] | 2094 | search_bytes = min(search_bytes, end - search_start + 1); |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2095 | ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes, |
| 2096 | false); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2097 | if (ret < 0 || search_start != *offset) |
| 2098 | return -EINVAL; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2099 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2100 | /* We may have found more bits than what we need */ |
| 2101 | search_bytes = min(search_bytes, *bytes); |
| 2102 | |
| 2103 | /* Cannot clear past the end of the bitmap */ |
| 2104 | search_bytes = min(search_bytes, end - search_start + 1); |
| 2105 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 2106 | bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes, true); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2107 | *offset += search_bytes; |
| 2108 | *bytes -= search_bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2109 | |
| 2110 | if (*bytes) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2111 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2112 | if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2113 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2114 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2115 | /* |
| 2116 | * no entry after this bitmap, but we still have bytes to |
| 2117 | * remove, so something has gone wrong. |
| 2118 | */ |
| 2119 | if (!next) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2120 | return -EINVAL; |
| 2121 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2122 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
| 2123 | offset_index); |
| 2124 | |
| 2125 | /* |
| 2126 | * if the next entry isn't a bitmap we need to return to let the |
| 2127 | * extent stuff do its work. |
| 2128 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2129 | if (!bitmap_info->bitmap) |
| 2130 | return -EAGAIN; |
| 2131 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2132 | /* |
| 2133 | * Ok the next item is a bitmap, but it may not actually hold |
| 2134 | * the information for the rest of this free space stuff, so |
| 2135 | * look for it, and if we don't find it return so we can try |
| 2136 | * everything over again. |
| 2137 | */ |
| 2138 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2139 | search_bytes = ctl->unit; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2140 | ret = search_bitmap(ctl, bitmap_info, &search_start, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2141 | &search_bytes, false); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2142 | if (ret < 0 || search_start != *offset) |
| 2143 | return -EAGAIN; |
| 2144 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2145 | goto again; |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2146 | } else if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2147 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2148 | |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2152 | static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2153 | struct btrfs_free_space *info, u64 offset, |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2154 | u64 bytes, enum btrfs_trim_state trim_state) |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2155 | { |
| 2156 | u64 bytes_to_set = 0; |
| 2157 | u64 end; |
| 2158 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2159 | /* |
| 2160 | * This is a tradeoff to make bitmap trim state minimal. We mark the |
| 2161 | * whole bitmap untrimmed if at any point we add untrimmed regions. |
| 2162 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2163 | if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2164 | if (btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2165 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 2166 | info->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2167 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 2168 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2169 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2170 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2171 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2172 | end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); |
| 2173 | |
| 2174 | bytes_to_set = min(end - offset, bytes); |
| 2175 | |
| 2176 | bitmap_set_bits(ctl, info, offset, bytes_to_set); |
| 2177 | |
| 2178 | return bytes_to_set; |
| 2179 | |
| 2180 | } |
| 2181 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2182 | static bool use_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2183 | struct btrfs_free_space *info) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2184 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2185 | struct btrfs_block_group *block_group = ctl->private; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2186 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2187 | bool forced = false; |
| 2188 | |
| 2189 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2190 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2191 | forced = true; |
| 2192 | #endif |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2193 | |
Dennis Zhou | 5d90c5c | 2020-01-02 16:26:43 -0500 | [diff] [blame] | 2194 | /* This is a way to reclaim large regions from the bitmaps. */ |
| 2195 | if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD) |
| 2196 | return false; |
| 2197 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2198 | /* |
| 2199 | * If we are below the extents threshold then we can add this as an |
| 2200 | * extent, and don't have to deal with the bitmap |
| 2201 | */ |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2202 | if (!forced && ctl->free_extents < ctl->extents_thresh) { |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2203 | /* |
| 2204 | * If this block group has some small extents we don't want to |
| 2205 | * 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] | 2206 | * to reserve them to larger extents, however if we have plenty |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2207 | * of cache left then go ahead an dadd them, no sense in adding |
| 2208 | * the overhead of a bitmap if we don't have to. |
| 2209 | */ |
Dennis Zhou | f9bb615 | 2020-01-02 16:26:44 -0500 | [diff] [blame] | 2210 | if (info->bytes <= fs_info->sectorsize * 8) { |
| 2211 | if (ctl->free_extents * 3 <= ctl->extents_thresh) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2212 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2213 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2214 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2215 | } |
| 2216 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2217 | |
| 2218 | /* |
Josef Bacik | dde5740 | 2013-02-12 14:07:51 -0500 | [diff] [blame] | 2219 | * The original block groups from mkfs can be really small, like 8 |
| 2220 | * megabytes, so don't bother with a bitmap for those entries. However |
| 2221 | * some block groups can be smaller than what a bitmap would cover but |
| 2222 | * are still large enough that they could overflow the 32k memory limit, |
| 2223 | * so allow those block groups to still be allowed to have a bitmap |
| 2224 | * entry. |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2225 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2226 | if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2227 | return false; |
| 2228 | |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 2232 | static const struct btrfs_free_space_op free_space_op = { |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2233 | .use_bitmap = use_bitmap, |
| 2234 | }; |
| 2235 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2236 | static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2237 | struct btrfs_free_space *info) |
| 2238 | { |
| 2239 | struct btrfs_free_space *bitmap_info; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2240 | struct btrfs_block_group *block_group = NULL; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2241 | int added = 0; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2242 | u64 bytes, offset, bytes_added; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2243 | enum btrfs_trim_state trim_state; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2244 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2245 | |
| 2246 | bytes = info->bytes; |
| 2247 | offset = info->offset; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2248 | trim_state = info->trim_state; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2249 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2250 | if (!ctl->op->use_bitmap(ctl, info)) |
| 2251 | return 0; |
| 2252 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2253 | if (ctl->op == &free_space_op) |
| 2254 | block_group = ctl->private; |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2255 | again: |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2256 | /* |
| 2257 | * Since we link bitmaps right into the cluster we need to see if we |
| 2258 | * have a cluster here, and if so and it has our bitmap we need to add |
| 2259 | * the free space to that bitmap. |
| 2260 | */ |
| 2261 | if (block_group && !list_empty(&block_group->cluster_list)) { |
| 2262 | struct btrfs_free_cluster *cluster; |
| 2263 | struct rb_node *node; |
| 2264 | struct btrfs_free_space *entry; |
| 2265 | |
| 2266 | cluster = list_entry(block_group->cluster_list.next, |
| 2267 | struct btrfs_free_cluster, |
| 2268 | block_group_list); |
| 2269 | spin_lock(&cluster->lock); |
| 2270 | node = rb_first(&cluster->root); |
| 2271 | if (!node) { |
| 2272 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2273 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2277 | if (!entry->bitmap) { |
| 2278 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2279 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | if (entry->offset == offset_to_bitmap(ctl, offset)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2283 | bytes_added = add_bytes_to_bitmap(ctl, entry, offset, |
| 2284 | bytes, trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2285 | bytes -= bytes_added; |
| 2286 | offset += bytes_added; |
| 2287 | } |
| 2288 | spin_unlock(&cluster->lock); |
| 2289 | if (!bytes) { |
| 2290 | ret = 1; |
| 2291 | goto out; |
| 2292 | } |
| 2293 | } |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2294 | |
| 2295 | no_cluster_bitmap: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2296 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2297 | 1, 0); |
| 2298 | if (!bitmap_info) { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2299 | ASSERT(added == 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2300 | goto new_bitmap; |
| 2301 | } |
| 2302 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2303 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 2304 | trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2305 | bytes -= bytes_added; |
| 2306 | offset += bytes_added; |
| 2307 | added = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2308 | |
| 2309 | if (!bytes) { |
| 2310 | ret = 1; |
| 2311 | goto out; |
| 2312 | } else |
| 2313 | goto again; |
| 2314 | |
| 2315 | new_bitmap: |
| 2316 | if (info && info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2317 | add_new_bitmap(ctl, info, offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2318 | added = 1; |
| 2319 | info = NULL; |
| 2320 | goto again; |
| 2321 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2322 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2323 | |
| 2324 | /* no pre-allocated info, allocate a new one */ |
| 2325 | if (!info) { |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2326 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 2327 | GFP_NOFS); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2328 | if (!info) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2329 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2330 | ret = -ENOMEM; |
| 2331 | goto out; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | /* allocate the bitmap */ |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2336 | info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, |
| 2337 | GFP_NOFS); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2338 | info->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2339 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2340 | if (!info->bitmap) { |
| 2341 | ret = -ENOMEM; |
| 2342 | goto out; |
| 2343 | } |
| 2344 | goto again; |
| 2345 | } |
| 2346 | |
| 2347 | out: |
| 2348 | if (info) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2349 | if (info->bitmap) |
| 2350 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 2351 | info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2352 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2353 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2354 | |
| 2355 | return ret; |
| 2356 | } |
| 2357 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2358 | /* |
| 2359 | * Free space merging rules: |
| 2360 | * 1) Merge trimmed areas together |
| 2361 | * 2) Let untrimmed areas coalesce with trimmed areas |
| 2362 | * 3) Always pull neighboring regions from bitmaps |
| 2363 | * |
| 2364 | * The above rules are for when we merge free space based on btrfs_trim_state. |
| 2365 | * Rules 2 and 3 are subtle because they are suboptimal, but are done for the |
| 2366 | * same reason: to promote larger extent regions which makes life easier for |
| 2367 | * find_free_extent(). Rule 2 enables coalescing based on the common path |
| 2368 | * being returning free space from btrfs_finish_extent_commit(). So when free |
| 2369 | * space is trimmed, it will prevent aggregating trimmed new region and |
| 2370 | * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents |
| 2371 | * and provide find_free_extent() with the largest extents possible hoping for |
| 2372 | * the reuse path. |
| 2373 | */ |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2374 | static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2375 | struct btrfs_free_space *info, bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2376 | { |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2377 | struct btrfs_free_space *left_info = NULL; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2378 | struct btrfs_free_space *right_info; |
| 2379 | bool merged = false; |
| 2380 | u64 offset = info->offset; |
| 2381 | u64 bytes = info->bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2382 | const bool is_trimmed = btrfs_free_space_trimmed(info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2383 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2384 | /* |
| 2385 | * first we want to see if there is free space adjacent to the range we |
| 2386 | * are adding, if there is remove that struct and add a new one to |
| 2387 | * cover the entire range |
| 2388 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2389 | right_info = tree_search_offset(ctl, offset + bytes, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2390 | if (right_info && rb_prev(&right_info->offset_index)) |
| 2391 | left_info = rb_entry(rb_prev(&right_info->offset_index), |
| 2392 | struct btrfs_free_space, offset_index); |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2393 | else if (!right_info) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2394 | left_info = tree_search_offset(ctl, offset - 1, 0, 0); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2395 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2396 | /* See try_merge_free_space() comment. */ |
| 2397 | if (right_info && !right_info->bitmap && |
| 2398 | (!is_trimmed || btrfs_free_space_trimmed(right_info))) { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 2399 | unlink_free_space(ctl, right_info, update_stat); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2400 | info->bytes += right_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2401 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2402 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2403 | } |
| 2404 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2405 | /* See try_merge_free_space() comment. */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2406 | if (left_info && !left_info->bitmap && |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2407 | left_info->offset + left_info->bytes == offset && |
| 2408 | (!is_trimmed || btrfs_free_space_trimmed(left_info))) { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 2409 | unlink_free_space(ctl, left_info, update_stat); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2410 | info->offset = left_info->offset; |
| 2411 | info->bytes += left_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2412 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2413 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2414 | } |
| 2415 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2416 | return merged; |
| 2417 | } |
| 2418 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2419 | static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl, |
| 2420 | struct btrfs_free_space *info, |
| 2421 | bool update_stat) |
| 2422 | { |
| 2423 | struct btrfs_free_space *bitmap; |
| 2424 | unsigned long i; |
| 2425 | unsigned long j; |
| 2426 | const u64 end = info->offset + info->bytes; |
| 2427 | const u64 bitmap_offset = offset_to_bitmap(ctl, end); |
| 2428 | u64 bytes; |
| 2429 | |
| 2430 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2431 | if (!bitmap) |
| 2432 | return false; |
| 2433 | |
| 2434 | i = offset_to_bit(bitmap->offset, ctl->unit, end); |
| 2435 | j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i); |
| 2436 | if (j == i) |
| 2437 | return false; |
| 2438 | bytes = (j - i) * ctl->unit; |
| 2439 | info->bytes += bytes; |
| 2440 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2441 | /* See try_merge_free_space() comment. */ |
| 2442 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2443 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2444 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 2445 | bitmap_clear_bits(ctl, bitmap, end, bytes, update_stat); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2446 | |
| 2447 | if (!bitmap->bytes) |
| 2448 | free_bitmap(ctl, bitmap); |
| 2449 | |
| 2450 | return true; |
| 2451 | } |
| 2452 | |
| 2453 | static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl, |
| 2454 | struct btrfs_free_space *info, |
| 2455 | bool update_stat) |
| 2456 | { |
| 2457 | struct btrfs_free_space *bitmap; |
| 2458 | u64 bitmap_offset; |
| 2459 | unsigned long i; |
| 2460 | unsigned long j; |
| 2461 | unsigned long prev_j; |
| 2462 | u64 bytes; |
| 2463 | |
| 2464 | bitmap_offset = offset_to_bitmap(ctl, info->offset); |
| 2465 | /* If we're on a boundary, try the previous logical bitmap. */ |
| 2466 | if (bitmap_offset == info->offset) { |
| 2467 | if (info->offset == 0) |
| 2468 | return false; |
| 2469 | bitmap_offset = offset_to_bitmap(ctl, info->offset - 1); |
| 2470 | } |
| 2471 | |
| 2472 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2473 | if (!bitmap) |
| 2474 | return false; |
| 2475 | |
| 2476 | i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1; |
| 2477 | j = 0; |
| 2478 | prev_j = (unsigned long)-1; |
| 2479 | for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) { |
| 2480 | if (j > i) |
| 2481 | break; |
| 2482 | prev_j = j; |
| 2483 | } |
| 2484 | if (prev_j == i) |
| 2485 | return false; |
| 2486 | |
| 2487 | if (prev_j == (unsigned long)-1) |
| 2488 | bytes = (i + 1) * ctl->unit; |
| 2489 | else |
| 2490 | bytes = (i - prev_j) * ctl->unit; |
| 2491 | |
| 2492 | info->offset -= bytes; |
| 2493 | info->bytes += bytes; |
| 2494 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2495 | /* See try_merge_free_space() comment. */ |
| 2496 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2497 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2498 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 2499 | bitmap_clear_bits(ctl, bitmap, info->offset, bytes, update_stat); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2500 | |
| 2501 | if (!bitmap->bytes) |
| 2502 | free_bitmap(ctl, bitmap); |
| 2503 | |
| 2504 | return true; |
| 2505 | } |
| 2506 | |
| 2507 | /* |
| 2508 | * We prefer always to allocate from extent entries, both for clustered and |
| 2509 | * non-clustered allocation requests. So when attempting to add a new extent |
| 2510 | * entry, try to see if there's adjacent free space in bitmap entries, and if |
| 2511 | * there is, migrate that space from the bitmaps to the extent. |
| 2512 | * Like this we get better chances of satisfying space allocation requests |
| 2513 | * because we attempt to satisfy them based on a single cache entry, and never |
| 2514 | * on 2 or more entries - even if the entries represent a contiguous free space |
| 2515 | * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry |
| 2516 | * ends). |
| 2517 | */ |
| 2518 | static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2519 | struct btrfs_free_space *info, |
| 2520 | bool update_stat) |
| 2521 | { |
| 2522 | /* |
| 2523 | * Only work with disconnected entries, as we can change their offset, |
| 2524 | * and must be extent entries. |
| 2525 | */ |
| 2526 | ASSERT(!info->bitmap); |
| 2527 | ASSERT(RB_EMPTY_NODE(&info->offset_index)); |
| 2528 | |
| 2529 | if (ctl->total_bitmaps > 0) { |
| 2530 | bool stole_end; |
| 2531 | bool stole_front = false; |
| 2532 | |
| 2533 | stole_end = steal_from_bitmap_to_end(ctl, info, update_stat); |
| 2534 | if (ctl->total_bitmaps > 0) |
| 2535 | stole_front = steal_from_bitmap_to_front(ctl, info, |
| 2536 | update_stat); |
| 2537 | |
| 2538 | if (stole_end || stole_front) |
| 2539 | try_merge_free_space(ctl, info, update_stat); |
| 2540 | } |
| 2541 | } |
| 2542 | |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 2543 | int __btrfs_add_free_space(struct btrfs_block_group *block_group, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2544 | u64 offset, u64 bytes, |
| 2545 | enum btrfs_trim_state trim_state) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2546 | { |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 2547 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 2548 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2549 | struct btrfs_free_space *info; |
| 2550 | int ret = 0; |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2551 | u64 filter_bytes = bytes; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2552 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2553 | ASSERT(!btrfs_is_zoned(fs_info)); |
| 2554 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2555 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2556 | if (!info) |
| 2557 | return -ENOMEM; |
| 2558 | |
| 2559 | info->offset = offset; |
| 2560 | info->bytes = bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2561 | info->trim_state = trim_state; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2562 | RB_CLEAR_NODE(&info->offset_index); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2563 | RB_CLEAR_NODE(&info->bytes_index); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2564 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2565 | spin_lock(&ctl->tree_lock); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2566 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2567 | if (try_merge_free_space(ctl, info, true)) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2568 | goto link; |
| 2569 | |
| 2570 | /* |
| 2571 | * There was no extent directly to the left or right of this new |
| 2572 | * extent then we know we're going to have to allocate a new extent, so |
| 2573 | * before we do that see if we need to drop this into a bitmap |
| 2574 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2575 | ret = insert_into_bitmap(ctl, info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2576 | if (ret < 0) { |
| 2577 | goto out; |
| 2578 | } else if (ret) { |
| 2579 | ret = 0; |
| 2580 | goto out; |
| 2581 | } |
| 2582 | link: |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2583 | /* |
| 2584 | * Only steal free space from adjacent bitmaps if we're sure we're not |
| 2585 | * going to add the new free space to existing bitmap entries - because |
| 2586 | * that would mean unnecessary work that would be reverted. Therefore |
| 2587 | * attempt to steal space from bitmaps if we're adding an extent entry. |
| 2588 | */ |
| 2589 | steal_from_bitmap(ctl, info, true); |
| 2590 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2591 | filter_bytes = max(filter_bytes, info->bytes); |
| 2592 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2593 | ret = link_free_space(ctl, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2594 | if (ret) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2595 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2596 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2597 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2598 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2599 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2600 | if (ret) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2601 | btrfs_crit(fs_info, "unable to add free space :%d", ret); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2602 | ASSERT(ret != -EEXIST); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2603 | } |
| 2604 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2605 | if (trim_state != BTRFS_TRIM_STATE_TRIMMED) { |
| 2606 | btrfs_discard_check_filter(block_group, filter_bytes); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2607 | btrfs_discard_queue_work(&fs_info->discard_ctl, block_group); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2608 | } |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2609 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2610 | return ret; |
| 2611 | } |
| 2612 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2613 | static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, |
| 2614 | u64 bytenr, u64 size, bool used) |
| 2615 | { |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2616 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2617 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 2618 | u64 offset = bytenr - block_group->start; |
| 2619 | u64 to_free, to_unusable; |
Johannes Thumshirn | 77233c2 | 2021-08-09 20:41:17 +0900 | [diff] [blame] | 2620 | const int bg_reclaim_threshold = READ_ONCE(fs_info->bg_reclaim_threshold); |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2621 | bool initial = (size == block_group->length); |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2622 | u64 reclaimable_unusable; |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2623 | |
| 2624 | WARN_ON(!initial && offset + size > block_group->zone_capacity); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2625 | |
| 2626 | spin_lock(&ctl->tree_lock); |
| 2627 | if (!used) |
| 2628 | to_free = size; |
Naohiro Aota | 9817325 | 2021-08-19 21:19:10 +0900 | [diff] [blame] | 2629 | else if (initial) |
| 2630 | to_free = block_group->zone_capacity; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2631 | else if (offset >= block_group->alloc_offset) |
| 2632 | to_free = size; |
| 2633 | else if (offset + size <= block_group->alloc_offset) |
| 2634 | to_free = 0; |
| 2635 | else |
| 2636 | to_free = offset + size - block_group->alloc_offset; |
| 2637 | to_unusable = size - to_free; |
| 2638 | |
| 2639 | ctl->free_space += to_free; |
Naohiro Aota | badae9c | 2021-03-03 17:55:48 +0900 | [diff] [blame] | 2640 | /* |
| 2641 | * If the block group is read-only, we should account freed space into |
| 2642 | * bytes_readonly. |
| 2643 | */ |
| 2644 | if (!block_group->ro) |
| 2645 | block_group->zone_unusable += to_unusable; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2646 | spin_unlock(&ctl->tree_lock); |
| 2647 | if (!used) { |
| 2648 | spin_lock(&block_group->lock); |
| 2649 | block_group->alloc_offset -= size; |
| 2650 | spin_unlock(&block_group->lock); |
| 2651 | } |
| 2652 | |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2653 | reclaimable_unusable = block_group->zone_unusable - |
| 2654 | (block_group->length - block_group->zone_capacity); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2655 | /* All the region is now unusable. Mark it as unused and reclaim */ |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2656 | if (block_group->zone_unusable == block_group->length) { |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2657 | btrfs_mark_bg_unused(block_group); |
Johannes Thumshirn | 77233c2 | 2021-08-09 20:41:17 +0900 | [diff] [blame] | 2658 | } else if (bg_reclaim_threshold && |
Naohiro Aota | d8da0e8 | 2021-08-19 21:19:11 +0900 | [diff] [blame] | 2659 | reclaimable_unusable >= |
| 2660 | div_factor_fine(block_group->zone_capacity, |
| 2661 | bg_reclaim_threshold)) { |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2662 | btrfs_mark_bg_to_reclaim(block_group); |
| 2663 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2664 | |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2668 | int btrfs_add_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2669 | u64 bytenr, u64 size) |
| 2670 | { |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2671 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2672 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2673 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2674 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2675 | true); |
| 2676 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2677 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC)) |
| 2678 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2679 | |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 2680 | return __btrfs_add_free_space(block_group, bytenr, size, trim_state); |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2681 | } |
| 2682 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2683 | int btrfs_add_free_space_unused(struct btrfs_block_group *block_group, |
| 2684 | u64 bytenr, u64 size) |
| 2685 | { |
| 2686 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2687 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2688 | false); |
| 2689 | |
| 2690 | return btrfs_add_free_space(block_group, bytenr, size); |
| 2691 | } |
| 2692 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2693 | /* |
| 2694 | * This is a subtle distinction because when adding free space back in general, |
| 2695 | * we want it to be added as untrimmed for async. But in the case where we add |
| 2696 | * it on loading of a block group, we want to consider it trimmed. |
| 2697 | */ |
| 2698 | int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group, |
| 2699 | u64 bytenr, u64 size) |
| 2700 | { |
| 2701 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2702 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2703 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2704 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2705 | true); |
| 2706 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2707 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) || |
| 2708 | btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC)) |
| 2709 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2710 | |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 2711 | return __btrfs_add_free_space(block_group, bytenr, size, trim_state); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2712 | } |
| 2713 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2714 | int btrfs_remove_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2715 | u64 offset, u64 bytes) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2716 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2717 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2718 | struct btrfs_free_space *info; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2719 | int ret; |
| 2720 | bool re_search = false; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2721 | |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2722 | if (btrfs_is_zoned(block_group->fs_info)) { |
| 2723 | /* |
| 2724 | * This can happen with conventional zones when replaying log. |
| 2725 | * Since the allocation info of tree-log nodes are not recorded |
| 2726 | * to the extent-tree, calculate_alloc_pointer() failed to |
| 2727 | * advance the allocation pointer after last allocated tree log |
| 2728 | * node blocks. |
| 2729 | * |
| 2730 | * This function is called from |
| 2731 | * btrfs_pin_extent_for_log_replay() when replaying the log. |
| 2732 | * Advance the pointer not to overwrite the tree-log nodes. |
| 2733 | */ |
Naohiro Aota | 0ae79c6 | 2021-08-09 13:13:44 +0900 | [diff] [blame] | 2734 | if (block_group->start + block_group->alloc_offset < |
| 2735 | offset + bytes) { |
| 2736 | block_group->alloc_offset = |
| 2737 | offset + bytes - block_group->start; |
| 2738 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2739 | return 0; |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2740 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2741 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2742 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2743 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2744 | again: |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2745 | ret = 0; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2746 | if (!bytes) |
| 2747 | goto out_lock; |
| 2748 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2749 | info = tree_search_offset(ctl, offset, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2750 | if (!info) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2751 | /* |
| 2752 | * oops didn't find an extent that matched the space we wanted |
| 2753 | * to remove, look for a bitmap instead |
| 2754 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2755 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2756 | 1, 0); |
| 2757 | if (!info) { |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2758 | /* |
| 2759 | * If we found a partial bit of our free space in a |
| 2760 | * bitmap but then couldn't find the other part this may |
| 2761 | * be a problem, so WARN about it. |
Chris Mason | 24a7031 | 2011-11-21 09:39:11 -0500 | [diff] [blame] | 2762 | */ |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2763 | WARN_ON(re_search); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2764 | goto out_lock; |
| 2765 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2766 | } |
| 2767 | |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2768 | re_search = false; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2769 | if (!info->bitmap) { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 2770 | unlink_free_space(ctl, info, true); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2771 | if (offset == info->offset) { |
| 2772 | u64 to_free = min(bytes, info->bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2773 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2774 | info->bytes -= to_free; |
| 2775 | info->offset += to_free; |
| 2776 | if (info->bytes) { |
| 2777 | ret = link_free_space(ctl, info); |
| 2778 | WARN_ON(ret); |
| 2779 | } else { |
| 2780 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2781 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2782 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2783 | offset += to_free; |
| 2784 | bytes -= to_free; |
| 2785 | goto again; |
| 2786 | } else { |
| 2787 | u64 old_end = info->bytes + info->offset; |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 2788 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2789 | info->bytes = offset - info->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2790 | ret = link_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2791 | WARN_ON(ret); |
| 2792 | if (ret) |
| 2793 | goto out_lock; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2794 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2795 | /* Not enough bytes in this entry to satisfy us */ |
| 2796 | if (old_end < offset + bytes) { |
| 2797 | bytes -= old_end - offset; |
| 2798 | offset = old_end; |
| 2799 | goto again; |
| 2800 | } else if (old_end == offset + bytes) { |
| 2801 | /* all done */ |
| 2802 | goto out_lock; |
| 2803 | } |
| 2804 | spin_unlock(&ctl->tree_lock); |
| 2805 | |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 2806 | ret = __btrfs_add_free_space(block_group, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2807 | offset + bytes, |
| 2808 | old_end - (offset + bytes), |
| 2809 | info->trim_state); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2810 | WARN_ON(ret); |
| 2811 | goto out; |
| 2812 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2813 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2814 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2815 | ret = remove_from_bitmap(ctl, info, &offset, &bytes); |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2816 | if (ret == -EAGAIN) { |
| 2817 | re_search = true; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2818 | goto again; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2819 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2820 | out_lock: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2821 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2822 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2823 | out: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2824 | return ret; |
| 2825 | } |
| 2826 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2827 | void btrfs_dump_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2828 | u64 bytes) |
| 2829 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2830 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2831 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2832 | struct btrfs_free_space *info; |
| 2833 | struct rb_node *n; |
| 2834 | int count = 0; |
| 2835 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2836 | /* |
| 2837 | * Zoned btrfs does not use free space tree and cluster. Just print |
| 2838 | * out the free space after the allocation offset. |
| 2839 | */ |
| 2840 | if (btrfs_is_zoned(fs_info)) { |
Naohiro Aota | afba2bc | 2021-08-19 21:19:17 +0900 | [diff] [blame] | 2841 | btrfs_info(fs_info, "free space %llu active %d", |
| 2842 | block_group->zone_capacity - block_group->alloc_offset, |
| 2843 | block_group->zone_is_active); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2844 | return; |
| 2845 | } |
| 2846 | |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2847 | spin_lock(&ctl->tree_lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2848 | 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] | 2849 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
Liu Bo | f6175ef | 2012-07-06 03:31:36 -0600 | [diff] [blame] | 2850 | if (info->bytes >= bytes && !block_group->ro) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2851 | count++; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2852 | btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2853 | info->offset, info->bytes, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2854 | (info->bitmap) ? "yes" : "no"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2855 | } |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2856 | spin_unlock(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2857 | btrfs_info(fs_info, "block group has cluster?: %s", |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2858 | list_empty(&block_group->cluster_list) ? "no" : "yes"); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2859 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2860 | "%d blocks of free space at or bigger than bytes is", count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2861 | } |
| 2862 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 2863 | void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group, |
| 2864 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2865 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2866 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2867 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2868 | spin_lock_init(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2869 | ctl->unit = fs_info->sectorsize; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2870 | ctl->start = block_group->start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2871 | ctl->private = block_group; |
| 2872 | ctl->op = &free_space_op; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2873 | ctl->free_space_bytes = RB_ROOT_CACHED; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 2874 | INIT_LIST_HEAD(&ctl->trimming_ranges); |
| 2875 | mutex_init(&ctl->cache_writeout_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2876 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2877 | /* |
| 2878 | * we only want to have 32k of ram per block group for keeping |
| 2879 | * track of free space, and if we pass 1/2 of that we want to |
| 2880 | * start converting things over to using bitmaps |
| 2881 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2882 | ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2883 | } |
| 2884 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2885 | /* |
| 2886 | * for a given cluster, put all of its extents back into the free |
| 2887 | * space cache. If the block group passed doesn't match the block group |
| 2888 | * pointed to by the cluster, someone else raced in and freed the |
| 2889 | * cluster already. In that case, we just return without changing anything |
| 2890 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 2891 | static void __btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2892 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2893 | struct btrfs_free_cluster *cluster) |
| 2894 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2895 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2896 | struct btrfs_free_space *entry; |
| 2897 | struct rb_node *node; |
| 2898 | |
| 2899 | spin_lock(&cluster->lock); |
Josef Bacik | 95c85fb | 2021-01-25 16:42:35 -0500 | [diff] [blame] | 2900 | if (cluster->block_group != block_group) { |
| 2901 | spin_unlock(&cluster->lock); |
| 2902 | return; |
| 2903 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2904 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2905 | cluster->block_group = NULL; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2906 | cluster->window_start = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2907 | list_del_init(&cluster->block_group_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2908 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2909 | node = rb_first(&cluster->root); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2910 | while (node) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2911 | bool bitmap; |
| 2912 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2913 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2914 | node = rb_next(&entry->offset_index); |
| 2915 | rb_erase(&entry->offset_index, &cluster->root); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2916 | RB_CLEAR_NODE(&entry->offset_index); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2917 | |
| 2918 | bitmap = (entry->bitmap != NULL); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2919 | if (!bitmap) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2920 | /* Merging treats extents as if they were new */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2921 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2922 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2923 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= |
| 2924 | entry->bytes; |
| 2925 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2926 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2927 | try_merge_free_space(ctl, entry, false); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2928 | steal_from_bitmap(ctl, entry, false); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2929 | |
| 2930 | /* As we insert directly, update these statistics */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2931 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2932 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2933 | ctl->discardable_bytes[BTRFS_STAT_CURR] += |
| 2934 | entry->bytes; |
| 2935 | } |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2936 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2937 | tree_insert_offset(&ctl->free_space_offset, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2938 | entry->offset, &entry->offset_index, bitmap); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 2939 | rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes, |
| 2940 | entry_less); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2941 | } |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 2942 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2943 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2944 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2945 | } |
| 2946 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2947 | static void __btrfs_remove_free_space_cache_locked( |
| 2948 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2949 | { |
| 2950 | struct btrfs_free_space *info; |
| 2951 | struct rb_node *node; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2952 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2953 | while ((node = rb_last(&ctl->free_space_offset)) != NULL) { |
| 2954 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
Josef Bacik | 9b90f51 | 2011-06-24 16:02:51 +0000 | [diff] [blame] | 2955 | if (!info->bitmap) { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 2956 | unlink_free_space(ctl, info, true); |
Josef Bacik | 9b90f51 | 2011-06-24 16:02:51 +0000 | [diff] [blame] | 2957 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2958 | } else { |
| 2959 | free_bitmap(ctl, info); |
| 2960 | } |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 2961 | |
| 2962 | cond_resched_lock(&ctl->tree_lock); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2963 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) |
| 2967 | { |
| 2968 | spin_lock(&ctl->tree_lock); |
| 2969 | __btrfs_remove_free_space_cache_locked(ctl); |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 2970 | if (ctl->private) |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2971 | btrfs_discard_update_discardable(ctl->private); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2972 | spin_unlock(&ctl->tree_lock); |
| 2973 | } |
| 2974 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2975 | void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2976 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2977 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2978 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2979 | struct list_head *head; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2980 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2981 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2982 | while ((head = block_group->cluster_list.next) != |
| 2983 | &block_group->cluster_list) { |
| 2984 | cluster = list_entry(head, struct btrfs_free_cluster, |
| 2985 | block_group_list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2986 | |
| 2987 | WARN_ON(cluster->block_group != block_group); |
| 2988 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 2989 | |
| 2990 | cond_resched_lock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2991 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 2992 | __btrfs_remove_free_space_cache_locked(ctl); |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2993 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2994 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2995 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2996 | } |
| 2997 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 2998 | /** |
| 2999 | * btrfs_is_free_space_trimmed - see if everything is trimmed |
| 3000 | * @block_group: block_group of interest |
| 3001 | * |
| 3002 | * Walk @block_group's free space rb_tree to determine if everything is trimmed. |
| 3003 | */ |
| 3004 | bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group) |
| 3005 | { |
| 3006 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 3007 | struct btrfs_free_space *info; |
| 3008 | struct rb_node *node; |
| 3009 | bool ret = true; |
| 3010 | |
| 3011 | spin_lock(&ctl->tree_lock); |
| 3012 | node = rb_first(&ctl->free_space_offset); |
| 3013 | |
| 3014 | while (node) { |
| 3015 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3016 | |
| 3017 | if (!btrfs_free_space_trimmed(info)) { |
| 3018 | ret = false; |
| 3019 | break; |
| 3020 | } |
| 3021 | |
| 3022 | node = rb_next(node); |
| 3023 | } |
| 3024 | |
| 3025 | spin_unlock(&ctl->tree_lock); |
| 3026 | return ret; |
| 3027 | } |
| 3028 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3029 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3030 | u64 offset, u64 bytes, u64 empty_size, |
| 3031 | u64 *max_extent_size) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3032 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3033 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3034 | struct btrfs_discard_ctl *discard_ctl = |
| 3035 | &block_group->fs_info->discard_ctl; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3036 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3037 | u64 bytes_search = bytes + empty_size; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3038 | u64 ret = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3039 | u64 align_gap = 0; |
| 3040 | u64 align_gap_len = 0; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3041 | enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3042 | bool use_bytes_index = (offset == block_group->start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3043 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3044 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3045 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3046 | spin_lock(&ctl->tree_lock); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3047 | entry = find_free_space(ctl, &offset, &bytes_search, |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3048 | block_group->full_stripe_len, max_extent_size, |
| 3049 | use_bytes_index); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3050 | if (!entry) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3051 | goto out; |
| 3052 | |
| 3053 | ret = offset; |
| 3054 | if (entry->bitmap) { |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 3055 | bitmap_clear_bits(ctl, entry, offset, bytes, true); |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3056 | |
| 3057 | if (!btrfs_free_space_trimmed(entry)) |
| 3058 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3059 | |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 3060 | if (!entry->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3061 | free_bitmap(ctl, entry); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3062 | } else { |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 3063 | unlink_free_space(ctl, entry, true); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3064 | align_gap_len = offset - entry->offset; |
| 3065 | align_gap = entry->offset; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3066 | align_gap_trim_state = entry->trim_state; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3067 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3068 | if (!btrfs_free_space_trimmed(entry)) |
| 3069 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3070 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3071 | entry->offset = offset + bytes; |
| 3072 | WARN_ON(entry->bytes < bytes + align_gap_len); |
| 3073 | |
| 3074 | entry->bytes -= bytes + align_gap_len; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3075 | if (!entry->bytes) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 3076 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3077 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3078 | link_free_space(ctl, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3079 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3080 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 3081 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3082 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3083 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3084 | if (align_gap_len) |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 3085 | __btrfs_add_free_space(block_group, align_gap, align_gap_len, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 3086 | align_gap_trim_state); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3087 | return ret; |
| 3088 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3089 | |
| 3090 | /* |
| 3091 | * given a cluster, put all of its extents back into the free space |
| 3092 | * cache. If a block group is passed, this function will only free |
| 3093 | * a cluster that belongs to the passed block group. |
| 3094 | * |
| 3095 | * Otherwise, it'll get a reference on the block group pointed to by the |
| 3096 | * cluster and remove the cluster from it. |
| 3097 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3098 | void btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3099 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3100 | struct btrfs_free_cluster *cluster) |
| 3101 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3102 | struct btrfs_free_space_ctl *ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3103 | |
| 3104 | /* first, get a safe pointer to the block group */ |
| 3105 | spin_lock(&cluster->lock); |
| 3106 | if (!block_group) { |
| 3107 | block_group = cluster->block_group; |
| 3108 | if (!block_group) { |
| 3109 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3110 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3111 | } |
| 3112 | } else if (cluster->block_group != block_group) { |
| 3113 | /* someone else has already freed it don't redo their work */ |
| 3114 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3115 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3116 | } |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3117 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3118 | spin_unlock(&cluster->lock); |
| 3119 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3120 | ctl = block_group->free_space_ctl; |
| 3121 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3122 | /* now return any extents the cluster had on it */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3123 | spin_lock(&ctl->tree_lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3124 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3125 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3126 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 3127 | btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group); |
| 3128 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3129 | /* finally drop our ref */ |
| 3130 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3131 | } |
| 3132 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3133 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3134 | struct btrfs_free_cluster *cluster, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3135 | struct btrfs_free_space *entry, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3136 | u64 bytes, u64 min_start, |
| 3137 | u64 *max_extent_size) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3138 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3139 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3140 | int err; |
| 3141 | u64 search_start = cluster->window_start; |
| 3142 | u64 search_bytes = bytes; |
| 3143 | u64 ret = 0; |
| 3144 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3145 | search_start = min_start; |
| 3146 | search_bytes = bytes; |
| 3147 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3148 | err = search_bitmap(ctl, entry, &search_start, &search_bytes, true); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3149 | if (err) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3150 | *max_extent_size = max(get_max_extent_size(entry), |
| 3151 | *max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3152 | return 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3153 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3154 | |
| 3155 | ret = search_start; |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 3156 | bitmap_clear_bits(ctl, entry, ret, bytes, false); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3157 | |
| 3158 | return ret; |
| 3159 | } |
| 3160 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3161 | /* |
| 3162 | * given a cluster, try to allocate 'bytes' from it, returns 0 |
| 3163 | * if it couldn't find anything suitably large, or a logical disk offset |
| 3164 | * if things worked out |
| 3165 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3166 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3167 | struct btrfs_free_cluster *cluster, u64 bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3168 | u64 min_start, u64 *max_extent_size) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3169 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3170 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3171 | struct btrfs_discard_ctl *discard_ctl = |
| 3172 | &block_group->fs_info->discard_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3173 | struct btrfs_free_space *entry = NULL; |
| 3174 | struct rb_node *node; |
| 3175 | u64 ret = 0; |
| 3176 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3177 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3178 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3179 | spin_lock(&cluster->lock); |
| 3180 | if (bytes > cluster->max_size) |
| 3181 | goto out; |
| 3182 | |
| 3183 | if (cluster->block_group != block_group) |
| 3184 | goto out; |
| 3185 | |
| 3186 | node = rb_first(&cluster->root); |
| 3187 | if (!node) |
| 3188 | goto out; |
| 3189 | |
| 3190 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 3191 | while (1) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3192 | if (entry->bytes < bytes) |
| 3193 | *max_extent_size = max(get_max_extent_size(entry), |
| 3194 | *max_extent_size); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3195 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3196 | if (entry->bytes < bytes || |
| 3197 | (!entry->bitmap && entry->offset < min_start)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3198 | node = rb_next(&entry->offset_index); |
| 3199 | if (!node) |
| 3200 | break; |
| 3201 | entry = rb_entry(node, struct btrfs_free_space, |
| 3202 | offset_index); |
| 3203 | continue; |
| 3204 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3205 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3206 | if (entry->bitmap) { |
| 3207 | ret = btrfs_alloc_from_bitmap(block_group, |
| 3208 | cluster, entry, bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3209 | cluster->window_start, |
| 3210 | max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3211 | if (ret == 0) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3212 | node = rb_next(&entry->offset_index); |
| 3213 | if (!node) |
| 3214 | break; |
| 3215 | entry = rb_entry(node, struct btrfs_free_space, |
| 3216 | offset_index); |
| 3217 | continue; |
| 3218 | } |
Josef Bacik | 9b23062 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3219 | cluster->window_start += bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3220 | } else { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3221 | ret = entry->offset; |
| 3222 | |
| 3223 | entry->offset += bytes; |
| 3224 | entry->bytes -= bytes; |
| 3225 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3226 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3227 | break; |
| 3228 | } |
| 3229 | out: |
| 3230 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3231 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3232 | if (!ret) |
| 3233 | return 0; |
| 3234 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3235 | spin_lock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3236 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3237 | if (!btrfs_free_space_trimmed(entry)) |
| 3238 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3239 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3240 | ctl->free_space -= bytes; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3241 | if (!entry->bitmap && !btrfs_free_space_trimmed(entry)) |
| 3242 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3243 | |
| 3244 | spin_lock(&cluster->lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3245 | if (entry->bytes == 0) { |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3246 | rb_erase(&entry->offset_index, &cluster->root); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3247 | ctl->free_extents--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3248 | if (entry->bitmap) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 3249 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 3250 | entry->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3251 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 3252 | recalculate_thresholds(ctl); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3253 | } else if (!btrfs_free_space_trimmed(entry)) { |
| 3254 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3255 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 3256 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3257 | } |
| 3258 | |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3259 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3260 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3261 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3262 | return ret; |
| 3263 | } |
| 3264 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3265 | static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3266 | struct btrfs_free_space *entry, |
| 3267 | struct btrfs_free_cluster *cluster, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3268 | u64 offset, u64 bytes, |
| 3269 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3270 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3271 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3272 | unsigned long next_zero; |
| 3273 | unsigned long i; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3274 | unsigned long want_bits; |
| 3275 | unsigned long min_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3276 | unsigned long found_bits; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3277 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3278 | unsigned long start = 0; |
| 3279 | unsigned long total_found = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3280 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3281 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3282 | i = offset_to_bit(entry->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3283 | max_t(u64, offset, entry->offset)); |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3284 | want_bits = bytes_to_bits(bytes, ctl->unit); |
| 3285 | min_bits = bytes_to_bits(min_bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3286 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3287 | /* |
| 3288 | * Don't bother looking for a cluster in this bitmap if it's heavily |
| 3289 | * fragmented. |
| 3290 | */ |
| 3291 | if (entry->max_extent_size && |
| 3292 | entry->max_extent_size < cont1_bytes) |
| 3293 | return -ENOSPC; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3294 | again: |
| 3295 | found_bits = 0; |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 3296 | for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3297 | next_zero = find_next_zero_bit(entry->bitmap, |
| 3298 | BITS_PER_BITMAP, i); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3299 | if (next_zero - i >= min_bits) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3300 | found_bits = next_zero - i; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3301 | if (found_bits > max_bits) |
| 3302 | max_bits = found_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3303 | break; |
| 3304 | } |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3305 | if (next_zero - i > max_bits) |
| 3306 | max_bits = next_zero - i; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3307 | i = next_zero; |
| 3308 | } |
| 3309 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3310 | if (!found_bits) { |
| 3311 | entry->max_extent_size = (u64)max_bits * ctl->unit; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3312 | return -ENOSPC; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3313 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3314 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3315 | if (!total_found) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3316 | start = i; |
Alexandre Oliva | b78d09b | 2011-11-30 13:43:00 -0500 | [diff] [blame] | 3317 | cluster->max_size = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | total_found += found_bits; |
| 3321 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3322 | if (cluster->max_size < found_bits * ctl->unit) |
| 3323 | cluster->max_size = found_bits * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3324 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3325 | if (total_found < want_bits || cluster->max_size < cont1_bytes) { |
| 3326 | i = next_zero + 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3327 | goto again; |
| 3328 | } |
| 3329 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3330 | cluster->window_start = start * ctl->unit + entry->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3331 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3332 | rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); |
| 3333 | |
| 3334 | /* |
| 3335 | * We need to know if we're currently on the normal space index when we |
| 3336 | * manipulate the bitmap so that we know we need to remove and re-insert |
| 3337 | * it into the space_index tree. Clear the bytes_index node here so the |
| 3338 | * bitmap manipulation helpers know not to mess with the space_index |
| 3339 | * until this bitmap entry is added back into the normal cache. |
| 3340 | */ |
| 3341 | RB_CLEAR_NODE(&entry->bytes_index); |
| 3342 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3343 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3344 | &entry->offset_index, 1); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3345 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3346 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3347 | trace_btrfs_setup_cluster(block_group, cluster, |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3348 | total_found * ctl->unit, 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3349 | return 0; |
| 3350 | } |
| 3351 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3352 | /* |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3353 | * 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] | 3354 | * Try to find a cluster with at least bytes total bytes, at least one |
| 3355 | * extent of cont1_bytes, and other clusters of at least min_bytes. |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3356 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3357 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3358 | setup_cluster_no_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3359 | struct btrfs_free_cluster *cluster, |
| 3360 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3361 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3362 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3363 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3364 | struct btrfs_free_space *first = NULL; |
| 3365 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3366 | struct btrfs_free_space *last; |
| 3367 | struct rb_node *node; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3368 | u64 window_free; |
| 3369 | u64 max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3370 | u64 total_size = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3371 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3372 | entry = tree_search_offset(ctl, offset, 0, 1); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3373 | if (!entry) |
| 3374 | return -ENOSPC; |
| 3375 | |
| 3376 | /* |
| 3377 | * We don't want bitmaps, so just move along until we find a normal |
| 3378 | * extent entry. |
| 3379 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3380 | while (entry->bitmap || entry->bytes < min_bytes) { |
| 3381 | if (entry->bitmap && list_empty(&entry->list)) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3382 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3383 | node = rb_next(&entry->offset_index); |
| 3384 | if (!node) |
| 3385 | return -ENOSPC; |
| 3386 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3387 | } |
| 3388 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3389 | window_free = entry->bytes; |
| 3390 | max_extent = entry->bytes; |
| 3391 | first = entry; |
| 3392 | last = entry; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3393 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3394 | for (node = rb_next(&entry->offset_index); node; |
| 3395 | node = rb_next(&entry->offset_index)) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3396 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3397 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3398 | if (entry->bitmap) { |
| 3399 | if (list_empty(&entry->list)) |
| 3400 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3401 | continue; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3402 | } |
| 3403 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3404 | if (entry->bytes < min_bytes) |
| 3405 | continue; |
| 3406 | |
| 3407 | last = entry; |
| 3408 | window_free += entry->bytes; |
| 3409 | if (entry->bytes > max_extent) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3410 | max_extent = entry->bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3411 | } |
| 3412 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3413 | if (window_free < bytes || max_extent < cont1_bytes) |
| 3414 | return -ENOSPC; |
| 3415 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3416 | cluster->window_start = first->offset; |
| 3417 | |
| 3418 | node = &first->offset_index; |
| 3419 | |
| 3420 | /* |
| 3421 | * now we've found our entries, pull them out of the free space |
| 3422 | * cache and put them into the cluster rbtree |
| 3423 | */ |
| 3424 | do { |
| 3425 | int ret; |
| 3426 | |
| 3427 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3428 | node = rb_next(&entry->offset_index); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3429 | if (entry->bitmap || entry->bytes < min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3430 | continue; |
| 3431 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3432 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 59c7b56 | 2021-11-18 16:33:15 -0500 | [diff] [blame] | 3433 | rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3434 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3435 | &entry->offset_index, 0); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3436 | total_size += entry->bytes; |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3437 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3438 | } while (node && entry != last); |
| 3439 | |
| 3440 | cluster->max_size = max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3441 | trace_btrfs_setup_cluster(block_group, cluster, total_size, 0); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3442 | return 0; |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * This specifically looks for bitmaps that may work in the cluster, we assume |
| 3447 | * that we have already failed to find extents that will work. |
| 3448 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3449 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3450 | setup_cluster_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3451 | struct btrfs_free_cluster *cluster, |
| 3452 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3453 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3454 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3455 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3456 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3457 | int ret = -ENOSPC; |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3458 | u64 bitmap_offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3459 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3460 | if (ctl->total_bitmaps == 0) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3461 | return -ENOSPC; |
| 3462 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3463 | /* |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3464 | * The bitmap that covers offset won't be in the list unless offset |
| 3465 | * is just its start offset. |
| 3466 | */ |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3467 | if (!list_empty(bitmaps)) |
| 3468 | entry = list_first_entry(bitmaps, struct btrfs_free_space, list); |
| 3469 | |
| 3470 | if (!entry || entry->offset != bitmap_offset) { |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3471 | entry = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 3472 | if (entry && list_empty(&entry->list)) |
| 3473 | list_add(&entry->list, bitmaps); |
| 3474 | } |
| 3475 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3476 | list_for_each_entry(entry, bitmaps, list) { |
Josef Bacik | 357b978 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 3477 | if (entry->bytes < bytes) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3478 | continue; |
| 3479 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3480 | bytes, cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3481 | if (!ret) |
| 3482 | return 0; |
| 3483 | } |
| 3484 | |
| 3485 | /* |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3486 | * The bitmaps list has all the bitmaps that record free space |
| 3487 | * starting after offset, so no more search is required. |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3488 | */ |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3489 | return -ENOSPC; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | /* |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3493 | * 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] | 3494 | * is to find at least bytes+empty_size. |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3495 | * We might not find them all in one contiguous area. |
| 3496 | * |
| 3497 | * returns zero and sets up cluster if things worked out, otherwise |
| 3498 | * it returns -enospc |
| 3499 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3500 | int btrfs_find_space_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3501 | struct btrfs_free_cluster *cluster, |
| 3502 | u64 offset, u64 bytes, u64 empty_size) |
| 3503 | { |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3504 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3505 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3506 | struct btrfs_free_space *entry, *tmp; |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3507 | LIST_HEAD(bitmaps); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3508 | u64 min_bytes; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3509 | u64 cont1_bytes; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3510 | int ret; |
| 3511 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3512 | /* |
| 3513 | * Choose the minimum extent size we'll require for this |
| 3514 | * cluster. For SSD_SPREAD, don't allow any fragmentation. |
| 3515 | * For metadata, allow allocates with smaller extents. For |
| 3516 | * data, keep it dense. |
| 3517 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3518 | if (btrfs_test_opt(fs_info, SSD_SPREAD)) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3519 | cont1_bytes = min_bytes = bytes + empty_size; |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 3520 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3521 | cont1_bytes = bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3522 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3523 | } else { |
| 3524 | cont1_bytes = max(bytes, (bytes + empty_size) >> 2); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3525 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3526 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3527 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3528 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3529 | |
| 3530 | /* |
| 3531 | * If we know we don't have enough space to make a cluster don't even |
| 3532 | * bother doing all the work to try and find one. |
| 3533 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3534 | if (ctl->free_space < bytes) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3535 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3536 | return -ENOSPC; |
| 3537 | } |
| 3538 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3539 | spin_lock(&cluster->lock); |
| 3540 | |
| 3541 | /* someone already found a cluster, hooray */ |
| 3542 | if (cluster->block_group) { |
| 3543 | ret = 0; |
| 3544 | goto out; |
| 3545 | } |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3546 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3547 | trace_btrfs_find_cluster(block_group, offset, bytes, empty_size, |
| 3548 | min_bytes); |
| 3549 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3550 | ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3551 | bytes + empty_size, |
| 3552 | cont1_bytes, min_bytes); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3553 | if (ret) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3554 | ret = setup_cluster_bitmap(block_group, cluster, &bitmaps, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3555 | offset, bytes + empty_size, |
| 3556 | cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3557 | |
| 3558 | /* Clear our temporary list */ |
| 3559 | list_for_each_entry_safe(entry, tmp, &bitmaps, list) |
| 3560 | list_del_init(&entry->list); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3561 | |
| 3562 | if (!ret) { |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3563 | btrfs_get_block_group(block_group); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3564 | list_add_tail(&cluster->block_group_list, |
| 3565 | &block_group->cluster_list); |
| 3566 | cluster->block_group = block_group; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3567 | } else { |
| 3568 | trace_btrfs_failed_cluster_setup(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3569 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3570 | out: |
| 3571 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3572 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3573 | |
| 3574 | return ret; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * simple code to zero out a cluster |
| 3579 | */ |
| 3580 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) |
| 3581 | { |
| 3582 | spin_lock_init(&cluster->lock); |
| 3583 | spin_lock_init(&cluster->refill_lock); |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 3584 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3585 | cluster->max_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3586 | cluster->fragmented = false; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3587 | INIT_LIST_HEAD(&cluster->block_group_list); |
| 3588 | cluster->block_group = NULL; |
| 3589 | } |
| 3590 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3591 | static int do_trimming(struct btrfs_block_group *block_group, |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3592 | u64 *total_trimmed, u64 start, u64 bytes, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3593 | u64 reserved_start, u64 reserved_bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3594 | enum btrfs_trim_state reserved_trim_state, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3595 | struct btrfs_trim_range *trim_entry) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3596 | { |
| 3597 | struct btrfs_space_info *space_info = block_group->space_info; |
| 3598 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3599 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3600 | int ret; |
| 3601 | int update = 0; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3602 | const u64 end = start + bytes; |
| 3603 | const u64 reserved_end = reserved_start + reserved_bytes; |
| 3604 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3605 | u64 trimmed = 0; |
| 3606 | |
| 3607 | spin_lock(&space_info->lock); |
| 3608 | spin_lock(&block_group->lock); |
| 3609 | if (!block_group->ro) { |
| 3610 | block_group->reserved += reserved_bytes; |
| 3611 | space_info->bytes_reserved += reserved_bytes; |
| 3612 | update = 1; |
| 3613 | } |
| 3614 | spin_unlock(&block_group->lock); |
| 3615 | spin_unlock(&space_info->lock); |
| 3616 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3617 | ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3618 | if (!ret) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3619 | *total_trimmed += trimmed; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3620 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 3621 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3622 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3623 | mutex_lock(&ctl->cache_writeout_mutex); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3624 | if (reserved_start < start) |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 3625 | __btrfs_add_free_space(block_group, reserved_start, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3626 | start - reserved_start, |
| 3627 | reserved_trim_state); |
| 3628 | if (start + bytes < reserved_start + reserved_bytes) |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 3629 | __btrfs_add_free_space(block_group, end, reserved_end - end, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3630 | reserved_trim_state); |
Nikolay Borisov | 290ef19 | 2021-11-23 14:44:21 +0200 | [diff] [blame^] | 3631 | __btrfs_add_free_space(block_group, start, bytes, trim_state); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3632 | list_del(&trim_entry->list); |
| 3633 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3634 | |
| 3635 | if (update) { |
| 3636 | spin_lock(&space_info->lock); |
| 3637 | spin_lock(&block_group->lock); |
| 3638 | if (block_group->ro) |
| 3639 | space_info->bytes_readonly += reserved_bytes; |
| 3640 | block_group->reserved -= reserved_bytes; |
| 3641 | space_info->bytes_reserved -= reserved_bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3642 | spin_unlock(&block_group->lock); |
Su Yue | 8f63a84 | 2018-11-28 11:21:12 +0800 | [diff] [blame] | 3643 | spin_unlock(&space_info->lock); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | return ret; |
| 3647 | } |
| 3648 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3649 | /* |
| 3650 | * If @async is set, then we will trim 1 region and return. |
| 3651 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3652 | static int trim_no_bitmap(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3653 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
| 3654 | bool async) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3655 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3656 | struct btrfs_discard_ctl *discard_ctl = |
| 3657 | &block_group->fs_info->discard_ctl; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3658 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3659 | struct btrfs_free_space *entry; |
| 3660 | struct rb_node *node; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3661 | int ret = 0; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3662 | u64 extent_start; |
| 3663 | u64 extent_bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3664 | enum btrfs_trim_state extent_trim_state; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3665 | u64 bytes; |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3666 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3667 | |
| 3668 | while (start < end) { |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3669 | struct btrfs_trim_range trim_entry; |
| 3670 | |
| 3671 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3672 | spin_lock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3673 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3674 | if (ctl->free_space < minlen) |
| 3675 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3676 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3677 | entry = tree_search_offset(ctl, start, 0, 1); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3678 | if (!entry) |
| 3679 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3680 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3681 | /* Skip bitmaps and if async, already trimmed entries */ |
| 3682 | while (entry->bitmap || |
| 3683 | (async && btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3684 | node = rb_next(&entry->offset_index); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3685 | if (!node) |
| 3686 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3687 | entry = rb_entry(node, struct btrfs_free_space, |
| 3688 | offset_index); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3691 | if (entry->offset >= end) |
| 3692 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3693 | |
| 3694 | extent_start = entry->offset; |
| 3695 | extent_bytes = entry->bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3696 | extent_trim_state = entry->trim_state; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3697 | if (async) { |
| 3698 | start = entry->offset; |
| 3699 | bytes = entry->bytes; |
| 3700 | if (bytes < minlen) { |
| 3701 | spin_unlock(&ctl->tree_lock); |
| 3702 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3703 | goto next; |
| 3704 | } |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 3705 | unlink_free_space(ctl, entry, true); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3706 | /* |
| 3707 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3708 | * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim |
| 3709 | * X when we come back around. So trim it now. |
| 3710 | */ |
| 3711 | if (max_discard_size && |
| 3712 | bytes >= (max_discard_size + |
| 3713 | BTRFS_ASYNC_DISCARD_MIN_FILTER)) { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3714 | bytes = max_discard_size; |
| 3715 | extent_bytes = max_discard_size; |
| 3716 | entry->offset += max_discard_size; |
| 3717 | entry->bytes -= max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3718 | link_free_space(ctl, entry); |
| 3719 | } else { |
| 3720 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3721 | } |
| 3722 | } else { |
| 3723 | start = max(start, extent_start); |
| 3724 | bytes = min(extent_start + extent_bytes, end) - start; |
| 3725 | if (bytes < minlen) { |
| 3726 | spin_unlock(&ctl->tree_lock); |
| 3727 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3728 | goto next; |
| 3729 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3730 | |
Nikolay Borisov | 32e1649 | 2021-11-23 14:44:20 +0200 | [diff] [blame] | 3731 | unlink_free_space(ctl, entry, true); |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3732 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3733 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3734 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3735 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3736 | trim_entry.start = extent_start; |
| 3737 | trim_entry.bytes = extent_bytes; |
| 3738 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3739 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3740 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3741 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3742 | extent_start, extent_bytes, extent_trim_state, |
| 3743 | &trim_entry); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3744 | if (ret) { |
| 3745 | block_group->discard_cursor = start + bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3746 | break; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3747 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3748 | next: |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3749 | start += bytes; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3750 | block_group->discard_cursor = start; |
| 3751 | if (async && *total_trimmed) |
| 3752 | break; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3753 | |
| 3754 | if (fatal_signal_pending(current)) { |
| 3755 | ret = -ERESTARTSYS; |
| 3756 | break; |
| 3757 | } |
| 3758 | |
| 3759 | cond_resched(); |
| 3760 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3761 | |
| 3762 | return ret; |
| 3763 | |
| 3764 | out_unlock: |
| 3765 | block_group->discard_cursor = btrfs_block_group_end(block_group); |
| 3766 | spin_unlock(&ctl->tree_lock); |
| 3767 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3768 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3769 | return ret; |
| 3770 | } |
| 3771 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3772 | /* |
| 3773 | * If we break out of trimming a bitmap prematurely, we should reset the |
| 3774 | * trimming bit. In a rather contrieved case, it's possible to race here so |
| 3775 | * reset the state to BTRFS_TRIM_STATE_UNTRIMMED. |
| 3776 | * |
| 3777 | * start = start of bitmap |
| 3778 | * end = near end of bitmap |
| 3779 | * |
| 3780 | * Thread 1: Thread 2: |
| 3781 | * trim_bitmaps(start) |
| 3782 | * trim_bitmaps(end) |
| 3783 | * end_trimming_bitmap() |
| 3784 | * reset_trimming_bitmap() |
| 3785 | */ |
| 3786 | static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset) |
| 3787 | { |
| 3788 | struct btrfs_free_space *entry; |
| 3789 | |
| 3790 | spin_lock(&ctl->tree_lock); |
| 3791 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3792 | if (entry) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3793 | if (btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3794 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 3795 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3796 | ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes; |
| 3797 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3798 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3801 | spin_unlock(&ctl->tree_lock); |
| 3802 | } |
| 3803 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3804 | static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl, |
| 3805 | struct btrfs_free_space *entry) |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3806 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3807 | if (btrfs_free_space_trimming_bitmap(entry)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3808 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3809 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 3810 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3811 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3812 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3813 | } |
| 3814 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3815 | /* |
| 3816 | * If @async is set, then we will trim 1 region and return. |
| 3817 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3818 | static int trim_bitmaps(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3819 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3820 | u64 maxlen, bool async) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3821 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3822 | struct btrfs_discard_ctl *discard_ctl = |
| 3823 | &block_group->fs_info->discard_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3824 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 3825 | struct btrfs_free_space *entry; |
| 3826 | int ret = 0; |
| 3827 | int ret2; |
| 3828 | u64 bytes; |
| 3829 | u64 offset = offset_to_bitmap(ctl, start); |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3830 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3831 | |
| 3832 | while (offset < end) { |
| 3833 | bool next_bitmap = false; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3834 | struct btrfs_trim_range trim_entry; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3835 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3836 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3837 | spin_lock(&ctl->tree_lock); |
| 3838 | |
| 3839 | if (ctl->free_space < minlen) { |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3840 | block_group->discard_cursor = |
| 3841 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3842 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3843 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3844 | break; |
| 3845 | } |
| 3846 | |
| 3847 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3848 | /* |
| 3849 | * Bitmaps are marked trimmed lossily now to prevent constant |
| 3850 | * discarding of the same bitmap (the reason why we are bound |
| 3851 | * by the filters). So, retrim the block group bitmaps when we |
| 3852 | * are preparing to punt to the unused_bgs list. This uses |
| 3853 | * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED |
| 3854 | * which is the only discard index which sets minlen to 0. |
| 3855 | */ |
| 3856 | if (!entry || (async && minlen && start == offset && |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3857 | btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3858 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3859 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3860 | next_bitmap = true; |
| 3861 | goto next; |
| 3862 | } |
| 3863 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3864 | /* |
| 3865 | * Async discard bitmap trimming begins at by setting the start |
| 3866 | * to be key.objectid and the offset_to_bitmap() aligns to the |
| 3867 | * start of the bitmap. This lets us know we are fully |
| 3868 | * scanning the bitmap rather than only some portion of it. |
| 3869 | */ |
| 3870 | if (start == offset) |
| 3871 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMING; |
| 3872 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3873 | bytes = minlen; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3874 | ret2 = search_bitmap(ctl, entry, &start, &bytes, false); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3875 | if (ret2 || start >= end) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3876 | /* |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3877 | * We lossily consider a bitmap trimmed if we only skip |
| 3878 | * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER. |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3879 | */ |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3880 | if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER) |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3881 | end_trimming_bitmap(ctl, entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3882 | else |
| 3883 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3884 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3885 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3886 | next_bitmap = true; |
| 3887 | goto next; |
| 3888 | } |
| 3889 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3890 | /* |
| 3891 | * We already trimmed a region, but are using the locking above |
| 3892 | * to reset the trim_state. |
| 3893 | */ |
| 3894 | if (async && *total_trimmed) { |
| 3895 | spin_unlock(&ctl->tree_lock); |
| 3896 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3897 | goto out; |
| 3898 | } |
| 3899 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3900 | bytes = min(bytes, end - start); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3901 | if (bytes < minlen || (async && maxlen && bytes > maxlen)) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3902 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3903 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3904 | goto next; |
| 3905 | } |
| 3906 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3907 | /* |
| 3908 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3909 | * If X < @minlen, we won't trim X when we come back around. |
| 3910 | * So trim it now. We differ here from trimming extents as we |
| 3911 | * don't keep individual state per bit. |
| 3912 | */ |
| 3913 | if (async && |
| 3914 | max_discard_size && |
| 3915 | bytes > (max_discard_size + minlen)) |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3916 | bytes = max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3917 | |
Nikolay Borisov | f594f13 | 2021-11-23 14:44:19 +0200 | [diff] [blame] | 3918 | bitmap_clear_bits(ctl, entry, start, bytes, true); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3919 | if (entry->bytes == 0) |
| 3920 | free_bitmap(ctl, entry); |
| 3921 | |
| 3922 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3923 | trim_entry.start = start; |
| 3924 | trim_entry.bytes = bytes; |
| 3925 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3926 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3927 | |
| 3928 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3929 | start, bytes, 0, &trim_entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3930 | if (ret) { |
| 3931 | reset_trimming_bitmap(ctl, offset); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3932 | block_group->discard_cursor = |
| 3933 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3934 | break; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3935 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3936 | next: |
| 3937 | if (next_bitmap) { |
| 3938 | offset += BITS_PER_BITMAP * ctl->unit; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3939 | start = offset; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3940 | } else { |
| 3941 | start += bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3942 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3943 | block_group->discard_cursor = start; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3944 | |
| 3945 | if (fatal_signal_pending(current)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3946 | if (start != offset) |
| 3947 | reset_trimming_bitmap(ctl, offset); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3948 | ret = -ERESTARTSYS; |
| 3949 | break; |
| 3950 | } |
| 3951 | |
| 3952 | cond_resched(); |
| 3953 | } |
| 3954 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3955 | if (offset >= end) |
| 3956 | block_group->discard_cursor = end; |
| 3957 | |
| 3958 | out: |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3959 | return ret; |
| 3960 | } |
| 3961 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3962 | int btrfs_trim_block_group(struct btrfs_block_group *block_group, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3963 | u64 *trimmed, u64 start, u64 end, u64 minlen) |
| 3964 | { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3965 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3966 | int ret; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3967 | u64 rem = 0; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3968 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3969 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3970 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3971 | *trimmed = 0; |
| 3972 | |
| 3973 | spin_lock(&block_group->lock); |
| 3974 | if (block_group->removed) { |
| 3975 | spin_unlock(&block_group->lock); |
| 3976 | return 0; |
| 3977 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3978 | btrfs_freeze_block_group(block_group); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3979 | spin_unlock(&block_group->lock); |
| 3980 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3981 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3982 | if (ret) |
| 3983 | goto out; |
| 3984 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3985 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3986 | div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem); |
| 3987 | /* If we ended in the middle of a bitmap, reset the trimming flag */ |
| 3988 | if (rem) |
| 3989 | reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end)); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3990 | out: |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3991 | btrfs_unfreeze_block_group(block_group); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3992 | return ret; |
| 3993 | } |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3994 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3995 | int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group, |
| 3996 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
| 3997 | bool async) |
| 3998 | { |
| 3999 | int ret; |
| 4000 | |
| 4001 | *trimmed = 0; |
| 4002 | |
| 4003 | spin_lock(&block_group->lock); |
| 4004 | if (block_group->removed) { |
| 4005 | spin_unlock(&block_group->lock); |
| 4006 | return 0; |
| 4007 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4008 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4009 | spin_unlock(&block_group->lock); |
| 4010 | |
| 4011 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async); |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4012 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4013 | |
| 4014 | return ret; |
| 4015 | } |
| 4016 | |
| 4017 | int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group, |
| 4018 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 4019 | u64 maxlen, bool async) |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4020 | { |
| 4021 | int ret; |
| 4022 | |
| 4023 | *trimmed = 0; |
| 4024 | |
| 4025 | spin_lock(&block_group->lock); |
| 4026 | if (block_group->removed) { |
| 4027 | spin_unlock(&block_group->lock); |
| 4028 | return 0; |
| 4029 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4030 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4031 | spin_unlock(&block_group->lock); |
| 4032 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 4033 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen, |
| 4034 | async); |
| 4035 | |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 4036 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 4037 | |
| 4038 | return ret; |
| 4039 | } |
| 4040 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4041 | bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info) |
| 4042 | { |
| 4043 | return btrfs_super_cache_generation(fs_info->super_copy); |
| 4044 | } |
| 4045 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4046 | static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info, |
| 4047 | struct btrfs_trans_handle *trans) |
| 4048 | { |
| 4049 | struct btrfs_block_group *block_group; |
| 4050 | struct rb_node *node; |
Tom Rix | 77364fa | 2021-04-30 11:06:55 -0700 | [diff] [blame] | 4051 | int ret = 0; |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4052 | |
| 4053 | btrfs_info(fs_info, "cleaning free space cache v1"); |
| 4054 | |
| 4055 | node = rb_first(&fs_info->block_group_cache_tree); |
| 4056 | while (node) { |
| 4057 | block_group = rb_entry(node, struct btrfs_block_group, cache_node); |
| 4058 | ret = btrfs_remove_free_space_inode(trans, NULL, block_group); |
| 4059 | if (ret) |
| 4060 | goto out; |
| 4061 | node = rb_next(node); |
| 4062 | } |
| 4063 | out: |
| 4064 | return ret; |
| 4065 | } |
| 4066 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4067 | int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active) |
| 4068 | { |
| 4069 | struct btrfs_trans_handle *trans; |
| 4070 | int ret; |
| 4071 | |
| 4072 | /* |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4073 | * update_super_roots will appropriately set or unset |
| 4074 | * super_copy->cache_generation based on SPACE_CACHE and |
| 4075 | * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a |
| 4076 | * transaction commit whether we are enabling space cache v1 and don't |
| 4077 | * have any other work to do, or are disabling it and removing free |
| 4078 | * space inodes. |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4079 | */ |
| 4080 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
| 4081 | if (IS_ERR(trans)) |
| 4082 | return PTR_ERR(trans); |
| 4083 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4084 | if (!active) { |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4085 | set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4086 | ret = cleanup_free_space_cache_v1(fs_info, trans); |
| 4087 | if (ret) { |
| 4088 | btrfs_abort_transaction(trans, ret); |
| 4089 | btrfs_end_transaction(trans); |
| 4090 | goto out; |
| 4091 | } |
| 4092 | } |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4093 | |
| 4094 | ret = btrfs_commit_transaction(trans); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 4095 | out: |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 4096 | clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
| 4097 | |
| 4098 | return ret; |
| 4099 | } |
| 4100 | |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4101 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4102 | /* |
| 4103 | * Use this if you need to make a bitmap or extent entry specifically, it |
| 4104 | * doesn't do any of the merging that add_free_space does, this acts a lot like |
| 4105 | * how the free space cache loading stuff works, so you can get really weird |
| 4106 | * configurations. |
| 4107 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4108 | int test_add_free_space_entry(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4109 | u64 offset, u64 bytes, bool bitmap) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4110 | { |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4111 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4112 | struct btrfs_free_space *info = NULL, *bitmap_info; |
| 4113 | void *map = NULL; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4114 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4115 | u64 bytes_added; |
| 4116 | int ret; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4117 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4118 | again: |
| 4119 | if (!info) { |
| 4120 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
| 4121 | if (!info) |
| 4122 | return -ENOMEM; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4123 | } |
| 4124 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4125 | if (!bitmap) { |
| 4126 | spin_lock(&ctl->tree_lock); |
| 4127 | info->offset = offset; |
| 4128 | info->bytes = bytes; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4129 | info->max_extent_size = 0; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4130 | ret = link_free_space(ctl, info); |
| 4131 | spin_unlock(&ctl->tree_lock); |
| 4132 | if (ret) |
| 4133 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4134 | return ret; |
| 4135 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4136 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4137 | if (!map) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4138 | map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4139 | if (!map) { |
| 4140 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4141 | return -ENOMEM; |
| 4142 | } |
| 4143 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4144 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4145 | spin_lock(&ctl->tree_lock); |
| 4146 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4147 | 1, 0); |
| 4148 | if (!bitmap_info) { |
| 4149 | info->bitmap = map; |
| 4150 | map = NULL; |
| 4151 | add_new_bitmap(ctl, info, offset); |
| 4152 | bitmap_info = info; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4153 | info = NULL; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4154 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4155 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4156 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 4157 | trim_state); |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4158 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4159 | bytes -= bytes_added; |
| 4160 | offset += bytes_added; |
| 4161 | spin_unlock(&ctl->tree_lock); |
| 4162 | |
| 4163 | if (bytes) |
| 4164 | goto again; |
| 4165 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4166 | if (info) |
| 4167 | kmem_cache_free(btrfs_free_space_cachep, info); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4168 | if (map) |
| 4169 | kmem_cache_free(btrfs_free_space_bitmap_cachep, map); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4170 | return 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | /* |
| 4174 | * Checks to see if the given range is in the free space cache. This is really |
| 4175 | * just used to check the absence of space, so if there is free space in the |
| 4176 | * range at all we will return 1. |
| 4177 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4178 | int test_check_exists(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4179 | u64 offset, u64 bytes) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4180 | { |
| 4181 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4182 | struct btrfs_free_space *info; |
| 4183 | int ret = 0; |
| 4184 | |
| 4185 | spin_lock(&ctl->tree_lock); |
| 4186 | info = tree_search_offset(ctl, offset, 0, 0); |
| 4187 | if (!info) { |
| 4188 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4189 | 1, 0); |
| 4190 | if (!info) |
| 4191 | goto out; |
| 4192 | } |
| 4193 | |
| 4194 | have_info: |
| 4195 | if (info->bitmap) { |
| 4196 | u64 bit_off, bit_bytes; |
| 4197 | struct rb_node *n; |
| 4198 | struct btrfs_free_space *tmp; |
| 4199 | |
| 4200 | bit_off = offset; |
| 4201 | bit_bytes = ctl->unit; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 4202 | ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4203 | if (!ret) { |
| 4204 | if (bit_off == offset) { |
| 4205 | ret = 1; |
| 4206 | goto out; |
| 4207 | } else if (bit_off > offset && |
| 4208 | offset + bytes > bit_off) { |
| 4209 | ret = 1; |
| 4210 | goto out; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | n = rb_prev(&info->offset_index); |
| 4215 | while (n) { |
| 4216 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4217 | offset_index); |
| 4218 | if (tmp->offset + tmp->bytes < offset) |
| 4219 | break; |
| 4220 | if (offset + bytes < tmp->offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4221 | n = rb_prev(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4222 | continue; |
| 4223 | } |
| 4224 | info = tmp; |
| 4225 | goto have_info; |
| 4226 | } |
| 4227 | |
| 4228 | n = rb_next(&info->offset_index); |
| 4229 | while (n) { |
| 4230 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4231 | offset_index); |
| 4232 | if (offset + bytes < tmp->offset) |
| 4233 | break; |
| 4234 | if (tmp->offset + tmp->bytes < offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4235 | n = rb_next(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4236 | continue; |
| 4237 | } |
| 4238 | info = tmp; |
| 4239 | goto have_info; |
| 4240 | } |
| 4241 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4242 | ret = 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4243 | goto out; |
| 4244 | } |
| 4245 | |
| 4246 | if (info->offset == offset) { |
| 4247 | ret = 1; |
| 4248 | goto out; |
| 4249 | } |
| 4250 | |
| 4251 | if (offset > info->offset && offset < info->offset + info->bytes) |
| 4252 | ret = 1; |
| 4253 | out: |
| 4254 | spin_unlock(&ctl->tree_lock); |
| 4255 | return ret; |
| 4256 | } |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4257 | #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */ |