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