David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
| 7 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 12 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 13 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 14 | #include <linux/semaphore.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 15 | #include <linux/uuid.h> |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 17 | #include "ctree.h" |
| 18 | #include "extent_map.h" |
| 19 | #include "disk-io.h" |
| 20 | #include "transaction.h" |
| 21 | #include "print-tree.h" |
| 22 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 23 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 24 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 25 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 26 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 27 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 28 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 29 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 30 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 32 | #include "block-group.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 34 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 35 | [BTRFS_RAID_RAID10] = { |
| 36 | .sub_stripes = 2, |
| 37 | .dev_stripes = 1, |
| 38 | .devs_max = 0, /* 0 == as many as possible */ |
| 39 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 40 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 41 | .devs_increment = 2, |
| 42 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 43 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 44 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 45 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 46 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 47 | }, |
| 48 | [BTRFS_RAID_RAID1] = { |
| 49 | .sub_stripes = 1, |
| 50 | .dev_stripes = 1, |
| 51 | .devs_max = 2, |
| 52 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 53 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 54 | .devs_increment = 2, |
| 55 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 56 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 57 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 58 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 59 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 60 | }, |
| 61 | [BTRFS_RAID_DUP] = { |
| 62 | .sub_stripes = 1, |
| 63 | .dev_stripes = 2, |
| 64 | .devs_max = 1, |
| 65 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 66 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 67 | .devs_increment = 1, |
| 68 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 69 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 70 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 71 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 72 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 73 | }, |
| 74 | [BTRFS_RAID_RAID0] = { |
| 75 | .sub_stripes = 1, |
| 76 | .dev_stripes = 1, |
| 77 | .devs_max = 0, |
| 78 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 79 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 80 | .devs_increment = 1, |
| 81 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 82 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 83 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 84 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 85 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 86 | }, |
| 87 | [BTRFS_RAID_SINGLE] = { |
| 88 | .sub_stripes = 1, |
| 89 | .dev_stripes = 1, |
| 90 | .devs_max = 1, |
| 91 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 92 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 93 | .devs_increment = 1, |
| 94 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 95 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 96 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 97 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 98 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 99 | }, |
| 100 | [BTRFS_RAID_RAID5] = { |
| 101 | .sub_stripes = 1, |
| 102 | .dev_stripes = 1, |
| 103 | .devs_max = 0, |
| 104 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 105 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 106 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 107 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 108 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 109 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 110 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 111 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 112 | }, |
| 113 | [BTRFS_RAID_RAID6] = { |
| 114 | .sub_stripes = 1, |
| 115 | .dev_stripes = 1, |
| 116 | .devs_max = 0, |
| 117 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 118 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 119 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 120 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 121 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 122 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 123 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 124 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 125 | }, |
| 126 | }; |
| 127 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 128 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 129 | { |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 130 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 131 | |
| 132 | if (index >= BTRFS_NR_RAID_TYPES) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 133 | return NULL; |
| 134 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 135 | return btrfs_raid_array[index].raid_name; |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 138 | /* |
| 139 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 140 | * bytes including terminating null byte. |
| 141 | */ |
| 142 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 143 | { |
| 144 | int i; |
| 145 | int ret; |
| 146 | char *bp = buf; |
| 147 | u64 flags = bg_flags; |
| 148 | u32 size_bp = size_buf; |
| 149 | |
| 150 | if (!flags) { |
| 151 | strcpy(bp, "NONE"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | #define DESCRIBE_FLAG(flag, desc) \ |
| 156 | do { \ |
| 157 | if (flags & (flag)) { \ |
| 158 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 159 | if (ret < 0 || ret >= size_bp) \ |
| 160 | goto out_overflow; \ |
| 161 | size_bp -= ret; \ |
| 162 | bp += ret; \ |
| 163 | flags &= ~(flag); \ |
| 164 | } \ |
| 165 | } while (0) |
| 166 | |
| 167 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 168 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 169 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 170 | |
| 171 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 172 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 173 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 174 | btrfs_raid_array[i].raid_name); |
| 175 | #undef DESCRIBE_FLAG |
| 176 | |
| 177 | if (flags) { |
| 178 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 179 | size_bp -= ret; |
| 180 | } |
| 181 | |
| 182 | if (size_bp < size_buf) |
| 183 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 184 | |
| 185 | /* |
| 186 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 187 | * large buffer |
| 188 | */ |
| 189 | out_overflow:; |
| 190 | } |
| 191 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 192 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 193 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 194 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 195 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 196 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 197 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 198 | enum btrfs_map_op op, |
| 199 | u64 logical, u64 *length, |
| 200 | struct btrfs_bio **bbio_ret, |
| 201 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 202 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 203 | /* |
| 204 | * Device locking |
| 205 | * ============== |
| 206 | * |
| 207 | * There are several mutexes that protect manipulation of devices and low-level |
| 208 | * structures like chunks but not block groups, extents or files |
| 209 | * |
| 210 | * uuid_mutex (global lock) |
| 211 | * ------------------------ |
| 212 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 213 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 214 | * device) or requested by the device= mount option |
| 215 | * |
| 216 | * the mutex can be very coarse and can cover long-running operations |
| 217 | * |
| 218 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 219 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 220 | * |
| 221 | * global::fs_devs - add, remove, updates to the global list |
| 222 | * |
| 223 | * does not protect: manipulation of the fs_devices::devices list! |
| 224 | * |
| 225 | * btrfs_device::name - renames (write side), read is RCU |
| 226 | * |
| 227 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 228 | * ------------------------------------------------ |
| 229 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 230 | * |
| 231 | * simple list traversal with read-only actions can be done with RCU protection |
| 232 | * |
| 233 | * may be used to exclude some operations from running concurrently without any |
| 234 | * modifications to the list (see write_all_supers) |
| 235 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 236 | * balance_mutex |
| 237 | * ------------- |
| 238 | * protects balance structures (status, state) and context accessed from |
| 239 | * several places (internally, ioctl) |
| 240 | * |
| 241 | * chunk_mutex |
| 242 | * ----------- |
| 243 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 244 | * device is added/removed. Additionally it also protects post_commit_list of |
| 245 | * individual devices, since they can be added to the transaction's |
| 246 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 247 | * |
| 248 | * cleaner_mutex |
| 249 | * ------------- |
| 250 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 251 | * cleaning together with relocation or delayed iputs |
| 252 | * |
| 253 | * |
| 254 | * Lock nesting |
| 255 | * ============ |
| 256 | * |
| 257 | * uuid_mutex |
| 258 | * volume_mutex |
| 259 | * device_list_mutex |
| 260 | * chunk_mutex |
| 261 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 262 | * |
| 263 | * |
| 264 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 265 | * ====================================== |
| 266 | * |
| 267 | * Maintains the exclusivity of the following operations that apply to the |
| 268 | * whole filesystem and cannot run in parallel. |
| 269 | * |
| 270 | * - Balance (*) |
| 271 | * - Device add |
| 272 | * - Device remove |
| 273 | * - Device replace (*) |
| 274 | * - Resize |
| 275 | * |
| 276 | * The device operations (as above) can be in one of the following states: |
| 277 | * |
| 278 | * - Running state |
| 279 | * - Paused state |
| 280 | * - Completed state |
| 281 | * |
| 282 | * Only device operations marked with (*) can go into the Paused state for the |
| 283 | * following reasons: |
| 284 | * |
| 285 | * - ioctl (only Balance can be Paused through ioctl) |
| 286 | * - filesystem remounted as read-only |
| 287 | * - filesystem unmounted and mounted as read-only |
| 288 | * - system power-cycle and filesystem mounted as read-only |
| 289 | * - filesystem or device errors leading to forced read-only |
| 290 | * |
| 291 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 292 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 293 | * A device operation in Paused or Running state can be canceled or resumed |
| 294 | * either by ioctl (Balance only) or when remounted as read-write. |
| 295 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 296 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 297 | */ |
| 298 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 299 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 300 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 301 | struct list_head *btrfs_get_fs_uuids(void) |
| 302 | { |
| 303 | return &fs_uuids; |
| 304 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 305 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 306 | /* |
| 307 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 308 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 309 | * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 310 | * |
| 311 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 312 | * The returned struct is not linked onto any lists and can be destroyed with |
| 313 | * kfree() right away. |
| 314 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 315 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 316 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 317 | { |
| 318 | struct btrfs_fs_devices *fs_devs; |
| 319 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 320 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 321 | if (!fs_devs) |
| 322 | return ERR_PTR(-ENOMEM); |
| 323 | |
| 324 | mutex_init(&fs_devs->device_list_mutex); |
| 325 | |
| 326 | INIT_LIST_HEAD(&fs_devs->devices); |
| 327 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 328 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 329 | if (fsid) |
| 330 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 331 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 332 | if (metadata_fsid) |
| 333 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 334 | else if (fsid) |
| 335 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 336 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 337 | return fs_devs; |
| 338 | } |
| 339 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 340 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 341 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 342 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 343 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 344 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 345 | bio_put(device->flush_bio); |
| 346 | kfree(device); |
| 347 | } |
| 348 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 349 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 350 | { |
| 351 | struct btrfs_device *device; |
| 352 | WARN_ON(fs_devices->opened); |
| 353 | while (!list_empty(&fs_devices->devices)) { |
| 354 | device = list_entry(fs_devices->devices.next, |
| 355 | struct btrfs_device, dev_list); |
| 356 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 357 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 358 | } |
| 359 | kfree(fs_devices); |
| 360 | } |
| 361 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 362 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 363 | enum kobject_action action) |
| 364 | { |
| 365 | int ret; |
| 366 | |
| 367 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 368 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 369 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 370 | action, |
| 371 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 372 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 373 | } |
| 374 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 375 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 376 | { |
| 377 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 378 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 379 | while (!list_empty(&fs_uuids)) { |
| 380 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 381 | struct btrfs_fs_devices, fs_list); |
| 382 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 383 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 384 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 385 | } |
| 386 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 387 | /* |
| 388 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 389 | * Returned struct is not linked onto any lists and must be destroyed using |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 390 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 391 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 392 | static struct btrfs_device *__alloc_device(void) |
| 393 | { |
| 394 | struct btrfs_device *dev; |
| 395 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 396 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 397 | if (!dev) |
| 398 | return ERR_PTR(-ENOMEM); |
| 399 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 400 | /* |
| 401 | * Preallocate a bio that's always going to be used for flushing device |
| 402 | * barriers and matches the device lifespan |
| 403 | */ |
| 404 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 405 | if (!dev->flush_bio) { |
| 406 | kfree(dev); |
| 407 | return ERR_PTR(-ENOMEM); |
| 408 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 409 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 410 | INIT_LIST_HEAD(&dev->dev_list); |
| 411 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 412 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 413 | |
| 414 | spin_lock_init(&dev->io_lock); |
| 415 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 416 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 417 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 418 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 419 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 420 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 421 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 422 | |
| 423 | return dev; |
| 424 | } |
| 425 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 426 | static noinline struct btrfs_fs_devices *find_fsid( |
| 427 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 428 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 429 | struct btrfs_fs_devices *fs_devices; |
| 430 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 431 | ASSERT(fsid); |
| 432 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 433 | if (metadata_fsid) { |
| 434 | /* |
| 435 | * Handle scanned device having completed its fsid change but |
| 436 | * belonging to a fs_devices that was created by first scanning |
| 437 | * a device which didn't have its fsid/metadata_uuid changed |
| 438 | * at all and the CHANGING_FSID_V2 flag set. |
| 439 | */ |
| 440 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 441 | if (fs_devices->fsid_change && |
| 442 | memcmp(metadata_fsid, fs_devices->fsid, |
| 443 | BTRFS_FSID_SIZE) == 0 && |
| 444 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 445 | BTRFS_FSID_SIZE) == 0) { |
| 446 | return fs_devices; |
| 447 | } |
| 448 | } |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 449 | /* |
| 450 | * Handle scanned device having completed its fsid change but |
| 451 | * belonging to a fs_devices that was created by a device that |
| 452 | * has an outdated pair of fsid/metadata_uuid and |
| 453 | * CHANGING_FSID_V2 flag set. |
| 454 | */ |
| 455 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 456 | if (fs_devices->fsid_change && |
| 457 | memcmp(fs_devices->metadata_uuid, |
| 458 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 459 | memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 460 | BTRFS_FSID_SIZE) == 0) { |
| 461 | return fs_devices; |
| 462 | } |
| 463 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 467 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 468 | if (metadata_fsid) { |
| 469 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 470 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 471 | BTRFS_FSID_SIZE) == 0) |
| 472 | return fs_devices; |
| 473 | } else { |
| 474 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 475 | return fs_devices; |
| 476 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 477 | } |
| 478 | return NULL; |
| 479 | } |
| 480 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 481 | static int |
| 482 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 483 | int flush, struct block_device **bdev, |
| 484 | struct buffer_head **bh) |
| 485 | { |
| 486 | int ret; |
| 487 | |
| 488 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 489 | |
| 490 | if (IS_ERR(*bdev)) { |
| 491 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 492 | goto error; |
| 493 | } |
| 494 | |
| 495 | if (flush) |
| 496 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 497 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 498 | if (ret) { |
| 499 | blkdev_put(*bdev, flags); |
| 500 | goto error; |
| 501 | } |
| 502 | invalidate_bdev(*bdev); |
| 503 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 504 | if (IS_ERR(*bh)) { |
| 505 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 506 | blkdev_put(*bdev, flags); |
| 507 | goto error; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | error: |
| 513 | *bdev = NULL; |
| 514 | *bh = NULL; |
| 515 | return ret; |
| 516 | } |
| 517 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 518 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 519 | struct bio *head, struct bio *tail) |
| 520 | { |
| 521 | |
| 522 | struct bio *old_head; |
| 523 | |
| 524 | old_head = pending_bios->head; |
| 525 | pending_bios->head = head; |
| 526 | if (pending_bios->tail) |
| 527 | tail->bi_next = old_head; |
| 528 | else |
| 529 | pending_bios->tail = tail; |
| 530 | } |
| 531 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 532 | /* |
| 533 | * we try to collect pending bios for a device so we don't get a large |
| 534 | * number of procs sending bios down to the same device. This greatly |
| 535 | * improves the schedulers ability to collect and merge the bios. |
| 536 | * |
| 537 | * But, it also turns into a long list of bios to process and that is sure |
| 538 | * to eventually make the worker thread block. The solution here is to |
| 539 | * make some progress and then put this work struct back at the end of |
| 540 | * the list if the block device is congested. This way, multiple devices |
| 541 | * can make progress from a single worker thread. |
| 542 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 543 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 544 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 545 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 546 | struct bio *pending; |
| 547 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 548 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 549 | struct bio *tail; |
| 550 | struct bio *cur; |
| 551 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 552 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 553 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 554 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 555 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 556 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 557 | struct blk_plug plug; |
| 558 | |
| 559 | /* |
| 560 | * this function runs all the bios we've collected for |
| 561 | * a particular device. We don't want to wander off to |
| 562 | * another device without first sending all of these down. |
| 563 | * So, setup a plug here and finish it off before we return |
| 564 | */ |
| 565 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 566 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 567 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 568 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 569 | loop: |
| 570 | spin_lock(&device->io_lock); |
| 571 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 572 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 573 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 574 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 575 | /* take all the bios off the list at once and process them |
| 576 | * later on (without the lock held). But, remember the |
| 577 | * tail and other pointers so the bios can be properly reinserted |
| 578 | * into the list if we hit congestion |
| 579 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 580 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 581 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 582 | force_reg = 1; |
| 583 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 584 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 585 | force_reg = 0; |
| 586 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 587 | |
| 588 | pending = pending_bios->head; |
| 589 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 590 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * if pending was null this time around, no bios need processing |
| 594 | * at all and we can stop. Otherwise it'll loop back up again |
| 595 | * and do an additional check so no bios are missed. |
| 596 | * |
| 597 | * device->running_pending is used to synchronize with the |
| 598 | * schedule_bio code. |
| 599 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 600 | if (device->pending_sync_bios.head == NULL && |
| 601 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 602 | again = 0; |
| 603 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 604 | } else { |
| 605 | again = 1; |
| 606 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 607 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 608 | |
| 609 | pending_bios->head = NULL; |
| 610 | pending_bios->tail = NULL; |
| 611 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 612 | spin_unlock(&device->io_lock); |
| 613 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 614 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 615 | |
| 616 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 617 | /* we want to work on both lists, but do more bios on the |
| 618 | * sync list than the regular list |
| 619 | */ |
| 620 | if ((num_run > 32 && |
| 621 | pending_bios != &device->pending_sync_bios && |
| 622 | device->pending_sync_bios.head) || |
| 623 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 624 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 625 | spin_lock(&device->io_lock); |
| 626 | requeue_list(pending_bios, pending, tail); |
| 627 | goto loop_lock; |
| 628 | } |
| 629 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 630 | cur = pending; |
| 631 | pending = pending->bi_next; |
| 632 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 633 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 634 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 635 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 636 | /* |
| 637 | * if we're doing the sync list, record that our |
| 638 | * plug has some sync requests on it |
| 639 | * |
| 640 | * If we're doing the regular list and there are |
| 641 | * sync requests sitting around, unplug before |
| 642 | * we add more |
| 643 | */ |
| 644 | if (pending_bios == &device->pending_sync_bios) { |
| 645 | sync_pending = 1; |
| 646 | } else if (sync_pending) { |
| 647 | blk_finish_plug(&plug); |
| 648 | blk_start_plug(&plug); |
| 649 | sync_pending = 0; |
| 650 | } |
| 651 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 652 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 653 | num_run++; |
| 654 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 655 | |
| 656 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * we made progress, there is more work to do and the bdi |
| 660 | * is now congested. Back off and let other work structs |
| 661 | * run instead |
| 662 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 663 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 664 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 665 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 666 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 667 | ioc = current->io_context; |
| 668 | |
| 669 | /* |
| 670 | * the main goal here is that we don't want to |
| 671 | * block if we're going to be able to submit |
| 672 | * more requests without blocking. |
| 673 | * |
| 674 | * This code does two great things, it pokes into |
| 675 | * the elevator code from a filesystem _and_ |
| 676 | * it makes assumptions about how batching works. |
| 677 | */ |
| 678 | if (ioc && ioc->nr_batch_requests > 0 && |
| 679 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 680 | (last_waited == 0 || |
| 681 | ioc->last_waited == last_waited)) { |
| 682 | /* |
| 683 | * we want to go through our batch of |
| 684 | * requests and stop. So, we copy out |
| 685 | * the ioc->last_waited time and test |
| 686 | * against it before looping |
| 687 | */ |
| 688 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 689 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 690 | continue; |
| 691 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 692 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 693 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 694 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 695 | |
| 696 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 697 | btrfs_queue_work(fs_info->submit_workers, |
| 698 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 699 | goto done; |
| 700 | } |
| 701 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 702 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 703 | cond_resched(); |
| 704 | if (again) |
| 705 | goto loop; |
| 706 | |
| 707 | spin_lock(&device->io_lock); |
| 708 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 709 | goto loop_lock; |
| 710 | spin_unlock(&device->io_lock); |
| 711 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 712 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 713 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 714 | } |
| 715 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 716 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 717 | { |
| 718 | struct btrfs_device *device; |
| 719 | |
| 720 | device = container_of(work, struct btrfs_device, work); |
| 721 | run_scheduled_bios(device); |
| 722 | } |
| 723 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 724 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 725 | { |
| 726 | int found; |
| 727 | |
| 728 | rcu_read_lock(); |
| 729 | found = strcmp(rcu_str_deref(device->name), path); |
| 730 | rcu_read_unlock(); |
| 731 | |
| 732 | return found == 0; |
| 733 | } |
| 734 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 735 | /* |
| 736 | * Search and remove all stale (devices which are not mounted) devices. |
| 737 | * When both inputs are NULL, it will search and release all stale devices. |
| 738 | * path: Optional. When provided will it release all unmounted devices |
| 739 | * matching this path only. |
| 740 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 741 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 742 | * Return: 0 for success or if @path is NULL. |
| 743 | * -EBUSY if @path is a mounted device. |
| 744 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 745 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 746 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 747 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 748 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 749 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 750 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 751 | int ret = 0; |
| 752 | |
| 753 | if (path) |
| 754 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 755 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 756 | list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 757 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 758 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 759 | list_for_each_entry_safe(device, tmp_device, |
| 760 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 761 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 762 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 763 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 764 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 765 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 766 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 767 | if (fs_devices->opened) { |
| 768 | /* for an already deleted device return 0 */ |
| 769 | if (path && ret != 0) |
| 770 | ret = -EBUSY; |
| 771 | break; |
| 772 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 773 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 774 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 775 | fs_devices->num_devices--; |
| 776 | list_del(&device->dev_list); |
| 777 | btrfs_free_device(device); |
| 778 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 779 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 780 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 781 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 782 | } |
| 783 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 784 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 785 | if (fs_devices->num_devices == 0) { |
| 786 | btrfs_sysfs_remove_fsid(fs_devices); |
| 787 | list_del(&fs_devices->fs_list); |
| 788 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 789 | } |
| 790 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 791 | |
| 792 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 793 | } |
| 794 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 795 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 796 | struct btrfs_device *device, fmode_t flags, |
| 797 | void *holder) |
| 798 | { |
| 799 | struct request_queue *q; |
| 800 | struct block_device *bdev; |
| 801 | struct buffer_head *bh; |
| 802 | struct btrfs_super_block *disk_super; |
| 803 | u64 devid; |
| 804 | int ret; |
| 805 | |
| 806 | if (device->bdev) |
| 807 | return -EINVAL; |
| 808 | if (!device->name) |
| 809 | return -EINVAL; |
| 810 | |
| 811 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 812 | &bdev, &bh); |
| 813 | if (ret) |
| 814 | return ret; |
| 815 | |
| 816 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 817 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 818 | if (devid != device->devid) |
| 819 | goto error_brelse; |
| 820 | |
| 821 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 822 | goto error_brelse; |
| 823 | |
| 824 | device->generation = btrfs_super_generation(disk_super); |
| 825 | |
| 826 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 827 | if (btrfs_super_incompat_flags(disk_super) & |
| 828 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 829 | pr_err( |
| 830 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 831 | goto error_brelse; |
| 832 | } |
| 833 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 834 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 835 | fs_devices->seeding = 1; |
| 836 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 837 | if (bdev_read_only(bdev)) |
| 838 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 839 | else |
| 840 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 844 | if (!blk_queue_nonrot(q)) |
| 845 | fs_devices->rotating = 1; |
| 846 | |
| 847 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 848 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 849 | device->mode = flags; |
| 850 | |
| 851 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 852 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 853 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 854 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 855 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 856 | } |
| 857 | brelse(bh); |
| 858 | |
| 859 | return 0; |
| 860 | |
| 861 | error_brelse: |
| 862 | brelse(bh); |
| 863 | blkdev_put(bdev, flags); |
| 864 | |
| 865 | return -EINVAL; |
| 866 | } |
| 867 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 868 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 869 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| 870 | * being created with a disk that has already completed its fsid change. |
| 871 | */ |
| 872 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 873 | struct btrfs_super_block *disk_super) |
| 874 | { |
| 875 | struct btrfs_fs_devices *fs_devices; |
| 876 | |
| 877 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 878 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 879 | BTRFS_FSID_SIZE) != 0 && |
| 880 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 881 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 882 | return fs_devices; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return NULL; |
| 887 | } |
| 888 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 889 | |
| 890 | static struct btrfs_fs_devices *find_fsid_changed( |
| 891 | struct btrfs_super_block *disk_super) |
| 892 | { |
| 893 | struct btrfs_fs_devices *fs_devices; |
| 894 | |
| 895 | /* |
| 896 | * Handles the case where scanned device is part of an fs that had |
| 897 | * multiple successful changes of FSID but curently device didn't |
| 898 | * observe it. Meaning our fsid will be different than theirs. |
| 899 | */ |
| 900 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 901 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 902 | BTRFS_FSID_SIZE) != 0 && |
| 903 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 904 | BTRFS_FSID_SIZE) == 0 && |
| 905 | memcmp(fs_devices->fsid, disk_super->fsid, |
| 906 | BTRFS_FSID_SIZE) != 0) { |
| 907 | return fs_devices; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | return NULL; |
| 912 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 913 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 914 | * Add new device to list of registered devices |
| 915 | * |
| 916 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 917 | * device pointer which was just added or updated when successful |
| 918 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 919 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 920 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 921 | struct btrfs_super_block *disk_super, |
| 922 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 923 | { |
| 924 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 925 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 926 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 927 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 928 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 929 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 930 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 931 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 932 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 933 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 934 | if (fsid_change_in_progress) { |
| 935 | if (!has_metadata_uuid) { |
| 936 | /* |
| 937 | * When we have an image which has CHANGING_FSID_V2 set |
| 938 | * it might belong to either a filesystem which has |
| 939 | * disks with completed fsid change or it might belong |
| 940 | * to fs with no UUID changes in effect, handle both. |
| 941 | */ |
| 942 | fs_devices = find_fsid_inprogress(disk_super); |
| 943 | if (!fs_devices) |
| 944 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 945 | } else { |
| 946 | fs_devices = find_fsid_changed(disk_super); |
| 947 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 948 | } else if (has_metadata_uuid) { |
| 949 | fs_devices = find_fsid(disk_super->fsid, |
| 950 | disk_super->metadata_uuid); |
| 951 | } else { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 952 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 955 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 956 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 957 | if (has_metadata_uuid) |
| 958 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 959 | disk_super->metadata_uuid); |
| 960 | else |
| 961 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 962 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 963 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 964 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 965 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 966 | fs_devices->fsid_change = fsid_change_in_progress; |
| 967 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 968 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 969 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 970 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 971 | device = NULL; |
| 972 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 973 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 974 | device = btrfs_find_device(fs_devices, devid, |
| 975 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 976 | |
| 977 | /* |
| 978 | * If this disk has been pulled into an fs devices created by |
| 979 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 980 | * metadata_uuid/fsid values of the fs_devices. |
| 981 | */ |
| 982 | if (has_metadata_uuid && fs_devices->fsid_change && |
| 983 | found_transid > fs_devices->latest_generation) { |
| 984 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 985 | BTRFS_FSID_SIZE); |
| 986 | memcpy(fs_devices->metadata_uuid, |
| 987 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 988 | |
| 989 | fs_devices->fsid_change = false; |
| 990 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 991 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 992 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 993 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 994 | if (fs_devices->opened) { |
| 995 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 996 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 997 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 998 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 999 | device = btrfs_alloc_device(NULL, &devid, |
| 1000 | disk_super->dev_item.uuid); |
| 1001 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1002 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1003 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1004 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1005 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1006 | |
| 1007 | name = rcu_string_strdup(path, GFP_NOFS); |
| 1008 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1009 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1010 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1011 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1012 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1013 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 1014 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1015 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1016 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1017 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1018 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1019 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1020 | |
| 1021 | if (disk_super->label[0]) |
| 1022 | pr_info("BTRFS: device label %s devid %llu transid %llu %s\n", |
| 1023 | disk_super->label, devid, found_transid, path); |
| 1024 | else |
| 1025 | pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n", |
| 1026 | disk_super->fsid, devid, found_transid, path); |
| 1027 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1028 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1029 | /* |
| 1030 | * When FS is already mounted. |
| 1031 | * 1. If you are here and if the device->name is NULL that |
| 1032 | * means this device was missing at time of FS mount. |
| 1033 | * 2. If you are here and if the device->name is different |
| 1034 | * from 'path' that means either |
| 1035 | * a. The same device disappeared and reappeared with |
| 1036 | * different name. or |
| 1037 | * b. The missing-disk-which-was-replaced, has |
| 1038 | * reappeared now. |
| 1039 | * |
| 1040 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 1041 | * and unintentional. |
| 1042 | * |
| 1043 | * Further in case of 1 and 2a above, the disk at 'path' |
| 1044 | * would have missed some transaction when it was away and |
| 1045 | * in case of 2a the stale bdev has to be updated as well. |
| 1046 | * 2b must not be allowed at all time. |
| 1047 | */ |
| 1048 | |
| 1049 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1050 | * For now, we do allow update to btrfs_fs_device through the |
| 1051 | * btrfs dev scan cli after FS has been mounted. We're still |
| 1052 | * tracking a problem where systems fail mount by subvolume id |
| 1053 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1054 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1055 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1056 | /* |
| 1057 | * That is if the FS is _not_ mounted and if you |
| 1058 | * are here, that means there is more than one |
| 1059 | * disk with same uuid and devid.We keep the one |
| 1060 | * with larger generation number or the last-in if |
| 1061 | * generation are equal. |
| 1062 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1063 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1064 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1065 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1066 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 1067 | /* |
| 1068 | * We are going to replace the device path for a given devid, |
| 1069 | * make sure it's the same device if the device is mounted |
| 1070 | */ |
| 1071 | if (device->bdev) { |
| 1072 | struct block_device *path_bdev; |
| 1073 | |
| 1074 | path_bdev = lookup_bdev(path); |
| 1075 | if (IS_ERR(path_bdev)) { |
| 1076 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1077 | return ERR_CAST(path_bdev); |
| 1078 | } |
| 1079 | |
| 1080 | if (device->bdev != path_bdev) { |
| 1081 | bdput(path_bdev); |
| 1082 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1083 | btrfs_warn_in_rcu(device->fs_info, |
| 1084 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 1085 | disk_super->fsid, devid, |
| 1086 | rcu_str_deref(device->name), path); |
| 1087 | return ERR_PTR(-EEXIST); |
| 1088 | } |
| 1089 | bdput(path_bdev); |
| 1090 | btrfs_info_in_rcu(device->fs_info, |
| 1091 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 1092 | disk_super->fsid, devid, |
| 1093 | rcu_str_deref(device->name), path); |
| 1094 | } |
| 1095 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1096 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1097 | if (!name) { |
| 1098 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1099 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1100 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1101 | rcu_string_free(device->name); |
| 1102 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1103 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1104 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1105 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1106 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1109 | /* |
| 1110 | * Unmount does not free the btrfs_device struct but would zero |
| 1111 | * generation along with most of the other members. So just update |
| 1112 | * it back. We need it to pick the disk with largest generation |
| 1113 | * (as above). |
| 1114 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1115 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1116 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1117 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 1118 | fs_devices->latest_generation); |
| 1119 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1120 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 1121 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 1122 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1123 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1124 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1125 | } |
| 1126 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1127 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 1128 | { |
| 1129 | struct btrfs_fs_devices *fs_devices; |
| 1130 | struct btrfs_device *device; |
| 1131 | struct btrfs_device *orig_dev; |
| 1132 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 1133 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1134 | if (IS_ERR(fs_devices)) |
| 1135 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1136 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1137 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1138 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1139 | |
| 1140 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1141 | struct rcu_string *name; |
| 1142 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 1143 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 1144 | orig_dev->uuid); |
| 1145 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1146 | goto error; |
| 1147 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1148 | /* |
| 1149 | * This is ok to do without rcu read locked because we hold the |
| 1150 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1151 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1152 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1153 | name = rcu_string_strdup(orig_dev->name->str, |
| 1154 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1155 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1156 | btrfs_free_device(device); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1157 | goto error; |
| 1158 | } |
| 1159 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1160 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1161 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1162 | list_add(&device->dev_list, &fs_devices->devices); |
| 1163 | device->fs_devices = fs_devices; |
| 1164 | fs_devices->num_devices++; |
| 1165 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1166 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1167 | return fs_devices; |
| 1168 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1169 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1170 | free_fs_devices(fs_devices); |
| 1171 | return ERR_PTR(-ENOMEM); |
| 1172 | } |
| 1173 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1174 | /* |
| 1175 | * After we have read the system tree and know devids belonging to |
| 1176 | * this filesystem, remove the device which does not belong there. |
| 1177 | */ |
| 1178 | void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1179 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1180 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1181 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1182 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1183 | mutex_lock(&uuid_mutex); |
| 1184 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1185 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1186 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1187 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1188 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1189 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1190 | &device->dev_state) && |
| 1191 | (!latest_dev || |
| 1192 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1193 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1194 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1195 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1196 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1197 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1198 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1199 | /* |
| 1200 | * In the first step, keep the device which has |
| 1201 | * the correct fsid and the devid that is used |
| 1202 | * for the dev_replace procedure. |
| 1203 | * In the second step, the dev_replace state is |
| 1204 | * read from the device tree and it is known |
| 1205 | * whether the procedure is really active or |
| 1206 | * not, which means whether this device is |
| 1207 | * used or whether it should be removed. |
| 1208 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1209 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1210 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1211 | continue; |
| 1212 | } |
| 1213 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1214 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1215 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1216 | device->bdev = NULL; |
| 1217 | fs_devices->open_devices--; |
| 1218 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1219 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1220 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1221 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1222 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1223 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1224 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1225 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1226 | list_del_init(&device->dev_list); |
| 1227 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1228 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1229 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1230 | |
| 1231 | if (fs_devices->seed) { |
| 1232 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1233 | goto again; |
| 1234 | } |
| 1235 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1236 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1237 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1238 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1239 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1240 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1241 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1242 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1243 | if (!device->bdev) |
| 1244 | return; |
| 1245 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1246 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1247 | sync_blockdev(device->bdev); |
| 1248 | invalidate_bdev(device->bdev); |
| 1249 | } |
| 1250 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1251 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1254 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1255 | { |
| 1256 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1257 | struct btrfs_device *new_device; |
| 1258 | struct rcu_string *name; |
| 1259 | |
| 1260 | if (device->bdev) |
| 1261 | fs_devices->open_devices--; |
| 1262 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1263 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1264 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1265 | list_del_init(&device->dev_alloc_list); |
| 1266 | fs_devices->rw_devices--; |
| 1267 | } |
| 1268 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1269 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1270 | fs_devices->missing_devices--; |
| 1271 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1272 | btrfs_close_bdev(device); |
| 1273 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1274 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1275 | device->uuid); |
| 1276 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1277 | |
| 1278 | /* Safe because we are under uuid_mutex */ |
| 1279 | if (device->name) { |
| 1280 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1281 | BUG_ON(!name); /* -ENOMEM */ |
| 1282 | rcu_assign_pointer(new_device->name, name); |
| 1283 | } |
| 1284 | |
| 1285 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1286 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1287 | |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 1288 | synchronize_rcu(); |
| 1289 | btrfs_free_device(device); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1292 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1293 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1294 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1295 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1296 | if (--fs_devices->opened > 0) |
| 1297 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1298 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1299 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1300 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1301 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1302 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1303 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1304 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1305 | WARN_ON(fs_devices->open_devices); |
| 1306 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1307 | fs_devices->opened = 0; |
| 1308 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1309 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1313 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1314 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1315 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1316 | int ret; |
| 1317 | |
| 1318 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1319 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1320 | if (!fs_devices->opened) { |
| 1321 | seed_devices = fs_devices->seed; |
| 1322 | fs_devices->seed = NULL; |
| 1323 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1324 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1325 | |
| 1326 | while (seed_devices) { |
| 1327 | fs_devices = seed_devices; |
| 1328 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1329 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1330 | free_fs_devices(fs_devices); |
| 1331 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1332 | return ret; |
| 1333 | } |
| 1334 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1335 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1336 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1337 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1338 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1339 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1340 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1341 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1342 | flags |= FMODE_EXCL; |
| 1343 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1344 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1345 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1346 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1347 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1348 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1349 | if (!latest_dev || |
| 1350 | device->generation > latest_dev->generation) |
| 1351 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1352 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1353 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1354 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1355 | goto out; |
| 1356 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1357 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1358 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1359 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1360 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1361 | return ret; |
| 1362 | } |
| 1363 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1364 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1365 | { |
| 1366 | struct btrfs_device *dev1, *dev2; |
| 1367 | |
| 1368 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1369 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1370 | |
| 1371 | if (dev1->devid < dev2->devid) |
| 1372 | return -1; |
| 1373 | else if (dev1->devid > dev2->devid) |
| 1374 | return 1; |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1378 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1379 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1380 | { |
| 1381 | int ret; |
| 1382 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1383 | lockdep_assert_held(&uuid_mutex); |
| 1384 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1385 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1386 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1387 | fs_devices->opened++; |
| 1388 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1389 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1390 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1391 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1392 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1393 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1394 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
| 1397 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1398 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1399 | { |
| 1400 | kunmap(page); |
| 1401 | put_page(page); |
| 1402 | } |
| 1403 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1404 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1405 | struct page **page, |
| 1406 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1407 | { |
| 1408 | void *p; |
| 1409 | pgoff_t index; |
| 1410 | |
| 1411 | /* make sure our super fits in the device */ |
| 1412 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1413 | return 1; |
| 1414 | |
| 1415 | /* make sure our super fits in the page */ |
| 1416 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1417 | return 1; |
| 1418 | |
| 1419 | /* make sure our super doesn't straddle pages on disk */ |
| 1420 | index = bytenr >> PAGE_SHIFT; |
| 1421 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1422 | return 1; |
| 1423 | |
| 1424 | /* pull in the page with our super */ |
| 1425 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1426 | index, GFP_KERNEL); |
| 1427 | |
| 1428 | if (IS_ERR_OR_NULL(*page)) |
| 1429 | return 1; |
| 1430 | |
| 1431 | p = kmap(*page); |
| 1432 | |
| 1433 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1434 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1435 | |
| 1436 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1437 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1438 | btrfs_release_disk_super(*page); |
| 1439 | return 1; |
| 1440 | } |
| 1441 | |
| 1442 | if ((*disk_super)->label[0] && |
| 1443 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1444 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1449 | int btrfs_forget_devices(const char *path) |
| 1450 | { |
| 1451 | int ret; |
| 1452 | |
| 1453 | mutex_lock(&uuid_mutex); |
| 1454 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1455 | mutex_unlock(&uuid_mutex); |
| 1456 | |
| 1457 | return ret; |
| 1458 | } |
| 1459 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1462 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1463 | * is read via pagecache |
| 1464 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1465 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1466 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1467 | { |
| 1468 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1469 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1470 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1471 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1472 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1473 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1474 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1475 | lockdep_assert_held(&uuid_mutex); |
| 1476 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1477 | /* |
| 1478 | * we would like to check all the supers, but that would make |
| 1479 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1480 | * So, we need to add a special mount option to scan for |
| 1481 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1482 | */ |
| 1483 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1484 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1485 | |
| 1486 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1487 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1488 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1489 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1490 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1491 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1492 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1493 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1494 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1495 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1496 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1497 | if (new_device_added) |
| 1498 | btrfs_free_stale_devices(path, device); |
| 1499 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1500 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1501 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1502 | |
| 1503 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1504 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1505 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1506 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1507 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1508 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1509 | /* |
| 1510 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1511 | * such is found, record the end of it in *start |
| 1512 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1513 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1514 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1515 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1516 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1517 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1518 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1519 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1520 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1521 | &physical_start, &physical_end, |
| 1522 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1523 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1524 | if (in_range(physical_start, *start, len) || |
| 1525 | in_range(*start, physical_start, |
| 1526 | physical_end - physical_start)) { |
| 1527 | *start = physical_end + 1; |
| 1528 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1529 | } |
| 1530 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1531 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1535 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1536 | * find_free_dev_extent_start - find free space in the specified device |
| 1537 | * @device: the device which we search the free space in |
| 1538 | * @num_bytes: the size of the free space that we need |
| 1539 | * @search_start: the position from which to begin the search |
| 1540 | * @start: store the start of the free space. |
| 1541 | * @len: the size of the free space. that we find, or the size |
| 1542 | * of the max free space if we don't find suitable free space |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1543 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1544 | * this uses a pretty simple search, the expectation is that it is |
| 1545 | * called very infrequently and that a given device has a small number |
| 1546 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1547 | * |
| 1548 | * @start is used to store the start of the free space if we find. But if we |
| 1549 | * don't find suitable free space, it will be used to store the start position |
| 1550 | * of the max free space. |
| 1551 | * |
| 1552 | * @len is used to store the size of the free space that we find. |
| 1553 | * But if we don't find suitable free space, it is used to store the size of |
| 1554 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1555 | * |
| 1556 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1557 | * check to ensure dev extents are not double allocated. |
| 1558 | * This makes the function safe to allocate dev extents but may not report |
| 1559 | * correct usable device space, as device extent freed in current transaction |
| 1560 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1561 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1562 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1563 | u64 num_bytes, u64 search_start, u64 *start, |
| 1564 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1565 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1566 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1567 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1568 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1569 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1570 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1571 | u64 hole_size; |
| 1572 | u64 max_hole_start; |
| 1573 | u64 max_hole_size; |
| 1574 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1575 | u64 search_end = device->total_bytes; |
| 1576 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1577 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1579 | |
| 1580 | /* |
| 1581 | * We don't want to overwrite the superblock on the drive nor any area |
| 1582 | * used by the boot loader (grub for example), so we make sure to start |
| 1583 | * at an offset of at least 1MB. |
| 1584 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1585 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1586 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1587 | path = btrfs_alloc_path(); |
| 1588 | if (!path) |
| 1589 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1590 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1591 | max_hole_start = search_start; |
| 1592 | max_hole_size = 0; |
| 1593 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1594 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1595 | if (search_start >= search_end || |
| 1596 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1597 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1598 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1601 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1602 | path->search_commit_root = 1; |
| 1603 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1604 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1605 | key.objectid = device->devid; |
| 1606 | key.offset = search_start; |
| 1607 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1608 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1609 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1610 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1611 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1612 | if (ret > 0) { |
| 1613 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1614 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1615 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1616 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1617 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1618 | while (1) { |
| 1619 | l = path->nodes[0]; |
| 1620 | slot = path->slots[0]; |
| 1621 | if (slot >= btrfs_header_nritems(l)) { |
| 1622 | ret = btrfs_next_leaf(root, path); |
| 1623 | if (ret == 0) |
| 1624 | continue; |
| 1625 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1626 | goto out; |
| 1627 | |
| 1628 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1629 | } |
| 1630 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1631 | |
| 1632 | if (key.objectid < device->devid) |
| 1633 | goto next; |
| 1634 | |
| 1635 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1636 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1637 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1638 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1640 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1641 | if (key.offset > search_start) { |
| 1642 | hole_size = key.offset - search_start; |
| 1643 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1644 | /* |
| 1645 | * Have to check before we set max_hole_start, otherwise |
| 1646 | * we could end up sending back this offset anyway. |
| 1647 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1648 | if (contains_pending_extent(device, &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1649 | hole_size)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1650 | if (key.offset >= search_start) |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1651 | hole_size = key.offset - search_start; |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1652 | else |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1653 | hole_size = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1654 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1655 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1656 | if (hole_size > max_hole_size) { |
| 1657 | max_hole_start = search_start; |
| 1658 | max_hole_size = hole_size; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * If this free space is greater than which we need, |
| 1663 | * it must be the max free space that we have found |
| 1664 | * until now, so max_hole_start must point to the start |
| 1665 | * of this free space and the length of this free space |
| 1666 | * is stored in max_hole_size. Thus, we return |
| 1667 | * max_hole_start and max_hole_size and go back to the |
| 1668 | * caller. |
| 1669 | */ |
| 1670 | if (hole_size >= num_bytes) { |
| 1671 | ret = 0; |
| 1672 | goto out; |
| 1673 | } |
| 1674 | } |
| 1675 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1676 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1677 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1678 | dev_extent); |
| 1679 | if (extent_end > search_start) |
| 1680 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1681 | next: |
| 1682 | path->slots[0]++; |
| 1683 | cond_resched(); |
| 1684 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1685 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | * At this point, search_start should be the end of |
| 1688 | * allocated dev extents, and when shrinking the device, |
| 1689 | * search_end may be smaller than search_start. |
| 1690 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1691 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1692 | hole_size = search_end - search_start; |
| 1693 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1694 | if (contains_pending_extent(device, &search_start, hole_size)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1695 | btrfs_release_path(path); |
| 1696 | goto again; |
| 1697 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1698 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1699 | if (hole_size > max_hole_size) { |
| 1700 | max_hole_start = search_start; |
| 1701 | max_hole_size = hole_size; |
| 1702 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1703 | } |
| 1704 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1705 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1706 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1707 | ret = -ENOSPC; |
| 1708 | else |
| 1709 | ret = 0; |
| 1710 | |
| 1711 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1712 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1713 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1714 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1715 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1716 | return ret; |
| 1717 | } |
| 1718 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1719 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1720 | u64 *start, u64 *len) |
| 1721 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1722 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1723 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1724 | } |
| 1725 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1726 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1727 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1728 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1729 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1730 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1731 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1732 | int ret; |
| 1733 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1734 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1735 | struct btrfs_key found_key; |
| 1736 | struct extent_buffer *leaf = NULL; |
| 1737 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1738 | |
| 1739 | path = btrfs_alloc_path(); |
| 1740 | if (!path) |
| 1741 | return -ENOMEM; |
| 1742 | |
| 1743 | key.objectid = device->devid; |
| 1744 | key.offset = start; |
| 1745 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1746 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1747 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1748 | if (ret > 0) { |
| 1749 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1750 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1751 | if (ret) |
| 1752 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1753 | leaf = path->nodes[0]; |
| 1754 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1755 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1756 | struct btrfs_dev_extent); |
| 1757 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1758 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1759 | key = found_key; |
| 1760 | btrfs_release_path(path); |
| 1761 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1762 | } else if (ret == 0) { |
| 1763 | leaf = path->nodes[0]; |
| 1764 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1765 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1766 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1767 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1768 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1769 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1770 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1771 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1772 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1773 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1774 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1775 | btrfs_handle_fs_error(fs_info, ret, |
| 1776 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1777 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1778 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1779 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1780 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1781 | btrfs_free_path(path); |
| 1782 | return ret; |
| 1783 | } |
| 1784 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1785 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1786 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1787 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1788 | { |
| 1789 | int ret; |
| 1790 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1791 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1792 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1793 | struct btrfs_dev_extent *extent; |
| 1794 | struct extent_buffer *leaf; |
| 1795 | struct btrfs_key key; |
| 1796 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1797 | WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1798 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1799 | path = btrfs_alloc_path(); |
| 1800 | if (!path) |
| 1801 | return -ENOMEM; |
| 1802 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1803 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1804 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1805 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1806 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1807 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1808 | if (ret) |
| 1809 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1810 | |
| 1811 | leaf = path->nodes[0]; |
| 1812 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1813 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1814 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1815 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1816 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1817 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1818 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1819 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1820 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1821 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1822 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1823 | btrfs_free_path(path); |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1827 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1828 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1829 | struct extent_map_tree *em_tree; |
| 1830 | struct extent_map *em; |
| 1831 | struct rb_node *n; |
| 1832 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1833 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1834 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1835 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1836 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1837 | if (n) { |
| 1838 | em = rb_entry(n, struct extent_map, rb_node); |
| 1839 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1840 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1841 | read_unlock(&em_tree->lock); |
| 1842 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1843 | return ret; |
| 1844 | } |
| 1845 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1846 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1847 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1848 | { |
| 1849 | int ret; |
| 1850 | struct btrfs_key key; |
| 1851 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1852 | struct btrfs_path *path; |
| 1853 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1854 | path = btrfs_alloc_path(); |
| 1855 | if (!path) |
| 1856 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1857 | |
| 1858 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1859 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1860 | key.offset = (u64)-1; |
| 1861 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1862 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1863 | if (ret < 0) |
| 1864 | goto error; |
| 1865 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1866 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1867 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1868 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1869 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1870 | BTRFS_DEV_ITEM_KEY); |
| 1871 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1872 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1873 | } else { |
| 1874 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1875 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1876 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1877 | } |
| 1878 | ret = 0; |
| 1879 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1880 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1881 | return ret; |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * the device information is stored in the chunk root |
| 1886 | * the btrfs_device struct should be fully filled in |
| 1887 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1888 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1889 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1890 | { |
| 1891 | int ret; |
| 1892 | struct btrfs_path *path; |
| 1893 | struct btrfs_dev_item *dev_item; |
| 1894 | struct extent_buffer *leaf; |
| 1895 | struct btrfs_key key; |
| 1896 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1897 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1898 | path = btrfs_alloc_path(); |
| 1899 | if (!path) |
| 1900 | return -ENOMEM; |
| 1901 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1902 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1903 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1904 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1905 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1906 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1907 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1908 | if (ret) |
| 1909 | goto out; |
| 1910 | |
| 1911 | leaf = path->nodes[0]; |
| 1912 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1913 | |
| 1914 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1915 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1916 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1917 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1918 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1919 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1920 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1921 | btrfs_device_get_disk_total_bytes(device)); |
| 1922 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1923 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1924 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1925 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1926 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1927 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1928 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1929 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1930 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1931 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1932 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1933 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1934 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1935 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1936 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1937 | out: |
| 1938 | btrfs_free_path(path); |
| 1939 | return ret; |
| 1940 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1941 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1942 | /* |
| 1943 | * Function to update ctime/mtime for a given device path. |
| 1944 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1945 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1946 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1947 | { |
| 1948 | struct file *filp; |
| 1949 | |
| 1950 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1951 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1952 | return; |
| 1953 | file_update_time(filp); |
| 1954 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1955 | } |
| 1956 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1957 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1958 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1959 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1960 | int ret; |
| 1961 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1962 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1963 | struct btrfs_trans_handle *trans; |
| 1964 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1965 | path = btrfs_alloc_path(); |
| 1966 | if (!path) |
| 1967 | return -ENOMEM; |
| 1968 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1969 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1970 | if (IS_ERR(trans)) { |
| 1971 | btrfs_free_path(path); |
| 1972 | return PTR_ERR(trans); |
| 1973 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1974 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1975 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1976 | key.offset = device->devid; |
| 1977 | |
| 1978 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1979 | if (ret) { |
| 1980 | if (ret > 0) |
| 1981 | ret = -ENOENT; |
| 1982 | btrfs_abort_transaction(trans, ret); |
| 1983 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1984 | goto out; |
| 1985 | } |
| 1986 | |
| 1987 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1988 | if (ret) { |
| 1989 | btrfs_abort_transaction(trans, ret); |
| 1990 | btrfs_end_transaction(trans); |
| 1991 | } |
| 1992 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1993 | out: |
| 1994 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1995 | if (!ret) |
| 1996 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1997 | return ret; |
| 1998 | } |
| 1999 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 2000 | /* |
| 2001 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 2002 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 2003 | * replace. |
| 2004 | */ |
| 2005 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 2006 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2007 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2008 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2009 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2010 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2011 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2012 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2013 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2014 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2015 | all_avail = fs_info->avail_data_alloc_bits | |
| 2016 | fs_info->avail_system_alloc_bits | |
| 2017 | fs_info->avail_metadata_alloc_bits; |
| 2018 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2019 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2020 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 2021 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2022 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2023 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2024 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 2025 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2026 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2027 | if (ret) |
| 2028 | return ret; |
| 2029 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 2035 | static struct btrfs_device * btrfs_find_next_active_device( |
| 2036 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2037 | { |
| 2038 | struct btrfs_device *next_device; |
| 2039 | |
| 2040 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 2041 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2042 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 2043 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2044 | return next_device; |
| 2045 | } |
| 2046 | |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 2052 | * and replace it with the provided or the next active device, in the context |
| 2053 | * where this function called, there should be always be another device (or |
| 2054 | * this_dev) which is active. |
| 2055 | */ |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2056 | void btrfs_assign_next_active_device(struct btrfs_device *device, |
| 2057 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2058 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2059 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2060 | struct btrfs_device *next_device; |
| 2061 | |
| 2062 | if (this_dev) |
| 2063 | next_device = this_dev; |
| 2064 | else |
| 2065 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 2066 | device); |
| 2067 | ASSERT(next_device); |
| 2068 | |
| 2069 | if (fs_info->sb->s_bdev && |
| 2070 | (fs_info->sb->s_bdev == device->bdev)) |
| 2071 | fs_info->sb->s_bdev = next_device->bdev; |
| 2072 | |
| 2073 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 2074 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2075 | } |
| 2076 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2077 | /* |
| 2078 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 2079 | * currently replaced. |
| 2080 | */ |
| 2081 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 2082 | { |
| 2083 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 2084 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2085 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2086 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 2087 | ASSERT(num_devices > 1); |
| 2088 | num_devices--; |
| 2089 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2090 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2091 | |
| 2092 | return num_devices; |
| 2093 | } |
| 2094 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2095 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2096 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2097 | { |
| 2098 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2099 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2100 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2101 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2102 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2103 | |
| 2104 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2105 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2106 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2107 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2108 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2109 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2110 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2111 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2112 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2113 | |
| 2114 | if (IS_ERR(device)) { |
| 2115 | if (PTR_ERR(device) == -ENOENT && |
| 2116 | strcmp(device_path, "missing") == 0) |
| 2117 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2118 | else |
| 2119 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2120 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2121 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2122 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2123 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2124 | btrfs_warn_in_rcu(fs_info, |
| 2125 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2126 | rcu_str_deref(device->name), device->devid); |
| 2127 | ret = -ETXTBSY; |
| 2128 | goto out; |
| 2129 | } |
| 2130 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2131 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2132 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2133 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2134 | } |
| 2135 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2136 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2137 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2138 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2139 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2140 | } |
| 2141 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2142 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2143 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2144 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2145 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2146 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2147 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2148 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2149 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2150 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2151 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2152 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2153 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2154 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2155 | /* |
| 2156 | * TODO: the superblock still includes this device in its num_devices |
| 2157 | * counter although write_all_supers() is not locked out. This |
| 2158 | * could give a filesystem state which requires a degraded mount. |
| 2159 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2160 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2161 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2162 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2163 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2164 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2165 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2166 | |
| 2167 | /* |
| 2168 | * the device list mutex makes sure that we don't change |
| 2169 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2170 | * the device supers. Whoever is writing all supers, should |
| 2171 | * lock the device list mutex before getting the number of |
| 2172 | * devices in the super block (super_copy). Conversely, |
| 2173 | * whoever updates the number of devices in the super block |
| 2174 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2175 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2176 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2177 | /* |
| 2178 | * In normal cases the cur_devices == fs_devices. But in case |
| 2179 | * of deleting a seed device, the cur_devices should point to |
| 2180 | * its own fs_devices listed under the fs_devices->seed. |
| 2181 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2182 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2183 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2184 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2185 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2186 | cur_devices->num_devices--; |
| 2187 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2188 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2189 | if (cur_devices != fs_devices) |
| 2190 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2191 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2192 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2193 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2194 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2195 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2196 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2197 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2198 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2199 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2200 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2201 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2202 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2203 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2204 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2205 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2206 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2207 | /* |
| 2208 | * at this point, the device is zero sized and detached from |
| 2209 | * the devices list. All that's left is to zero out the old |
| 2210 | * supers and free the device. |
| 2211 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2212 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2213 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2214 | |
| 2215 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2216 | synchronize_rcu(); |
| 2217 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2218 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2219 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2220 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2221 | if (fs_devices->seed == cur_devices) { |
| 2222 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2223 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2224 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2225 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2226 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2227 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2228 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2229 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2230 | } |
| 2231 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2232 | out: |
| 2233 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2234 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2235 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2236 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2237 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2238 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2239 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2240 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2241 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2242 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2243 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2244 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2245 | } |
| 2246 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2247 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2248 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2249 | struct btrfs_fs_devices *fs_devices; |
| 2250 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2251 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2252 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2253 | /* |
| 2254 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2255 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2256 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2257 | * srcdev will have its correct fs_devices in both the cases. |
| 2258 | */ |
| 2259 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2260 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2261 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2262 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2263 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2264 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2265 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2266 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2267 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2268 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2269 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2270 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2271 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2272 | } |
| 2273 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2274 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2275 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2276 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2277 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2278 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2279 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2280 | /* zero out the old super if it is writable */ |
| 2281 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2282 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2283 | |
| 2284 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2285 | synchronize_rcu(); |
| 2286 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2287 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2288 | /* if this is no devs we rather delete the fs_devices */ |
| 2289 | if (!fs_devices->num_devices) { |
| 2290 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2291 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2292 | /* |
| 2293 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2294 | * seed. In case of a seed device being replaced, the replace |
| 2295 | * target added to the sprout FS, so there will be no more |
| 2296 | * device left under the seed FS. |
| 2297 | */ |
| 2298 | ASSERT(fs_devices->seeding); |
| 2299 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2300 | tmp_fs_devices = fs_info->fs_devices; |
| 2301 | while (tmp_fs_devices) { |
| 2302 | if (tmp_fs_devices->seed == fs_devices) { |
| 2303 | tmp_fs_devices->seed = fs_devices->seed; |
| 2304 | break; |
| 2305 | } |
| 2306 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2307 | } |
| 2308 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2309 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2310 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2311 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2314 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2315 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2316 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2317 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2318 | WARN_ON(!tgtdev); |
| 2319 | mutex_lock(&fs_devices->device_list_mutex); |
| 2320 | |
| 2321 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2322 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2323 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2324 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2325 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2326 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2327 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2328 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2329 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2330 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2331 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2332 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2333 | |
| 2334 | /* |
| 2335 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2336 | * may lead to a call to btrfs_show_devname() which will try |
| 2337 | * to hold device_list_mutex. And here this device |
| 2338 | * is already out of device list, so we don't have to hold |
| 2339 | * the device_list_mutex lock. |
| 2340 | */ |
| 2341 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2342 | |
| 2343 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2344 | synchronize_rcu(); |
| 2345 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2348 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2349 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2350 | { |
| 2351 | int ret = 0; |
| 2352 | struct btrfs_super_block *disk_super; |
| 2353 | u64 devid; |
| 2354 | u8 *dev_uuid; |
| 2355 | struct block_device *bdev; |
| 2356 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2357 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2358 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2359 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2360 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2361 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2362 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2363 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2364 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2365 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2366 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2367 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2368 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2369 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2370 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2371 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2372 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2373 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2374 | if (!device) |
| 2375 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2376 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2377 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2378 | } |
| 2379 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2380 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2381 | * Lookup a device given by device id, or the path if the id is 0. |
| 2382 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2383 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2384 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2385 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2386 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2387 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2388 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2389 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2390 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2391 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2392 | if (!device) |
| 2393 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2394 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2395 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2396 | |
| 2397 | if (!device_path || !device_path[0]) |
| 2398 | return ERR_PTR(-EINVAL); |
| 2399 | |
| 2400 | if (strcmp(device_path, "missing") == 0) { |
| 2401 | /* Find first missing device */ |
| 2402 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2403 | dev_list) { |
| 2404 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2405 | &device->dev_state) && !device->bdev) |
| 2406 | return device; |
| 2407 | } |
| 2408 | return ERR_PTR(-ENOENT); |
| 2409 | } |
| 2410 | |
| 2411 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2414 | /* |
| 2415 | * does all the dirty work required for changing file system's UUID. |
| 2416 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2417 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2418 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2419 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2420 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2421 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2422 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2423 | struct btrfs_device *device; |
| 2424 | u64 super_flags; |
| 2425 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2426 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2427 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2428 | return -EINVAL; |
| 2429 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2430 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2431 | if (IS_ERR(seed_devices)) |
| 2432 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2433 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2434 | old_devices = clone_fs_devices(fs_devices); |
| 2435 | if (IS_ERR(old_devices)) { |
| 2436 | kfree(seed_devices); |
| 2437 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2438 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2439 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2440 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2441 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2442 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2443 | seed_devices->opened = 1; |
| 2444 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2445 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2446 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2447 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2448 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2449 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2450 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2451 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2452 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2453 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2454 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2455 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2456 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2457 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2458 | fs_devices->seeding = 0; |
| 2459 | fs_devices->num_devices = 0; |
| 2460 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2461 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2462 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2463 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2464 | |
| 2465 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2466 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2467 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2468 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2469 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2470 | super_flags = btrfs_super_flags(disk_super) & |
| 2471 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2472 | btrfs_set_super_flags(disk_super, super_flags); |
| 2473 | |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
| 2477 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2478 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2479 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2480 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2481 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2482 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2483 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2484 | struct btrfs_path *path; |
| 2485 | struct extent_buffer *leaf; |
| 2486 | struct btrfs_dev_item *dev_item; |
| 2487 | struct btrfs_device *device; |
| 2488 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2489 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2490 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2491 | u64 devid; |
| 2492 | int ret; |
| 2493 | |
| 2494 | path = btrfs_alloc_path(); |
| 2495 | if (!path) |
| 2496 | return -ENOMEM; |
| 2497 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2498 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2499 | key.offset = 0; |
| 2500 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2501 | |
| 2502 | while (1) { |
| 2503 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2504 | if (ret < 0) |
| 2505 | goto error; |
| 2506 | |
| 2507 | leaf = path->nodes[0]; |
| 2508 | next_slot: |
| 2509 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2510 | ret = btrfs_next_leaf(root, path); |
| 2511 | if (ret > 0) |
| 2512 | break; |
| 2513 | if (ret < 0) |
| 2514 | goto error; |
| 2515 | leaf = path->nodes[0]; |
| 2516 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2517 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2518 | continue; |
| 2519 | } |
| 2520 | |
| 2521 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2522 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2523 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2524 | break; |
| 2525 | |
| 2526 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2527 | struct btrfs_dev_item); |
| 2528 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2529 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2530 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2531 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2532 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2533 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2534 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2535 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2536 | |
| 2537 | if (device->fs_devices->seeding) { |
| 2538 | btrfs_set_device_generation(leaf, dev_item, |
| 2539 | device->generation); |
| 2540 | btrfs_mark_buffer_dirty(leaf); |
| 2541 | } |
| 2542 | |
| 2543 | path->slots[0]++; |
| 2544 | goto next_slot; |
| 2545 | } |
| 2546 | ret = 0; |
| 2547 | error: |
| 2548 | btrfs_free_path(path); |
| 2549 | return ret; |
| 2550 | } |
| 2551 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2552 | int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path) |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2553 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2554 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2555 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2556 | struct btrfs_trans_handle *trans; |
| 2557 | struct btrfs_device *device; |
| 2558 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2559 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2560 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2561 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2562 | u64 orig_super_total_bytes; |
| 2563 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2564 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2565 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2566 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2567 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2568 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2569 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2570 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2571 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2572 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2573 | if (IS_ERR(bdev)) |
| 2574 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2575 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2576 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2577 | seeding_dev = 1; |
| 2578 | down_write(&sb->s_umount); |
| 2579 | mutex_lock(&uuid_mutex); |
| 2580 | } |
| 2581 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2582 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2583 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2584 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2585 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2586 | if (device->bdev == bdev) { |
| 2587 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2588 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2589 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2590 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2591 | } |
| 2592 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2593 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2594 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2595 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2596 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2597 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2598 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2599 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2600 | } |
| 2601 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2602 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2603 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2604 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2605 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2606 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2607 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2608 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2609 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2610 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2611 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2612 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2615 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2616 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2617 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2618 | device->io_width = fs_info->sectorsize; |
| 2619 | device->io_align = fs_info->sectorsize; |
| 2620 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2621 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2622 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2623 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2624 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2625 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2626 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2627 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2628 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2629 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2630 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2631 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2632 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2633 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2634 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2635 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2636 | if (ret) { |
| 2637 | btrfs_abort_transaction(trans, ret); |
| 2638 | goto error_trans; |
| 2639 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2640 | } |
| 2641 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2642 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2643 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2644 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2645 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2646 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2647 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2648 | fs_devices->num_devices++; |
| 2649 | fs_devices->open_devices++; |
| 2650 | fs_devices->rw_devices++; |
| 2651 | fs_devices->total_devices++; |
| 2652 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2653 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2654 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2655 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2656 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2657 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2658 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2659 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2660 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2661 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2662 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2663 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2664 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2665 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2666 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2667 | |
| 2668 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2669 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2670 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2671 | /* |
| 2672 | * we've got more storage, clear any full flags on the space |
| 2673 | * infos |
| 2674 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2675 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2676 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2677 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2678 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2679 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2680 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2681 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2682 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2683 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2684 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2685 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2686 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2687 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2688 | } |
| 2689 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2690 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2691 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2692 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2693 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | if (seeding_dev) { |
| 2697 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2698 | |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2699 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2700 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2701 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2702 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2703 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2704 | |
| 2705 | /* Sprouting would change fsid of the mounted root, |
| 2706 | * so rename the fsid on the sysfs |
| 2707 | */ |
| 2708 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 2709 | fs_info->fs_devices->fsid); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2710 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2711 | btrfs_warn(fs_info, |
| 2712 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2713 | } |
| 2714 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2715 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2716 | |
| 2717 | if (seeding_dev) { |
| 2718 | mutex_unlock(&uuid_mutex); |
| 2719 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2720 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2721 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2722 | if (ret) /* transaction commit */ |
| 2723 | return ret; |
| 2724 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2725 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2726 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2727 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2728 | "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2729 | trans = btrfs_attach_transaction(root); |
| 2730 | if (IS_ERR(trans)) { |
| 2731 | if (PTR_ERR(trans) == -ENOENT) |
| 2732 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2733 | ret = PTR_ERR(trans); |
| 2734 | trans = NULL; |
| 2735 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2736 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2737 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2738 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2739 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2740 | /* Update ctime/mtime for libblkid */ |
| 2741 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2742 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2743 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2744 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2745 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2746 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2747 | mutex_lock(&fs_info->chunk_mutex); |
| 2748 | list_del_rcu(&device->dev_list); |
| 2749 | list_del(&device->dev_alloc_list); |
| 2750 | fs_info->fs_devices->num_devices--; |
| 2751 | fs_info->fs_devices->open_devices--; |
| 2752 | fs_info->fs_devices->rw_devices--; |
| 2753 | fs_info->fs_devices->total_devices--; |
| 2754 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2755 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2756 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2757 | orig_super_total_bytes); |
| 2758 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2759 | orig_super_num_devices); |
| 2760 | mutex_unlock(&fs_info->chunk_mutex); |
| 2761 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2762 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2763 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2764 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2765 | if (trans) |
| 2766 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2767 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2768 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2769 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2770 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2771 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2772 | mutex_unlock(&uuid_mutex); |
| 2773 | up_write(&sb->s_umount); |
| 2774 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2775 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2776 | } |
| 2777 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2778 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2779 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2780 | { |
| 2781 | int ret; |
| 2782 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2783 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2784 | struct btrfs_dev_item *dev_item; |
| 2785 | struct extent_buffer *leaf; |
| 2786 | struct btrfs_key key; |
| 2787 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2788 | path = btrfs_alloc_path(); |
| 2789 | if (!path) |
| 2790 | return -ENOMEM; |
| 2791 | |
| 2792 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2793 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2794 | key.offset = device->devid; |
| 2795 | |
| 2796 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2797 | if (ret < 0) |
| 2798 | goto out; |
| 2799 | |
| 2800 | if (ret > 0) { |
| 2801 | ret = -ENOENT; |
| 2802 | goto out; |
| 2803 | } |
| 2804 | |
| 2805 | leaf = path->nodes[0]; |
| 2806 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2807 | |
| 2808 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2809 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2810 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2811 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2812 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2813 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2814 | btrfs_device_get_disk_total_bytes(device)); |
| 2815 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2816 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2817 | btrfs_mark_buffer_dirty(leaf); |
| 2818 | |
| 2819 | out: |
| 2820 | btrfs_free_path(path); |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2824 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2825 | struct btrfs_device *device, u64 new_size) |
| 2826 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2827 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2828 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2829 | u64 old_total; |
| 2830 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2831 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2832 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2833 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2834 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2835 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2836 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2837 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2838 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2839 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2840 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2841 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2842 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2843 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2844 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2845 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2846 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2847 | btrfs_set_super_total_bytes(super_copy, |
| 2848 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2849 | device->fs_devices->total_rw_bytes += diff; |
| 2850 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2851 | btrfs_device_set_total_bytes(device, new_size); |
| 2852 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2853 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2854 | if (list_empty(&device->post_commit_list)) |
| 2855 | list_add_tail(&device->post_commit_list, |
| 2856 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2857 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2858 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2859 | return btrfs_update_device(trans, device); |
| 2860 | } |
| 2861 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2862 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2863 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2864 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2865 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2866 | int ret; |
| 2867 | struct btrfs_path *path; |
| 2868 | struct btrfs_key key; |
| 2869 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2870 | path = btrfs_alloc_path(); |
| 2871 | if (!path) |
| 2872 | return -ENOMEM; |
| 2873 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2874 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2875 | key.offset = chunk_offset; |
| 2876 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2877 | |
| 2878 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2879 | if (ret < 0) |
| 2880 | goto out; |
| 2881 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2882 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2883 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2884 | ret = -ENOENT; |
| 2885 | goto out; |
| 2886 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2887 | |
| 2888 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2889 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2890 | btrfs_handle_fs_error(fs_info, ret, |
| 2891 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2892 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2893 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2894 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2895 | } |
| 2896 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2897 | static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2898 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2899 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2900 | struct btrfs_disk_key *disk_key; |
| 2901 | struct btrfs_chunk *chunk; |
| 2902 | u8 *ptr; |
| 2903 | int ret = 0; |
| 2904 | u32 num_stripes; |
| 2905 | u32 array_size; |
| 2906 | u32 len = 0; |
| 2907 | u32 cur; |
| 2908 | struct btrfs_key key; |
| 2909 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2910 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2911 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2912 | |
| 2913 | ptr = super_copy->sys_chunk_array; |
| 2914 | cur = 0; |
| 2915 | |
| 2916 | while (cur < array_size) { |
| 2917 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2918 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2919 | |
| 2920 | len = sizeof(*disk_key); |
| 2921 | |
| 2922 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2923 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2924 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2925 | len += btrfs_chunk_item_size(num_stripes); |
| 2926 | } else { |
| 2927 | ret = -EIO; |
| 2928 | break; |
| 2929 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2930 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2931 | key.offset == chunk_offset) { |
| 2932 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2933 | array_size -= len; |
| 2934 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2935 | } else { |
| 2936 | ptr += len; |
| 2937 | cur += len; |
| 2938 | } |
| 2939 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2940 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2941 | return ret; |
| 2942 | } |
| 2943 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2944 | /* |
| 2945 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2946 | * @logical: Logical block offset in bytes. |
| 2947 | * @length: Length of extent in bytes. |
| 2948 | * |
| 2949 | * Return: Chunk mapping or ERR_PTR. |
| 2950 | */ |
| 2951 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2952 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2953 | { |
| 2954 | struct extent_map_tree *em_tree; |
| 2955 | struct extent_map *em; |
| 2956 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2957 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2958 | read_lock(&em_tree->lock); |
| 2959 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2960 | read_unlock(&em_tree->lock); |
| 2961 | |
| 2962 | if (!em) { |
| 2963 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2964 | logical, length); |
| 2965 | return ERR_PTR(-EINVAL); |
| 2966 | } |
| 2967 | |
| 2968 | if (em->start > logical || em->start + em->len < logical) { |
| 2969 | btrfs_crit(fs_info, |
| 2970 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2971 | logical, length, em->start, em->start + em->len); |
| 2972 | free_extent_map(em); |
| 2973 | return ERR_PTR(-EINVAL); |
| 2974 | } |
| 2975 | |
| 2976 | /* callers are responsible for dropping em's ref. */ |
| 2977 | return em; |
| 2978 | } |
| 2979 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2980 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2981 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2982 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2983 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2984 | struct map_lookup *map; |
| 2985 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2986 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2987 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2988 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2989 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2990 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2991 | /* |
| 2992 | * This is a logic error, but we don't want to just rely on the |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 2993 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2994 | * do anything we still error out. |
| 2995 | */ |
| 2996 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2997 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2998 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2999 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3000 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 3001 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3002 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3003 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3004 | /* |
| 3005 | * Take the device list mutex to prevent races with the final phase of |
| 3006 | * a device replace operation that replaces the device object associated |
| 3007 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 3008 | */ |
| 3009 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3010 | for (i = 0; i < map->num_stripes; i++) { |
| 3011 | struct btrfs_device *device = map->stripes[i].dev; |
| 3012 | ret = btrfs_free_dev_extent(trans, device, |
| 3013 | map->stripes[i].physical, |
| 3014 | &dev_extent_len); |
| 3015 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3016 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3017 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3018 | goto out; |
| 3019 | } |
| 3020 | |
| 3021 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3022 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3023 | btrfs_device_set_bytes_used(device, |
| 3024 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 3025 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3026 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3027 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3028 | } |
| 3029 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 3030 | ret = btrfs_update_device(trans, device); |
| 3031 | if (ret) { |
| 3032 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3033 | btrfs_abort_transaction(trans, ret); |
| 3034 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3035 | } |
| 3036 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3037 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3038 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 3039 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3040 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3041 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3042 | goto out; |
| 3043 | } |
| 3044 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3045 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3046 | |
| 3047 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 3048 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3049 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3050 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3051 | goto out; |
| 3052 | } |
| 3053 | } |
| 3054 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 3055 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3056 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3057 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3058 | goto out; |
| 3059 | } |
| 3060 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3061 | out: |
| 3062 | /* once for us */ |
| 3063 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3064 | return ret; |
| 3065 | } |
| 3066 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3067 | static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3068 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3069 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3070 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3071 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3072 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3073 | /* |
| 3074 | * Prevent races with automatic removal of unused block groups. |
| 3075 | * After we relocate and before we remove the chunk with offset |
| 3076 | * chunk_offset, automatic removal of the block group can kick in, |
| 3077 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3078 | * |
| 3079 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3080 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3081 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3082 | * we release the path used to search the chunk/dev tree and before |
| 3083 | * the current task acquires this mutex and calls us. |
| 3084 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3085 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3086 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3087 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3088 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3089 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3090 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3091 | if (ret) |
| 3092 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3093 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3094 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3095 | chunk_offset); |
| 3096 | if (IS_ERR(trans)) { |
| 3097 | ret = PTR_ERR(trans); |
| 3098 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3099 | return ret; |
| 3100 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3101 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3102 | /* |
| 3103 | * step two, delete the device extents and the |
| 3104 | * chunk tree entries |
| 3105 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3106 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3107 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3108 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3109 | } |
| 3110 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3111 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3112 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3113 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3114 | struct btrfs_path *path; |
| 3115 | struct extent_buffer *leaf; |
| 3116 | struct btrfs_chunk *chunk; |
| 3117 | struct btrfs_key key; |
| 3118 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3119 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3120 | bool retried = false; |
| 3121 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3122 | int ret; |
| 3123 | |
| 3124 | path = btrfs_alloc_path(); |
| 3125 | if (!path) |
| 3126 | return -ENOMEM; |
| 3127 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3128 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3129 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3130 | key.offset = (u64)-1; |
| 3131 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3132 | |
| 3133 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3134 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3135 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3136 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3137 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3138 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3139 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3140 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3141 | |
| 3142 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3143 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3144 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3145 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3146 | if (ret < 0) |
| 3147 | goto error; |
| 3148 | if (ret > 0) |
| 3149 | break; |
| 3150 | |
| 3151 | leaf = path->nodes[0]; |
| 3152 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3153 | |
| 3154 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3155 | struct btrfs_chunk); |
| 3156 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3157 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3158 | |
| 3159 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3160 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3161 | if (ret == -ENOSPC) |
| 3162 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3163 | else |
| 3164 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3165 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3166 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3167 | |
| 3168 | if (found_key.offset == 0) |
| 3169 | break; |
| 3170 | key.offset = found_key.offset - 1; |
| 3171 | } |
| 3172 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3173 | if (failed && !retried) { |
| 3174 | failed = 0; |
| 3175 | retried = true; |
| 3176 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3177 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3178 | ret = -ENOSPC; |
| 3179 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3180 | error: |
| 3181 | btrfs_free_path(path); |
| 3182 | return ret; |
| 3183 | } |
| 3184 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3185 | /* |
| 3186 | * return 1 : allocate a data chunk successfully, |
| 3187 | * return <0: errors during allocating a data chunk, |
| 3188 | * return 0 : no need to allocate a data chunk. |
| 3189 | */ |
| 3190 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3191 | u64 chunk_offset) |
| 3192 | { |
| 3193 | struct btrfs_block_group_cache *cache; |
| 3194 | u64 bytes_used; |
| 3195 | u64 chunk_type; |
| 3196 | |
| 3197 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3198 | ASSERT(cache); |
| 3199 | chunk_type = cache->flags; |
| 3200 | btrfs_put_block_group(cache); |
| 3201 | |
| 3202 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 3203 | spin_lock(&fs_info->data_sinfo->lock); |
| 3204 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3205 | spin_unlock(&fs_info->data_sinfo->lock); |
| 3206 | |
| 3207 | if (!bytes_used) { |
| 3208 | struct btrfs_trans_handle *trans; |
| 3209 | int ret; |
| 3210 | |
| 3211 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3212 | if (IS_ERR(trans)) |
| 3213 | return PTR_ERR(trans); |
| 3214 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 3215 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3216 | BTRFS_BLOCK_GROUP_DATA); |
| 3217 | btrfs_end_transaction(trans); |
| 3218 | if (ret < 0) |
| 3219 | return ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3220 | return 1; |
| 3221 | } |
| 3222 | } |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3226 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3227 | struct btrfs_balance_control *bctl) |
| 3228 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3229 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3230 | struct btrfs_trans_handle *trans; |
| 3231 | struct btrfs_balance_item *item; |
| 3232 | struct btrfs_disk_balance_args disk_bargs; |
| 3233 | struct btrfs_path *path; |
| 3234 | struct extent_buffer *leaf; |
| 3235 | struct btrfs_key key; |
| 3236 | int ret, err; |
| 3237 | |
| 3238 | path = btrfs_alloc_path(); |
| 3239 | if (!path) |
| 3240 | return -ENOMEM; |
| 3241 | |
| 3242 | trans = btrfs_start_transaction(root, 0); |
| 3243 | if (IS_ERR(trans)) { |
| 3244 | btrfs_free_path(path); |
| 3245 | return PTR_ERR(trans); |
| 3246 | } |
| 3247 | |
| 3248 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3249 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3250 | key.offset = 0; |
| 3251 | |
| 3252 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3253 | sizeof(*item)); |
| 3254 | if (ret) |
| 3255 | goto out; |
| 3256 | |
| 3257 | leaf = path->nodes[0]; |
| 3258 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3259 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3260 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3261 | |
| 3262 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3263 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3264 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3265 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3266 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3267 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3268 | |
| 3269 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3270 | |
| 3271 | btrfs_mark_buffer_dirty(leaf); |
| 3272 | out: |
| 3273 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3274 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3275 | if (err && !ret) |
| 3276 | ret = err; |
| 3277 | return ret; |
| 3278 | } |
| 3279 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3280 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3281 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3282 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3283 | struct btrfs_trans_handle *trans; |
| 3284 | struct btrfs_path *path; |
| 3285 | struct btrfs_key key; |
| 3286 | int ret, err; |
| 3287 | |
| 3288 | path = btrfs_alloc_path(); |
| 3289 | if (!path) |
| 3290 | return -ENOMEM; |
| 3291 | |
| 3292 | trans = btrfs_start_transaction(root, 0); |
| 3293 | if (IS_ERR(trans)) { |
| 3294 | btrfs_free_path(path); |
| 3295 | return PTR_ERR(trans); |
| 3296 | } |
| 3297 | |
| 3298 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3299 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3300 | key.offset = 0; |
| 3301 | |
| 3302 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3303 | if (ret < 0) |
| 3304 | goto out; |
| 3305 | if (ret > 0) { |
| 3306 | ret = -ENOENT; |
| 3307 | goto out; |
| 3308 | } |
| 3309 | |
| 3310 | ret = btrfs_del_item(trans, root, path); |
| 3311 | out: |
| 3312 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3313 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3314 | if (err && !ret) |
| 3315 | ret = err; |
| 3316 | return ret; |
| 3317 | } |
| 3318 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3319 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3320 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3321 | * resume after balance was interrupted. |
| 3322 | */ |
| 3323 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3324 | { |
| 3325 | /* |
| 3326 | * Turn on soft mode for chunk types that were being converted. |
| 3327 | */ |
| 3328 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3329 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3330 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3331 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3332 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3333 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3334 | |
| 3335 | /* |
| 3336 | * Turn on usage filter if is not already used. The idea is |
| 3337 | * that chunks that we have already balanced should be |
| 3338 | * reasonably full. Don't do it for chunks that are being |
| 3339 | * converted - that will keep us from relocating unconverted |
| 3340 | * (albeit full) chunks. |
| 3341 | */ |
| 3342 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3343 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3344 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3345 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3346 | bctl->data.usage = 90; |
| 3347 | } |
| 3348 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3349 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3350 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3351 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3352 | bctl->sys.usage = 90; |
| 3353 | } |
| 3354 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3355 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3356 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3357 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3358 | bctl->meta.usage = 90; |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3363 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3364 | */ |
| 3365 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3366 | { |
| 3367 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3368 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3369 | |
| 3370 | BUG_ON(!fs_info->balance_ctl); |
| 3371 | |
| 3372 | spin_lock(&fs_info->balance_lock); |
| 3373 | fs_info->balance_ctl = NULL; |
| 3374 | spin_unlock(&fs_info->balance_lock); |
| 3375 | |
| 3376 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3377 | ret = del_balance_item(fs_info); |
| 3378 | if (ret) |
| 3379 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3380 | } |
| 3381 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3382 | /* |
| 3383 | * Balance filters. Return 1 if chunk should be filtered out |
| 3384 | * (should not be balanced). |
| 3385 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3386 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3387 | struct btrfs_balance_args *bargs) |
| 3388 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3389 | chunk_type = chunk_to_extended(chunk_type) & |
| 3390 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3391 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3392 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3393 | return 0; |
| 3394 | |
| 3395 | return 1; |
| 3396 | } |
| 3397 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3398 | static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3399 | struct btrfs_balance_args *bargs) |
| 3400 | { |
| 3401 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3402 | u64 chunk_used; |
| 3403 | u64 user_thresh_min; |
| 3404 | u64 user_thresh_max; |
| 3405 | int ret = 1; |
| 3406 | |
| 3407 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3408 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3409 | |
| 3410 | if (bargs->usage_min == 0) |
| 3411 | user_thresh_min = 0; |
| 3412 | else |
| 3413 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3414 | bargs->usage_min); |
| 3415 | |
| 3416 | if (bargs->usage_max == 0) |
| 3417 | user_thresh_max = 1; |
| 3418 | else if (bargs->usage_max > 100) |
| 3419 | user_thresh_max = cache->key.offset; |
| 3420 | else |
| 3421 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3422 | bargs->usage_max); |
| 3423 | |
| 3424 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3425 | ret = 0; |
| 3426 | |
| 3427 | btrfs_put_block_group(cache); |
| 3428 | return ret; |
| 3429 | } |
| 3430 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3431 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3432 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3433 | { |
| 3434 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3435 | u64 chunk_used, user_thresh; |
| 3436 | int ret = 1; |
| 3437 | |
| 3438 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3439 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3440 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3441 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3442 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3443 | else if (bargs->usage > 100) |
| 3444 | user_thresh = cache->key.offset; |
| 3445 | else |
| 3446 | user_thresh = div_factor_fine(cache->key.offset, |
| 3447 | bargs->usage); |
| 3448 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3449 | if (chunk_used < user_thresh) |
| 3450 | ret = 0; |
| 3451 | |
| 3452 | btrfs_put_block_group(cache); |
| 3453 | return ret; |
| 3454 | } |
| 3455 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3456 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3457 | struct btrfs_chunk *chunk, |
| 3458 | struct btrfs_balance_args *bargs) |
| 3459 | { |
| 3460 | struct btrfs_stripe *stripe; |
| 3461 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3462 | int i; |
| 3463 | |
| 3464 | for (i = 0; i < num_stripes; i++) { |
| 3465 | stripe = btrfs_stripe_nr(chunk, i); |
| 3466 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
| 3470 | return 1; |
| 3471 | } |
| 3472 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3473 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3474 | { |
| 3475 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3476 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3477 | const int nparity = btrfs_raid_array[index].nparity; |
| 3478 | |
| 3479 | if (nparity) |
| 3480 | return num_stripes - nparity; |
| 3481 | else |
| 3482 | return num_stripes / ncopies; |
| 3483 | } |
| 3484 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3485 | /* [pstart, pend) */ |
| 3486 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3487 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3488 | struct btrfs_balance_args *bargs) |
| 3489 | { |
| 3490 | struct btrfs_stripe *stripe; |
| 3491 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3492 | u64 stripe_offset; |
| 3493 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3494 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3495 | int factor; |
| 3496 | int i; |
| 3497 | |
| 3498 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3499 | return 0; |
| 3500 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3501 | type = btrfs_chunk_type(leaf, chunk); |
| 3502 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3503 | |
| 3504 | for (i = 0; i < num_stripes; i++) { |
| 3505 | stripe = btrfs_stripe_nr(chunk, i); |
| 3506 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3507 | continue; |
| 3508 | |
| 3509 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3510 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3511 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3512 | |
| 3513 | if (stripe_offset < bargs->pend && |
| 3514 | stripe_offset + stripe_length > bargs->pstart) |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | return 1; |
| 3519 | } |
| 3520 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3521 | /* [vstart, vend) */ |
| 3522 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3523 | struct btrfs_chunk *chunk, |
| 3524 | u64 chunk_offset, |
| 3525 | struct btrfs_balance_args *bargs) |
| 3526 | { |
| 3527 | if (chunk_offset < bargs->vend && |
| 3528 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3529 | /* at least part of the chunk is inside this vrange */ |
| 3530 | return 0; |
| 3531 | |
| 3532 | return 1; |
| 3533 | } |
| 3534 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3535 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3536 | struct btrfs_chunk *chunk, |
| 3537 | struct btrfs_balance_args *bargs) |
| 3538 | { |
| 3539 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3540 | |
| 3541 | if (bargs->stripes_min <= num_stripes |
| 3542 | && num_stripes <= bargs->stripes_max) |
| 3543 | return 0; |
| 3544 | |
| 3545 | return 1; |
| 3546 | } |
| 3547 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3548 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3549 | struct btrfs_balance_args *bargs) |
| 3550 | { |
| 3551 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3552 | return 0; |
| 3553 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3554 | chunk_type = chunk_to_extended(chunk_type) & |
| 3555 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3556 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3557 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3558 | return 1; |
| 3559 | |
| 3560 | return 0; |
| 3561 | } |
| 3562 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3563 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3564 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3565 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3566 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3567 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3568 | struct btrfs_balance_args *bargs = NULL; |
| 3569 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3570 | |
| 3571 | /* type filter */ |
| 3572 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3573 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
| 3577 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3578 | bargs = &bctl->data; |
| 3579 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3580 | bargs = &bctl->sys; |
| 3581 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3582 | bargs = &bctl->meta; |
| 3583 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3584 | /* profiles filter */ |
| 3585 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3586 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3587 | return 0; |
| 3588 | } |
| 3589 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3590 | /* usage filter */ |
| 3591 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3592 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3593 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3594 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3595 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3596 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3597 | } |
| 3598 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3599 | /* devid filter */ |
| 3600 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3601 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3602 | return 0; |
| 3603 | } |
| 3604 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3605 | /* drange filter, makes sense only with devid filter */ |
| 3606 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3607 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3608 | return 0; |
| 3609 | } |
| 3610 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3611 | /* vrange filter */ |
| 3612 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3613 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3617 | /* stripes filter */ |
| 3618 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3619 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3620 | return 0; |
| 3621 | } |
| 3622 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3623 | /* soft profile changing mode */ |
| 3624 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3625 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3629 | /* |
| 3630 | * limited by count, must be the last filter |
| 3631 | */ |
| 3632 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3633 | if (bargs->limit == 0) |
| 3634 | return 0; |
| 3635 | else |
| 3636 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3637 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3638 | /* |
| 3639 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3640 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3641 | * about the count of all chunks that satisfy the filters. |
| 3642 | */ |
| 3643 | if (bargs->limit_max == 0) |
| 3644 | return 0; |
| 3645 | else |
| 3646 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3649 | return 1; |
| 3650 | } |
| 3651 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3652 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3653 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3654 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3655 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3656 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3657 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3658 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3659 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3660 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3661 | struct extent_buffer *leaf; |
| 3662 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3663 | int ret; |
| 3664 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3665 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3666 | /* The single value limit and min/max limits use the same bytes in the */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3667 | u64 limit_data = bctl->data.limit; |
| 3668 | u64 limit_meta = bctl->meta.limit; |
| 3669 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3670 | u32 count_data = 0; |
| 3671 | u32 count_meta = 0; |
| 3672 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3673 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3674 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3675 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3676 | if (!path) { |
| 3677 | ret = -ENOMEM; |
| 3678 | goto error; |
| 3679 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3680 | |
| 3681 | /* zero out stat counters */ |
| 3682 | spin_lock(&fs_info->balance_lock); |
| 3683 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3684 | spin_unlock(&fs_info->balance_lock); |
| 3685 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3686 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3687 | /* |
| 3688 | * The single value limit and min/max limits use the same bytes |
| 3689 | * in the |
| 3690 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3691 | bctl->data.limit = limit_data; |
| 3692 | bctl->meta.limit = limit_meta; |
| 3693 | bctl->sys.limit = limit_sys; |
| 3694 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3695 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3696 | key.offset = (u64)-1; |
| 3697 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3698 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3699 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3700 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3701 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3702 | ret = -ECANCELED; |
| 3703 | goto error; |
| 3704 | } |
| 3705 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3706 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3707 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3708 | if (ret < 0) { |
| 3709 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3710 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3711 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3712 | |
| 3713 | /* |
| 3714 | * this shouldn't happen, it means the last relocate |
| 3715 | * failed |
| 3716 | */ |
| 3717 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3718 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3719 | |
| 3720 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3721 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3722 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3723 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3724 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3725 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3726 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3727 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3728 | leaf = path->nodes[0]; |
| 3729 | slot = path->slots[0]; |
| 3730 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3731 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3732 | if (found_key.objectid != key.objectid) { |
| 3733 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3734 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3735 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3736 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3737 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3738 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3739 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3740 | if (!counting) { |
| 3741 | spin_lock(&fs_info->balance_lock); |
| 3742 | bctl->stat.considered++; |
| 3743 | spin_unlock(&fs_info->balance_lock); |
| 3744 | } |
| 3745 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3746 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3747 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3748 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3749 | if (!ret) { |
| 3750 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3751 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3752 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3753 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3754 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3755 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3756 | spin_lock(&fs_info->balance_lock); |
| 3757 | bctl->stat.expected++; |
| 3758 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3759 | |
| 3760 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3761 | count_data++; |
| 3762 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3763 | count_sys++; |
| 3764 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3765 | count_meta++; |
| 3766 | |
| 3767 | goto loop; |
| 3768 | } |
| 3769 | |
| 3770 | /* |
| 3771 | * Apply limit_min filter, no need to check if the LIMITS |
| 3772 | * filter is used, limit_min is 0 by default |
| 3773 | */ |
| 3774 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3775 | count_data < bctl->data.limit_min) |
| 3776 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3777 | count_meta < bctl->meta.limit_min) |
| 3778 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3779 | count_sys < bctl->sys.limit_min)) { |
| 3780 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3781 | goto loop; |
| 3782 | } |
| 3783 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3784 | if (!chunk_reserved) { |
| 3785 | /* |
| 3786 | * We may be relocating the only data chunk we have, |
| 3787 | * which could potentially end up with losing data's |
| 3788 | * raid profile, so lets allocate an empty one in |
| 3789 | * advance. |
| 3790 | */ |
| 3791 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3792 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3793 | if (ret < 0) { |
| 3794 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3795 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3796 | } else if (ret == 1) { |
| 3797 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3798 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3799 | } |
| 3800 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3801 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3802 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3803 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3804 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3805 | } else if (ret == -ETXTBSY) { |
| 3806 | btrfs_info(fs_info, |
| 3807 | "skipping relocation of block group %llu due to active swapfile", |
| 3808 | found_key.offset); |
| 3809 | ret = 0; |
| 3810 | } else if (ret) { |
| 3811 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3812 | } else { |
| 3813 | spin_lock(&fs_info->balance_lock); |
| 3814 | bctl->stat.completed++; |
| 3815 | spin_unlock(&fs_info->balance_lock); |
| 3816 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3817 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3818 | if (found_key.offset == 0) |
| 3819 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3820 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3821 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3822 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3823 | if (counting) { |
| 3824 | btrfs_release_path(path); |
| 3825 | counting = false; |
| 3826 | goto again; |
| 3827 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3828 | error: |
| 3829 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3830 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3831 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3832 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3833 | if (!ret) |
| 3834 | ret = -ENOSPC; |
| 3835 | } |
| 3836 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3837 | return ret; |
| 3838 | } |
| 3839 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3840 | /** |
| 3841 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3842 | * @flags: profile to validate |
| 3843 | * @extended: if true @flags is treated as an extended profile |
| 3844 | */ |
| 3845 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3846 | { |
| 3847 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3848 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3849 | |
| 3850 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3851 | |
| 3852 | /* 1) check that all other bits are zeroed */ |
| 3853 | if (flags & ~mask) |
| 3854 | return 0; |
| 3855 | |
| 3856 | /* 2) see if profile is reduced */ |
| 3857 | if (flags == 0) |
| 3858 | return !extended; /* "0" is valid for usual profiles */ |
| 3859 | |
| 3860 | /* true if exactly one bit set */ |
David Sterba | 818255f | 2018-09-21 14:26:34 +0200 | [diff] [blame] | 3861 | return is_power_of_2(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3862 | } |
| 3863 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3864 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3865 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3866 | /* cancel requested || normal exit path */ |
| 3867 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3868 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3869 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3872 | /* Non-zero return value signifies invalidity */ |
| 3873 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3874 | u64 allowed) |
| 3875 | { |
| 3876 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3877 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3878 | (bctl_arg->target & ~allowed))); |
| 3879 | } |
| 3880 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3881 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3882 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3883 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3884 | * does not fit into the provided buffer. |
| 3885 | */ |
| 3886 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3887 | u32 size_buf) |
| 3888 | { |
| 3889 | int ret; |
| 3890 | u32 size_bp = size_buf; |
| 3891 | char *bp = buf; |
| 3892 | u64 flags = bargs->flags; |
| 3893 | char tmp_buf[128] = {'\0'}; |
| 3894 | |
| 3895 | if (!flags) |
| 3896 | return; |
| 3897 | |
| 3898 | #define CHECK_APPEND_NOARG(a) \ |
| 3899 | do { \ |
| 3900 | ret = snprintf(bp, size_bp, (a)); \ |
| 3901 | if (ret < 0 || ret >= size_bp) \ |
| 3902 | goto out_overflow; \ |
| 3903 | size_bp -= ret; \ |
| 3904 | bp += ret; \ |
| 3905 | } while (0) |
| 3906 | |
| 3907 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3908 | do { \ |
| 3909 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3910 | if (ret < 0 || ret >= size_bp) \ |
| 3911 | goto out_overflow; \ |
| 3912 | size_bp -= ret; \ |
| 3913 | bp += ret; \ |
| 3914 | } while (0) |
| 3915 | |
| 3916 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3917 | do { \ |
| 3918 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3919 | if (ret < 0 || ret >= size_bp) \ |
| 3920 | goto out_overflow; \ |
| 3921 | size_bp -= ret; \ |
| 3922 | bp += ret; \ |
| 3923 | } while (0) |
| 3924 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3925 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3926 | CHECK_APPEND_1ARG("convert=%s,", |
| 3927 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3928 | |
| 3929 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3930 | CHECK_APPEND_NOARG("soft,"); |
| 3931 | |
| 3932 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3933 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3934 | sizeof(tmp_buf)); |
| 3935 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3936 | } |
| 3937 | |
| 3938 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3939 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3940 | |
| 3941 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3942 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3943 | bargs->usage_min, bargs->usage_max); |
| 3944 | |
| 3945 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3946 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3947 | |
| 3948 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3949 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3950 | bargs->pstart, bargs->pend); |
| 3951 | |
| 3952 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3953 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3954 | bargs->vstart, bargs->vend); |
| 3955 | |
| 3956 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3957 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3958 | |
| 3959 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3960 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3961 | bargs->limit_min, bargs->limit_max); |
| 3962 | |
| 3963 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3964 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3965 | bargs->stripes_min, bargs->stripes_max); |
| 3966 | |
| 3967 | #undef CHECK_APPEND_2ARG |
| 3968 | #undef CHECK_APPEND_1ARG |
| 3969 | #undef CHECK_APPEND_NOARG |
| 3970 | |
| 3971 | out_overflow: |
| 3972 | |
| 3973 | if (size_bp < size_buf) |
| 3974 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3975 | else |
| 3976 | buf[0] = '\0'; |
| 3977 | } |
| 3978 | |
| 3979 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3980 | { |
| 3981 | u32 size_buf = 1024; |
| 3982 | char tmp_buf[192] = {'\0'}; |
| 3983 | char *buf; |
| 3984 | char *bp; |
| 3985 | u32 size_bp = size_buf; |
| 3986 | int ret; |
| 3987 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3988 | |
| 3989 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3990 | if (!buf) |
| 3991 | return; |
| 3992 | |
| 3993 | bp = buf; |
| 3994 | |
| 3995 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3996 | do { \ |
| 3997 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3998 | if (ret < 0 || ret >= size_bp) \ |
| 3999 | goto out_overflow; \ |
| 4000 | size_bp -= ret; \ |
| 4001 | bp += ret; \ |
| 4002 | } while (0) |
| 4003 | |
| 4004 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 4005 | CHECK_APPEND_1ARG("%s", "-f "); |
| 4006 | |
| 4007 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 4008 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 4009 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 4010 | } |
| 4011 | |
| 4012 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 4013 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 4014 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 4015 | } |
| 4016 | |
| 4017 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 4018 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 4019 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 4020 | } |
| 4021 | |
| 4022 | #undef CHECK_APPEND_1ARG |
| 4023 | |
| 4024 | out_overflow: |
| 4025 | |
| 4026 | if (size_bp < size_buf) |
| 4027 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 4028 | btrfs_info(fs_info, "balance: %s %s", |
| 4029 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 4030 | "resume" : "start", buf); |
| 4031 | |
| 4032 | kfree(buf); |
| 4033 | } |
| 4034 | |
| 4035 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4036 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4037 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4038 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 4039 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4040 | struct btrfs_ioctl_balance_args *bargs) |
| 4041 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4042 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4043 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4044 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4045 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 4046 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4047 | unsigned seq; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4048 | bool reducing_integrity; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4049 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4050 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4051 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4052 | atomic_read(&fs_info->balance_pause_req) || |
| 4053 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4054 | ret = -EINVAL; |
| 4055 | goto out; |
| 4056 | } |
| 4057 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4058 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 4059 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 4060 | mixed = 1; |
| 4061 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4062 | /* |
| 4063 | * In case of mixed groups both data and meta should be picked, |
| 4064 | * and identical options should be given for both of them. |
| 4065 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4066 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 4067 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4068 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4069 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4070 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4071 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4072 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4073 | ret = -EINVAL; |
| 4074 | goto out; |
| 4075 | } |
| 4076 | } |
| 4077 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4078 | num_devices = btrfs_num_devices(fs_info); |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4079 | allowed = 0; |
| 4080 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 4081 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 4082 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4083 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4084 | if (validate_convert_profile(&bctl->data, allowed)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4085 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4086 | "balance: invalid convert data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4087 | btrfs_bg_type_to_raid_name(bctl->data.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4088 | ret = -EINVAL; |
| 4089 | goto out; |
| 4090 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4091 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4092 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4093 | "balance: invalid convert metadata profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4094 | btrfs_bg_type_to_raid_name(bctl->meta.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4095 | ret = -EINVAL; |
| 4096 | goto out; |
| 4097 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4098 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4099 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4100 | "balance: invalid convert system profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4101 | btrfs_bg_type_to_raid_name(bctl->sys.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4102 | ret = -EINVAL; |
| 4103 | goto out; |
| 4104 | } |
| 4105 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 4106 | /* |
| 4107 | * Allow to reduce metadata or system integrity only if force set for |
| 4108 | * profiles with redundancy (copies, parity) |
| 4109 | */ |
| 4110 | allowed = 0; |
| 4111 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4112 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4113 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4114 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4115 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4116 | do { |
| 4117 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4118 | |
| 4119 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4120 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4121 | !(bctl->sys.target & allowed)) || |
| 4122 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4123 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4124 | !(bctl->meta.target & allowed))) |
| 4125 | reducing_integrity = true; |
| 4126 | else |
| 4127 | reducing_integrity = false; |
| 4128 | |
| 4129 | /* if we're not converting, the target field is uninitialized */ |
| 4130 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4131 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4132 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4133 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4134 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4135 | |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4136 | if (reducing_integrity) { |
| 4137 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4138 | btrfs_info(fs_info, |
| 4139 | "balance: force reducing metadata integrity"); |
| 4140 | } else { |
| 4141 | btrfs_err(fs_info, |
| 4142 | "balance: reduces metadata integrity, use --force if you want this"); |
| 4143 | ret = -EINVAL; |
| 4144 | goto out; |
| 4145 | } |
| 4146 | } |
| 4147 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4148 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4149 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4150 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4151 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4152 | btrfs_bg_type_to_raid_name(meta_target), |
| 4153 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 4156 | if (fs_info->send_in_progress) { |
| 4157 | btrfs_warn_rl(fs_info, |
| 4158 | "cannot run balance while send operations are in progress (%d in progress)", |
| 4159 | fs_info->send_in_progress); |
| 4160 | ret = -EAGAIN; |
| 4161 | goto out; |
| 4162 | } |
| 4163 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4164 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4165 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4166 | goto out; |
| 4167 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4168 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4169 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4170 | BUG_ON(fs_info->balance_ctl); |
| 4171 | spin_lock(&fs_info->balance_lock); |
| 4172 | fs_info->balance_ctl = bctl; |
| 4173 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4174 | } else { |
| 4175 | BUG_ON(ret != -EEXIST); |
| 4176 | spin_lock(&fs_info->balance_lock); |
| 4177 | update_balance_args(bctl); |
| 4178 | spin_unlock(&fs_info->balance_lock); |
| 4179 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4180 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4181 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4182 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4183 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4184 | mutex_unlock(&fs_info->balance_mutex); |
| 4185 | |
| 4186 | ret = __btrfs_balance(fs_info); |
| 4187 | |
| 4188 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4189 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4190 | btrfs_info(fs_info, "balance: paused"); |
| 4191 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4192 | btrfs_info(fs_info, "balance: canceled"); |
| 4193 | else |
| 4194 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4195 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4196 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4197 | |
| 4198 | if (bargs) { |
| 4199 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4200 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4203 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4204 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4205 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4206 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4207 | } |
| 4208 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4209 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4210 | |
| 4211 | return ret; |
| 4212 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4213 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4214 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4215 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4216 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4217 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4218 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4219 | return ret; |
| 4220 | } |
| 4221 | |
| 4222 | static int balance_kthread(void *data) |
| 4223 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4224 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4225 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4226 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4227 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4228 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4229 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4230 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4231 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4232 | return ret; |
| 4233 | } |
| 4234 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4235 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4236 | { |
| 4237 | struct task_struct *tsk; |
| 4238 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4239 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4240 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4241 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4242 | return 0; |
| 4243 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4244 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4245 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4246 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4247 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4248 | return 0; |
| 4249 | } |
| 4250 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4251 | /* |
| 4252 | * A ro->rw remount sequence should continue with the paused balance |
| 4253 | * regardless of who pauses it, system or the user as of now, so set |
| 4254 | * the resume flag. |
| 4255 | */ |
| 4256 | spin_lock(&fs_info->balance_lock); |
| 4257 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4258 | spin_unlock(&fs_info->balance_lock); |
| 4259 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4260 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4261 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4262 | } |
| 4263 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4264 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4265 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4266 | struct btrfs_balance_control *bctl; |
| 4267 | struct btrfs_balance_item *item; |
| 4268 | struct btrfs_disk_balance_args disk_bargs; |
| 4269 | struct btrfs_path *path; |
| 4270 | struct extent_buffer *leaf; |
| 4271 | struct btrfs_key key; |
| 4272 | int ret; |
| 4273 | |
| 4274 | path = btrfs_alloc_path(); |
| 4275 | if (!path) |
| 4276 | return -ENOMEM; |
| 4277 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4278 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4279 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4280 | key.offset = 0; |
| 4281 | |
| 4282 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4283 | if (ret < 0) |
| 4284 | goto out; |
| 4285 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4286 | ret = 0; |
| 4287 | goto out; |
| 4288 | } |
| 4289 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4290 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4291 | if (!bctl) { |
| 4292 | ret = -ENOMEM; |
| 4293 | goto out; |
| 4294 | } |
| 4295 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4296 | leaf = path->nodes[0]; |
| 4297 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4298 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4299 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4300 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4301 | |
| 4302 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4303 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4304 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4305 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4306 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4307 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4308 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4309 | /* |
| 4310 | * This should never happen, as the paused balance state is recovered |
| 4311 | * during mount without any chance of other exclusive ops to collide. |
| 4312 | * |
| 4313 | * This gives the exclusive op status to balance and keeps in paused |
| 4314 | * state until user intervention (cancel or umount). If the ownership |
| 4315 | * cannot be assigned, show a message but do not fail. The balance |
| 4316 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4317 | * set up. |
| 4318 | */ |
| 4319 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4320 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4321 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4322 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4323 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4324 | BUG_ON(fs_info->balance_ctl); |
| 4325 | spin_lock(&fs_info->balance_lock); |
| 4326 | fs_info->balance_ctl = bctl; |
| 4327 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4328 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4329 | out: |
| 4330 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4331 | return ret; |
| 4332 | } |
| 4333 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4334 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4335 | { |
| 4336 | int ret = 0; |
| 4337 | |
| 4338 | mutex_lock(&fs_info->balance_mutex); |
| 4339 | if (!fs_info->balance_ctl) { |
| 4340 | mutex_unlock(&fs_info->balance_mutex); |
| 4341 | return -ENOTCONN; |
| 4342 | } |
| 4343 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4344 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4345 | atomic_inc(&fs_info->balance_pause_req); |
| 4346 | mutex_unlock(&fs_info->balance_mutex); |
| 4347 | |
| 4348 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4349 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4350 | |
| 4351 | mutex_lock(&fs_info->balance_mutex); |
| 4352 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4353 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4354 | atomic_dec(&fs_info->balance_pause_req); |
| 4355 | } else { |
| 4356 | ret = -ENOTCONN; |
| 4357 | } |
| 4358 | |
| 4359 | mutex_unlock(&fs_info->balance_mutex); |
| 4360 | return ret; |
| 4361 | } |
| 4362 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4363 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4364 | { |
| 4365 | mutex_lock(&fs_info->balance_mutex); |
| 4366 | if (!fs_info->balance_ctl) { |
| 4367 | mutex_unlock(&fs_info->balance_mutex); |
| 4368 | return -ENOTCONN; |
| 4369 | } |
| 4370 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4371 | /* |
| 4372 | * A paused balance with the item stored on disk can be resumed at |
| 4373 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4374 | * and we must not allow cancelling as it deletes the item. |
| 4375 | */ |
| 4376 | if (sb_rdonly(fs_info->sb)) { |
| 4377 | mutex_unlock(&fs_info->balance_mutex); |
| 4378 | return -EROFS; |
| 4379 | } |
| 4380 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4381 | atomic_inc(&fs_info->balance_cancel_req); |
| 4382 | /* |
| 4383 | * if we are running just wait and return, balance item is |
| 4384 | * deleted in btrfs_balance in this case |
| 4385 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4386 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4387 | mutex_unlock(&fs_info->balance_mutex); |
| 4388 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4389 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4390 | mutex_lock(&fs_info->balance_mutex); |
| 4391 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4392 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4393 | /* |
| 4394 | * Lock released to allow other waiters to continue, we'll |
| 4395 | * reexamine the status again. |
| 4396 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4397 | mutex_lock(&fs_info->balance_mutex); |
| 4398 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4399 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4400 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4401 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4402 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4403 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4406 | BUG_ON(fs_info->balance_ctl || |
| 4407 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4408 | atomic_dec(&fs_info->balance_cancel_req); |
| 4409 | mutex_unlock(&fs_info->balance_mutex); |
| 4410 | return 0; |
| 4411 | } |
| 4412 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4413 | static int btrfs_uuid_scan_kthread(void *data) |
| 4414 | { |
| 4415 | struct btrfs_fs_info *fs_info = data; |
| 4416 | struct btrfs_root *root = fs_info->tree_root; |
| 4417 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4418 | struct btrfs_path *path = NULL; |
| 4419 | int ret = 0; |
| 4420 | struct extent_buffer *eb; |
| 4421 | int slot; |
| 4422 | struct btrfs_root_item root_item; |
| 4423 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4424 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4425 | |
| 4426 | path = btrfs_alloc_path(); |
| 4427 | if (!path) { |
| 4428 | ret = -ENOMEM; |
| 4429 | goto out; |
| 4430 | } |
| 4431 | |
| 4432 | key.objectid = 0; |
| 4433 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4434 | key.offset = 0; |
| 4435 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4436 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4437 | ret = btrfs_search_forward(root, &key, path, |
| 4438 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4439 | if (ret) { |
| 4440 | if (ret > 0) |
| 4441 | ret = 0; |
| 4442 | break; |
| 4443 | } |
| 4444 | |
| 4445 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4446 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4447 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4448 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4449 | goto skip; |
| 4450 | |
| 4451 | eb = path->nodes[0]; |
| 4452 | slot = path->slots[0]; |
| 4453 | item_size = btrfs_item_size_nr(eb, slot); |
| 4454 | if (item_size < sizeof(root_item)) |
| 4455 | goto skip; |
| 4456 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4457 | read_extent_buffer(eb, &root_item, |
| 4458 | btrfs_item_ptr_offset(eb, slot), |
| 4459 | (int)sizeof(root_item)); |
| 4460 | if (btrfs_root_refs(&root_item) == 0) |
| 4461 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4462 | |
| 4463 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4464 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4465 | if (trans) |
| 4466 | goto update_tree; |
| 4467 | |
| 4468 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4469 | /* |
| 4470 | * 1 - subvol uuid item |
| 4471 | * 1 - received_subvol uuid item |
| 4472 | */ |
| 4473 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4474 | if (IS_ERR(trans)) { |
| 4475 | ret = PTR_ERR(trans); |
| 4476 | break; |
| 4477 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4478 | continue; |
| 4479 | } else { |
| 4480 | goto skip; |
| 4481 | } |
| 4482 | update_tree: |
| 4483 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4484 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4485 | BTRFS_UUID_KEY_SUBVOL, |
| 4486 | key.objectid); |
| 4487 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4488 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4489 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4490 | break; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4495 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4496 | root_item.received_uuid, |
| 4497 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4498 | key.objectid); |
| 4499 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4500 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4501 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4502 | break; |
| 4503 | } |
| 4504 | } |
| 4505 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4506 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4507 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4508 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4509 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4510 | if (ret) |
| 4511 | break; |
| 4512 | } |
| 4513 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4514 | btrfs_release_path(path); |
| 4515 | if (key.offset < (u64)-1) { |
| 4516 | key.offset++; |
| 4517 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4518 | key.offset = 0; |
| 4519 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4520 | } else if (key.objectid < (u64)-1) { |
| 4521 | key.offset = 0; |
| 4522 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4523 | key.objectid++; |
| 4524 | } else { |
| 4525 | break; |
| 4526 | } |
| 4527 | cond_resched(); |
| 4528 | } |
| 4529 | |
| 4530 | out: |
| 4531 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4532 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4533 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4534 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4535 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4536 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4537 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4538 | up(&fs_info->uuid_tree_rescan_sem); |
| 4539 | return 0; |
| 4540 | } |
| 4541 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4542 | /* |
| 4543 | * Callback for btrfs_uuid_tree_iterate(). |
| 4544 | * returns: |
| 4545 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4546 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4547 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4548 | */ |
| 4549 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4550 | u8 *uuid, u8 type, u64 subid) |
| 4551 | { |
| 4552 | struct btrfs_key key; |
| 4553 | int ret = 0; |
| 4554 | struct btrfs_root *subvol_root; |
| 4555 | |
| 4556 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4557 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4558 | goto out; |
| 4559 | |
| 4560 | key.objectid = subid; |
| 4561 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4562 | key.offset = (u64)-1; |
| 4563 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4564 | if (IS_ERR(subvol_root)) { |
| 4565 | ret = PTR_ERR(subvol_root); |
| 4566 | if (ret == -ENOENT) |
| 4567 | ret = 1; |
| 4568 | goto out; |
| 4569 | } |
| 4570 | |
| 4571 | switch (type) { |
| 4572 | case BTRFS_UUID_KEY_SUBVOL: |
| 4573 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4574 | ret = 1; |
| 4575 | break; |
| 4576 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4577 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4578 | BTRFS_UUID_SIZE)) |
| 4579 | ret = 1; |
| 4580 | break; |
| 4581 | } |
| 4582 | |
| 4583 | out: |
| 4584 | return ret; |
| 4585 | } |
| 4586 | |
| 4587 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4588 | { |
| 4589 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4590 | int ret; |
| 4591 | |
| 4592 | /* |
| 4593 | * 1st step is to iterate through the existing UUID tree and |
| 4594 | * to delete all entries that contain outdated data. |
| 4595 | * 2nd step is to add all missing entries to the UUID tree. |
| 4596 | */ |
| 4597 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4598 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4599 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4600 | up(&fs_info->uuid_tree_rescan_sem); |
| 4601 | return ret; |
| 4602 | } |
| 4603 | return btrfs_uuid_scan_kthread(data); |
| 4604 | } |
| 4605 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4606 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4607 | { |
| 4608 | struct btrfs_trans_handle *trans; |
| 4609 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4610 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4611 | struct task_struct *task; |
| 4612 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4613 | |
| 4614 | /* |
| 4615 | * 1 - root node |
| 4616 | * 1 - root item |
| 4617 | */ |
| 4618 | trans = btrfs_start_transaction(tree_root, 2); |
| 4619 | if (IS_ERR(trans)) |
| 4620 | return PTR_ERR(trans); |
| 4621 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4622 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4623 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4624 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4625 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4626 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4627 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | fs_info->uuid_root = uuid_root; |
| 4631 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4632 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4633 | if (ret) |
| 4634 | return ret; |
| 4635 | |
| 4636 | down(&fs_info->uuid_tree_rescan_sem); |
| 4637 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4638 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4639 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4640 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4641 | up(&fs_info->uuid_tree_rescan_sem); |
| 4642 | return PTR_ERR(task); |
| 4643 | } |
| 4644 | |
| 4645 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4646 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4647 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4648 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4649 | { |
| 4650 | struct task_struct *task; |
| 4651 | |
| 4652 | down(&fs_info->uuid_tree_rescan_sem); |
| 4653 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4654 | if (IS_ERR(task)) { |
| 4655 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4656 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4657 | up(&fs_info->uuid_tree_rescan_sem); |
| 4658 | return PTR_ERR(task); |
| 4659 | } |
| 4660 | |
| 4661 | return 0; |
| 4662 | } |
| 4663 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4664 | /* |
| 4665 | * shrinking a device means finding all of the device extents past |
| 4666 | * the new size, and then following the back refs to the chunks. |
| 4667 | * The chunk relocation code actually frees the device extent |
| 4668 | */ |
| 4669 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4670 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4671 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4672 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4673 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4674 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4675 | struct btrfs_path *path; |
| 4676 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4677 | u64 chunk_offset; |
| 4678 | int ret; |
| 4679 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4680 | int failed = 0; |
| 4681 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4682 | struct extent_buffer *l; |
| 4683 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4684 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4685 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4686 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4687 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4688 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4689 | |
| 4690 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4691 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4692 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4693 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4694 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4695 | return -EINVAL; |
| 4696 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4697 | path = btrfs_alloc_path(); |
| 4698 | if (!path) |
| 4699 | return -ENOMEM; |
| 4700 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4701 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4702 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4703 | trans = btrfs_start_transaction(root, 0); |
| 4704 | if (IS_ERR(trans)) { |
| 4705 | btrfs_free_path(path); |
| 4706 | return PTR_ERR(trans); |
| 4707 | } |
| 4708 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4709 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4710 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4711 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4712 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4713 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4714 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4715 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4716 | |
| 4717 | /* |
| 4718 | * Once the device's size has been set to the new size, ensure all |
| 4719 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4720 | * and relocates them accordingly. |
| 4721 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4722 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4723 | mutex_unlock(&fs_info->chunk_mutex); |
| 4724 | ret = btrfs_commit_transaction(trans); |
| 4725 | if (ret) |
| 4726 | goto done; |
| 4727 | } else { |
| 4728 | mutex_unlock(&fs_info->chunk_mutex); |
| 4729 | btrfs_end_transaction(trans); |
| 4730 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4731 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4732 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4733 | key.objectid = device->devid; |
| 4734 | key.offset = (u64)-1; |
| 4735 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4736 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4737 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4738 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4739 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4740 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4741 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4742 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4743 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4744 | |
| 4745 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4746 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4747 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4748 | if (ret < 0) |
| 4749 | goto done; |
| 4750 | if (ret) { |
| 4751 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4752 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4753 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | l = path->nodes[0]; |
| 4757 | slot = path->slots[0]; |
| 4758 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4759 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4760 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4761 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4762 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4763 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4764 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4765 | |
| 4766 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4767 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4768 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4769 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4770 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4771 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4772 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4773 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4774 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4775 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4776 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4777 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4778 | /* |
| 4779 | * We may be relocating the only data chunk we have, |
| 4780 | * which could potentially end up with losing data's |
| 4781 | * raid profile, so lets allocate an empty one in |
| 4782 | * advance. |
| 4783 | */ |
| 4784 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4785 | if (ret < 0) { |
| 4786 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4787 | goto done; |
| 4788 | } |
| 4789 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4790 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4791 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4792 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4793 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4794 | } else if (ret) { |
| 4795 | if (ret == -ETXTBSY) { |
| 4796 | btrfs_warn(fs_info, |
| 4797 | "could not shrink block group %llu due to active swapfile", |
| 4798 | chunk_offset); |
| 4799 | } |
| 4800 | goto done; |
| 4801 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4802 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4803 | |
| 4804 | if (failed && !retried) { |
| 4805 | failed = 0; |
| 4806 | retried = true; |
| 4807 | goto again; |
| 4808 | } else if (failed && retried) { |
| 4809 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4810 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4811 | } |
| 4812 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4813 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4814 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4815 | if (IS_ERR(trans)) { |
| 4816 | ret = PTR_ERR(trans); |
| 4817 | goto done; |
| 4818 | } |
| 4819 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4820 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4821 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4822 | if (list_empty(&device->post_commit_list)) |
| 4823 | list_add_tail(&device->post_commit_list, |
| 4824 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4825 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4826 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4827 | btrfs_set_super_total_bytes(super_copy, |
| 4828 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4829 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4830 | |
| 4831 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4832 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4833 | if (ret < 0) { |
| 4834 | btrfs_abort_transaction(trans, ret); |
| 4835 | btrfs_end_transaction(trans); |
| 4836 | } else { |
| 4837 | ret = btrfs_commit_transaction(trans); |
| 4838 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4839 | done: |
| 4840 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4841 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4842 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4843 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4844 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4845 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4846 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4847 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4848 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4849 | return ret; |
| 4850 | } |
| 4851 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4852 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4853 | struct btrfs_key *key, |
| 4854 | struct btrfs_chunk *chunk, int item_size) |
| 4855 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4856 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4857 | struct btrfs_disk_key disk_key; |
| 4858 | u32 array_size; |
| 4859 | u8 *ptr; |
| 4860 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4861 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4862 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4863 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4864 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4865 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4866 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4867 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4868 | |
| 4869 | ptr = super_copy->sys_chunk_array + array_size; |
| 4870 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4871 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4872 | ptr += sizeof(disk_key); |
| 4873 | memcpy(ptr, chunk, item_size); |
| 4874 | item_size += sizeof(disk_key); |
| 4875 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4876 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4877 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4878 | return 0; |
| 4879 | } |
| 4880 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4881 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4882 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4883 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4884 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4885 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4886 | const struct btrfs_device_info *di_a = a; |
| 4887 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4888 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4889 | if (di_a->max_avail > di_b->max_avail) |
| 4890 | return -1; |
| 4891 | if (di_a->max_avail < di_b->max_avail) |
| 4892 | return 1; |
| 4893 | if (di_a->total_avail > di_b->total_avail) |
| 4894 | return -1; |
| 4895 | if (di_a->total_avail < di_b->total_avail) |
| 4896 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4897 | return 0; |
| 4898 | } |
| 4899 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4900 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4901 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4902 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4903 | return; |
| 4904 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4905 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4906 | } |
| 4907 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4908 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4909 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4910 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4911 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4912 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4913 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4914 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4915 | struct extent_map_tree *em_tree; |
| 4916 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4917 | struct btrfs_device_info *devices_info = NULL; |
| 4918 | u64 total_avail; |
| 4919 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4920 | int data_stripes; /* number of stripes that count for |
| 4921 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4922 | int sub_stripes; /* sub_stripes info for map */ |
| 4923 | int dev_stripes; /* stripes per dev */ |
| 4924 | int devs_max; /* max devs to use */ |
| 4925 | int devs_min; /* min devs needed */ |
| 4926 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4927 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4928 | int nparity; /* number of stripes worth of bytes to |
| 4929 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4930 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4931 | u64 max_stripe_size; |
| 4932 | u64 max_chunk_size; |
| 4933 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4934 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4935 | int ndevs; |
| 4936 | int i; |
| 4937 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4938 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4939 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4940 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4941 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4942 | if (list_empty(&fs_devices->alloc_list)) { |
| 4943 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4944 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4945 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4946 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4947 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4948 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4949 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4950 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4951 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4952 | devs_max = btrfs_raid_array[index].devs_max; |
David Sterba | e3ecdb3 | 2019-05-17 11:43:38 +0200 | [diff] [blame] | 4953 | if (!devs_max) |
| 4954 | devs_max = BTRFS_MAX_DEVS(info); |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4955 | devs_min = btrfs_raid_array[index].devs_min; |
| 4956 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4957 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4958 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4959 | |
| 4960 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4961 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4962 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4963 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4964 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4965 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4966 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4967 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4968 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4969 | max_chunk_size = max_stripe_size; |
| 4970 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4971 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4972 | max_chunk_size = 2 * max_stripe_size; |
| 4973 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4974 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4975 | type); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4976 | BUG(); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4977 | } |
| 4978 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4979 | /* We don't want a chunk larger than 10% of writable space */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4980 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4981 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4982 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4983 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4984 | GFP_NOFS); |
| 4985 | if (!devices_info) |
| 4986 | return -ENOMEM; |
| 4987 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4988 | /* |
| 4989 | * in the first pass through the devices list, we gather information |
| 4990 | * about the available holes on each device. |
| 4991 | */ |
| 4992 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4993 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4994 | u64 max_avail; |
| 4995 | u64 dev_offset; |
| 4996 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4997 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4998 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4999 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5000 | continue; |
| 5001 | } |
| 5002 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 5003 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 5004 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 5005 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5006 | continue; |
| 5007 | |
| 5008 | if (device->total_bytes > device->bytes_used) |
| 5009 | total_avail = device->total_bytes - device->bytes_used; |
| 5010 | else |
| 5011 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 5012 | |
| 5013 | /* If there is no space on this device, skip it. */ |
| 5014 | if (total_avail == 0) |
| 5015 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5016 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 5017 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5018 | max_stripe_size * dev_stripes, |
| 5019 | &dev_offset, &max_avail); |
| 5020 | if (ret && ret != -ENOSPC) |
| 5021 | goto error; |
| 5022 | |
| 5023 | if (ret == 0) |
| 5024 | max_avail = max_stripe_size * dev_stripes; |
| 5025 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5026 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 5027 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 5028 | btrfs_debug(info, |
| 5029 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 5030 | __func__, device->devid, max_avail, |
| 5031 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5032 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5033 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5034 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 5035 | if (ndevs == fs_devices->rw_devices) { |
| 5036 | WARN(1, "%s: found more than %llu devices\n", |
| 5037 | __func__, fs_devices->rw_devices); |
| 5038 | break; |
| 5039 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5040 | devices_info[ndevs].dev_offset = dev_offset; |
| 5041 | devices_info[ndevs].max_avail = max_avail; |
| 5042 | devices_info[ndevs].total_avail = total_avail; |
| 5043 | devices_info[ndevs].dev = device; |
| 5044 | ++ndevs; |
| 5045 | } |
| 5046 | |
| 5047 | /* |
| 5048 | * now sort the devices by hole size / available space |
| 5049 | */ |
| 5050 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 5051 | btrfs_cmp_device_info, NULL); |
| 5052 | |
| 5053 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 5054 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5055 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5056 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5057 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5058 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5059 | btrfs_debug(info, |
| 5060 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5061 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5062 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5063 | goto error; |
| 5064 | } |
| 5065 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 5066 | ndevs = min(ndevs, devs_max); |
| 5067 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5068 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5069 | * The primary goal is to maximize the number of stripes, so use as |
| 5070 | * many devices as possible, even if the stripes are not maximum sized. |
| 5071 | * |
| 5072 | * The DUP profile stores more than one stripe per device, the |
| 5073 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5074 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5075 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5076 | num_stripes = ndevs * dev_stripes; |
| 5077 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5078 | /* |
| 5079 | * this will have to be fixed for RAID1 and RAID10 over |
| 5080 | * more drives |
| 5081 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 5082 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5083 | |
| 5084 | /* |
| 5085 | * Use the number of data stripes to figure out how big this chunk |
| 5086 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5087 | * and compare that answer with the max chunk size. If it's higher, |
| 5088 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5089 | */ |
| 5090 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5091 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5092 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5093 | * then use it, unless it ends up being even bigger than the |
| 5094 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5095 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5096 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 5097 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5098 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5099 | } |
| 5100 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5101 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5102 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5103 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5104 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 5105 | if (!map) { |
| 5106 | ret = -ENOMEM; |
| 5107 | goto error; |
| 5108 | } |
| 5109 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5110 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5111 | for (i = 0; i < ndevs; ++i) { |
| 5112 | for (j = 0; j < dev_stripes; ++j) { |
| 5113 | int s = i * dev_stripes + j; |
| 5114 | map->stripes[s].dev = devices_info[i].dev; |
| 5115 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5116 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5117 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5118 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5119 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5120 | map->io_align = BTRFS_STRIPE_LEN; |
| 5121 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5122 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5123 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5124 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5125 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5126 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5127 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5128 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5129 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5130 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5131 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5132 | ret = -ENOMEM; |
| 5133 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5134 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5135 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5136 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5137 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5138 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5139 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5140 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5141 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5142 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5143 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5144 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5145 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5146 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5147 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5148 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5149 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5150 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5151 | write_unlock(&em_tree->lock); |
| 5152 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5153 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5154 | if (ret) |
| 5155 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5156 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5157 | for (i = 0; i < map->num_stripes; i++) { |
| 5158 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5159 | |
| 5160 | btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size); |
| 5161 | if (list_empty(&dev->post_commit_list)) |
| 5162 | list_add_tail(&dev->post_commit_list, |
| 5163 | &trans->transaction->dev_update_list); |
| 5164 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5165 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 5166 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5167 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5168 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5169 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5170 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5171 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5172 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5173 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5174 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5175 | write_lock(&em_tree->lock); |
| 5176 | remove_extent_mapping(em_tree, em); |
| 5177 | write_unlock(&em_tree->lock); |
| 5178 | |
| 5179 | /* One for our allocation */ |
| 5180 | free_extent_map(em); |
| 5181 | /* One for the tree reference */ |
| 5182 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5183 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5184 | kfree(devices_info); |
| 5185 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5186 | } |
| 5187 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5188 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5189 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5190 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5191 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5192 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5193 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5194 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5195 | struct btrfs_device *device; |
| 5196 | struct btrfs_chunk *chunk; |
| 5197 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5198 | struct extent_map *em; |
| 5199 | struct map_lookup *map; |
| 5200 | size_t item_size; |
| 5201 | u64 dev_offset; |
| 5202 | u64 stripe_size; |
| 5203 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5204 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5205 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5206 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5207 | if (IS_ERR(em)) |
| 5208 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5209 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5210 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5211 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5212 | stripe_size = em->orig_block_len; |
| 5213 | |
| 5214 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5215 | if (!chunk) { |
| 5216 | ret = -ENOMEM; |
| 5217 | goto out; |
| 5218 | } |
| 5219 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5220 | /* |
| 5221 | * Take the device list mutex to prevent races with the final phase of |
| 5222 | * a device replace operation that replaces the device object associated |
| 5223 | * with the map's stripes, because the device object's id can change |
| 5224 | * at any time during that final phase of the device replace operation |
| 5225 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5226 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5227 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5228 | for (i = 0; i < map->num_stripes; i++) { |
| 5229 | device = map->stripes[i].dev; |
| 5230 | dev_offset = map->stripes[i].physical; |
| 5231 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5232 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5233 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5234 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5235 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5236 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5237 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5238 | break; |
| 5239 | } |
| 5240 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5241 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5242 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5243 | } |
| 5244 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5245 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5246 | for (i = 0; i < map->num_stripes; i++) { |
| 5247 | device = map->stripes[i].dev; |
| 5248 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5249 | |
| 5250 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5251 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5252 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5253 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5254 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5255 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5256 | |
| 5257 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5258 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5259 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5260 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5261 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5262 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5263 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5264 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5265 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5266 | |
| 5267 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5268 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5269 | key.offset = chunk_offset; |
| 5270 | |
| 5271 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5272 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5273 | /* |
| 5274 | * TODO: Cleanup of inserted chunk root in case of |
| 5275 | * failure. |
| 5276 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5277 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5278 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5279 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5280 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5281 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5282 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5283 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5287 | * Chunk allocation falls into two parts. The first part does work |
| 5288 | * that makes the new allocated chunk usable, but does not do any operation |
| 5289 | * that modifies the chunk tree. The second part does the work that |
| 5290 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5291 | * bootstrap process of adding storage to a seed btrfs. |
| 5292 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5293 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5294 | { |
| 5295 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5296 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5297 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5298 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5299 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5300 | } |
| 5301 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5302 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5303 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5304 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5305 | u64 chunk_offset; |
| 5306 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5307 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5308 | int ret; |
| 5309 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5310 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5311 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5312 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5313 | if (ret) |
| 5314 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5315 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5316 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5317 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5318 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5319 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5320 | } |
| 5321 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5322 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5323 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5324 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5325 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5326 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5327 | } |
| 5328 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5329 | int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5330 | { |
| 5331 | struct extent_map *em; |
| 5332 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5333 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5334 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5335 | int i; |
| 5336 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5337 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5338 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5339 | return 1; |
| 5340 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5341 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5342 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5343 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5344 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5345 | miss_ndevs++; |
| 5346 | continue; |
| 5347 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5348 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5349 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5350 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5351 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5352 | } |
| 5353 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5354 | |
| 5355 | /* |
| 5356 | * If the number of missing devices is larger than max errors, |
| 5357 | * we can not write the data into that chunk successfully, so |
| 5358 | * set it readonly. |
| 5359 | */ |
| 5360 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5361 | readonly = 1; |
| 5362 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5363 | free_extent_map(em); |
| 5364 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5365 | } |
| 5366 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5367 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5368 | { |
| 5369 | struct extent_map *em; |
| 5370 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5371 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5372 | write_lock(&tree->lock); |
| 5373 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5374 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5375 | remove_extent_mapping(tree, em); |
| 5376 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5377 | if (!em) |
| 5378 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5379 | /* once for us */ |
| 5380 | free_extent_map(em); |
| 5381 | /* once for the tree */ |
| 5382 | free_extent_map(em); |
| 5383 | } |
| 5384 | } |
| 5385 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5386 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5387 | { |
| 5388 | struct extent_map *em; |
| 5389 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5390 | int ret; |
| 5391 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5392 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5393 | if (IS_ERR(em)) |
| 5394 | /* |
| 5395 | * We could return errors for these cases, but that could get |
| 5396 | * ugly and we'd probably do the same thing which is just not do |
| 5397 | * anything else and exit, so return 1 so the callers don't try |
| 5398 | * to use other copies. |
| 5399 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5400 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5401 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5402 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5403 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5404 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5405 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5406 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5407 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5408 | ret = 2; |
| 5409 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5410 | /* |
| 5411 | * There could be two corrupted data stripes, we need |
| 5412 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5413 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5414 | * Fail a stripe at a time on every retry except the |
| 5415 | * stripe under reconstruction. |
| 5416 | */ |
| 5417 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5418 | else |
| 5419 | ret = 1; |
| 5420 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5421 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5422 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5423 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5424 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5425 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5426 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5427 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5428 | return ret; |
| 5429 | } |
| 5430 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5431 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5432 | u64 logical) |
| 5433 | { |
| 5434 | struct extent_map *em; |
| 5435 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5436 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5437 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5438 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5439 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5440 | if (!WARN_ON(IS_ERR(em))) { |
| 5441 | map = em->map_lookup; |
| 5442 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5443 | len = map->stripe_len * nr_data_stripes(map); |
| 5444 | free_extent_map(em); |
| 5445 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5446 | return len; |
| 5447 | } |
| 5448 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5449 | int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5450 | { |
| 5451 | struct extent_map *em; |
| 5452 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5453 | int ret = 0; |
| 5454 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5455 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5456 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5457 | if(!WARN_ON(IS_ERR(em))) { |
| 5458 | map = em->map_lookup; |
| 5459 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5460 | ret = 1; |
| 5461 | free_extent_map(em); |
| 5462 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5463 | return ret; |
| 5464 | } |
| 5465 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5466 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5467 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5468 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5469 | { |
| 5470 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5471 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5472 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5473 | int tolerance; |
| 5474 | struct btrfs_device *srcdev; |
| 5475 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5476 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5477 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5478 | |
| 5479 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5480 | num_stripes = map->sub_stripes; |
| 5481 | else |
| 5482 | num_stripes = map->num_stripes; |
| 5483 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5484 | preferred_mirror = first + current->pid % num_stripes; |
| 5485 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5486 | if (dev_replace_is_ongoing && |
| 5487 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5488 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5489 | srcdev = fs_info->dev_replace.srcdev; |
| 5490 | else |
| 5491 | srcdev = NULL; |
| 5492 | |
| 5493 | /* |
| 5494 | * try to avoid the drive that is the source drive for a |
| 5495 | * dev-replace procedure, only choose it if no other non-missing |
| 5496 | * mirror is available |
| 5497 | */ |
| 5498 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5499 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5500 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5501 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5502 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5503 | if (map->stripes[i].dev->bdev && |
| 5504 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5505 | return i; |
| 5506 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5507 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5508 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5509 | /* we couldn't find one that doesn't fail. Just return something |
| 5510 | * and the io error handling code will clean up eventually |
| 5511 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5512 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5513 | } |
| 5514 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5515 | static inline int parity_smaller(u64 a, u64 b) |
| 5516 | { |
| 5517 | return a > b; |
| 5518 | } |
| 5519 | |
| 5520 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5521 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5522 | { |
| 5523 | struct btrfs_bio_stripe s; |
| 5524 | int i; |
| 5525 | u64 l; |
| 5526 | int again = 1; |
| 5527 | |
| 5528 | while (again) { |
| 5529 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5530 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5531 | if (parity_smaller(bbio->raid_map[i], |
| 5532 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5533 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5534 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5535 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5536 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5537 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5538 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5539 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5540 | again = 1; |
| 5541 | } |
| 5542 | } |
| 5543 | } |
| 5544 | } |
| 5545 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5546 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5547 | { |
| 5548 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5549 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5550 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5551 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5552 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5553 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5554 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5555 | /* |
| 5556 | * plus the raid_map, which includes both the tgt dev |
| 5557 | * and the stripes |
| 5558 | */ |
| 5559 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5560 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5561 | |
| 5562 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5563 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5564 | |
| 5565 | return bbio; |
| 5566 | } |
| 5567 | |
| 5568 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5569 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5570 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5571 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5575 | { |
| 5576 | if (!bbio) |
| 5577 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5578 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5579 | kfree(bbio); |
| 5580 | } |
| 5581 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5582 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5583 | /* |
| 5584 | * Please note that, discard won't be sent to target device of device |
| 5585 | * replace. |
| 5586 | */ |
| 5587 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5588 | u64 logical, u64 length, |
| 5589 | struct btrfs_bio **bbio_ret) |
| 5590 | { |
| 5591 | struct extent_map *em; |
| 5592 | struct map_lookup *map; |
| 5593 | struct btrfs_bio *bbio; |
| 5594 | u64 offset; |
| 5595 | u64 stripe_nr; |
| 5596 | u64 stripe_nr_end; |
| 5597 | u64 stripe_end_offset; |
| 5598 | u64 stripe_cnt; |
| 5599 | u64 stripe_len; |
| 5600 | u64 stripe_offset; |
| 5601 | u64 num_stripes; |
| 5602 | u32 stripe_index; |
| 5603 | u32 factor = 0; |
| 5604 | u32 sub_stripes = 0; |
| 5605 | u64 stripes_per_dev = 0; |
| 5606 | u32 remaining_stripes = 0; |
| 5607 | u32 last_stripe = 0; |
| 5608 | int ret = 0; |
| 5609 | int i; |
| 5610 | |
| 5611 | /* discard always return a bbio */ |
| 5612 | ASSERT(bbio_ret); |
| 5613 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5614 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5615 | if (IS_ERR(em)) |
| 5616 | return PTR_ERR(em); |
| 5617 | |
| 5618 | map = em->map_lookup; |
| 5619 | /* we don't discard raid56 yet */ |
| 5620 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5621 | ret = -EOPNOTSUPP; |
| 5622 | goto out; |
| 5623 | } |
| 5624 | |
| 5625 | offset = logical - em->start; |
| 5626 | length = min_t(u64, em->len - offset, length); |
| 5627 | |
| 5628 | stripe_len = map->stripe_len; |
| 5629 | /* |
| 5630 | * stripe_nr counts the total number of stripes we have to stride |
| 5631 | * to get to this block |
| 5632 | */ |
| 5633 | stripe_nr = div64_u64(offset, stripe_len); |
| 5634 | |
| 5635 | /* stripe_offset is the offset of this block in its stripe */ |
| 5636 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5637 | |
| 5638 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5639 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5640 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5641 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5642 | (offset + length); |
| 5643 | /* |
| 5644 | * after this, stripe_nr is the number of stripes on this |
| 5645 | * device we have to walk to find the data, and stripe_index is |
| 5646 | * the number of our device in the stripe array |
| 5647 | */ |
| 5648 | num_stripes = 1; |
| 5649 | stripe_index = 0; |
| 5650 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5651 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5652 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5653 | sub_stripes = 1; |
| 5654 | else |
| 5655 | sub_stripes = map->sub_stripes; |
| 5656 | |
| 5657 | factor = map->num_stripes / sub_stripes; |
| 5658 | num_stripes = min_t(u64, map->num_stripes, |
| 5659 | sub_stripes * stripe_cnt); |
| 5660 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5661 | stripe_index *= sub_stripes; |
| 5662 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5663 | &remaining_stripes); |
| 5664 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5665 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5666 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5667 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5668 | num_stripes = map->num_stripes; |
| 5669 | } else { |
| 5670 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5671 | &stripe_index); |
| 5672 | } |
| 5673 | |
| 5674 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5675 | if (!bbio) { |
| 5676 | ret = -ENOMEM; |
| 5677 | goto out; |
| 5678 | } |
| 5679 | |
| 5680 | for (i = 0; i < num_stripes; i++) { |
| 5681 | bbio->stripes[i].physical = |
| 5682 | map->stripes[stripe_index].physical + |
| 5683 | stripe_offset + stripe_nr * map->stripe_len; |
| 5684 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5685 | |
| 5686 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5687 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5688 | bbio->stripes[i].length = stripes_per_dev * |
| 5689 | map->stripe_len; |
| 5690 | |
| 5691 | if (i / sub_stripes < remaining_stripes) |
| 5692 | bbio->stripes[i].length += |
| 5693 | map->stripe_len; |
| 5694 | |
| 5695 | /* |
| 5696 | * Special for the first stripe and |
| 5697 | * the last stripe: |
| 5698 | * |
| 5699 | * |-------|...|-------| |
| 5700 | * |----------| |
| 5701 | * off end_off |
| 5702 | */ |
| 5703 | if (i < sub_stripes) |
| 5704 | bbio->stripes[i].length -= |
| 5705 | stripe_offset; |
| 5706 | |
| 5707 | if (stripe_index >= last_stripe && |
| 5708 | stripe_index <= (last_stripe + |
| 5709 | sub_stripes - 1)) |
| 5710 | bbio->stripes[i].length -= |
| 5711 | stripe_end_offset; |
| 5712 | |
| 5713 | if (i == sub_stripes - 1) |
| 5714 | stripe_offset = 0; |
| 5715 | } else { |
| 5716 | bbio->stripes[i].length = length; |
| 5717 | } |
| 5718 | |
| 5719 | stripe_index++; |
| 5720 | if (stripe_index == map->num_stripes) { |
| 5721 | stripe_index = 0; |
| 5722 | stripe_nr++; |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | *bbio_ret = bbio; |
| 5727 | bbio->map_type = map->type; |
| 5728 | bbio->num_stripes = num_stripes; |
| 5729 | out: |
| 5730 | free_extent_map(em); |
| 5731 | return ret; |
| 5732 | } |
| 5733 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5734 | /* |
| 5735 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5736 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5737 | * left cursor can also be read from the target drive. |
| 5738 | * |
| 5739 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5740 | * array of stripes. |
| 5741 | * For READ, it also needs to be supported using the same mirror number. |
| 5742 | * |
| 5743 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5744 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5745 | * case. |
| 5746 | */ |
| 5747 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5748 | u64 logical, u64 length, |
| 5749 | u64 srcdev_devid, int *mirror_num, |
| 5750 | u64 *physical) |
| 5751 | { |
| 5752 | struct btrfs_bio *bbio = NULL; |
| 5753 | int num_stripes; |
| 5754 | int index_srcdev = 0; |
| 5755 | int found = 0; |
| 5756 | u64 physical_of_found = 0; |
| 5757 | int i; |
| 5758 | int ret = 0; |
| 5759 | |
| 5760 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5761 | logical, &length, &bbio, 0, 0); |
| 5762 | if (ret) { |
| 5763 | ASSERT(bbio == NULL); |
| 5764 | return ret; |
| 5765 | } |
| 5766 | |
| 5767 | num_stripes = bbio->num_stripes; |
| 5768 | if (*mirror_num > num_stripes) { |
| 5769 | /* |
| 5770 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5771 | * that means that the requested area is not left of the left |
| 5772 | * cursor |
| 5773 | */ |
| 5774 | btrfs_put_bbio(bbio); |
| 5775 | return -EIO; |
| 5776 | } |
| 5777 | |
| 5778 | /* |
| 5779 | * process the rest of the function using the mirror_num of the source |
| 5780 | * drive. Therefore look it up first. At the end, patch the device |
| 5781 | * pointer to the one of the target drive. |
| 5782 | */ |
| 5783 | for (i = 0; i < num_stripes; i++) { |
| 5784 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5785 | continue; |
| 5786 | |
| 5787 | /* |
| 5788 | * In case of DUP, in order to keep it simple, only add the |
| 5789 | * mirror with the lowest physical address |
| 5790 | */ |
| 5791 | if (found && |
| 5792 | physical_of_found <= bbio->stripes[i].physical) |
| 5793 | continue; |
| 5794 | |
| 5795 | index_srcdev = i; |
| 5796 | found = 1; |
| 5797 | physical_of_found = bbio->stripes[i].physical; |
| 5798 | } |
| 5799 | |
| 5800 | btrfs_put_bbio(bbio); |
| 5801 | |
| 5802 | ASSERT(found); |
| 5803 | if (!found) |
| 5804 | return -EIO; |
| 5805 | |
| 5806 | *mirror_num = index_srcdev + 1; |
| 5807 | *physical = physical_of_found; |
| 5808 | return ret; |
| 5809 | } |
| 5810 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5811 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5812 | struct btrfs_bio **bbio_ret, |
| 5813 | struct btrfs_dev_replace *dev_replace, |
| 5814 | int *num_stripes_ret, int *max_errors_ret) |
| 5815 | { |
| 5816 | struct btrfs_bio *bbio = *bbio_ret; |
| 5817 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5818 | int tgtdev_indexes = 0; |
| 5819 | int num_stripes = *num_stripes_ret; |
| 5820 | int max_errors = *max_errors_ret; |
| 5821 | int i; |
| 5822 | |
| 5823 | if (op == BTRFS_MAP_WRITE) { |
| 5824 | int index_where_to_add; |
| 5825 | |
| 5826 | /* |
| 5827 | * duplicate the write operations while the dev replace |
| 5828 | * procedure is running. Since the copying of the old disk to |
| 5829 | * the new disk takes place at run time while the filesystem is |
| 5830 | * mounted writable, the regular write operations to the old |
| 5831 | * disk have to be duplicated to go to the new disk as well. |
| 5832 | * |
| 5833 | * Note that device->missing is handled by the caller, and that |
| 5834 | * the write to the old disk is already set up in the stripes |
| 5835 | * array. |
| 5836 | */ |
| 5837 | index_where_to_add = num_stripes; |
| 5838 | for (i = 0; i < num_stripes; i++) { |
| 5839 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5840 | /* write to new disk, too */ |
| 5841 | struct btrfs_bio_stripe *new = |
| 5842 | bbio->stripes + index_where_to_add; |
| 5843 | struct btrfs_bio_stripe *old = |
| 5844 | bbio->stripes + i; |
| 5845 | |
| 5846 | new->physical = old->physical; |
| 5847 | new->length = old->length; |
| 5848 | new->dev = dev_replace->tgtdev; |
| 5849 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5850 | index_where_to_add++; |
| 5851 | max_errors++; |
| 5852 | tgtdev_indexes++; |
| 5853 | } |
| 5854 | } |
| 5855 | num_stripes = index_where_to_add; |
| 5856 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5857 | int index_srcdev = 0; |
| 5858 | int found = 0; |
| 5859 | u64 physical_of_found = 0; |
| 5860 | |
| 5861 | /* |
| 5862 | * During the dev-replace procedure, the target drive can also |
| 5863 | * be used to read data in case it is needed to repair a corrupt |
| 5864 | * block elsewhere. This is possible if the requested area is |
| 5865 | * left of the left cursor. In this area, the target drive is a |
| 5866 | * full copy of the source drive. |
| 5867 | */ |
| 5868 | for (i = 0; i < num_stripes; i++) { |
| 5869 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5870 | /* |
| 5871 | * In case of DUP, in order to keep it simple, |
| 5872 | * only add the mirror with the lowest physical |
| 5873 | * address |
| 5874 | */ |
| 5875 | if (found && |
| 5876 | physical_of_found <= |
| 5877 | bbio->stripes[i].physical) |
| 5878 | continue; |
| 5879 | index_srcdev = i; |
| 5880 | found = 1; |
| 5881 | physical_of_found = bbio->stripes[i].physical; |
| 5882 | } |
| 5883 | } |
| 5884 | if (found) { |
| 5885 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5886 | bbio->stripes + num_stripes; |
| 5887 | |
| 5888 | tgtdev_stripe->physical = physical_of_found; |
| 5889 | tgtdev_stripe->length = |
| 5890 | bbio->stripes[index_srcdev].length; |
| 5891 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5892 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5893 | |
| 5894 | tgtdev_indexes++; |
| 5895 | num_stripes++; |
| 5896 | } |
| 5897 | } |
| 5898 | |
| 5899 | *num_stripes_ret = num_stripes; |
| 5900 | *max_errors_ret = max_errors; |
| 5901 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5902 | *bbio_ret = bbio; |
| 5903 | } |
| 5904 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5905 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5906 | { |
| 5907 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5908 | } |
| 5909 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5910 | /* |
| 5911 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5912 | * tuple. This information is used to calculate how big a |
| 5913 | * particular bio can get before it straddles a stripe. |
| 5914 | * |
| 5915 | * @fs_info - the filesystem |
| 5916 | * @logical - address that we want to figure out the geometry of |
| 5917 | * @len - the length of IO we are going to perform, starting at @logical |
| 5918 | * @op - type of operation - write or read |
| 5919 | * @io_geom - pointer used to return values |
| 5920 | * |
| 5921 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5922 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5923 | */ |
| 5924 | int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5925 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5926 | { |
| 5927 | struct extent_map *em; |
| 5928 | struct map_lookup *map; |
| 5929 | u64 offset; |
| 5930 | u64 stripe_offset; |
| 5931 | u64 stripe_nr; |
| 5932 | u64 stripe_len; |
| 5933 | u64 raid56_full_stripe_start = (u64)-1; |
| 5934 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5935 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5936 | |
| 5937 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5938 | |
| 5939 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5940 | if (IS_ERR(em)) |
| 5941 | return PTR_ERR(em); |
| 5942 | |
| 5943 | map = em->map_lookup; |
| 5944 | /* Offset of this logical address in the chunk */ |
| 5945 | offset = logical - em->start; |
| 5946 | /* Len of a stripe in a chunk */ |
| 5947 | stripe_len = map->stripe_len; |
| 5948 | /* Stripe wher this block falls in */ |
| 5949 | stripe_nr = div64_u64(offset, stripe_len); |
| 5950 | /* Offset of stripe in the chunk */ |
| 5951 | stripe_offset = stripe_nr * stripe_len; |
| 5952 | if (offset < stripe_offset) { |
| 5953 | btrfs_crit(fs_info, |
| 5954 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5955 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5956 | ret = -EINVAL; |
| 5957 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | /* stripe_offset is the offset of this block in its stripe */ |
| 5961 | stripe_offset = offset - stripe_offset; |
| 5962 | data_stripes = nr_data_stripes(map); |
| 5963 | |
| 5964 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5965 | u64 max_len = stripe_len - stripe_offset; |
| 5966 | |
| 5967 | /* |
| 5968 | * In case of raid56, we need to know the stripe aligned start |
| 5969 | */ |
| 5970 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5971 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5972 | raid56_full_stripe_start = offset; |
| 5973 | |
| 5974 | /* |
| 5975 | * Allow a write of a full stripe, but make sure we |
| 5976 | * don't allow straddling of stripes |
| 5977 | */ |
| 5978 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5979 | full_stripe_len); |
| 5980 | raid56_full_stripe_start *= full_stripe_len; |
| 5981 | |
| 5982 | /* |
| 5983 | * For writes to RAID[56], allow a full stripeset across |
| 5984 | * all disks. For other RAID types and for RAID[56] |
| 5985 | * reads, just allow a single stripe (on a single disk). |
| 5986 | */ |
| 5987 | if (op == BTRFS_MAP_WRITE) { |
| 5988 | max_len = stripe_len * data_stripes - |
| 5989 | (offset - raid56_full_stripe_start); |
| 5990 | } |
| 5991 | } |
| 5992 | len = min_t(u64, em->len - offset, max_len); |
| 5993 | } else { |
| 5994 | len = em->len - offset; |
| 5995 | } |
| 5996 | |
| 5997 | io_geom->len = len; |
| 5998 | io_geom->offset = offset; |
| 5999 | io_geom->stripe_len = stripe_len; |
| 6000 | io_geom->stripe_nr = stripe_nr; |
| 6001 | io_geom->stripe_offset = stripe_offset; |
| 6002 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 6003 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 6004 | out: |
| 6005 | /* once for us */ |
| 6006 | free_extent_map(em); |
| 6007 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 6008 | } |
| 6009 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6010 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 6011 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6012 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6013 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6014 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6015 | { |
| 6016 | struct extent_map *em; |
| 6017 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6018 | u64 stripe_offset; |
| 6019 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6020 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6021 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6022 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6023 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6024 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6025 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6026 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6027 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6028 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6029 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 6030 | int dev_replace_is_ongoing = 0; |
| 6031 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6032 | int patch_the_first_stripe_for_dev_replace = 0; |
| 6033 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6034 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6035 | struct btrfs_io_geometry geom; |
| 6036 | |
| 6037 | ASSERT(bbio_ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6038 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6039 | if (op == BTRFS_MAP_DISCARD) |
| 6040 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 6041 | *length, bbio_ret); |
| 6042 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6043 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 6044 | if (ret < 0) |
| 6045 | return ret; |
| 6046 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6047 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6048 | ASSERT(em); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6049 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6050 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6051 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6052 | stripe_len = geom.stripe_len; |
| 6053 | stripe_nr = geom.stripe_nr; |
| 6054 | stripe_offset = geom.stripe_offset; |
| 6055 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6056 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6057 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6058 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6059 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6060 | /* |
| 6061 | * Hold the semaphore for read during the whole operation, write is |
| 6062 | * requested at commit time but must wait. |
| 6063 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6064 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6065 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6066 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6067 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6068 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6069 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 6070 | dev_replace->srcdev->devid, |
| 6071 | &mirror_num, |
| 6072 | &physical_to_patch_in_first_stripe); |
| 6073 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6074 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6075 | else |
| 6076 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6077 | } else if (mirror_num > map->num_stripes) { |
| 6078 | mirror_num = 0; |
| 6079 | } |
| 6080 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6081 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6082 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6083 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6084 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6085 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6086 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6087 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 6088 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6089 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6090 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6091 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6092 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6093 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6094 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6095 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6096 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6097 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6098 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6099 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6100 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6101 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6102 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6103 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6104 | } else { |
| 6105 | mirror_num = 1; |
| 6106 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6107 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6108 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6109 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6110 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6111 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6112 | stripe_index *= map->sub_stripes; |
| 6113 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6114 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6115 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6116 | else if (mirror_num) |
| 6117 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6118 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6119 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6120 | stripe_index = find_live_mirror(fs_info, map, |
| 6121 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6122 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6123 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6124 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6125 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6126 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6127 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6128 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6129 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6130 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6131 | |
| 6132 | /* RAID[56] write or recovery. Return all stripes */ |
| 6133 | num_stripes = map->num_stripes; |
| 6134 | max_errors = nr_parity_stripes(map); |
| 6135 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6136 | *length = map->stripe_len; |
| 6137 | stripe_index = 0; |
| 6138 | stripe_offset = 0; |
| 6139 | } else { |
| 6140 | /* |
| 6141 | * Mirror #0 or #1 means the original data block. |
| 6142 | * Mirror #2 is RAID5 parity block. |
| 6143 | * Mirror #3 is RAID6 Q block. |
| 6144 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6145 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6146 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6147 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6148 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6149 | |
| 6150 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6151 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6152 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6153 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6154 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6155 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6156 | } else { |
| 6157 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6158 | * after this, stripe_nr is the number of stripes on this |
| 6159 | * device we have to walk to find the data, and stripe_index is |
| 6160 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6161 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6162 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6163 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6164 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6165 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6166 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6167 | btrfs_crit(fs_info, |
| 6168 | "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u", |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6169 | stripe_index, map->num_stripes); |
| 6170 | ret = -EINVAL; |
| 6171 | goto out; |
| 6172 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6173 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6174 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6175 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6176 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6177 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6178 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6179 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6180 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6181 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6182 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6183 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6184 | if (!bbio) { |
| 6185 | ret = -ENOMEM; |
| 6186 | goto out; |
| 6187 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6188 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6189 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6190 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6191 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6192 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6193 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6194 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6195 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6196 | |
| 6197 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6198 | sizeof(struct btrfs_bio_stripe) * |
| 6199 | num_alloc_stripes + |
| 6200 | sizeof(int) * tgtdev_indexes); |
| 6201 | |
| 6202 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6203 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6204 | |
| 6205 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6206 | tmp = stripe_nr * data_stripes; |
| 6207 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6208 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6209 | em->start + (tmp + i) * map->stripe_len; |
| 6210 | |
| 6211 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6212 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6213 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6214 | RAID6_Q_STRIPE; |
| 6215 | } |
| 6216 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6217 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6218 | for (i = 0; i < num_stripes; i++) { |
| 6219 | bbio->stripes[i].physical = |
| 6220 | map->stripes[stripe_index].physical + |
| 6221 | stripe_offset + |
| 6222 | stripe_nr * map->stripe_len; |
| 6223 | bbio->stripes[i].dev = |
| 6224 | map->stripes[stripe_index].dev; |
| 6225 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6226 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6227 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6228 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6229 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6230 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6231 | if (bbio->raid_map) |
| 6232 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6233 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6234 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6235 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6236 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6237 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6238 | } |
| 6239 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6240 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6241 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6242 | bbio->num_stripes = num_stripes; |
| 6243 | bbio->max_errors = max_errors; |
| 6244 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6245 | |
| 6246 | /* |
| 6247 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6248 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6249 | * available as a mirror |
| 6250 | */ |
| 6251 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6252 | WARN_ON(num_stripes > 1); |
| 6253 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6254 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6255 | bbio->mirror_num = map->num_stripes + 1; |
| 6256 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6257 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6258 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6259 | lockdep_assert_held(&dev_replace->rwsem); |
| 6260 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6261 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6262 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6263 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6264 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6265 | } |
| 6266 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6267 | int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6268 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6269 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6270 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6271 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6272 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6273 | } |
| 6274 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6275 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6276 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6277 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6278 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6279 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6280 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6281 | } |
| 6282 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6283 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6284 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6285 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6286 | struct extent_map *em; |
| 6287 | struct map_lookup *map; |
| 6288 | u64 *buf; |
| 6289 | u64 bytenr; |
| 6290 | u64 length; |
| 6291 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6292 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6293 | int i, j, nr = 0; |
| 6294 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6295 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6296 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6297 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6298 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6299 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6300 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6301 | rmap_len = map->stripe_len; |
| 6302 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6303 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6304 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6305 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6306 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6307 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6308 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6309 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6310 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6311 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6312 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6313 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6314 | |
| 6315 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6316 | if (map->stripes[i].physical > physical || |
| 6317 | map->stripes[i].physical + length <= physical) |
| 6318 | continue; |
| 6319 | |
| 6320 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6321 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6322 | |
| 6323 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6324 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6325 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6326 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6327 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6328 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6329 | * Alternatively, just use rmap_len below instead of |
| 6330 | * map->stripe_len */ |
| 6331 | |
| 6332 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6333 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6334 | for (j = 0; j < nr; j++) { |
| 6335 | if (buf[j] == bytenr) |
| 6336 | break; |
| 6337 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6338 | if (j == nr) { |
| 6339 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6340 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6341 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6342 | } |
| 6343 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6344 | *logical = buf; |
| 6345 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6346 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6347 | |
| 6348 | free_extent_map(em); |
| 6349 | return 0; |
| 6350 | } |
| 6351 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6352 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio) |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6353 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6354 | bio->bi_private = bbio->private; |
| 6355 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6356 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6357 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6358 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6359 | } |
| 6360 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6361 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6362 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6363 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6364 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6365 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6366 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6367 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6368 | if (bio->bi_status == BLK_STS_IOERR || |
| 6369 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6370 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6371 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6372 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6373 | |
| 6374 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6375 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6376 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6377 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6378 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6379 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6380 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6381 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6382 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6383 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6384 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6385 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6386 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6387 | } |
| 6388 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6389 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6390 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6391 | is_orig_bio = 1; |
| 6392 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6393 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6394 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6395 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6396 | if (!is_orig_bio) { |
| 6397 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6398 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6399 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6400 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6401 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6402 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6403 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6404 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6405 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6406 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6407 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6408 | /* |
| 6409 | * this bio is actually up to date, we didn't |
| 6410 | * go over the max number of errors |
| 6411 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6412 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6413 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6414 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6415 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6416 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6417 | bio_put(bio); |
| 6418 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6419 | } |
| 6420 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6421 | /* |
| 6422 | * see run_scheduled_bios for a description of why bios are collected for |
| 6423 | * async submit. |
| 6424 | * |
| 6425 | * This will add one bio to the pending list for a device and make sure |
| 6426 | * the work struct is scheduled. |
| 6427 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6428 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6429 | struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6430 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6431 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6432 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6433 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6434 | |
| 6435 | /* don't bother with additional async steps for reads, right now */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6436 | if (bio_op(bio) == REQ_OP_READ) { |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6437 | btrfsic_submit_bio(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6438 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6439 | } |
| 6440 | |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 6441 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6442 | bio->bi_next = NULL; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6443 | |
| 6444 | spin_lock(&device->io_lock); |
Christoph Hellwig | 67f055c | 2016-11-01 07:40:06 -0600 | [diff] [blame] | 6445 | if (op_is_sync(bio->bi_opf)) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6446 | pending_bios = &device->pending_sync_bios; |
| 6447 | else |
| 6448 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6449 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6450 | if (pending_bios->tail) |
| 6451 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6452 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6453 | pending_bios->tail = bio; |
| 6454 | if (!pending_bios->head) |
| 6455 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6456 | if (device->running_pending) |
| 6457 | should_queue = 0; |
| 6458 | |
| 6459 | spin_unlock(&device->io_lock); |
| 6460 | |
| 6461 | if (should_queue) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6462 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6463 | } |
| 6464 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6465 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
| 6466 | u64 physical, int dev_nr, int async) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6467 | { |
| 6468 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6469 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6470 | |
| 6471 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6472 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6473 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6474 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6475 | btrfs_debug_in_rcu(fs_info, |
| 6476 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6477 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6478 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6479 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6480 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6481 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6482 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6483 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6484 | if (async) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6485 | btrfs_schedule_bio(dev, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6486 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6487 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6488 | } |
| 6489 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6490 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6491 | { |
| 6492 | atomic_inc(&bbio->error); |
| 6493 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6494 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6495 | WARN_ON(bio != bbio->orig_bio); |
| 6496 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6497 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6498 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6499 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6500 | bio->bi_status = BLK_STS_IOERR; |
| 6501 | else |
| 6502 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6503 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6504 | } |
| 6505 | } |
| 6506 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6507 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
| 6508 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6509 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6510 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6511 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6512 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6513 | u64 length = 0; |
| 6514 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6515 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6516 | int dev_nr; |
| 6517 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6518 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6519 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6520 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6521 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6522 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6523 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6524 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6525 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6526 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6527 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6528 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6529 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6530 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6531 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6532 | bbio->orig_bio = first_bio; |
| 6533 | bbio->private = first_bio->bi_private; |
| 6534 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6535 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6536 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6537 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6538 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6539 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6540 | /* In this case, map_length has been set to the length of |
| 6541 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6542 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6543 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6544 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6545 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6546 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6547 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6548 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6549 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6550 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6551 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6552 | } |
| 6553 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6554 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6555 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6556 | "mapping failed logical %llu bio len %llu len %llu", |
| 6557 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6558 | BUG(); |
| 6559 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6560 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6561 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6562 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6563 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6564 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6565 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6566 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6567 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6568 | continue; |
| 6569 | } |
| 6570 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6571 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6572 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6573 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6574 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6575 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6576 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
| 6577 | dev_nr, async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6578 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6579 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6580 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6581 | } |
| 6582 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6583 | /* |
| 6584 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6585 | * return NULL. |
| 6586 | * |
| 6587 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6588 | * only devid is used. |
| 6589 | * |
| 6590 | * If @seed is true, traverse through the seed devices. |
| 6591 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6592 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6593 | u64 devid, u8 *uuid, u8 *fsid, |
| 6594 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6595 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6596 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6597 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6598 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6599 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6600 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6601 | list_for_each_entry(device, &fs_devices->devices, |
| 6602 | dev_list) { |
| 6603 | if (device->devid == devid && |
| 6604 | (!uuid || memcmp(device->uuid, uuid, |
| 6605 | BTRFS_UUID_SIZE) == 0)) |
| 6606 | return device; |
| 6607 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6608 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6609 | if (seed) |
| 6610 | fs_devices = fs_devices->seed; |
| 6611 | else |
| 6612 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6613 | } |
| 6614 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6615 | } |
| 6616 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6617 | static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6618 | u64 devid, u8 *dev_uuid) |
| 6619 | { |
| 6620 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6621 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6622 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6623 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6624 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6625 | |
| 6626 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6627 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6628 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6629 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6630 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6631 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6632 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6633 | return device; |
| 6634 | } |
| 6635 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6636 | /** |
| 6637 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6638 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6639 | * devid is provided (i.e. @devid != NULL). |
| 6640 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6641 | * is generated. |
| 6642 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6643 | * is generated. |
| 6644 | * |
| 6645 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 6646 | * on error. Returned struct is not linked onto any lists and must be |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6647 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6648 | */ |
| 6649 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6650 | const u64 *devid, |
| 6651 | const u8 *uuid) |
| 6652 | { |
| 6653 | struct btrfs_device *dev; |
| 6654 | u64 tmp; |
| 6655 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6656 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6657 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6658 | |
| 6659 | dev = __alloc_device(); |
| 6660 | if (IS_ERR(dev)) |
| 6661 | return dev; |
| 6662 | |
| 6663 | if (devid) |
| 6664 | tmp = *devid; |
| 6665 | else { |
| 6666 | int ret; |
| 6667 | |
| 6668 | ret = find_next_devid(fs_info, &tmp); |
| 6669 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6670 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6671 | return ERR_PTR(ret); |
| 6672 | } |
| 6673 | } |
| 6674 | dev->devid = tmp; |
| 6675 | |
| 6676 | if (uuid) |
| 6677 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6678 | else |
| 6679 | generate_random_uuid(dev->uuid); |
| 6680 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6681 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6682 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6683 | |
| 6684 | return dev; |
| 6685 | } |
| 6686 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6687 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6688 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6689 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6690 | if (error) |
| 6691 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6692 | devid, uuid); |
| 6693 | else |
| 6694 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6695 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6696 | } |
| 6697 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6698 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6699 | { |
| 6700 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6701 | int ncopies = btrfs_raid_array[index].ncopies; |
| 6702 | int data_stripes; |
| 6703 | |
| 6704 | switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 6705 | case BTRFS_BLOCK_GROUP_RAID5: |
| 6706 | data_stripes = num_stripes - 1; |
| 6707 | break; |
| 6708 | case BTRFS_BLOCK_GROUP_RAID6: |
| 6709 | data_stripes = num_stripes - 2; |
| 6710 | break; |
| 6711 | default: |
| 6712 | data_stripes = num_stripes / ncopies; |
| 6713 | break; |
| 6714 | } |
| 6715 | return div_u64(chunk_len, data_stripes); |
| 6716 | } |
| 6717 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6718 | static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6719 | struct btrfs_chunk *chunk) |
| 6720 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6721 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6722 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6723 | struct map_lookup *map; |
| 6724 | struct extent_map *em; |
| 6725 | u64 logical; |
| 6726 | u64 length; |
| 6727 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6728 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6729 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6730 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6731 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6732 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6733 | logical = key->offset; |
| 6734 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6735 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6736 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6737 | /* |
| 6738 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6739 | * as chunk item in tree block is already verified by tree-checker. |
| 6740 | */ |
| 6741 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6742 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6743 | if (ret) |
| 6744 | return ret; |
| 6745 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6746 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6747 | read_lock(&map_tree->lock); |
| 6748 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6749 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6750 | |
| 6751 | /* already mapped? */ |
| 6752 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6753 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6754 | return 0; |
| 6755 | } else if (em) { |
| 6756 | free_extent_map(em); |
| 6757 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6758 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6759 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6760 | if (!em) |
| 6761 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6762 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6763 | if (!map) { |
| 6764 | free_extent_map(em); |
| 6765 | return -ENOMEM; |
| 6766 | } |
| 6767 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6768 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6769 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6770 | em->start = logical; |
| 6771 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6772 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6773 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6774 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6775 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6776 | map->num_stripes = num_stripes; |
| 6777 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6778 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6779 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6780 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6781 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6782 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6783 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6784 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6785 | for (i = 0; i < num_stripes; i++) { |
| 6786 | map->stripes[i].physical = |
| 6787 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6788 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6789 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6790 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6791 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6792 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6793 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6794 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6795 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6796 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6797 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6798 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6799 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6800 | if (!map->stripes[i].dev) { |
| 6801 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6802 | add_missing_dev(fs_info->fs_devices, devid, |
| 6803 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6804 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6805 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6806 | btrfs_err(fs_info, |
| 6807 | "failed to init missing dev %llu: %ld", |
| 6808 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6809 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6810 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6811 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6812 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6813 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6814 | &(map->stripes[i].dev->dev_state)); |
| 6815 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6816 | } |
| 6817 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6818 | write_lock(&map_tree->lock); |
| 6819 | ret = add_extent_mapping(map_tree, em, 0); |
| 6820 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6821 | if (ret < 0) { |
| 6822 | btrfs_err(fs_info, |
| 6823 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6824 | em->start, em->len, ret); |
| 6825 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6826 | free_extent_map(em); |
| 6827 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6828 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6829 | } |
| 6830 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6831 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6832 | struct btrfs_dev_item *dev_item, |
| 6833 | struct btrfs_device *device) |
| 6834 | { |
| 6835 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6836 | |
| 6837 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6838 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6839 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6840 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6841 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6842 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6843 | device->type = btrfs_device_type(leaf, dev_item); |
| 6844 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6845 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6846 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6847 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6848 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6849 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6850 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6851 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6852 | } |
| 6853 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6854 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6855 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6856 | { |
| 6857 | struct btrfs_fs_devices *fs_devices; |
| 6858 | int ret; |
| 6859 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6860 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6861 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6862 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6863 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6864 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6865 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6866 | return fs_devices; |
| 6867 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6868 | fs_devices = fs_devices->seed; |
| 6869 | } |
| 6870 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6871 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6872 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6873 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6874 | return ERR_PTR(-ENOENT); |
| 6875 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6876 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6877 | if (IS_ERR(fs_devices)) |
| 6878 | return fs_devices; |
| 6879 | |
| 6880 | fs_devices->seeding = 1; |
| 6881 | fs_devices->opened = 1; |
| 6882 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6883 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6884 | |
| 6885 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6886 | if (IS_ERR(fs_devices)) |
| 6887 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6888 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6889 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6890 | if (ret) { |
| 6891 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6892 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6893 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6894 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6895 | |
| 6896 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6897 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6898 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6899 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6900 | goto out; |
| 6901 | } |
| 6902 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6903 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6904 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6905 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6906 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6907 | } |
| 6908 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6909 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6910 | struct btrfs_dev_item *dev_item) |
| 6911 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6912 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6913 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6914 | struct btrfs_device *device; |
| 6915 | u64 devid; |
| 6916 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6917 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6918 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6919 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6920 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6921 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6922 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6923 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6924 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6925 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6926 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6927 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6928 | if (IS_ERR(fs_devices)) |
| 6929 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6930 | } |
| 6931 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6932 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6933 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6934 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6935 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6936 | btrfs_report_missing_device(fs_info, devid, |
| 6937 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6938 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6939 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6940 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6941 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6942 | if (IS_ERR(device)) { |
| 6943 | btrfs_err(fs_info, |
| 6944 | "failed to add missing dev %llu: %ld", |
| 6945 | devid, PTR_ERR(device)); |
| 6946 | return PTR_ERR(device); |
| 6947 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6948 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6949 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6950 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6951 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6952 | btrfs_report_missing_device(fs_info, |
| 6953 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6954 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6955 | } |
| 6956 | btrfs_report_missing_device(fs_info, devid, |
| 6957 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6958 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6959 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6960 | if (!device->bdev && |
| 6961 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6962 | /* |
| 6963 | * this happens when a device that was properly setup |
| 6964 | * in the device info lists suddenly goes bad. |
| 6965 | * device->bdev is NULL, and so we have to set |
| 6966 | * device->missing to one here |
| 6967 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6968 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6969 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6970 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6971 | |
| 6972 | /* Move the device to its own fs_devices */ |
| 6973 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6974 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6975 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6976 | |
| 6977 | list_move(&device->dev_list, &fs_devices->devices); |
| 6978 | device->fs_devices->num_devices--; |
| 6979 | fs_devices->num_devices++; |
| 6980 | |
| 6981 | device->fs_devices->missing_devices--; |
| 6982 | fs_devices->missing_devices++; |
| 6983 | |
| 6984 | device->fs_devices = fs_devices; |
| 6985 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6986 | } |
| 6987 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6988 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6989 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6990 | if (device->generation != |
| 6991 | btrfs_device_generation(leaf, dev_item)) |
| 6992 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6993 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6994 | |
| 6995 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6996 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6997 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6998 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6999 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 7000 | atomic64_add(device->total_bytes - device->bytes_used, |
| 7001 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 7002 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7003 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7004 | return ret; |
| 7005 | } |
| 7006 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7007 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7008 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7009 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7010 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7011 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7012 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7013 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7014 | u8 *array_ptr; |
| 7015 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7016 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7017 | u32 num_stripes; |
| 7018 | u32 array_size; |
| 7019 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7020 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7021 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7022 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7023 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7024 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 7025 | /* |
| 7026 | * This will create extent buffer of nodesize, superblock size is |
| 7027 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 7028 | * overallocate but we can keep it as-is, only the first page is used. |
| 7029 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7030 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 7031 | if (IS_ERR(sb)) |
| 7032 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 7033 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 7034 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 7035 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7036 | * The sb extent buffer is artificial and just used to read the system array. |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 7037 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 7038 | * pages up-to-date when the page is larger: extent does not cover the |
| 7039 | * whole page and consequently check_page_uptodate does not find all |
| 7040 | * the page's extents up-to-date (the hole beyond sb), |
| 7041 | * write_extent_buffer then triggers a WARN_ON. |
| 7042 | * |
| 7043 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 7044 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 7045 | * to silence the warning eg. on PowerPC 64. |
| 7046 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7047 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7048 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 7049 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7050 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7051 | array_size = btrfs_super_sys_array_size(super_copy); |
| 7052 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7053 | array_ptr = super_copy->sys_chunk_array; |
| 7054 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 7055 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7056 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7057 | while (cur_offset < array_size) { |
| 7058 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7059 | len = sizeof(*disk_key); |
| 7060 | if (cur_offset + len > array_size) |
| 7061 | goto out_short_read; |
| 7062 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7063 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 7064 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7065 | array_ptr += len; |
| 7066 | sb_array_offset += len; |
| 7067 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7068 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7069 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7070 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7071 | /* |
| 7072 | * At least one btrfs_chunk with one stripe must be |
| 7073 | * present, exact stripe count check comes afterwards |
| 7074 | */ |
| 7075 | len = btrfs_chunk_item_size(1); |
| 7076 | if (cur_offset + len > array_size) |
| 7077 | goto out_short_read; |
| 7078 | |
| 7079 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7080 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7081 | btrfs_err(fs_info, |
| 7082 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7083 | num_stripes, cur_offset); |
| 7084 | ret = -EIO; |
| 7085 | break; |
| 7086 | } |
| 7087 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7088 | type = btrfs_chunk_type(sb, chunk); |
| 7089 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7090 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7091 | "invalid chunk type %llu in sys_array at offset %u", |
| 7092 | type, cur_offset); |
| 7093 | ret = -EIO; |
| 7094 | break; |
| 7095 | } |
| 7096 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7097 | len = btrfs_chunk_item_size(num_stripes); |
| 7098 | if (cur_offset + len > array_size) |
| 7099 | goto out_short_read; |
| 7100 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7101 | ret = read_one_chunk(&key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7102 | if (ret) |
| 7103 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7104 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7105 | btrfs_err(fs_info, |
| 7106 | "unexpected item type %u in sys_array at offset %u", |
| 7107 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7108 | ret = -EIO; |
| 7109 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7110 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7111 | array_ptr += len; |
| 7112 | sb_array_offset += len; |
| 7113 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7114 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7115 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7116 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7117 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7118 | |
| 7119 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7120 | btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u", |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7121 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7122 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7123 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7124 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7125 | } |
| 7126 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7127 | /* |
| 7128 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 7129 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7130 | * If the @failing_dev is specified, it's accounted as missing. |
| 7131 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7132 | * Return true if all chunks meet the minimal RW mount requirements. |
| 7133 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 7134 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7135 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 7136 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7137 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7138 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7139 | struct extent_map *em; |
| 7140 | u64 next_start = 0; |
| 7141 | bool ret = true; |
| 7142 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7143 | read_lock(&map_tree->lock); |
| 7144 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 7145 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7146 | /* No chunk at all? Return false anyway */ |
| 7147 | if (!em) { |
| 7148 | ret = false; |
| 7149 | goto out; |
| 7150 | } |
| 7151 | while (em) { |
| 7152 | struct map_lookup *map; |
| 7153 | int missing = 0; |
| 7154 | int max_tolerated; |
| 7155 | int i; |
| 7156 | |
| 7157 | map = em->map_lookup; |
| 7158 | max_tolerated = |
| 7159 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 7160 | map->type); |
| 7161 | for (i = 0; i < map->num_stripes; i++) { |
| 7162 | struct btrfs_device *dev = map->stripes[i].dev; |
| 7163 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 7164 | if (!dev || !dev->bdev || |
| 7165 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7166 | dev->last_flush_error) |
| 7167 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7168 | else if (failing_dev && failing_dev == dev) |
| 7169 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7170 | } |
| 7171 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7172 | if (!failing_dev) |
| 7173 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7174 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7175 | em->start, missing, max_tolerated); |
| 7176 | free_extent_map(em); |
| 7177 | ret = false; |
| 7178 | goto out; |
| 7179 | } |
| 7180 | next_start = extent_map_end(em); |
| 7181 | free_extent_map(em); |
| 7182 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7183 | read_lock(&map_tree->lock); |
| 7184 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7185 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7186 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7187 | } |
| 7188 | out: |
| 7189 | return ret; |
| 7190 | } |
| 7191 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7192 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7193 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7194 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7195 | struct btrfs_path *path; |
| 7196 | struct extent_buffer *leaf; |
| 7197 | struct btrfs_key key; |
| 7198 | struct btrfs_key found_key; |
| 7199 | int ret; |
| 7200 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7201 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7202 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7203 | path = btrfs_alloc_path(); |
| 7204 | if (!path) |
| 7205 | return -ENOMEM; |
| 7206 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7207 | /* |
| 7208 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7209 | * otherwise we don't need it. |
| 7210 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7211 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7212 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7213 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7214 | /* |
| 7215 | * Read all device items, and then all the chunk items. All |
| 7216 | * device items are found before any chunk item (their object id |
| 7217 | * is smaller than the lowest possible object id for a chunk |
| 7218 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7219 | */ |
| 7220 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7221 | key.offset = 0; |
| 7222 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7223 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7224 | if (ret < 0) |
| 7225 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7226 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7227 | leaf = path->nodes[0]; |
| 7228 | slot = path->slots[0]; |
| 7229 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7230 | ret = btrfs_next_leaf(root, path); |
| 7231 | if (ret == 0) |
| 7232 | continue; |
| 7233 | if (ret < 0) |
| 7234 | goto error; |
| 7235 | break; |
| 7236 | } |
| 7237 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7238 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7239 | struct btrfs_dev_item *dev_item; |
| 7240 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7241 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7242 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7243 | if (ret) |
| 7244 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7245 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7246 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7247 | struct btrfs_chunk *chunk; |
| 7248 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7249 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7250 | if (ret) |
| 7251 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7252 | } |
| 7253 | path->slots[0]++; |
| 7254 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7255 | |
| 7256 | /* |
| 7257 | * After loading chunk tree, we've got all device information, |
| 7258 | * do another round of validation checks. |
| 7259 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7260 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7261 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7262 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7263 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7264 | total_dev); |
| 7265 | ret = -EINVAL; |
| 7266 | goto error; |
| 7267 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7268 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7269 | fs_info->fs_devices->total_rw_bytes) { |
| 7270 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7271 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7272 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7273 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7274 | ret = -EINVAL; |
| 7275 | goto error; |
| 7276 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7277 | ret = 0; |
| 7278 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7279 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7280 | mutex_unlock(&uuid_mutex); |
| 7281 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7282 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7283 | return ret; |
| 7284 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7285 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7286 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7287 | { |
| 7288 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7289 | struct btrfs_device *device; |
| 7290 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7291 | while (fs_devices) { |
| 7292 | mutex_lock(&fs_devices->device_list_mutex); |
| 7293 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7294 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7295 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7296 | |
| 7297 | fs_devices = fs_devices->seed; |
| 7298 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7299 | } |
| 7300 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7301 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 7302 | { |
| 7303 | int i; |
| 7304 | |
| 7305 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame^] | 7306 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7310 | { |
| 7311 | struct btrfs_key key; |
| 7312 | struct btrfs_key found_key; |
| 7313 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7314 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7315 | struct extent_buffer *eb; |
| 7316 | int slot; |
| 7317 | int ret = 0; |
| 7318 | struct btrfs_device *device; |
| 7319 | struct btrfs_path *path = NULL; |
| 7320 | int i; |
| 7321 | |
| 7322 | path = btrfs_alloc_path(); |
| 7323 | if (!path) { |
| 7324 | ret = -ENOMEM; |
| 7325 | goto out; |
| 7326 | } |
| 7327 | |
| 7328 | mutex_lock(&fs_devices->device_list_mutex); |
| 7329 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7330 | int item_size; |
| 7331 | struct btrfs_dev_stats_item *ptr; |
| 7332 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7333 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7334 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7335 | key.offset = device->devid; |
| 7336 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7337 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7338 | __btrfs_reset_dev_stats(device); |
| 7339 | device->dev_stats_valid = 1; |
| 7340 | btrfs_release_path(path); |
| 7341 | continue; |
| 7342 | } |
| 7343 | slot = path->slots[0]; |
| 7344 | eb = path->nodes[0]; |
| 7345 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 7346 | item_size = btrfs_item_size_nr(eb, slot); |
| 7347 | |
| 7348 | ptr = btrfs_item_ptr(eb, slot, |
| 7349 | struct btrfs_dev_stats_item); |
| 7350 | |
| 7351 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7352 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7353 | btrfs_dev_stat_set(device, i, |
| 7354 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7355 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame^] | 7356 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7357 | } |
| 7358 | |
| 7359 | device->dev_stats_valid = 1; |
| 7360 | btrfs_dev_stat_print_on_load(device); |
| 7361 | btrfs_release_path(path); |
| 7362 | } |
| 7363 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7364 | |
| 7365 | out: |
| 7366 | btrfs_free_path(path); |
| 7367 | return ret < 0 ? ret : 0; |
| 7368 | } |
| 7369 | |
| 7370 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7371 | struct btrfs_device *device) |
| 7372 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7373 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7374 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7375 | struct btrfs_path *path; |
| 7376 | struct btrfs_key key; |
| 7377 | struct extent_buffer *eb; |
| 7378 | struct btrfs_dev_stats_item *ptr; |
| 7379 | int ret; |
| 7380 | int i; |
| 7381 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7382 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7383 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7384 | key.offset = device->devid; |
| 7385 | |
| 7386 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7387 | if (!path) |
| 7388 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7389 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7390 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7391 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7392 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7393 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7394 | goto out; |
| 7395 | } |
| 7396 | |
| 7397 | if (ret == 0 && |
| 7398 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7399 | /* need to delete old one and insert a new one */ |
| 7400 | ret = btrfs_del_item(trans, dev_root, path); |
| 7401 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7402 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7403 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7404 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7405 | goto out; |
| 7406 | } |
| 7407 | ret = 1; |
| 7408 | } |
| 7409 | |
| 7410 | if (ret == 1) { |
| 7411 | /* need to insert a new item */ |
| 7412 | btrfs_release_path(path); |
| 7413 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7414 | &key, sizeof(*ptr)); |
| 7415 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7416 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7417 | "insert dev_stats item for device %s failed %d", |
| 7418 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7419 | goto out; |
| 7420 | } |
| 7421 | } |
| 7422 | |
| 7423 | eb = path->nodes[0]; |
| 7424 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7425 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7426 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7427 | btrfs_dev_stat_read(device, i)); |
| 7428 | btrfs_mark_buffer_dirty(eb); |
| 7429 | |
| 7430 | out: |
| 7431 | btrfs_free_path(path); |
| 7432 | return ret; |
| 7433 | } |
| 7434 | |
| 7435 | /* |
| 7436 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7437 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7438 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7439 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7440 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7441 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7442 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7443 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7444 | int ret = 0; |
| 7445 | |
| 7446 | mutex_lock(&fs_devices->device_list_mutex); |
| 7447 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7448 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7449 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7450 | continue; |
| 7451 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7452 | |
| 7453 | /* |
| 7454 | * There is a LOAD-LOAD control dependency between the value of |
| 7455 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7456 | * reading the in-memory counters. Such control dependencies |
| 7457 | * require explicit read memory barriers. |
| 7458 | * |
| 7459 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7460 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7461 | * barrier implied by atomic_xchg in |
| 7462 | * btrfs_dev_stats_read_and_reset |
| 7463 | */ |
| 7464 | smp_rmb(); |
| 7465 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7466 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7467 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7468 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7469 | } |
| 7470 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7471 | |
| 7472 | return ret; |
| 7473 | } |
| 7474 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7475 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7476 | { |
| 7477 | btrfs_dev_stat_inc(dev, index); |
| 7478 | btrfs_dev_stat_print_on_error(dev); |
| 7479 | } |
| 7480 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7481 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7482 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7483 | if (!dev->dev_stats_valid) |
| 7484 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7485 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7486 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7487 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7488 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7489 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7490 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7491 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7492 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7493 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7494 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7495 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7496 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7497 | int i; |
| 7498 | |
| 7499 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7500 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7501 | break; |
| 7502 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7503 | return; /* all values == 0, suppress message */ |
| 7504 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7505 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7506 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7507 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7508 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7509 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7510 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7511 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7512 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7513 | } |
| 7514 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7515 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7516 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7517 | { |
| 7518 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7519 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7520 | int i; |
| 7521 | |
| 7522 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7523 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7524 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7525 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7526 | |
| 7527 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7528 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7529 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7530 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7531 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7532 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7533 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7534 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7535 | if (stats->nr_items > i) |
| 7536 | stats->values[i] = |
| 7537 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7538 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame^] | 7539 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7540 | } |
| 7541 | } else { |
| 7542 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7543 | if (stats->nr_items > i) |
| 7544 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7545 | } |
| 7546 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7547 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7548 | return 0; |
| 7549 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7550 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7551 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7552 | { |
| 7553 | struct buffer_head *bh; |
| 7554 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7555 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7556 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7557 | if (!bdev) |
| 7558 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7559 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7560 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7561 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7562 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7563 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7564 | continue; |
| 7565 | |
| 7566 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7567 | |
| 7568 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7569 | set_buffer_dirty(bh); |
| 7570 | sync_dirty_buffer(bh); |
| 7571 | brelse(bh); |
| 7572 | } |
| 7573 | |
| 7574 | /* Notify udev that device has changed */ |
| 7575 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7576 | |
| 7577 | /* Update ctime/mtime for device path for libblkid */ |
| 7578 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7579 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7580 | |
| 7581 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7582 | * Update the size and bytes used for each device where it changed. This is |
| 7583 | * delayed since we would otherwise get errors while writing out the |
| 7584 | * superblocks. |
| 7585 | * |
| 7586 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7587 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7588 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7589 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7590 | struct btrfs_device *curr, *next; |
| 7591 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7592 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7593 | |
| 7594 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7595 | return; |
| 7596 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7597 | /* |
| 7598 | * We don't need the device_list_mutex here. This list is owned by the |
| 7599 | * transaction and the transaction must complete before the device is |
| 7600 | * released. |
| 7601 | */ |
| 7602 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7603 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7604 | post_commit_list) { |
| 7605 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7606 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7607 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7608 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7609 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7610 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7611 | |
| 7612 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7613 | { |
| 7614 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7615 | while (fs_devices) { |
| 7616 | fs_devices->fs_info = fs_info; |
| 7617 | fs_devices = fs_devices->seed; |
| 7618 | } |
| 7619 | } |
| 7620 | |
| 7621 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7622 | { |
| 7623 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7624 | while (fs_devices) { |
| 7625 | fs_devices->fs_info = NULL; |
| 7626 | fs_devices = fs_devices->seed; |
| 7627 | } |
| 7628 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7629 | |
| 7630 | /* |
| 7631 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7632 | */ |
| 7633 | int btrfs_bg_type_to_factor(u64 flags) |
| 7634 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7635 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7636 | |
| 7637 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7638 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7639 | |
| 7640 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7641 | |
| 7642 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7643 | u64 chunk_offset, u64 devid, |
| 7644 | u64 physical_offset, u64 physical_len) |
| 7645 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7646 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7647 | struct extent_map *em; |
| 7648 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7649 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7650 | u64 stripe_len; |
| 7651 | bool found = false; |
| 7652 | int ret = 0; |
| 7653 | int i; |
| 7654 | |
| 7655 | read_lock(&em_tree->lock); |
| 7656 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7657 | read_unlock(&em_tree->lock); |
| 7658 | |
| 7659 | if (!em) { |
| 7660 | btrfs_err(fs_info, |
| 7661 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7662 | physical_offset, devid); |
| 7663 | ret = -EUCLEAN; |
| 7664 | goto out; |
| 7665 | } |
| 7666 | |
| 7667 | map = em->map_lookup; |
| 7668 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7669 | if (physical_len != stripe_len) { |
| 7670 | btrfs_err(fs_info, |
| 7671 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7672 | physical_offset, devid, em->start, physical_len, |
| 7673 | stripe_len); |
| 7674 | ret = -EUCLEAN; |
| 7675 | goto out; |
| 7676 | } |
| 7677 | |
| 7678 | for (i = 0; i < map->num_stripes; i++) { |
| 7679 | if (map->stripes[i].dev->devid == devid && |
| 7680 | map->stripes[i].physical == physical_offset) { |
| 7681 | found = true; |
| 7682 | if (map->verified_stripes >= map->num_stripes) { |
| 7683 | btrfs_err(fs_info, |
| 7684 | "too many dev extents for chunk %llu found", |
| 7685 | em->start); |
| 7686 | ret = -EUCLEAN; |
| 7687 | goto out; |
| 7688 | } |
| 7689 | map->verified_stripes++; |
| 7690 | break; |
| 7691 | } |
| 7692 | } |
| 7693 | if (!found) { |
| 7694 | btrfs_err(fs_info, |
| 7695 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7696 | physical_offset, devid); |
| 7697 | ret = -EUCLEAN; |
| 7698 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7699 | |
| 7700 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7701 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7702 | if (!dev) { |
| 7703 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7704 | ret = -EUCLEAN; |
| 7705 | goto out; |
| 7706 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7707 | |
| 7708 | /* It's possible this device is a dummy for seed device */ |
| 7709 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7710 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7711 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7712 | if (!dev) { |
| 7713 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7714 | devid); |
| 7715 | ret = -EUCLEAN; |
| 7716 | goto out; |
| 7717 | } |
| 7718 | } |
| 7719 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7720 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7721 | btrfs_err(fs_info, |
| 7722 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7723 | devid, physical_offset, physical_len, |
| 7724 | dev->disk_total_bytes); |
| 7725 | ret = -EUCLEAN; |
| 7726 | goto out; |
| 7727 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7728 | out: |
| 7729 | free_extent_map(em); |
| 7730 | return ret; |
| 7731 | } |
| 7732 | |
| 7733 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7734 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7735 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7736 | struct extent_map *em; |
| 7737 | struct rb_node *node; |
| 7738 | int ret = 0; |
| 7739 | |
| 7740 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7741 | for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7742 | em = rb_entry(node, struct extent_map, rb_node); |
| 7743 | if (em->map_lookup->num_stripes != |
| 7744 | em->map_lookup->verified_stripes) { |
| 7745 | btrfs_err(fs_info, |
| 7746 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7747 | em->start, em->map_lookup->verified_stripes, |
| 7748 | em->map_lookup->num_stripes); |
| 7749 | ret = -EUCLEAN; |
| 7750 | goto out; |
| 7751 | } |
| 7752 | } |
| 7753 | out: |
| 7754 | read_unlock(&em_tree->lock); |
| 7755 | return ret; |
| 7756 | } |
| 7757 | |
| 7758 | /* |
| 7759 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7760 | * later chunk allocation/free would cause unexpected behavior. |
| 7761 | * |
| 7762 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7763 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7764 | */ |
| 7765 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7766 | { |
| 7767 | struct btrfs_path *path; |
| 7768 | struct btrfs_root *root = fs_info->dev_root; |
| 7769 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7770 | u64 prev_devid = 0; |
| 7771 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7772 | int ret = 0; |
| 7773 | |
| 7774 | key.objectid = 1; |
| 7775 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7776 | key.offset = 0; |
| 7777 | |
| 7778 | path = btrfs_alloc_path(); |
| 7779 | if (!path) |
| 7780 | return -ENOMEM; |
| 7781 | |
| 7782 | path->reada = READA_FORWARD; |
| 7783 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7784 | if (ret < 0) |
| 7785 | goto out; |
| 7786 | |
| 7787 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7788 | ret = btrfs_next_item(root, path); |
| 7789 | if (ret < 0) |
| 7790 | goto out; |
| 7791 | /* No dev extents at all? Not good */ |
| 7792 | if (ret > 0) { |
| 7793 | ret = -EUCLEAN; |
| 7794 | goto out; |
| 7795 | } |
| 7796 | } |
| 7797 | while (1) { |
| 7798 | struct extent_buffer *leaf = path->nodes[0]; |
| 7799 | struct btrfs_dev_extent *dext; |
| 7800 | int slot = path->slots[0]; |
| 7801 | u64 chunk_offset; |
| 7802 | u64 physical_offset; |
| 7803 | u64 physical_len; |
| 7804 | u64 devid; |
| 7805 | |
| 7806 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7807 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7808 | break; |
| 7809 | devid = key.objectid; |
| 7810 | physical_offset = key.offset; |
| 7811 | |
| 7812 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7813 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7814 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7815 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7816 | /* Check if this dev extent overlaps with the previous one */ |
| 7817 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7818 | btrfs_err(fs_info, |
| 7819 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7820 | devid, physical_offset, prev_dev_ext_end); |
| 7821 | ret = -EUCLEAN; |
| 7822 | goto out; |
| 7823 | } |
| 7824 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7825 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7826 | physical_offset, physical_len); |
| 7827 | if (ret < 0) |
| 7828 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7829 | prev_devid = devid; |
| 7830 | prev_dev_ext_end = physical_offset + physical_len; |
| 7831 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7832 | ret = btrfs_next_item(root, path); |
| 7833 | if (ret < 0) |
| 7834 | goto out; |
| 7835 | if (ret > 0) { |
| 7836 | ret = 0; |
| 7837 | break; |
| 7838 | } |
| 7839 | } |
| 7840 | |
| 7841 | /* Ensure all chunks have corresponding dev extents */ |
| 7842 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7843 | out: |
| 7844 | btrfs_free_path(path); |
| 7845 | return ret; |
| 7846 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7847 | |
| 7848 | /* |
| 7849 | * Check whether the given block group or device is pinned by any inode being |
| 7850 | * used as a swapfile. |
| 7851 | */ |
| 7852 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7853 | { |
| 7854 | struct btrfs_swapfile_pin *sp; |
| 7855 | struct rb_node *node; |
| 7856 | |
| 7857 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7858 | node = fs_info->swapfile_pins.rb_node; |
| 7859 | while (node) { |
| 7860 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7861 | if (ptr < sp->ptr) |
| 7862 | node = node->rb_left; |
| 7863 | else if (ptr > sp->ptr) |
| 7864 | node = node->rb_right; |
| 7865 | else |
| 7866 | break; |
| 7867 | } |
| 7868 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7869 | return node != NULL; |
| 7870 | } |