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" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 32 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 33 | [BTRFS_RAID_RAID10] = { |
| 34 | .sub_stripes = 2, |
| 35 | .dev_stripes = 1, |
| 36 | .devs_max = 0, /* 0 == as many as possible */ |
| 37 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 38 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 39 | .devs_increment = 2, |
| 40 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 41 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 42 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 43 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 44 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 45 | }, |
| 46 | [BTRFS_RAID_RAID1] = { |
| 47 | .sub_stripes = 1, |
| 48 | .dev_stripes = 1, |
| 49 | .devs_max = 2, |
| 50 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 51 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 52 | .devs_increment = 2, |
| 53 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 54 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 55 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 56 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 57 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 58 | }, |
| 59 | [BTRFS_RAID_DUP] = { |
| 60 | .sub_stripes = 1, |
| 61 | .dev_stripes = 2, |
| 62 | .devs_max = 1, |
| 63 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 64 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 65 | .devs_increment = 1, |
| 66 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 67 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 68 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 69 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 70 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 71 | }, |
| 72 | [BTRFS_RAID_RAID0] = { |
| 73 | .sub_stripes = 1, |
| 74 | .dev_stripes = 1, |
| 75 | .devs_max = 0, |
| 76 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 77 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 78 | .devs_increment = 1, |
| 79 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 80 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 81 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 82 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 83 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 84 | }, |
| 85 | [BTRFS_RAID_SINGLE] = { |
| 86 | .sub_stripes = 1, |
| 87 | .dev_stripes = 1, |
| 88 | .devs_max = 1, |
| 89 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 90 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 91 | .devs_increment = 1, |
| 92 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 93 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 94 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 95 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 96 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 97 | }, |
| 98 | [BTRFS_RAID_RAID5] = { |
| 99 | .sub_stripes = 1, |
| 100 | .dev_stripes = 1, |
| 101 | .devs_max = 0, |
| 102 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 103 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 104 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 105 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 106 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 107 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 108 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 109 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 110 | }, |
| 111 | [BTRFS_RAID_RAID6] = { |
| 112 | .sub_stripes = 1, |
| 113 | .dev_stripes = 1, |
| 114 | .devs_max = 0, |
| 115 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 116 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 117 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 118 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 119 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 120 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 121 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 122 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 123 | }, |
| 124 | }; |
| 125 | |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 126 | const char *get_raid_name(enum btrfs_raid_types type) |
| 127 | { |
| 128 | if (type >= BTRFS_NR_RAID_TYPES) |
| 129 | return NULL; |
| 130 | |
| 131 | return btrfs_raid_array[type].raid_name; |
| 132 | } |
| 133 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 134 | /* |
| 135 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 136 | * bytes including terminating null byte. |
| 137 | */ |
| 138 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 139 | { |
| 140 | int i; |
| 141 | int ret; |
| 142 | char *bp = buf; |
| 143 | u64 flags = bg_flags; |
| 144 | u32 size_bp = size_buf; |
| 145 | |
| 146 | if (!flags) { |
| 147 | strcpy(bp, "NONE"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | #define DESCRIBE_FLAG(flag, desc) \ |
| 152 | do { \ |
| 153 | if (flags & (flag)) { \ |
| 154 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 155 | if (ret < 0 || ret >= size_bp) \ |
| 156 | goto out_overflow; \ |
| 157 | size_bp -= ret; \ |
| 158 | bp += ret; \ |
| 159 | flags &= ~(flag); \ |
| 160 | } \ |
| 161 | } while (0) |
| 162 | |
| 163 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 164 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 165 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 166 | |
| 167 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 168 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 169 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 170 | btrfs_raid_array[i].raid_name); |
| 171 | #undef DESCRIBE_FLAG |
| 172 | |
| 173 | if (flags) { |
| 174 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 175 | size_bp -= ret; |
| 176 | } |
| 177 | |
| 178 | if (size_bp < size_buf) |
| 179 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 180 | |
| 181 | /* |
| 182 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 183 | * large buffer |
| 184 | */ |
| 185 | out_overflow:; |
| 186 | } |
| 187 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 188 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 189 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 190 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 191 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 192 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 193 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 194 | enum btrfs_map_op op, |
| 195 | u64 logical, u64 *length, |
| 196 | struct btrfs_bio **bbio_ret, |
| 197 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 198 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 199 | /* |
| 200 | * Device locking |
| 201 | * ============== |
| 202 | * |
| 203 | * There are several mutexes that protect manipulation of devices and low-level |
| 204 | * structures like chunks but not block groups, extents or files |
| 205 | * |
| 206 | * uuid_mutex (global lock) |
| 207 | * ------------------------ |
| 208 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 209 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 210 | * device) or requested by the device= mount option |
| 211 | * |
| 212 | * the mutex can be very coarse and can cover long-running operations |
| 213 | * |
| 214 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 215 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 216 | * |
| 217 | * global::fs_devs - add, remove, updates to the global list |
| 218 | * |
| 219 | * does not protect: manipulation of the fs_devices::devices list! |
| 220 | * |
| 221 | * btrfs_device::name - renames (write side), read is RCU |
| 222 | * |
| 223 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 224 | * ------------------------------------------------ |
| 225 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 226 | * |
| 227 | * simple list traversal with read-only actions can be done with RCU protection |
| 228 | * |
| 229 | * may be used to exclude some operations from running concurrently without any |
| 230 | * modifications to the list (see write_all_supers) |
| 231 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 232 | * balance_mutex |
| 233 | * ------------- |
| 234 | * protects balance structures (status, state) and context accessed from |
| 235 | * several places (internally, ioctl) |
| 236 | * |
| 237 | * chunk_mutex |
| 238 | * ----------- |
| 239 | * protects chunks, adding or removing during allocation, trim or when a new |
| 240 | * device is added/removed |
| 241 | * |
| 242 | * cleaner_mutex |
| 243 | * ------------- |
| 244 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 245 | * cleaning together with relocation or delayed iputs |
| 246 | * |
| 247 | * |
| 248 | * Lock nesting |
| 249 | * ============ |
| 250 | * |
| 251 | * uuid_mutex |
| 252 | * volume_mutex |
| 253 | * device_list_mutex |
| 254 | * chunk_mutex |
| 255 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 256 | * |
| 257 | * |
| 258 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 259 | * ====================================== |
| 260 | * |
| 261 | * Maintains the exclusivity of the following operations that apply to the |
| 262 | * whole filesystem and cannot run in parallel. |
| 263 | * |
| 264 | * - Balance (*) |
| 265 | * - Device add |
| 266 | * - Device remove |
| 267 | * - Device replace (*) |
| 268 | * - Resize |
| 269 | * |
| 270 | * The device operations (as above) can be in one of the following states: |
| 271 | * |
| 272 | * - Running state |
| 273 | * - Paused state |
| 274 | * - Completed state |
| 275 | * |
| 276 | * Only device operations marked with (*) can go into the Paused state for the |
| 277 | * following reasons: |
| 278 | * |
| 279 | * - ioctl (only Balance can be Paused through ioctl) |
| 280 | * - filesystem remounted as read-only |
| 281 | * - filesystem unmounted and mounted as read-only |
| 282 | * - system power-cycle and filesystem mounted as read-only |
| 283 | * - filesystem or device errors leading to forced read-only |
| 284 | * |
| 285 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 286 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 287 | * A device operation in Paused or Running state can be canceled or resumed |
| 288 | * either by ioctl (Balance only) or when remounted as read-write. |
| 289 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 290 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 291 | */ |
| 292 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 293 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 294 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 295 | struct list_head *btrfs_get_fs_uuids(void) |
| 296 | { |
| 297 | return &fs_uuids; |
| 298 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 299 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 300 | /* |
| 301 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 302 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 303 | * @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] | 304 | * |
| 305 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 306 | * The returned struct is not linked onto any lists and can be destroyed with |
| 307 | * kfree() right away. |
| 308 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 309 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 310 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 311 | { |
| 312 | struct btrfs_fs_devices *fs_devs; |
| 313 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 314 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 315 | if (!fs_devs) |
| 316 | return ERR_PTR(-ENOMEM); |
| 317 | |
| 318 | mutex_init(&fs_devs->device_list_mutex); |
| 319 | |
| 320 | INIT_LIST_HEAD(&fs_devs->devices); |
| 321 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 322 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 323 | if (fsid) |
| 324 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 325 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 326 | if (metadata_fsid) |
| 327 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 328 | else if (fsid) |
| 329 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 330 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 331 | return fs_devs; |
| 332 | } |
| 333 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 334 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 335 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 336 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 337 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 338 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 339 | bio_put(device->flush_bio); |
| 340 | kfree(device); |
| 341 | } |
| 342 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 343 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 344 | { |
| 345 | struct btrfs_device *device; |
| 346 | WARN_ON(fs_devices->opened); |
| 347 | while (!list_empty(&fs_devices->devices)) { |
| 348 | device = list_entry(fs_devices->devices.next, |
| 349 | struct btrfs_device, dev_list); |
| 350 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 351 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 352 | } |
| 353 | kfree(fs_devices); |
| 354 | } |
| 355 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 356 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 357 | enum kobject_action action) |
| 358 | { |
| 359 | int ret; |
| 360 | |
| 361 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 362 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 363 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 364 | action, |
| 365 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 366 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 367 | } |
| 368 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 369 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 370 | { |
| 371 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 372 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 373 | while (!list_empty(&fs_uuids)) { |
| 374 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 375 | struct btrfs_fs_devices, fs_list); |
| 376 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 377 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 378 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 379 | } |
| 380 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 381 | /* |
| 382 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 383 | * 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] | 384 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 385 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 386 | static struct btrfs_device *__alloc_device(void) |
| 387 | { |
| 388 | struct btrfs_device *dev; |
| 389 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 390 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 391 | if (!dev) |
| 392 | return ERR_PTR(-ENOMEM); |
| 393 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Preallocate a bio that's always going to be used for flushing device |
| 396 | * barriers and matches the device lifespan |
| 397 | */ |
| 398 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 399 | if (!dev->flush_bio) { |
| 400 | kfree(dev); |
| 401 | return ERR_PTR(-ENOMEM); |
| 402 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 403 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 404 | INIT_LIST_HEAD(&dev->dev_list); |
| 405 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 406 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 407 | |
| 408 | spin_lock_init(&dev->io_lock); |
| 409 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 410 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 411 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 412 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 413 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 414 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 415 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 416 | |
| 417 | return dev; |
| 418 | } |
| 419 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 420 | static noinline struct btrfs_fs_devices *find_fsid( |
| 421 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 422 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 423 | struct btrfs_fs_devices *fs_devices; |
| 424 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 425 | ASSERT(fsid); |
| 426 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 427 | if (metadata_fsid) { |
| 428 | /* |
| 429 | * Handle scanned device having completed its fsid change but |
| 430 | * belonging to a fs_devices that was created by first scanning |
| 431 | * a device which didn't have its fsid/metadata_uuid changed |
| 432 | * at all and the CHANGING_FSID_V2 flag set. |
| 433 | */ |
| 434 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 435 | if (fs_devices->fsid_change && |
| 436 | memcmp(metadata_fsid, fs_devices->fsid, |
| 437 | BTRFS_FSID_SIZE) == 0 && |
| 438 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 439 | BTRFS_FSID_SIZE) == 0) { |
| 440 | return fs_devices; |
| 441 | } |
| 442 | } |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Handle scanned device having completed its fsid change but |
| 445 | * belonging to a fs_devices that was created by a device that |
| 446 | * has an outdated pair of fsid/metadata_uuid and |
| 447 | * CHANGING_FSID_V2 flag set. |
| 448 | */ |
| 449 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 450 | if (fs_devices->fsid_change && |
| 451 | memcmp(fs_devices->metadata_uuid, |
| 452 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 453 | memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 454 | BTRFS_FSID_SIZE) == 0) { |
| 455 | return fs_devices; |
| 456 | } |
| 457 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 461 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 462 | if (metadata_fsid) { |
| 463 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 464 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 465 | BTRFS_FSID_SIZE) == 0) |
| 466 | return fs_devices; |
| 467 | } else { |
| 468 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 469 | return fs_devices; |
| 470 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 471 | } |
| 472 | return NULL; |
| 473 | } |
| 474 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 475 | static int |
| 476 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 477 | int flush, struct block_device **bdev, |
| 478 | struct buffer_head **bh) |
| 479 | { |
| 480 | int ret; |
| 481 | |
| 482 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 483 | |
| 484 | if (IS_ERR(*bdev)) { |
| 485 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 486 | goto error; |
| 487 | } |
| 488 | |
| 489 | if (flush) |
| 490 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 491 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 492 | if (ret) { |
| 493 | blkdev_put(*bdev, flags); |
| 494 | goto error; |
| 495 | } |
| 496 | invalidate_bdev(*bdev); |
| 497 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 498 | if (IS_ERR(*bh)) { |
| 499 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 500 | blkdev_put(*bdev, flags); |
| 501 | goto error; |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | |
| 506 | error: |
| 507 | *bdev = NULL; |
| 508 | *bh = NULL; |
| 509 | return ret; |
| 510 | } |
| 511 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 512 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 513 | struct bio *head, struct bio *tail) |
| 514 | { |
| 515 | |
| 516 | struct bio *old_head; |
| 517 | |
| 518 | old_head = pending_bios->head; |
| 519 | pending_bios->head = head; |
| 520 | if (pending_bios->tail) |
| 521 | tail->bi_next = old_head; |
| 522 | else |
| 523 | pending_bios->tail = tail; |
| 524 | } |
| 525 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 526 | /* |
| 527 | * we try to collect pending bios for a device so we don't get a large |
| 528 | * number of procs sending bios down to the same device. This greatly |
| 529 | * improves the schedulers ability to collect and merge the bios. |
| 530 | * |
| 531 | * But, it also turns into a long list of bios to process and that is sure |
| 532 | * to eventually make the worker thread block. The solution here is to |
| 533 | * make some progress and then put this work struct back at the end of |
| 534 | * the list if the block device is congested. This way, multiple devices |
| 535 | * can make progress from a single worker thread. |
| 536 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 537 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 538 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 539 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 540 | struct bio *pending; |
| 541 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 542 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 543 | struct bio *tail; |
| 544 | struct bio *cur; |
| 545 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 546 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 547 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 548 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 549 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 550 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 551 | struct blk_plug plug; |
| 552 | |
| 553 | /* |
| 554 | * this function runs all the bios we've collected for |
| 555 | * a particular device. We don't want to wander off to |
| 556 | * another device without first sending all of these down. |
| 557 | * So, setup a plug here and finish it off before we return |
| 558 | */ |
| 559 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 560 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 561 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 562 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 563 | loop: |
| 564 | spin_lock(&device->io_lock); |
| 565 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 566 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 567 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 568 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 569 | /* take all the bios off the list at once and process them |
| 570 | * later on (without the lock held). But, remember the |
| 571 | * tail and other pointers so the bios can be properly reinserted |
| 572 | * into the list if we hit congestion |
| 573 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 574 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 575 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 576 | force_reg = 1; |
| 577 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 578 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 579 | force_reg = 0; |
| 580 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 581 | |
| 582 | pending = pending_bios->head; |
| 583 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 584 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * if pending was null this time around, no bios need processing |
| 588 | * at all and we can stop. Otherwise it'll loop back up again |
| 589 | * and do an additional check so no bios are missed. |
| 590 | * |
| 591 | * device->running_pending is used to synchronize with the |
| 592 | * schedule_bio code. |
| 593 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 594 | if (device->pending_sync_bios.head == NULL && |
| 595 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 596 | again = 0; |
| 597 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 598 | } else { |
| 599 | again = 1; |
| 600 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 601 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 602 | |
| 603 | pending_bios->head = NULL; |
| 604 | pending_bios->tail = NULL; |
| 605 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 606 | spin_unlock(&device->io_lock); |
| 607 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 608 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 609 | |
| 610 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 611 | /* we want to work on both lists, but do more bios on the |
| 612 | * sync list than the regular list |
| 613 | */ |
| 614 | if ((num_run > 32 && |
| 615 | pending_bios != &device->pending_sync_bios && |
| 616 | device->pending_sync_bios.head) || |
| 617 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 618 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 619 | spin_lock(&device->io_lock); |
| 620 | requeue_list(pending_bios, pending, tail); |
| 621 | goto loop_lock; |
| 622 | } |
| 623 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 624 | cur = pending; |
| 625 | pending = pending->bi_next; |
| 626 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 627 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 628 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 629 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 630 | /* |
| 631 | * if we're doing the sync list, record that our |
| 632 | * plug has some sync requests on it |
| 633 | * |
| 634 | * If we're doing the regular list and there are |
| 635 | * sync requests sitting around, unplug before |
| 636 | * we add more |
| 637 | */ |
| 638 | if (pending_bios == &device->pending_sync_bios) { |
| 639 | sync_pending = 1; |
| 640 | } else if (sync_pending) { |
| 641 | blk_finish_plug(&plug); |
| 642 | blk_start_plug(&plug); |
| 643 | sync_pending = 0; |
| 644 | } |
| 645 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 646 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 647 | num_run++; |
| 648 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 649 | |
| 650 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * we made progress, there is more work to do and the bdi |
| 654 | * is now congested. Back off and let other work structs |
| 655 | * run instead |
| 656 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 657 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 658 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 659 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 660 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 661 | ioc = current->io_context; |
| 662 | |
| 663 | /* |
| 664 | * the main goal here is that we don't want to |
| 665 | * block if we're going to be able to submit |
| 666 | * more requests without blocking. |
| 667 | * |
| 668 | * This code does two great things, it pokes into |
| 669 | * the elevator code from a filesystem _and_ |
| 670 | * it makes assumptions about how batching works. |
| 671 | */ |
| 672 | if (ioc && ioc->nr_batch_requests > 0 && |
| 673 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 674 | (last_waited == 0 || |
| 675 | ioc->last_waited == last_waited)) { |
| 676 | /* |
| 677 | * we want to go through our batch of |
| 678 | * requests and stop. So, we copy out |
| 679 | * the ioc->last_waited time and test |
| 680 | * against it before looping |
| 681 | */ |
| 682 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 683 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 684 | continue; |
| 685 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 686 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 687 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 688 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 689 | |
| 690 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 691 | btrfs_queue_work(fs_info->submit_workers, |
| 692 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 693 | goto done; |
| 694 | } |
| 695 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 696 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 697 | cond_resched(); |
| 698 | if (again) |
| 699 | goto loop; |
| 700 | |
| 701 | spin_lock(&device->io_lock); |
| 702 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 703 | goto loop_lock; |
| 704 | spin_unlock(&device->io_lock); |
| 705 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 706 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 707 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 710 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 711 | { |
| 712 | struct btrfs_device *device; |
| 713 | |
| 714 | device = container_of(work, struct btrfs_device, work); |
| 715 | run_scheduled_bios(device); |
| 716 | } |
| 717 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 718 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 719 | { |
| 720 | int found; |
| 721 | |
| 722 | rcu_read_lock(); |
| 723 | found = strcmp(rcu_str_deref(device->name), path); |
| 724 | rcu_read_unlock(); |
| 725 | |
| 726 | return found == 0; |
| 727 | } |
| 728 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 729 | /* |
| 730 | * Search and remove all stale (devices which are not mounted) devices. |
| 731 | * When both inputs are NULL, it will search and release all stale devices. |
| 732 | * path: Optional. When provided will it release all unmounted devices |
| 733 | * matching this path only. |
| 734 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 735 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 736 | * Return: 0 for success or if @path is NULL. |
| 737 | * -EBUSY if @path is a mounted device. |
| 738 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 739 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 740 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 741 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 742 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 743 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 744 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 745 | int ret = 0; |
| 746 | |
| 747 | if (path) |
| 748 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 749 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 750 | 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] | 751 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 752 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 753 | list_for_each_entry_safe(device, tmp_device, |
| 754 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 755 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 756 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 757 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 758 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 759 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 760 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 761 | if (fs_devices->opened) { |
| 762 | /* for an already deleted device return 0 */ |
| 763 | if (path && ret != 0) |
| 764 | ret = -EBUSY; |
| 765 | break; |
| 766 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 767 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 768 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 769 | fs_devices->num_devices--; |
| 770 | list_del(&device->dev_list); |
| 771 | btrfs_free_device(device); |
| 772 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 773 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 774 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 775 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 776 | } |
| 777 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 778 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 779 | if (fs_devices->num_devices == 0) { |
| 780 | btrfs_sysfs_remove_fsid(fs_devices); |
| 781 | list_del(&fs_devices->fs_list); |
| 782 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 783 | } |
| 784 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 785 | |
| 786 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 787 | } |
| 788 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 789 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 790 | struct btrfs_device *device, fmode_t flags, |
| 791 | void *holder) |
| 792 | { |
| 793 | struct request_queue *q; |
| 794 | struct block_device *bdev; |
| 795 | struct buffer_head *bh; |
| 796 | struct btrfs_super_block *disk_super; |
| 797 | u64 devid; |
| 798 | int ret; |
| 799 | |
| 800 | if (device->bdev) |
| 801 | return -EINVAL; |
| 802 | if (!device->name) |
| 803 | return -EINVAL; |
| 804 | |
| 805 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 806 | &bdev, &bh); |
| 807 | if (ret) |
| 808 | return ret; |
| 809 | |
| 810 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 811 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 812 | if (devid != device->devid) |
| 813 | goto error_brelse; |
| 814 | |
| 815 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 816 | goto error_brelse; |
| 817 | |
| 818 | device->generation = btrfs_super_generation(disk_super); |
| 819 | |
| 820 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 821 | if (btrfs_super_incompat_flags(disk_super) & |
| 822 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 823 | pr_err( |
| 824 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 825 | goto error_brelse; |
| 826 | } |
| 827 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 828 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 829 | fs_devices->seeding = 1; |
| 830 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 831 | if (bdev_read_only(bdev)) |
| 832 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 833 | else |
| 834 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 838 | if (!blk_queue_nonrot(q)) |
| 839 | fs_devices->rotating = 1; |
| 840 | |
| 841 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 842 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 843 | device->mode = flags; |
| 844 | |
| 845 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 846 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 847 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 848 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 849 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 850 | } |
| 851 | brelse(bh); |
| 852 | |
| 853 | return 0; |
| 854 | |
| 855 | error_brelse: |
| 856 | brelse(bh); |
| 857 | blkdev_put(bdev, flags); |
| 858 | |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 862 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 863 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| 864 | * being created with a disk that has already completed its fsid change. |
| 865 | */ |
| 866 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 867 | struct btrfs_super_block *disk_super) |
| 868 | { |
| 869 | struct btrfs_fs_devices *fs_devices; |
| 870 | |
| 871 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 872 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 873 | BTRFS_FSID_SIZE) != 0 && |
| 874 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 875 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 876 | return fs_devices; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return NULL; |
| 881 | } |
| 882 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 883 | |
| 884 | static struct btrfs_fs_devices *find_fsid_changed( |
| 885 | struct btrfs_super_block *disk_super) |
| 886 | { |
| 887 | struct btrfs_fs_devices *fs_devices; |
| 888 | |
| 889 | /* |
| 890 | * Handles the case where scanned device is part of an fs that had |
| 891 | * multiple successful changes of FSID but curently device didn't |
| 892 | * observe it. Meaning our fsid will be different than theirs. |
| 893 | */ |
| 894 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 895 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 896 | BTRFS_FSID_SIZE) != 0 && |
| 897 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 898 | BTRFS_FSID_SIZE) == 0 && |
| 899 | memcmp(fs_devices->fsid, disk_super->fsid, |
| 900 | BTRFS_FSID_SIZE) != 0) { |
| 901 | return fs_devices; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | return NULL; |
| 906 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 907 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 908 | * Add new device to list of registered devices |
| 909 | * |
| 910 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 911 | * device pointer which was just added or updated when successful |
| 912 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 913 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 914 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 915 | struct btrfs_super_block *disk_super, |
| 916 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 917 | { |
| 918 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 919 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 920 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 921 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 922 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 923 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 924 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 925 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 926 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 927 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 928 | if (fsid_change_in_progress) { |
| 929 | if (!has_metadata_uuid) { |
| 930 | /* |
| 931 | * When we have an image which has CHANGING_FSID_V2 set |
| 932 | * it might belong to either a filesystem which has |
| 933 | * disks with completed fsid change or it might belong |
| 934 | * to fs with no UUID changes in effect, handle both. |
| 935 | */ |
| 936 | fs_devices = find_fsid_inprogress(disk_super); |
| 937 | if (!fs_devices) |
| 938 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 939 | } else { |
| 940 | fs_devices = find_fsid_changed(disk_super); |
| 941 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 942 | } else if (has_metadata_uuid) { |
| 943 | fs_devices = find_fsid(disk_super->fsid, |
| 944 | disk_super->metadata_uuid); |
| 945 | } else { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 946 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 949 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 950 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 951 | if (has_metadata_uuid) |
| 952 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 953 | disk_super->metadata_uuid); |
| 954 | else |
| 955 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 956 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 957 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 958 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 959 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 960 | fs_devices->fsid_change = fsid_change_in_progress; |
| 961 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 962 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 963 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 964 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 965 | device = NULL; |
| 966 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 967 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 968 | device = btrfs_find_device(fs_devices, devid, |
| 969 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 970 | |
| 971 | /* |
| 972 | * If this disk has been pulled into an fs devices created by |
| 973 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 974 | * metadata_uuid/fsid values of the fs_devices. |
| 975 | */ |
| 976 | if (has_metadata_uuid && fs_devices->fsid_change && |
| 977 | found_transid > fs_devices->latest_generation) { |
| 978 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 979 | BTRFS_FSID_SIZE); |
| 980 | memcpy(fs_devices->metadata_uuid, |
| 981 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 982 | |
| 983 | fs_devices->fsid_change = false; |
| 984 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 985 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 986 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 987 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 988 | if (fs_devices->opened) { |
| 989 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 990 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 991 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 992 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 993 | device = btrfs_alloc_device(NULL, &devid, |
| 994 | disk_super->dev_item.uuid); |
| 995 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 996 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 997 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 998 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 999 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1000 | |
| 1001 | name = rcu_string_strdup(path, GFP_NOFS); |
| 1002 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1003 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1004 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1005 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1006 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1007 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 1008 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1009 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1010 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1011 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1012 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1013 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1014 | |
| 1015 | if (disk_super->label[0]) |
| 1016 | pr_info("BTRFS: device label %s devid %llu transid %llu %s\n", |
| 1017 | disk_super->label, devid, found_transid, path); |
| 1018 | else |
| 1019 | pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n", |
| 1020 | disk_super->fsid, devid, found_transid, path); |
| 1021 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1022 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1023 | /* |
| 1024 | * When FS is already mounted. |
| 1025 | * 1. If you are here and if the device->name is NULL that |
| 1026 | * means this device was missing at time of FS mount. |
| 1027 | * 2. If you are here and if the device->name is different |
| 1028 | * from 'path' that means either |
| 1029 | * a. The same device disappeared and reappeared with |
| 1030 | * different name. or |
| 1031 | * b. The missing-disk-which-was-replaced, has |
| 1032 | * reappeared now. |
| 1033 | * |
| 1034 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 1035 | * and unintentional. |
| 1036 | * |
| 1037 | * Further in case of 1 and 2a above, the disk at 'path' |
| 1038 | * would have missed some transaction when it was away and |
| 1039 | * in case of 2a the stale bdev has to be updated as well. |
| 1040 | * 2b must not be allowed at all time. |
| 1041 | */ |
| 1042 | |
| 1043 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1044 | * For now, we do allow update to btrfs_fs_device through the |
| 1045 | * btrfs dev scan cli after FS has been mounted. We're still |
| 1046 | * tracking a problem where systems fail mount by subvolume id |
| 1047 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1048 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1049 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1050 | /* |
| 1051 | * That is if the FS is _not_ mounted and if you |
| 1052 | * are here, that means there is more than one |
| 1053 | * disk with same uuid and devid.We keep the one |
| 1054 | * with larger generation number or the last-in if |
| 1055 | * generation are equal. |
| 1056 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1057 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1058 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1059 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1060 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 1061 | /* |
| 1062 | * We are going to replace the device path for a given devid, |
| 1063 | * make sure it's the same device if the device is mounted |
| 1064 | */ |
| 1065 | if (device->bdev) { |
| 1066 | struct block_device *path_bdev; |
| 1067 | |
| 1068 | path_bdev = lookup_bdev(path); |
| 1069 | if (IS_ERR(path_bdev)) { |
| 1070 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1071 | return ERR_CAST(path_bdev); |
| 1072 | } |
| 1073 | |
| 1074 | if (device->bdev != path_bdev) { |
| 1075 | bdput(path_bdev); |
| 1076 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1077 | btrfs_warn_in_rcu(device->fs_info, |
| 1078 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 1079 | disk_super->fsid, devid, |
| 1080 | rcu_str_deref(device->name), path); |
| 1081 | return ERR_PTR(-EEXIST); |
| 1082 | } |
| 1083 | bdput(path_bdev); |
| 1084 | btrfs_info_in_rcu(device->fs_info, |
| 1085 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 1086 | disk_super->fsid, devid, |
| 1087 | rcu_str_deref(device->name), path); |
| 1088 | } |
| 1089 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1090 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1091 | if (!name) { |
| 1092 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1093 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1094 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1095 | rcu_string_free(device->name); |
| 1096 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1097 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1098 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1099 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1100 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1103 | /* |
| 1104 | * Unmount does not free the btrfs_device struct but would zero |
| 1105 | * generation along with most of the other members. So just update |
| 1106 | * it back. We need it to pick the disk with largest generation |
| 1107 | * (as above). |
| 1108 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1109 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1110 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1111 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 1112 | fs_devices->latest_generation); |
| 1113 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1114 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 1115 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 1116 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1117 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1118 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1119 | } |
| 1120 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1121 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 1122 | { |
| 1123 | struct btrfs_fs_devices *fs_devices; |
| 1124 | struct btrfs_device *device; |
| 1125 | struct btrfs_device *orig_dev; |
| 1126 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 1127 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1128 | if (IS_ERR(fs_devices)) |
| 1129 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1130 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1131 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1132 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1133 | |
| 1134 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1135 | struct rcu_string *name; |
| 1136 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 1137 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 1138 | orig_dev->uuid); |
| 1139 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1140 | goto error; |
| 1141 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1142 | /* |
| 1143 | * This is ok to do without rcu read locked because we hold the |
| 1144 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1145 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1146 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1147 | name = rcu_string_strdup(orig_dev->name->str, |
| 1148 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1149 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1150 | btrfs_free_device(device); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1151 | goto error; |
| 1152 | } |
| 1153 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1154 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1155 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1156 | list_add(&device->dev_list, &fs_devices->devices); |
| 1157 | device->fs_devices = fs_devices; |
| 1158 | fs_devices->num_devices++; |
| 1159 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1160 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1161 | return fs_devices; |
| 1162 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1163 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1164 | free_fs_devices(fs_devices); |
| 1165 | return ERR_PTR(-ENOMEM); |
| 1166 | } |
| 1167 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1168 | /* |
| 1169 | * After we have read the system tree and know devids belonging to |
| 1170 | * this filesystem, remove the device which does not belong there. |
| 1171 | */ |
| 1172 | 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] | 1173 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1174 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1175 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1176 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1177 | mutex_lock(&uuid_mutex); |
| 1178 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1179 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1180 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1181 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1182 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1183 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1184 | &device->dev_state) && |
| 1185 | (!latest_dev || |
| 1186 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1187 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1188 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1189 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1190 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1191 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1192 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1193 | /* |
| 1194 | * In the first step, keep the device which has |
| 1195 | * the correct fsid and the devid that is used |
| 1196 | * for the dev_replace procedure. |
| 1197 | * In the second step, the dev_replace state is |
| 1198 | * read from the device tree and it is known |
| 1199 | * whether the procedure is really active or |
| 1200 | * not, which means whether this device is |
| 1201 | * used or whether it should be removed. |
| 1202 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1203 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1204 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1205 | continue; |
| 1206 | } |
| 1207 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1208 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1209 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1210 | device->bdev = NULL; |
| 1211 | fs_devices->open_devices--; |
| 1212 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1213 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1214 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1215 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1216 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1217 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1218 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1219 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1220 | list_del_init(&device->dev_list); |
| 1221 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1222 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1223 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1224 | |
| 1225 | if (fs_devices->seed) { |
| 1226 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1227 | goto again; |
| 1228 | } |
| 1229 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1230 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1231 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1232 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1233 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1234 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1235 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1236 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1237 | if (!device->bdev) |
| 1238 | return; |
| 1239 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1240 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1241 | sync_blockdev(device->bdev); |
| 1242 | invalidate_bdev(device->bdev); |
| 1243 | } |
| 1244 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1245 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1248 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1249 | { |
| 1250 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1251 | struct btrfs_device *new_device; |
| 1252 | struct rcu_string *name; |
| 1253 | |
| 1254 | if (device->bdev) |
| 1255 | fs_devices->open_devices--; |
| 1256 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1257 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1258 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1259 | list_del_init(&device->dev_alloc_list); |
| 1260 | fs_devices->rw_devices--; |
| 1261 | } |
| 1262 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1263 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1264 | fs_devices->missing_devices--; |
| 1265 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1266 | btrfs_close_bdev(device); |
| 1267 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1268 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1269 | device->uuid); |
| 1270 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1271 | |
| 1272 | /* Safe because we are under uuid_mutex */ |
| 1273 | if (device->name) { |
| 1274 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1275 | BUG_ON(!name); /* -ENOMEM */ |
| 1276 | rcu_assign_pointer(new_device->name, name); |
| 1277 | } |
| 1278 | |
| 1279 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1280 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1281 | |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 1282 | synchronize_rcu(); |
| 1283 | btrfs_free_device(device); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1286 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1287 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1288 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1289 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1290 | if (--fs_devices->opened > 0) |
| 1291 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1292 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1293 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1294 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1295 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1296 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1297 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1298 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1299 | WARN_ON(fs_devices->open_devices); |
| 1300 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1301 | fs_devices->opened = 0; |
| 1302 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1303 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1307 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1308 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1309 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1310 | int ret; |
| 1311 | |
| 1312 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1313 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1314 | if (!fs_devices->opened) { |
| 1315 | seed_devices = fs_devices->seed; |
| 1316 | fs_devices->seed = NULL; |
| 1317 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1318 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1319 | |
| 1320 | while (seed_devices) { |
| 1321 | fs_devices = seed_devices; |
| 1322 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1323 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1324 | free_fs_devices(fs_devices); |
| 1325 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1326 | return ret; |
| 1327 | } |
| 1328 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1329 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1330 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1331 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1332 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1333 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1334 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1335 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1336 | flags |= FMODE_EXCL; |
| 1337 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1338 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1339 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1340 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1341 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1342 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1343 | if (!latest_dev || |
| 1344 | device->generation > latest_dev->generation) |
| 1345 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1346 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1347 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1348 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1349 | goto out; |
| 1350 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1351 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1352 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1353 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1354 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1355 | return ret; |
| 1356 | } |
| 1357 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1358 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1359 | { |
| 1360 | struct btrfs_device *dev1, *dev2; |
| 1361 | |
| 1362 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1363 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1364 | |
| 1365 | if (dev1->devid < dev2->devid) |
| 1366 | return -1; |
| 1367 | else if (dev1->devid > dev2->devid) |
| 1368 | return 1; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1372 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1373 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1374 | { |
| 1375 | int ret; |
| 1376 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1377 | lockdep_assert_held(&uuid_mutex); |
| 1378 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1379 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1380 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1381 | fs_devices->opened++; |
| 1382 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1383 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1384 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1385 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1386 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1387 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1388 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1389 | return ret; |
| 1390 | } |
| 1391 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1392 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1393 | { |
| 1394 | kunmap(page); |
| 1395 | put_page(page); |
| 1396 | } |
| 1397 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1398 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1399 | struct page **page, |
| 1400 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1401 | { |
| 1402 | void *p; |
| 1403 | pgoff_t index; |
| 1404 | |
| 1405 | /* make sure our super fits in the device */ |
| 1406 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1407 | return 1; |
| 1408 | |
| 1409 | /* make sure our super fits in the page */ |
| 1410 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1411 | return 1; |
| 1412 | |
| 1413 | /* make sure our super doesn't straddle pages on disk */ |
| 1414 | index = bytenr >> PAGE_SHIFT; |
| 1415 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1416 | return 1; |
| 1417 | |
| 1418 | /* pull in the page with our super */ |
| 1419 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1420 | index, GFP_KERNEL); |
| 1421 | |
| 1422 | if (IS_ERR_OR_NULL(*page)) |
| 1423 | return 1; |
| 1424 | |
| 1425 | p = kmap(*page); |
| 1426 | |
| 1427 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1428 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1429 | |
| 1430 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1431 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1432 | btrfs_release_disk_super(*page); |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
| 1436 | if ((*disk_super)->label[0] && |
| 1437 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1438 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1443 | int btrfs_forget_devices(const char *path) |
| 1444 | { |
| 1445 | int ret; |
| 1446 | |
| 1447 | mutex_lock(&uuid_mutex); |
| 1448 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1449 | mutex_unlock(&uuid_mutex); |
| 1450 | |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1456 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1457 | * is read via pagecache |
| 1458 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1459 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1460 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1461 | { |
| 1462 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1463 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1464 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1465 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1466 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1467 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1468 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1469 | lockdep_assert_held(&uuid_mutex); |
| 1470 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1471 | /* |
| 1472 | * we would like to check all the supers, but that would make |
| 1473 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1474 | * So, we need to add a special mount option to scan for |
| 1475 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1476 | */ |
| 1477 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1478 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1479 | |
| 1480 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1481 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1482 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1483 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1484 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1485 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1486 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1487 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1488 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1489 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1490 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1491 | if (new_device_added) |
| 1492 | btrfs_free_stale_devices(path, device); |
| 1493 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1494 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1495 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1496 | |
| 1497 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1498 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1499 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1500 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1501 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1502 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1503 | /* |
| 1504 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1505 | * such is found, record the end of it in *start |
| 1506 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1507 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1508 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1509 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1510 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1511 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1512 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1513 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1514 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1515 | &physical_start, &physical_end, |
| 1516 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1517 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1518 | if (in_range(physical_start, *start, len) || |
| 1519 | in_range(*start, physical_start, |
| 1520 | physical_end - physical_start)) { |
| 1521 | *start = physical_end + 1; |
| 1522 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1523 | } |
| 1524 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1525 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1529 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1530 | * find_free_dev_extent_start - find free space in the specified device |
| 1531 | * @device: the device which we search the free space in |
| 1532 | * @num_bytes: the size of the free space that we need |
| 1533 | * @search_start: the position from which to begin the search |
| 1534 | * @start: store the start of the free space. |
| 1535 | * @len: the size of the free space. that we find, or the size |
| 1536 | * 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] | 1537 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1538 | * this uses a pretty simple search, the expectation is that it is |
| 1539 | * called very infrequently and that a given device has a small number |
| 1540 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1541 | * |
| 1542 | * @start is used to store the start of the free space if we find. But if we |
| 1543 | * don't find suitable free space, it will be used to store the start position |
| 1544 | * of the max free space. |
| 1545 | * |
| 1546 | * @len is used to store the size of the free space that we find. |
| 1547 | * But if we don't find suitable free space, it is used to store the size of |
| 1548 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1549 | */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1550 | int find_free_dev_extent_start(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1551 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1552 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1553 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1554 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1555 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1556 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1557 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1558 | u64 hole_size; |
| 1559 | u64 max_hole_start; |
| 1560 | u64 max_hole_size; |
| 1561 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1562 | u64 search_end = device->total_bytes; |
| 1563 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1564 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1565 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * We don't want to overwrite the superblock on the drive nor any area |
| 1569 | * used by the boot loader (grub for example), so we make sure to start |
| 1570 | * at an offset of at least 1MB. |
| 1571 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1572 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1573 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1574 | path = btrfs_alloc_path(); |
| 1575 | if (!path) |
| 1576 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1577 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1578 | max_hole_start = search_start; |
| 1579 | max_hole_size = 0; |
| 1580 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1581 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1582 | if (search_start >= search_end || |
| 1583 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1584 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1585 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1588 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1589 | path->search_commit_root = 1; |
| 1590 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1591 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1592 | key.objectid = device->devid; |
| 1593 | key.offset = search_start; |
| 1594 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1595 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1596 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1597 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1598 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1599 | if (ret > 0) { |
| 1600 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1601 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1602 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1603 | } |
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 | while (1) { |
| 1606 | l = path->nodes[0]; |
| 1607 | slot = path->slots[0]; |
| 1608 | if (slot >= btrfs_header_nritems(l)) { |
| 1609 | ret = btrfs_next_leaf(root, path); |
| 1610 | if (ret == 0) |
| 1611 | continue; |
| 1612 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1613 | goto out; |
| 1614 | |
| 1615 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1616 | } |
| 1617 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1618 | |
| 1619 | if (key.objectid < device->devid) |
| 1620 | goto next; |
| 1621 | |
| 1622 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1623 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1624 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1625 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1626 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1627 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1628 | if (key.offset > search_start) { |
| 1629 | hole_size = key.offset - search_start; |
| 1630 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1631 | /* |
| 1632 | * Have to check before we set max_hole_start, otherwise |
| 1633 | * we could end up sending back this offset anyway. |
| 1634 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1635 | if (contains_pending_extent(device, &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1636 | hole_size)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1637 | if (key.offset >= search_start) |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1638 | hole_size = key.offset - search_start; |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1639 | else |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1640 | hole_size = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1641 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1642 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1643 | if (hole_size > max_hole_size) { |
| 1644 | max_hole_start = search_start; |
| 1645 | max_hole_size = hole_size; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * If this free space is greater than which we need, |
| 1650 | * it must be the max free space that we have found |
| 1651 | * until now, so max_hole_start must point to the start |
| 1652 | * of this free space and the length of this free space |
| 1653 | * is stored in max_hole_size. Thus, we return |
| 1654 | * max_hole_start and max_hole_size and go back to the |
| 1655 | * caller. |
| 1656 | */ |
| 1657 | if (hole_size >= num_bytes) { |
| 1658 | ret = 0; |
| 1659 | goto out; |
| 1660 | } |
| 1661 | } |
| 1662 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1663 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1664 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1665 | dev_extent); |
| 1666 | if (extent_end > search_start) |
| 1667 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1668 | next: |
| 1669 | path->slots[0]++; |
| 1670 | cond_resched(); |
| 1671 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1672 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1673 | /* |
| 1674 | * At this point, search_start should be the end of |
| 1675 | * allocated dev extents, and when shrinking the device, |
| 1676 | * search_end may be smaller than search_start. |
| 1677 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1678 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1679 | hole_size = search_end - search_start; |
| 1680 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1681 | if (contains_pending_extent(device, &search_start, hole_size)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1682 | btrfs_release_path(path); |
| 1683 | goto again; |
| 1684 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1685 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1686 | if (hole_size > max_hole_size) { |
| 1687 | max_hole_start = search_start; |
| 1688 | max_hole_size = hole_size; |
| 1689 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1690 | } |
| 1691 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1692 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1693 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1694 | ret = -ENOSPC; |
| 1695 | else |
| 1696 | ret = 0; |
| 1697 | |
| 1698 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1699 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1700 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1701 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1702 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1703 | return ret; |
| 1704 | } |
| 1705 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1706 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1707 | u64 *start, u64 *len) |
| 1708 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1709 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1710 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1711 | } |
| 1712 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1713 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1714 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1715 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1716 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1717 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1718 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1719 | int ret; |
| 1720 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1721 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1722 | struct btrfs_key found_key; |
| 1723 | struct extent_buffer *leaf = NULL; |
| 1724 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1725 | |
| 1726 | path = btrfs_alloc_path(); |
| 1727 | if (!path) |
| 1728 | return -ENOMEM; |
| 1729 | |
| 1730 | key.objectid = device->devid; |
| 1731 | key.offset = start; |
| 1732 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1733 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1734 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1735 | if (ret > 0) { |
| 1736 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1737 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1738 | if (ret) |
| 1739 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1740 | leaf = path->nodes[0]; |
| 1741 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1742 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1743 | struct btrfs_dev_extent); |
| 1744 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1745 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1746 | key = found_key; |
| 1747 | btrfs_release_path(path); |
| 1748 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1749 | } else if (ret == 0) { |
| 1750 | leaf = path->nodes[0]; |
| 1751 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1752 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1753 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1754 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1755 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1756 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1757 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1758 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1759 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1760 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1761 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1762 | btrfs_handle_fs_error(fs_info, ret, |
| 1763 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1764 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1765 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1766 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1767 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1768 | btrfs_free_path(path); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1772 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1773 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1774 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1775 | { |
| 1776 | int ret; |
| 1777 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1778 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1779 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1780 | struct btrfs_dev_extent *extent; |
| 1781 | struct extent_buffer *leaf; |
| 1782 | struct btrfs_key key; |
| 1783 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1784 | 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] | 1785 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1786 | path = btrfs_alloc_path(); |
| 1787 | if (!path) |
| 1788 | return -ENOMEM; |
| 1789 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1790 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1791 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1792 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1793 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1794 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1795 | if (ret) |
| 1796 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1797 | |
| 1798 | leaf = path->nodes[0]; |
| 1799 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1800 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1801 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1802 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1803 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1804 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1805 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1806 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1807 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1808 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1809 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1810 | btrfs_free_path(path); |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1814 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1815 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1816 | struct extent_map_tree *em_tree; |
| 1817 | struct extent_map *em; |
| 1818 | struct rb_node *n; |
| 1819 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1820 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1821 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1822 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1823 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1824 | if (n) { |
| 1825 | em = rb_entry(n, struct extent_map, rb_node); |
| 1826 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1827 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1828 | read_unlock(&em_tree->lock); |
| 1829 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1830 | return ret; |
| 1831 | } |
| 1832 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1833 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1834 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1835 | { |
| 1836 | int ret; |
| 1837 | struct btrfs_key key; |
| 1838 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1839 | struct btrfs_path *path; |
| 1840 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1841 | path = btrfs_alloc_path(); |
| 1842 | if (!path) |
| 1843 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1844 | |
| 1845 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1846 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1847 | key.offset = (u64)-1; |
| 1848 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1849 | 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] | 1850 | if (ret < 0) |
| 1851 | goto error; |
| 1852 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1853 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1854 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1855 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1856 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1857 | BTRFS_DEV_ITEM_KEY); |
| 1858 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1859 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1860 | } else { |
| 1861 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1862 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1863 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1864 | } |
| 1865 | ret = 0; |
| 1866 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1867 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1868 | return ret; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * the device information is stored in the chunk root |
| 1873 | * the btrfs_device struct should be fully filled in |
| 1874 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1875 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1876 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1877 | { |
| 1878 | int ret; |
| 1879 | struct btrfs_path *path; |
| 1880 | struct btrfs_dev_item *dev_item; |
| 1881 | struct extent_buffer *leaf; |
| 1882 | struct btrfs_key key; |
| 1883 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1884 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1885 | path = btrfs_alloc_path(); |
| 1886 | if (!path) |
| 1887 | return -ENOMEM; |
| 1888 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1889 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1890 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1891 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1892 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1893 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1894 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1895 | if (ret) |
| 1896 | goto out; |
| 1897 | |
| 1898 | leaf = path->nodes[0]; |
| 1899 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1900 | |
| 1901 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1902 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1903 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1904 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1905 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1906 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1907 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1908 | btrfs_device_get_disk_total_bytes(device)); |
| 1909 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1910 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1911 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1912 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1913 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1914 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1915 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1916 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1917 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1918 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1919 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1920 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1921 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1922 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1923 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1924 | out: |
| 1925 | btrfs_free_path(path); |
| 1926 | return ret; |
| 1927 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1928 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1929 | /* |
| 1930 | * Function to update ctime/mtime for a given device path. |
| 1931 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1932 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1933 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1934 | { |
| 1935 | struct file *filp; |
| 1936 | |
| 1937 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1938 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1939 | return; |
| 1940 | file_update_time(filp); |
| 1941 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1942 | } |
| 1943 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1944 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1945 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1946 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1947 | int ret; |
| 1948 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1949 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1950 | struct btrfs_trans_handle *trans; |
| 1951 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1952 | path = btrfs_alloc_path(); |
| 1953 | if (!path) |
| 1954 | return -ENOMEM; |
| 1955 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1956 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1957 | if (IS_ERR(trans)) { |
| 1958 | btrfs_free_path(path); |
| 1959 | return PTR_ERR(trans); |
| 1960 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1961 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1962 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1963 | key.offset = device->devid; |
| 1964 | |
| 1965 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1966 | if (ret) { |
| 1967 | if (ret > 0) |
| 1968 | ret = -ENOENT; |
| 1969 | btrfs_abort_transaction(trans, ret); |
| 1970 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1971 | goto out; |
| 1972 | } |
| 1973 | |
| 1974 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1975 | if (ret) { |
| 1976 | btrfs_abort_transaction(trans, ret); |
| 1977 | btrfs_end_transaction(trans); |
| 1978 | } |
| 1979 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1980 | out: |
| 1981 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1982 | if (!ret) |
| 1983 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1984 | return ret; |
| 1985 | } |
| 1986 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1987 | /* |
| 1988 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1989 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1990 | * replace. |
| 1991 | */ |
| 1992 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1993 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1994 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1995 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1996 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1997 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1998 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1999 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2000 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2001 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2002 | all_avail = fs_info->avail_data_alloc_bits | |
| 2003 | fs_info->avail_system_alloc_bits | |
| 2004 | fs_info->avail_metadata_alloc_bits; |
| 2005 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2006 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2007 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 2008 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2009 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2010 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2011 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 2012 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2013 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2014 | if (ret) |
| 2015 | return ret; |
| 2016 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2020 | } |
| 2021 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 2022 | static struct btrfs_device * btrfs_find_next_active_device( |
| 2023 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2024 | { |
| 2025 | struct btrfs_device *next_device; |
| 2026 | |
| 2027 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 2028 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2029 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 2030 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2031 | return next_device; |
| 2032 | } |
| 2033 | |
| 2034 | return NULL; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
| 2038 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 2039 | * and replace it with the provided or the next active device, in the context |
| 2040 | * where this function called, there should be always be another device (or |
| 2041 | * this_dev) which is active. |
| 2042 | */ |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2043 | void btrfs_assign_next_active_device(struct btrfs_device *device, |
| 2044 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2045 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2046 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2047 | struct btrfs_device *next_device; |
| 2048 | |
| 2049 | if (this_dev) |
| 2050 | next_device = this_dev; |
| 2051 | else |
| 2052 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 2053 | device); |
| 2054 | ASSERT(next_device); |
| 2055 | |
| 2056 | if (fs_info->sb->s_bdev && |
| 2057 | (fs_info->sb->s_bdev == device->bdev)) |
| 2058 | fs_info->sb->s_bdev = next_device->bdev; |
| 2059 | |
| 2060 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 2061 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2062 | } |
| 2063 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2064 | /* |
| 2065 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 2066 | * currently replaced. |
| 2067 | */ |
| 2068 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 2069 | { |
| 2070 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 2071 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2072 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2073 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 2074 | ASSERT(num_devices > 1); |
| 2075 | num_devices--; |
| 2076 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2077 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2078 | |
| 2079 | return num_devices; |
| 2080 | } |
| 2081 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2082 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2083 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2084 | { |
| 2085 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2086 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2087 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2088 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2089 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2090 | |
| 2091 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2092 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2093 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2094 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2095 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2096 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2097 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2098 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2099 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2100 | |
| 2101 | if (IS_ERR(device)) { |
| 2102 | if (PTR_ERR(device) == -ENOENT && |
| 2103 | strcmp(device_path, "missing") == 0) |
| 2104 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2105 | else |
| 2106 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2107 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2108 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2109 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2110 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2111 | btrfs_warn_in_rcu(fs_info, |
| 2112 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2113 | rcu_str_deref(device->name), device->devid); |
| 2114 | ret = -ETXTBSY; |
| 2115 | goto out; |
| 2116 | } |
| 2117 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2118 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2119 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2120 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2123 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2124 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2125 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2126 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2127 | } |
| 2128 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2129 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2130 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2131 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2132 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2133 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2134 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2135 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2136 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2137 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2138 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2139 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2140 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2141 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2142 | /* |
| 2143 | * TODO: the superblock still includes this device in its num_devices |
| 2144 | * counter although write_all_supers() is not locked out. This |
| 2145 | * could give a filesystem state which requires a degraded mount. |
| 2146 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2147 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2148 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2149 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2150 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2151 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2152 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2153 | |
| 2154 | /* |
| 2155 | * the device list mutex makes sure that we don't change |
| 2156 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2157 | * the device supers. Whoever is writing all supers, should |
| 2158 | * lock the device list mutex before getting the number of |
| 2159 | * devices in the super block (super_copy). Conversely, |
| 2160 | * whoever updates the number of devices in the super block |
| 2161 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2162 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2163 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2164 | /* |
| 2165 | * In normal cases the cur_devices == fs_devices. But in case |
| 2166 | * of deleting a seed device, the cur_devices should point to |
| 2167 | * its own fs_devices listed under the fs_devices->seed. |
| 2168 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2169 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2170 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2171 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2172 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2173 | cur_devices->num_devices--; |
| 2174 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2175 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2176 | if (cur_devices != fs_devices) |
| 2177 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2178 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2179 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2180 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2181 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2182 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2183 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2184 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2185 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2186 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2187 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2188 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2189 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2190 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2191 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2192 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2193 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2194 | /* |
| 2195 | * at this point, the device is zero sized and detached from |
| 2196 | * the devices list. All that's left is to zero out the old |
| 2197 | * supers and free the device. |
| 2198 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2199 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2200 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2201 | |
| 2202 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2203 | synchronize_rcu(); |
| 2204 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2205 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2206 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2207 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2208 | if (fs_devices->seed == cur_devices) { |
| 2209 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2210 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2211 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2212 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2213 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2214 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2215 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2216 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2217 | } |
| 2218 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2219 | out: |
| 2220 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2221 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2222 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2223 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2224 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2225 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2226 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2227 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2228 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2229 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2230 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2231 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2232 | } |
| 2233 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2234 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2235 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2236 | struct btrfs_fs_devices *fs_devices; |
| 2237 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2238 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2239 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2240 | /* |
| 2241 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2242 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2243 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2244 | * srcdev will have its correct fs_devices in both the cases. |
| 2245 | */ |
| 2246 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2247 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2248 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2249 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2250 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2251 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2252 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2253 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2254 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2255 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2256 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2257 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2258 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2259 | } |
| 2260 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2261 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2262 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2263 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2264 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2265 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2266 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2267 | /* zero out the old super if it is writable */ |
| 2268 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2269 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2270 | |
| 2271 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2272 | synchronize_rcu(); |
| 2273 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2274 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2275 | /* if this is no devs we rather delete the fs_devices */ |
| 2276 | if (!fs_devices->num_devices) { |
| 2277 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2278 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2279 | /* |
| 2280 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2281 | * seed. In case of a seed device being replaced, the replace |
| 2282 | * target added to the sprout FS, so there will be no more |
| 2283 | * device left under the seed FS. |
| 2284 | */ |
| 2285 | ASSERT(fs_devices->seeding); |
| 2286 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2287 | tmp_fs_devices = fs_info->fs_devices; |
| 2288 | while (tmp_fs_devices) { |
| 2289 | if (tmp_fs_devices->seed == fs_devices) { |
| 2290 | tmp_fs_devices->seed = fs_devices->seed; |
| 2291 | break; |
| 2292 | } |
| 2293 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2294 | } |
| 2295 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2296 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2297 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2298 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2301 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2302 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2303 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2304 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2305 | WARN_ON(!tgtdev); |
| 2306 | mutex_lock(&fs_devices->device_list_mutex); |
| 2307 | |
| 2308 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2309 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2310 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2311 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2312 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2313 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2314 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2315 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2316 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2317 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2318 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2319 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2320 | |
| 2321 | /* |
| 2322 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2323 | * may lead to a call to btrfs_show_devname() which will try |
| 2324 | * to hold device_list_mutex. And here this device |
| 2325 | * is already out of device list, so we don't have to hold |
| 2326 | * the device_list_mutex lock. |
| 2327 | */ |
| 2328 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2329 | |
| 2330 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2331 | synchronize_rcu(); |
| 2332 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2335 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2336 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2337 | { |
| 2338 | int ret = 0; |
| 2339 | struct btrfs_super_block *disk_super; |
| 2340 | u64 devid; |
| 2341 | u8 *dev_uuid; |
| 2342 | struct block_device *bdev; |
| 2343 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2344 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2345 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2346 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2347 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2348 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2349 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2350 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2351 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2352 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2353 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2354 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2355 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2356 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2357 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2358 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2359 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2360 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2361 | if (!device) |
| 2362 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2363 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2364 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2367 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2368 | * Lookup a device given by device id, or the path if the id is 0. |
| 2369 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2370 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2371 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2372 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2373 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2374 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2375 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2376 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2377 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2378 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2379 | if (!device) |
| 2380 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2381 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2382 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2383 | |
| 2384 | if (!device_path || !device_path[0]) |
| 2385 | return ERR_PTR(-EINVAL); |
| 2386 | |
| 2387 | if (strcmp(device_path, "missing") == 0) { |
| 2388 | /* Find first missing device */ |
| 2389 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2390 | dev_list) { |
| 2391 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2392 | &device->dev_state) && !device->bdev) |
| 2393 | return device; |
| 2394 | } |
| 2395 | return ERR_PTR(-ENOENT); |
| 2396 | } |
| 2397 | |
| 2398 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2399 | } |
| 2400 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2401 | /* |
| 2402 | * does all the dirty work required for changing file system's UUID. |
| 2403 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2404 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2405 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2406 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2407 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2408 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2409 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2410 | struct btrfs_device *device; |
| 2411 | u64 super_flags; |
| 2412 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2413 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2414 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2415 | return -EINVAL; |
| 2416 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2417 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2418 | if (IS_ERR(seed_devices)) |
| 2419 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2420 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2421 | old_devices = clone_fs_devices(fs_devices); |
| 2422 | if (IS_ERR(old_devices)) { |
| 2423 | kfree(seed_devices); |
| 2424 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2425 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2426 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2427 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2428 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2429 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2430 | seed_devices->opened = 1; |
| 2431 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2432 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2433 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2434 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2435 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2436 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2437 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2438 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2439 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2440 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2441 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2442 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2443 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2444 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2445 | fs_devices->seeding = 0; |
| 2446 | fs_devices->num_devices = 0; |
| 2447 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2448 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2449 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2450 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2451 | |
| 2452 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2453 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2454 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2455 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2456 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2457 | super_flags = btrfs_super_flags(disk_super) & |
| 2458 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2459 | btrfs_set_super_flags(disk_super, super_flags); |
| 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
| 2464 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2465 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2466 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2467 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2468 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2469 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2470 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2471 | struct btrfs_path *path; |
| 2472 | struct extent_buffer *leaf; |
| 2473 | struct btrfs_dev_item *dev_item; |
| 2474 | struct btrfs_device *device; |
| 2475 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2476 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2477 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2478 | u64 devid; |
| 2479 | int ret; |
| 2480 | |
| 2481 | path = btrfs_alloc_path(); |
| 2482 | if (!path) |
| 2483 | return -ENOMEM; |
| 2484 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2485 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2486 | key.offset = 0; |
| 2487 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2488 | |
| 2489 | while (1) { |
| 2490 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2491 | if (ret < 0) |
| 2492 | goto error; |
| 2493 | |
| 2494 | leaf = path->nodes[0]; |
| 2495 | next_slot: |
| 2496 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2497 | ret = btrfs_next_leaf(root, path); |
| 2498 | if (ret > 0) |
| 2499 | break; |
| 2500 | if (ret < 0) |
| 2501 | goto error; |
| 2502 | leaf = path->nodes[0]; |
| 2503 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2504 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2505 | continue; |
| 2506 | } |
| 2507 | |
| 2508 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2509 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2510 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2511 | break; |
| 2512 | |
| 2513 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2514 | struct btrfs_dev_item); |
| 2515 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2516 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2517 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2518 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2519 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2520 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2521 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2522 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2523 | |
| 2524 | if (device->fs_devices->seeding) { |
| 2525 | btrfs_set_device_generation(leaf, dev_item, |
| 2526 | device->generation); |
| 2527 | btrfs_mark_buffer_dirty(leaf); |
| 2528 | } |
| 2529 | |
| 2530 | path->slots[0]++; |
| 2531 | goto next_slot; |
| 2532 | } |
| 2533 | ret = 0; |
| 2534 | error: |
| 2535 | btrfs_free_path(path); |
| 2536 | return ret; |
| 2537 | } |
| 2538 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2539 | 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] | 2540 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2541 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2542 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2543 | struct btrfs_trans_handle *trans; |
| 2544 | struct btrfs_device *device; |
| 2545 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2546 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2547 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2548 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2549 | u64 orig_super_total_bytes; |
| 2550 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2551 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2552 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2553 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2554 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2555 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2556 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2557 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2558 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2559 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2560 | if (IS_ERR(bdev)) |
| 2561 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2562 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2563 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2564 | seeding_dev = 1; |
| 2565 | down_write(&sb->s_umount); |
| 2566 | mutex_lock(&uuid_mutex); |
| 2567 | } |
| 2568 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2569 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2570 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2571 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2572 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2573 | if (device->bdev == bdev) { |
| 2574 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2575 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2576 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2577 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2578 | } |
| 2579 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2580 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2581 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2582 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2583 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2584 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2585 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2586 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2587 | } |
| 2588 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2589 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2590 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2591 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2592 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2593 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2594 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2595 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2596 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2597 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2598 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2599 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2602 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2603 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2604 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2605 | device->io_width = fs_info->sectorsize; |
| 2606 | device->io_align = fs_info->sectorsize; |
| 2607 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2608 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2609 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2610 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2611 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2612 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2613 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2614 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2615 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2616 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2617 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2618 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2619 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2620 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2621 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2622 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2623 | if (ret) { |
| 2624 | btrfs_abort_transaction(trans, ret); |
| 2625 | goto error_trans; |
| 2626 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2627 | } |
| 2628 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2629 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2630 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2631 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2632 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2633 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2634 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2635 | fs_devices->num_devices++; |
| 2636 | fs_devices->open_devices++; |
| 2637 | fs_devices->rw_devices++; |
| 2638 | fs_devices->total_devices++; |
| 2639 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2640 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2641 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2642 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2643 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2644 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2645 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2646 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2647 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2648 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2649 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2650 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2651 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2652 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2653 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2654 | |
| 2655 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2656 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2657 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2658 | /* |
| 2659 | * we've got more storage, clear any full flags on the space |
| 2660 | * infos |
| 2661 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2662 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2663 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2664 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2665 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2666 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2667 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2668 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2669 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2670 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2671 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2672 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2673 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2674 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2675 | } |
| 2676 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2677 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2678 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2679 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2680 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | if (seeding_dev) { |
| 2684 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2685 | |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2686 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2687 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2688 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2689 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2690 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2691 | |
| 2692 | /* Sprouting would change fsid of the mounted root, |
| 2693 | * so rename the fsid on the sysfs |
| 2694 | */ |
| 2695 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 2696 | fs_info->fs_devices->fsid); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2697 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2698 | btrfs_warn(fs_info, |
| 2699 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2700 | } |
| 2701 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2702 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2703 | |
| 2704 | if (seeding_dev) { |
| 2705 | mutex_unlock(&uuid_mutex); |
| 2706 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2707 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2708 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2709 | if (ret) /* transaction commit */ |
| 2710 | return ret; |
| 2711 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2712 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2713 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2714 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2715 | "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] | 2716 | trans = btrfs_attach_transaction(root); |
| 2717 | if (IS_ERR(trans)) { |
| 2718 | if (PTR_ERR(trans) == -ENOENT) |
| 2719 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2720 | ret = PTR_ERR(trans); |
| 2721 | trans = NULL; |
| 2722 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2723 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2724 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2725 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2726 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2727 | /* Update ctime/mtime for libblkid */ |
| 2728 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2729 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2730 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2731 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2732 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2733 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2734 | mutex_lock(&fs_info->chunk_mutex); |
| 2735 | list_del_rcu(&device->dev_list); |
| 2736 | list_del(&device->dev_alloc_list); |
| 2737 | fs_info->fs_devices->num_devices--; |
| 2738 | fs_info->fs_devices->open_devices--; |
| 2739 | fs_info->fs_devices->rw_devices--; |
| 2740 | fs_info->fs_devices->total_devices--; |
| 2741 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2742 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2743 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2744 | orig_super_total_bytes); |
| 2745 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2746 | orig_super_num_devices); |
| 2747 | mutex_unlock(&fs_info->chunk_mutex); |
| 2748 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2749 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2750 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2751 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2752 | if (trans) |
| 2753 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2754 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2755 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2756 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2757 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2758 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2759 | mutex_unlock(&uuid_mutex); |
| 2760 | up_write(&sb->s_umount); |
| 2761 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2762 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2763 | } |
| 2764 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2765 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2766 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2767 | { |
| 2768 | int ret; |
| 2769 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2770 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2771 | struct btrfs_dev_item *dev_item; |
| 2772 | struct extent_buffer *leaf; |
| 2773 | struct btrfs_key key; |
| 2774 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2775 | path = btrfs_alloc_path(); |
| 2776 | if (!path) |
| 2777 | return -ENOMEM; |
| 2778 | |
| 2779 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2780 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2781 | key.offset = device->devid; |
| 2782 | |
| 2783 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2784 | if (ret < 0) |
| 2785 | goto out; |
| 2786 | |
| 2787 | if (ret > 0) { |
| 2788 | ret = -ENOENT; |
| 2789 | goto out; |
| 2790 | } |
| 2791 | |
| 2792 | leaf = path->nodes[0]; |
| 2793 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2794 | |
| 2795 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2796 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2797 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2798 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2799 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2800 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2801 | btrfs_device_get_disk_total_bytes(device)); |
| 2802 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2803 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2804 | btrfs_mark_buffer_dirty(leaf); |
| 2805 | |
| 2806 | out: |
| 2807 | btrfs_free_path(path); |
| 2808 | return ret; |
| 2809 | } |
| 2810 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2811 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2812 | struct btrfs_device *device, u64 new_size) |
| 2813 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2814 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2815 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2816 | u64 old_total; |
| 2817 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2818 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2819 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2820 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2821 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2822 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2823 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2824 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2825 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2826 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2827 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2828 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2829 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2830 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2831 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2832 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2833 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2834 | btrfs_set_super_total_bytes(super_copy, |
| 2835 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2836 | device->fs_devices->total_rw_bytes += diff; |
| 2837 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2838 | btrfs_device_set_total_bytes(device, new_size); |
| 2839 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2840 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2841 | if (list_empty(&device->post_commit_list)) |
| 2842 | list_add_tail(&device->post_commit_list, |
| 2843 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2844 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2845 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2846 | return btrfs_update_device(trans, device); |
| 2847 | } |
| 2848 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2849 | 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] | 2850 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2851 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2852 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2853 | int ret; |
| 2854 | struct btrfs_path *path; |
| 2855 | struct btrfs_key key; |
| 2856 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2857 | path = btrfs_alloc_path(); |
| 2858 | if (!path) |
| 2859 | return -ENOMEM; |
| 2860 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2861 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2862 | key.offset = chunk_offset; |
| 2863 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2864 | |
| 2865 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2866 | if (ret < 0) |
| 2867 | goto out; |
| 2868 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2869 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2870 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2871 | ret = -ENOENT; |
| 2872 | goto out; |
| 2873 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2874 | |
| 2875 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2876 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2877 | btrfs_handle_fs_error(fs_info, ret, |
| 2878 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2879 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2880 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2881 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2882 | } |
| 2883 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2884 | 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] | 2885 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2886 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2887 | struct btrfs_disk_key *disk_key; |
| 2888 | struct btrfs_chunk *chunk; |
| 2889 | u8 *ptr; |
| 2890 | int ret = 0; |
| 2891 | u32 num_stripes; |
| 2892 | u32 array_size; |
| 2893 | u32 len = 0; |
| 2894 | u32 cur; |
| 2895 | struct btrfs_key key; |
| 2896 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2897 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2898 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2899 | |
| 2900 | ptr = super_copy->sys_chunk_array; |
| 2901 | cur = 0; |
| 2902 | |
| 2903 | while (cur < array_size) { |
| 2904 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2905 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2906 | |
| 2907 | len = sizeof(*disk_key); |
| 2908 | |
| 2909 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2910 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2911 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2912 | len += btrfs_chunk_item_size(num_stripes); |
| 2913 | } else { |
| 2914 | ret = -EIO; |
| 2915 | break; |
| 2916 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2917 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2918 | key.offset == chunk_offset) { |
| 2919 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2920 | array_size -= len; |
| 2921 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2922 | } else { |
| 2923 | ptr += len; |
| 2924 | cur += len; |
| 2925 | } |
| 2926 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2927 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2928 | return ret; |
| 2929 | } |
| 2930 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2931 | /* |
| 2932 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2933 | * @logical: Logical block offset in bytes. |
| 2934 | * @length: Length of extent in bytes. |
| 2935 | * |
| 2936 | * Return: Chunk mapping or ERR_PTR. |
| 2937 | */ |
| 2938 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2939 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2940 | { |
| 2941 | struct extent_map_tree *em_tree; |
| 2942 | struct extent_map *em; |
| 2943 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2944 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2945 | read_lock(&em_tree->lock); |
| 2946 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2947 | read_unlock(&em_tree->lock); |
| 2948 | |
| 2949 | if (!em) { |
| 2950 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2951 | logical, length); |
| 2952 | return ERR_PTR(-EINVAL); |
| 2953 | } |
| 2954 | |
| 2955 | if (em->start > logical || em->start + em->len < logical) { |
| 2956 | btrfs_crit(fs_info, |
| 2957 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2958 | logical, length, em->start, em->start + em->len); |
| 2959 | free_extent_map(em); |
| 2960 | return ERR_PTR(-EINVAL); |
| 2961 | } |
| 2962 | |
| 2963 | /* callers are responsible for dropping em's ref. */ |
| 2964 | return em; |
| 2965 | } |
| 2966 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2967 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2968 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2969 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2970 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2971 | struct map_lookup *map; |
| 2972 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2973 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2974 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2975 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2976 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2977 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2978 | /* |
| 2979 | * 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] | 2980 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2981 | * do anything we still error out. |
| 2982 | */ |
| 2983 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2984 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2985 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2986 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2987 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2988 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2989 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2990 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2991 | /* |
| 2992 | * Take the device list mutex to prevent races with the final phase of |
| 2993 | * a device replace operation that replaces the device object associated |
| 2994 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2995 | */ |
| 2996 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2997 | for (i = 0; i < map->num_stripes; i++) { |
| 2998 | struct btrfs_device *device = map->stripes[i].dev; |
| 2999 | ret = btrfs_free_dev_extent(trans, device, |
| 3000 | map->stripes[i].physical, |
| 3001 | &dev_extent_len); |
| 3002 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3003 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3004 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3005 | goto out; |
| 3006 | } |
| 3007 | |
| 3008 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3009 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3010 | btrfs_device_set_bytes_used(device, |
| 3011 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 3012 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3013 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3014 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3015 | } |
| 3016 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 3017 | ret = btrfs_update_device(trans, device); |
| 3018 | if (ret) { |
| 3019 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3020 | btrfs_abort_transaction(trans, ret); |
| 3021 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3022 | } |
| 3023 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3024 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3025 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 3026 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3027 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3028 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3029 | goto out; |
| 3030 | } |
| 3031 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3032 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3033 | |
| 3034 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 3035 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3036 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3037 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3038 | goto out; |
| 3039 | } |
| 3040 | } |
| 3041 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 3042 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3043 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3044 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3045 | goto out; |
| 3046 | } |
| 3047 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3048 | out: |
| 3049 | /* once for us */ |
| 3050 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3051 | return ret; |
| 3052 | } |
| 3053 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3054 | 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] | 3055 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3056 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3057 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3058 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3059 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3060 | /* |
| 3061 | * Prevent races with automatic removal of unused block groups. |
| 3062 | * After we relocate and before we remove the chunk with offset |
| 3063 | * chunk_offset, automatic removal of the block group can kick in, |
| 3064 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3065 | * |
| 3066 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3067 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3068 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3069 | * we release the path used to search the chunk/dev tree and before |
| 3070 | * the current task acquires this mutex and calls us. |
| 3071 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3072 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3073 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3074 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3075 | if (ret) |
| 3076 | return -ENOSPC; |
| 3077 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3078 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3079 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3080 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3081 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3082 | if (ret) |
| 3083 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3084 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 3085 | /* |
| 3086 | * We add the kobjects here (and after forcing data chunk creation) |
| 3087 | * since relocation is the only place we'll create chunks of a new |
| 3088 | * type at runtime. The only place where we'll remove the last |
| 3089 | * chunk of a type is the call immediately below this one. Even |
| 3090 | * so, we're protected against races with the cleaner thread since |
| 3091 | * we're covered by the delete_unused_bgs_mutex. |
| 3092 | */ |
| 3093 | btrfs_add_raid_kobjects(fs_info); |
| 3094 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3095 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3096 | chunk_offset); |
| 3097 | if (IS_ERR(trans)) { |
| 3098 | ret = PTR_ERR(trans); |
| 3099 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3100 | return ret; |
| 3101 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3102 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3103 | /* |
| 3104 | * step two, delete the device extents and the |
| 3105 | * chunk tree entries |
| 3106 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3107 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3108 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3109 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3110 | } |
| 3111 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3112 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3113 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3114 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3115 | struct btrfs_path *path; |
| 3116 | struct extent_buffer *leaf; |
| 3117 | struct btrfs_chunk *chunk; |
| 3118 | struct btrfs_key key; |
| 3119 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3120 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3121 | bool retried = false; |
| 3122 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3123 | int ret; |
| 3124 | |
| 3125 | path = btrfs_alloc_path(); |
| 3126 | if (!path) |
| 3127 | return -ENOMEM; |
| 3128 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3129 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3130 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3131 | key.offset = (u64)-1; |
| 3132 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3133 | |
| 3134 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3135 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3136 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3137 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3138 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3139 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3140 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3141 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3142 | |
| 3143 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3144 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3145 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3146 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3147 | if (ret < 0) |
| 3148 | goto error; |
| 3149 | if (ret > 0) |
| 3150 | break; |
| 3151 | |
| 3152 | leaf = path->nodes[0]; |
| 3153 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3154 | |
| 3155 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3156 | struct btrfs_chunk); |
| 3157 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3158 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3159 | |
| 3160 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3161 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3162 | if (ret == -ENOSPC) |
| 3163 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3164 | else |
| 3165 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3166 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3167 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3168 | |
| 3169 | if (found_key.offset == 0) |
| 3170 | break; |
| 3171 | key.offset = found_key.offset - 1; |
| 3172 | } |
| 3173 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3174 | if (failed && !retried) { |
| 3175 | failed = 0; |
| 3176 | retried = true; |
| 3177 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3178 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3179 | ret = -ENOSPC; |
| 3180 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3181 | error: |
| 3182 | btrfs_free_path(path); |
| 3183 | return ret; |
| 3184 | } |
| 3185 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3186 | /* |
| 3187 | * return 1 : allocate a data chunk successfully, |
| 3188 | * return <0: errors during allocating a data chunk, |
| 3189 | * return 0 : no need to allocate a data chunk. |
| 3190 | */ |
| 3191 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3192 | u64 chunk_offset) |
| 3193 | { |
| 3194 | struct btrfs_block_group_cache *cache; |
| 3195 | u64 bytes_used; |
| 3196 | u64 chunk_type; |
| 3197 | |
| 3198 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3199 | ASSERT(cache); |
| 3200 | chunk_type = cache->flags; |
| 3201 | btrfs_put_block_group(cache); |
| 3202 | |
| 3203 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 3204 | spin_lock(&fs_info->data_sinfo->lock); |
| 3205 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3206 | spin_unlock(&fs_info->data_sinfo->lock); |
| 3207 | |
| 3208 | if (!bytes_used) { |
| 3209 | struct btrfs_trans_handle *trans; |
| 3210 | int ret; |
| 3211 | |
| 3212 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3213 | if (IS_ERR(trans)) |
| 3214 | return PTR_ERR(trans); |
| 3215 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 3216 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3217 | BTRFS_BLOCK_GROUP_DATA); |
| 3218 | btrfs_end_transaction(trans); |
| 3219 | if (ret < 0) |
| 3220 | return ret; |
| 3221 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 3222 | btrfs_add_raid_kobjects(fs_info); |
| 3223 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3224 | return 1; |
| 3225 | } |
| 3226 | } |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3230 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3231 | struct btrfs_balance_control *bctl) |
| 3232 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3233 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3234 | struct btrfs_trans_handle *trans; |
| 3235 | struct btrfs_balance_item *item; |
| 3236 | struct btrfs_disk_balance_args disk_bargs; |
| 3237 | struct btrfs_path *path; |
| 3238 | struct extent_buffer *leaf; |
| 3239 | struct btrfs_key key; |
| 3240 | int ret, err; |
| 3241 | |
| 3242 | path = btrfs_alloc_path(); |
| 3243 | if (!path) |
| 3244 | return -ENOMEM; |
| 3245 | |
| 3246 | trans = btrfs_start_transaction(root, 0); |
| 3247 | if (IS_ERR(trans)) { |
| 3248 | btrfs_free_path(path); |
| 3249 | return PTR_ERR(trans); |
| 3250 | } |
| 3251 | |
| 3252 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3253 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3254 | key.offset = 0; |
| 3255 | |
| 3256 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3257 | sizeof(*item)); |
| 3258 | if (ret) |
| 3259 | goto out; |
| 3260 | |
| 3261 | leaf = path->nodes[0]; |
| 3262 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3263 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3264 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3265 | |
| 3266 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3267 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3268 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3269 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3270 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3271 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3272 | |
| 3273 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3274 | |
| 3275 | btrfs_mark_buffer_dirty(leaf); |
| 3276 | out: |
| 3277 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3278 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3279 | if (err && !ret) |
| 3280 | ret = err; |
| 3281 | return ret; |
| 3282 | } |
| 3283 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3284 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3285 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3286 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3287 | struct btrfs_trans_handle *trans; |
| 3288 | struct btrfs_path *path; |
| 3289 | struct btrfs_key key; |
| 3290 | int ret, err; |
| 3291 | |
| 3292 | path = btrfs_alloc_path(); |
| 3293 | if (!path) |
| 3294 | return -ENOMEM; |
| 3295 | |
| 3296 | trans = btrfs_start_transaction(root, 0); |
| 3297 | if (IS_ERR(trans)) { |
| 3298 | btrfs_free_path(path); |
| 3299 | return PTR_ERR(trans); |
| 3300 | } |
| 3301 | |
| 3302 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3303 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3304 | key.offset = 0; |
| 3305 | |
| 3306 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3307 | if (ret < 0) |
| 3308 | goto out; |
| 3309 | if (ret > 0) { |
| 3310 | ret = -ENOENT; |
| 3311 | goto out; |
| 3312 | } |
| 3313 | |
| 3314 | ret = btrfs_del_item(trans, root, path); |
| 3315 | out: |
| 3316 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3317 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3318 | if (err && !ret) |
| 3319 | ret = err; |
| 3320 | return ret; |
| 3321 | } |
| 3322 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3323 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3324 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3325 | * resume after balance was interrupted. |
| 3326 | */ |
| 3327 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3328 | { |
| 3329 | /* |
| 3330 | * Turn on soft mode for chunk types that were being converted. |
| 3331 | */ |
| 3332 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3333 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3334 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3335 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3336 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3337 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3338 | |
| 3339 | /* |
| 3340 | * Turn on usage filter if is not already used. The idea is |
| 3341 | * that chunks that we have already balanced should be |
| 3342 | * reasonably full. Don't do it for chunks that are being |
| 3343 | * converted - that will keep us from relocating unconverted |
| 3344 | * (albeit full) chunks. |
| 3345 | */ |
| 3346 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3347 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3348 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3349 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3350 | bctl->data.usage = 90; |
| 3351 | } |
| 3352 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3353 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3354 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3355 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3356 | bctl->sys.usage = 90; |
| 3357 | } |
| 3358 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3359 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3360 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3361 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3362 | bctl->meta.usage = 90; |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3367 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3368 | */ |
| 3369 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3370 | { |
| 3371 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3372 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3373 | |
| 3374 | BUG_ON(!fs_info->balance_ctl); |
| 3375 | |
| 3376 | spin_lock(&fs_info->balance_lock); |
| 3377 | fs_info->balance_ctl = NULL; |
| 3378 | spin_unlock(&fs_info->balance_lock); |
| 3379 | |
| 3380 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3381 | ret = del_balance_item(fs_info); |
| 3382 | if (ret) |
| 3383 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3386 | /* |
| 3387 | * Balance filters. Return 1 if chunk should be filtered out |
| 3388 | * (should not be balanced). |
| 3389 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3390 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3391 | struct btrfs_balance_args *bargs) |
| 3392 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3393 | chunk_type = chunk_to_extended(chunk_type) & |
| 3394 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3395 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3396 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3397 | return 0; |
| 3398 | |
| 3399 | return 1; |
| 3400 | } |
| 3401 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3402 | 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] | 3403 | struct btrfs_balance_args *bargs) |
| 3404 | { |
| 3405 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3406 | u64 chunk_used; |
| 3407 | u64 user_thresh_min; |
| 3408 | u64 user_thresh_max; |
| 3409 | int ret = 1; |
| 3410 | |
| 3411 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3412 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3413 | |
| 3414 | if (bargs->usage_min == 0) |
| 3415 | user_thresh_min = 0; |
| 3416 | else |
| 3417 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3418 | bargs->usage_min); |
| 3419 | |
| 3420 | if (bargs->usage_max == 0) |
| 3421 | user_thresh_max = 1; |
| 3422 | else if (bargs->usage_max > 100) |
| 3423 | user_thresh_max = cache->key.offset; |
| 3424 | else |
| 3425 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3426 | bargs->usage_max); |
| 3427 | |
| 3428 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3429 | ret = 0; |
| 3430 | |
| 3431 | btrfs_put_block_group(cache); |
| 3432 | return ret; |
| 3433 | } |
| 3434 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3435 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3436 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3437 | { |
| 3438 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3439 | u64 chunk_used, user_thresh; |
| 3440 | int ret = 1; |
| 3441 | |
| 3442 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3443 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3444 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3445 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3446 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3447 | else if (bargs->usage > 100) |
| 3448 | user_thresh = cache->key.offset; |
| 3449 | else |
| 3450 | user_thresh = div_factor_fine(cache->key.offset, |
| 3451 | bargs->usage); |
| 3452 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3453 | if (chunk_used < user_thresh) |
| 3454 | ret = 0; |
| 3455 | |
| 3456 | btrfs_put_block_group(cache); |
| 3457 | return ret; |
| 3458 | } |
| 3459 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3460 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3461 | struct btrfs_chunk *chunk, |
| 3462 | struct btrfs_balance_args *bargs) |
| 3463 | { |
| 3464 | struct btrfs_stripe *stripe; |
| 3465 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3466 | int i; |
| 3467 | |
| 3468 | for (i = 0; i < num_stripes; i++) { |
| 3469 | stripe = btrfs_stripe_nr(chunk, i); |
| 3470 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
| 3474 | return 1; |
| 3475 | } |
| 3476 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3477 | /* [pstart, pend) */ |
| 3478 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3479 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3480 | struct btrfs_balance_args *bargs) |
| 3481 | { |
| 3482 | struct btrfs_stripe *stripe; |
| 3483 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3484 | u64 stripe_offset; |
| 3485 | u64 stripe_length; |
| 3486 | int factor; |
| 3487 | int i; |
| 3488 | |
| 3489 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3490 | return 0; |
| 3491 | |
| 3492 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3493 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3494 | factor = num_stripes / 2; |
| 3495 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3496 | factor = num_stripes - 1; |
| 3497 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3498 | factor = num_stripes - 2; |
| 3499 | } else { |
| 3500 | factor = num_stripes; |
| 3501 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3502 | |
| 3503 | for (i = 0; i < num_stripes; i++) { |
| 3504 | stripe = btrfs_stripe_nr(chunk, i); |
| 3505 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3506 | continue; |
| 3507 | |
| 3508 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3509 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3510 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3511 | |
| 3512 | if (stripe_offset < bargs->pend && |
| 3513 | stripe_offset + stripe_length > bargs->pstart) |
| 3514 | return 0; |
| 3515 | } |
| 3516 | |
| 3517 | return 1; |
| 3518 | } |
| 3519 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3520 | /* [vstart, vend) */ |
| 3521 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3522 | struct btrfs_chunk *chunk, |
| 3523 | u64 chunk_offset, |
| 3524 | struct btrfs_balance_args *bargs) |
| 3525 | { |
| 3526 | if (chunk_offset < bargs->vend && |
| 3527 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3528 | /* at least part of the chunk is inside this vrange */ |
| 3529 | return 0; |
| 3530 | |
| 3531 | return 1; |
| 3532 | } |
| 3533 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3534 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3535 | struct btrfs_chunk *chunk, |
| 3536 | struct btrfs_balance_args *bargs) |
| 3537 | { |
| 3538 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3539 | |
| 3540 | if (bargs->stripes_min <= num_stripes |
| 3541 | && num_stripes <= bargs->stripes_max) |
| 3542 | return 0; |
| 3543 | |
| 3544 | return 1; |
| 3545 | } |
| 3546 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3547 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3548 | struct btrfs_balance_args *bargs) |
| 3549 | { |
| 3550 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3551 | return 0; |
| 3552 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3553 | chunk_type = chunk_to_extended(chunk_type) & |
| 3554 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3555 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3556 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3557 | return 1; |
| 3558 | |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3562 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3563 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3564 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3565 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3566 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3567 | struct btrfs_balance_args *bargs = NULL; |
| 3568 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3569 | |
| 3570 | /* type filter */ |
| 3571 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3572 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
| 3576 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3577 | bargs = &bctl->data; |
| 3578 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3579 | bargs = &bctl->sys; |
| 3580 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3581 | bargs = &bctl->meta; |
| 3582 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3583 | /* profiles filter */ |
| 3584 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3585 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3586 | return 0; |
| 3587 | } |
| 3588 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3589 | /* usage filter */ |
| 3590 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3591 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3592 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3593 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3594 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3595 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3598 | /* devid filter */ |
| 3599 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3600 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3601 | return 0; |
| 3602 | } |
| 3603 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3604 | /* drange filter, makes sense only with devid filter */ |
| 3605 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3606 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3607 | return 0; |
| 3608 | } |
| 3609 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3610 | /* vrange filter */ |
| 3611 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3612 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3613 | return 0; |
| 3614 | } |
| 3615 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3616 | /* stripes filter */ |
| 3617 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3618 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3619 | return 0; |
| 3620 | } |
| 3621 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3622 | /* soft profile changing mode */ |
| 3623 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3624 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3628 | /* |
| 3629 | * limited by count, must be the last filter |
| 3630 | */ |
| 3631 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3632 | if (bargs->limit == 0) |
| 3633 | return 0; |
| 3634 | else |
| 3635 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3636 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3637 | /* |
| 3638 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3639 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3640 | * about the count of all chunks that satisfy the filters. |
| 3641 | */ |
| 3642 | if (bargs->limit_max == 0) |
| 3643 | return 0; |
| 3644 | else |
| 3645 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3646 | } |
| 3647 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3648 | return 1; |
| 3649 | } |
| 3650 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3651 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3652 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3653 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3654 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3655 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3656 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3657 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3658 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3659 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3660 | struct extent_buffer *leaf; |
| 3661 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3662 | int ret; |
| 3663 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3664 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3665 | /* 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] | 3666 | u64 limit_data = bctl->data.limit; |
| 3667 | u64 limit_meta = bctl->meta.limit; |
| 3668 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3669 | u32 count_data = 0; |
| 3670 | u32 count_meta = 0; |
| 3671 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3672 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3673 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3674 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3675 | if (!path) { |
| 3676 | ret = -ENOMEM; |
| 3677 | goto error; |
| 3678 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3679 | |
| 3680 | /* zero out stat counters */ |
| 3681 | spin_lock(&fs_info->balance_lock); |
| 3682 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3683 | spin_unlock(&fs_info->balance_lock); |
| 3684 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3685 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3686 | /* |
| 3687 | * The single value limit and min/max limits use the same bytes |
| 3688 | * in the |
| 3689 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3690 | bctl->data.limit = limit_data; |
| 3691 | bctl->meta.limit = limit_meta; |
| 3692 | bctl->sys.limit = limit_sys; |
| 3693 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3694 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3695 | key.offset = (u64)-1; |
| 3696 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3697 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3698 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3699 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3700 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3701 | ret = -ECANCELED; |
| 3702 | goto error; |
| 3703 | } |
| 3704 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3705 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3706 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3707 | if (ret < 0) { |
| 3708 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3709 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3710 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3711 | |
| 3712 | /* |
| 3713 | * this shouldn't happen, it means the last relocate |
| 3714 | * failed |
| 3715 | */ |
| 3716 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3717 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3718 | |
| 3719 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3720 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3721 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3722 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3723 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3724 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3725 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3726 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3727 | leaf = path->nodes[0]; |
| 3728 | slot = path->slots[0]; |
| 3729 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3730 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3731 | if (found_key.objectid != key.objectid) { |
| 3732 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3733 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3734 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3735 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3736 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3737 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3738 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3739 | if (!counting) { |
| 3740 | spin_lock(&fs_info->balance_lock); |
| 3741 | bctl->stat.considered++; |
| 3742 | spin_unlock(&fs_info->balance_lock); |
| 3743 | } |
| 3744 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3745 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3746 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3747 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3748 | if (!ret) { |
| 3749 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3750 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3751 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3752 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3753 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3754 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3755 | spin_lock(&fs_info->balance_lock); |
| 3756 | bctl->stat.expected++; |
| 3757 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3758 | |
| 3759 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3760 | count_data++; |
| 3761 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3762 | count_sys++; |
| 3763 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3764 | count_meta++; |
| 3765 | |
| 3766 | goto loop; |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * Apply limit_min filter, no need to check if the LIMITS |
| 3771 | * filter is used, limit_min is 0 by default |
| 3772 | */ |
| 3773 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3774 | count_data < bctl->data.limit_min) |
| 3775 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3776 | count_meta < bctl->meta.limit_min) |
| 3777 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3778 | count_sys < bctl->sys.limit_min)) { |
| 3779 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3780 | goto loop; |
| 3781 | } |
| 3782 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3783 | if (!chunk_reserved) { |
| 3784 | /* |
| 3785 | * We may be relocating the only data chunk we have, |
| 3786 | * which could potentially end up with losing data's |
| 3787 | * raid profile, so lets allocate an empty one in |
| 3788 | * advance. |
| 3789 | */ |
| 3790 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3791 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3792 | if (ret < 0) { |
| 3793 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3794 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3795 | } else if (ret == 1) { |
| 3796 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3797 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3798 | } |
| 3799 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3800 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3801 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3802 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3803 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3804 | } else if (ret == -ETXTBSY) { |
| 3805 | btrfs_info(fs_info, |
| 3806 | "skipping relocation of block group %llu due to active swapfile", |
| 3807 | found_key.offset); |
| 3808 | ret = 0; |
| 3809 | } else if (ret) { |
| 3810 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3811 | } else { |
| 3812 | spin_lock(&fs_info->balance_lock); |
| 3813 | bctl->stat.completed++; |
| 3814 | spin_unlock(&fs_info->balance_lock); |
| 3815 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3816 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3817 | if (found_key.offset == 0) |
| 3818 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3819 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3820 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3821 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3822 | if (counting) { |
| 3823 | btrfs_release_path(path); |
| 3824 | counting = false; |
| 3825 | goto again; |
| 3826 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3827 | error: |
| 3828 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3829 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3830 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3831 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3832 | if (!ret) |
| 3833 | ret = -ENOSPC; |
| 3834 | } |
| 3835 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3836 | return ret; |
| 3837 | } |
| 3838 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3839 | /** |
| 3840 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3841 | * @flags: profile to validate |
| 3842 | * @extended: if true @flags is treated as an extended profile |
| 3843 | */ |
| 3844 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3845 | { |
| 3846 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3847 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3848 | |
| 3849 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3850 | |
| 3851 | /* 1) check that all other bits are zeroed */ |
| 3852 | if (flags & ~mask) |
| 3853 | return 0; |
| 3854 | |
| 3855 | /* 2) see if profile is reduced */ |
| 3856 | if (flags == 0) |
| 3857 | return !extended; /* "0" is valid for usual profiles */ |
| 3858 | |
| 3859 | /* true if exactly one bit set */ |
David Sterba | 818255f | 2018-09-21 14:26:34 +0200 | [diff] [blame] | 3860 | return is_power_of_2(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3861 | } |
| 3862 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3863 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3864 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3865 | /* cancel requested || normal exit path */ |
| 3866 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3867 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3868 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3871 | /* Non-zero return value signifies invalidity */ |
| 3872 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3873 | u64 allowed) |
| 3874 | { |
| 3875 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3876 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3877 | (bctl_arg->target & ~allowed))); |
| 3878 | } |
| 3879 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3880 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3881 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3882 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3883 | * does not fit into the provided buffer. |
| 3884 | */ |
| 3885 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3886 | u32 size_buf) |
| 3887 | { |
| 3888 | int ret; |
| 3889 | u32 size_bp = size_buf; |
| 3890 | char *bp = buf; |
| 3891 | u64 flags = bargs->flags; |
| 3892 | char tmp_buf[128] = {'\0'}; |
| 3893 | |
| 3894 | if (!flags) |
| 3895 | return; |
| 3896 | |
| 3897 | #define CHECK_APPEND_NOARG(a) \ |
| 3898 | do { \ |
| 3899 | ret = snprintf(bp, size_bp, (a)); \ |
| 3900 | if (ret < 0 || ret >= size_bp) \ |
| 3901 | goto out_overflow; \ |
| 3902 | size_bp -= ret; \ |
| 3903 | bp += ret; \ |
| 3904 | } while (0) |
| 3905 | |
| 3906 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3907 | do { \ |
| 3908 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3909 | if (ret < 0 || ret >= size_bp) \ |
| 3910 | goto out_overflow; \ |
| 3911 | size_bp -= ret; \ |
| 3912 | bp += ret; \ |
| 3913 | } while (0) |
| 3914 | |
| 3915 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3916 | do { \ |
| 3917 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3918 | if (ret < 0 || ret >= size_bp) \ |
| 3919 | goto out_overflow; \ |
| 3920 | size_bp -= ret; \ |
| 3921 | bp += ret; \ |
| 3922 | } while (0) |
| 3923 | |
| 3924 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3925 | int index = btrfs_bg_flags_to_raid_index(bargs->target); |
| 3926 | |
| 3927 | CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index)); |
| 3928 | } |
| 3929 | |
| 3930 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3931 | CHECK_APPEND_NOARG("soft,"); |
| 3932 | |
| 3933 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3934 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3935 | sizeof(tmp_buf)); |
| 3936 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3937 | } |
| 3938 | |
| 3939 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3940 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3941 | |
| 3942 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3943 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3944 | bargs->usage_min, bargs->usage_max); |
| 3945 | |
| 3946 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3947 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3948 | |
| 3949 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3950 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3951 | bargs->pstart, bargs->pend); |
| 3952 | |
| 3953 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3954 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3955 | bargs->vstart, bargs->vend); |
| 3956 | |
| 3957 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3958 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3959 | |
| 3960 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3961 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3962 | bargs->limit_min, bargs->limit_max); |
| 3963 | |
| 3964 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3965 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3966 | bargs->stripes_min, bargs->stripes_max); |
| 3967 | |
| 3968 | #undef CHECK_APPEND_2ARG |
| 3969 | #undef CHECK_APPEND_1ARG |
| 3970 | #undef CHECK_APPEND_NOARG |
| 3971 | |
| 3972 | out_overflow: |
| 3973 | |
| 3974 | if (size_bp < size_buf) |
| 3975 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3976 | else |
| 3977 | buf[0] = '\0'; |
| 3978 | } |
| 3979 | |
| 3980 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3981 | { |
| 3982 | u32 size_buf = 1024; |
| 3983 | char tmp_buf[192] = {'\0'}; |
| 3984 | char *buf; |
| 3985 | char *bp; |
| 3986 | u32 size_bp = size_buf; |
| 3987 | int ret; |
| 3988 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3989 | |
| 3990 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3991 | if (!buf) |
| 3992 | return; |
| 3993 | |
| 3994 | bp = buf; |
| 3995 | |
| 3996 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3997 | do { \ |
| 3998 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3999 | if (ret < 0 || ret >= size_bp) \ |
| 4000 | goto out_overflow; \ |
| 4001 | size_bp -= ret; \ |
| 4002 | bp += ret; \ |
| 4003 | } while (0) |
| 4004 | |
| 4005 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 4006 | CHECK_APPEND_1ARG("%s", "-f "); |
| 4007 | |
| 4008 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 4009 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 4010 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 4011 | } |
| 4012 | |
| 4013 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 4014 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 4015 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 4016 | } |
| 4017 | |
| 4018 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 4019 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 4020 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 4021 | } |
| 4022 | |
| 4023 | #undef CHECK_APPEND_1ARG |
| 4024 | |
| 4025 | out_overflow: |
| 4026 | |
| 4027 | if (size_bp < size_buf) |
| 4028 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 4029 | btrfs_info(fs_info, "balance: %s %s", |
| 4030 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 4031 | "resume" : "start", buf); |
| 4032 | |
| 4033 | kfree(buf); |
| 4034 | } |
| 4035 | |
| 4036 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4037 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4038 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4039 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 4040 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4041 | struct btrfs_ioctl_balance_args *bargs) |
| 4042 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4043 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4044 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4045 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4046 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 4047 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4048 | unsigned seq; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4049 | bool reducing_integrity; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4050 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4051 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4052 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4053 | atomic_read(&fs_info->balance_pause_req) || |
| 4054 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4055 | ret = -EINVAL; |
| 4056 | goto out; |
| 4057 | } |
| 4058 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4059 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 4060 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 4061 | mixed = 1; |
| 4062 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4063 | /* |
| 4064 | * In case of mixed groups both data and meta should be picked, |
| 4065 | * and identical options should be given for both of them. |
| 4066 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4067 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 4068 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4069 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4070 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4071 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4072 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4073 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4074 | ret = -EINVAL; |
| 4075 | goto out; |
| 4076 | } |
| 4077 | } |
| 4078 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4079 | num_devices = btrfs_num_devices(fs_info); |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4080 | allowed = 0; |
| 4081 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 4082 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 4083 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4084 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4085 | if (validate_convert_profile(&bctl->data, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4086 | int index = btrfs_bg_flags_to_raid_index(bctl->data.target); |
| 4087 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4088 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4089 | "balance: invalid convert data profile %s", |
| 4090 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4091 | ret = -EINVAL; |
| 4092 | goto out; |
| 4093 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4094 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4095 | int index = btrfs_bg_flags_to_raid_index(bctl->meta.target); |
| 4096 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4097 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4098 | "balance: invalid convert metadata profile %s", |
| 4099 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4100 | ret = -EINVAL; |
| 4101 | goto out; |
| 4102 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4103 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4104 | int index = btrfs_bg_flags_to_raid_index(bctl->sys.target); |
| 4105 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4106 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4107 | "balance: invalid convert system profile %s", |
| 4108 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4109 | ret = -EINVAL; |
| 4110 | goto out; |
| 4111 | } |
| 4112 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 4113 | /* |
| 4114 | * Allow to reduce metadata or system integrity only if force set for |
| 4115 | * profiles with redundancy (copies, parity) |
| 4116 | */ |
| 4117 | allowed = 0; |
| 4118 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4119 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4120 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4121 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4122 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4123 | do { |
| 4124 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4125 | |
| 4126 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4127 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4128 | !(bctl->sys.target & allowed)) || |
| 4129 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4130 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4131 | !(bctl->meta.target & allowed))) |
| 4132 | reducing_integrity = true; |
| 4133 | else |
| 4134 | reducing_integrity = false; |
| 4135 | |
| 4136 | /* if we're not converting, the target field is uninitialized */ |
| 4137 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4138 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4139 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4140 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4141 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4142 | |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4143 | if (reducing_integrity) { |
| 4144 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4145 | btrfs_info(fs_info, |
| 4146 | "balance: force reducing metadata integrity"); |
| 4147 | } else { |
| 4148 | btrfs_err(fs_info, |
| 4149 | "balance: reduces metadata integrity, use --force if you want this"); |
| 4150 | ret = -EINVAL; |
| 4151 | goto out; |
| 4152 | } |
| 4153 | } |
| 4154 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4155 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4156 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4157 | int meta_index = btrfs_bg_flags_to_raid_index(meta_target); |
| 4158 | int data_index = btrfs_bg_flags_to_raid_index(data_target); |
| 4159 | |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4160 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4161 | "balance: metadata profile %s has lower redundancy than data profile %s", |
| 4162 | get_raid_name(meta_index), get_raid_name(data_index)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4165 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4166 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4167 | goto out; |
| 4168 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4169 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4170 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4171 | BUG_ON(fs_info->balance_ctl); |
| 4172 | spin_lock(&fs_info->balance_lock); |
| 4173 | fs_info->balance_ctl = bctl; |
| 4174 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4175 | } else { |
| 4176 | BUG_ON(ret != -EEXIST); |
| 4177 | spin_lock(&fs_info->balance_lock); |
| 4178 | update_balance_args(bctl); |
| 4179 | spin_unlock(&fs_info->balance_lock); |
| 4180 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4181 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4182 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4183 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4184 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4185 | mutex_unlock(&fs_info->balance_mutex); |
| 4186 | |
| 4187 | ret = __btrfs_balance(fs_info); |
| 4188 | |
| 4189 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4190 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4191 | btrfs_info(fs_info, "balance: paused"); |
| 4192 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4193 | btrfs_info(fs_info, "balance: canceled"); |
| 4194 | else |
| 4195 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4196 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4197 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4198 | |
| 4199 | if (bargs) { |
| 4200 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4201 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4204 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4205 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4206 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4207 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4208 | } |
| 4209 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4210 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4211 | |
| 4212 | return ret; |
| 4213 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4214 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4215 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4216 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4217 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4218 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4219 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4220 | return ret; |
| 4221 | } |
| 4222 | |
| 4223 | static int balance_kthread(void *data) |
| 4224 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4225 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4226 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4227 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4228 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4229 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4230 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4231 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4232 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4233 | return ret; |
| 4234 | } |
| 4235 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4236 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4237 | { |
| 4238 | struct task_struct *tsk; |
| 4239 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4240 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4241 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4242 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4243 | return 0; |
| 4244 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4245 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4246 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4247 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4248 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4249 | return 0; |
| 4250 | } |
| 4251 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4252 | /* |
| 4253 | * A ro->rw remount sequence should continue with the paused balance |
| 4254 | * regardless of who pauses it, system or the user as of now, so set |
| 4255 | * the resume flag. |
| 4256 | */ |
| 4257 | spin_lock(&fs_info->balance_lock); |
| 4258 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4259 | spin_unlock(&fs_info->balance_lock); |
| 4260 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4261 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4262 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4263 | } |
| 4264 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4265 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4266 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4267 | struct btrfs_balance_control *bctl; |
| 4268 | struct btrfs_balance_item *item; |
| 4269 | struct btrfs_disk_balance_args disk_bargs; |
| 4270 | struct btrfs_path *path; |
| 4271 | struct extent_buffer *leaf; |
| 4272 | struct btrfs_key key; |
| 4273 | int ret; |
| 4274 | |
| 4275 | path = btrfs_alloc_path(); |
| 4276 | if (!path) |
| 4277 | return -ENOMEM; |
| 4278 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4279 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4280 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4281 | key.offset = 0; |
| 4282 | |
| 4283 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4284 | if (ret < 0) |
| 4285 | goto out; |
| 4286 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4287 | ret = 0; |
| 4288 | goto out; |
| 4289 | } |
| 4290 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4291 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4292 | if (!bctl) { |
| 4293 | ret = -ENOMEM; |
| 4294 | goto out; |
| 4295 | } |
| 4296 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4297 | leaf = path->nodes[0]; |
| 4298 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4299 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4300 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4301 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4302 | |
| 4303 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4304 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4305 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4306 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4307 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4308 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4309 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4310 | /* |
| 4311 | * This should never happen, as the paused balance state is recovered |
| 4312 | * during mount without any chance of other exclusive ops to collide. |
| 4313 | * |
| 4314 | * This gives the exclusive op status to balance and keeps in paused |
| 4315 | * state until user intervention (cancel or umount). If the ownership |
| 4316 | * cannot be assigned, show a message but do not fail. The balance |
| 4317 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4318 | * set up. |
| 4319 | */ |
| 4320 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4321 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4322 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4323 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4324 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4325 | BUG_ON(fs_info->balance_ctl); |
| 4326 | spin_lock(&fs_info->balance_lock); |
| 4327 | fs_info->balance_ctl = bctl; |
| 4328 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4329 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4330 | out: |
| 4331 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4332 | return ret; |
| 4333 | } |
| 4334 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4335 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4336 | { |
| 4337 | int ret = 0; |
| 4338 | |
| 4339 | mutex_lock(&fs_info->balance_mutex); |
| 4340 | if (!fs_info->balance_ctl) { |
| 4341 | mutex_unlock(&fs_info->balance_mutex); |
| 4342 | return -ENOTCONN; |
| 4343 | } |
| 4344 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4345 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4346 | atomic_inc(&fs_info->balance_pause_req); |
| 4347 | mutex_unlock(&fs_info->balance_mutex); |
| 4348 | |
| 4349 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4350 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4351 | |
| 4352 | mutex_lock(&fs_info->balance_mutex); |
| 4353 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4354 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4355 | atomic_dec(&fs_info->balance_pause_req); |
| 4356 | } else { |
| 4357 | ret = -ENOTCONN; |
| 4358 | } |
| 4359 | |
| 4360 | mutex_unlock(&fs_info->balance_mutex); |
| 4361 | return ret; |
| 4362 | } |
| 4363 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4364 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4365 | { |
| 4366 | mutex_lock(&fs_info->balance_mutex); |
| 4367 | if (!fs_info->balance_ctl) { |
| 4368 | mutex_unlock(&fs_info->balance_mutex); |
| 4369 | return -ENOTCONN; |
| 4370 | } |
| 4371 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4372 | /* |
| 4373 | * A paused balance with the item stored on disk can be resumed at |
| 4374 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4375 | * and we must not allow cancelling as it deletes the item. |
| 4376 | */ |
| 4377 | if (sb_rdonly(fs_info->sb)) { |
| 4378 | mutex_unlock(&fs_info->balance_mutex); |
| 4379 | return -EROFS; |
| 4380 | } |
| 4381 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4382 | atomic_inc(&fs_info->balance_cancel_req); |
| 4383 | /* |
| 4384 | * if we are running just wait and return, balance item is |
| 4385 | * deleted in btrfs_balance in this case |
| 4386 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4387 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4388 | mutex_unlock(&fs_info->balance_mutex); |
| 4389 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4390 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4391 | mutex_lock(&fs_info->balance_mutex); |
| 4392 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4393 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4394 | /* |
| 4395 | * Lock released to allow other waiters to continue, we'll |
| 4396 | * reexamine the status again. |
| 4397 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4398 | mutex_lock(&fs_info->balance_mutex); |
| 4399 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4400 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4401 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4402 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4403 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4404 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4405 | } |
| 4406 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4407 | BUG_ON(fs_info->balance_ctl || |
| 4408 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4409 | atomic_dec(&fs_info->balance_cancel_req); |
| 4410 | mutex_unlock(&fs_info->balance_mutex); |
| 4411 | return 0; |
| 4412 | } |
| 4413 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4414 | static int btrfs_uuid_scan_kthread(void *data) |
| 4415 | { |
| 4416 | struct btrfs_fs_info *fs_info = data; |
| 4417 | struct btrfs_root *root = fs_info->tree_root; |
| 4418 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4419 | struct btrfs_path *path = NULL; |
| 4420 | int ret = 0; |
| 4421 | struct extent_buffer *eb; |
| 4422 | int slot; |
| 4423 | struct btrfs_root_item root_item; |
| 4424 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4425 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4426 | |
| 4427 | path = btrfs_alloc_path(); |
| 4428 | if (!path) { |
| 4429 | ret = -ENOMEM; |
| 4430 | goto out; |
| 4431 | } |
| 4432 | |
| 4433 | key.objectid = 0; |
| 4434 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4435 | key.offset = 0; |
| 4436 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4437 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4438 | ret = btrfs_search_forward(root, &key, path, |
| 4439 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4440 | if (ret) { |
| 4441 | if (ret > 0) |
| 4442 | ret = 0; |
| 4443 | break; |
| 4444 | } |
| 4445 | |
| 4446 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4447 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4448 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4449 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4450 | goto skip; |
| 4451 | |
| 4452 | eb = path->nodes[0]; |
| 4453 | slot = path->slots[0]; |
| 4454 | item_size = btrfs_item_size_nr(eb, slot); |
| 4455 | if (item_size < sizeof(root_item)) |
| 4456 | goto skip; |
| 4457 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4458 | read_extent_buffer(eb, &root_item, |
| 4459 | btrfs_item_ptr_offset(eb, slot), |
| 4460 | (int)sizeof(root_item)); |
| 4461 | if (btrfs_root_refs(&root_item) == 0) |
| 4462 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4463 | |
| 4464 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4465 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4466 | if (trans) |
| 4467 | goto update_tree; |
| 4468 | |
| 4469 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4470 | /* |
| 4471 | * 1 - subvol uuid item |
| 4472 | * 1 - received_subvol uuid item |
| 4473 | */ |
| 4474 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4475 | if (IS_ERR(trans)) { |
| 4476 | ret = PTR_ERR(trans); |
| 4477 | break; |
| 4478 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4479 | continue; |
| 4480 | } else { |
| 4481 | goto skip; |
| 4482 | } |
| 4483 | update_tree: |
| 4484 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4485 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4486 | BTRFS_UUID_KEY_SUBVOL, |
| 4487 | key.objectid); |
| 4488 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4489 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4490 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4496 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4497 | root_item.received_uuid, |
| 4498 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4499 | key.objectid); |
| 4500 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4501 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4502 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4503 | break; |
| 4504 | } |
| 4505 | } |
| 4506 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4507 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4508 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4509 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4510 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4511 | if (ret) |
| 4512 | break; |
| 4513 | } |
| 4514 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4515 | btrfs_release_path(path); |
| 4516 | if (key.offset < (u64)-1) { |
| 4517 | key.offset++; |
| 4518 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4519 | key.offset = 0; |
| 4520 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4521 | } else if (key.objectid < (u64)-1) { |
| 4522 | key.offset = 0; |
| 4523 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4524 | key.objectid++; |
| 4525 | } else { |
| 4526 | break; |
| 4527 | } |
| 4528 | cond_resched(); |
| 4529 | } |
| 4530 | |
| 4531 | out: |
| 4532 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4533 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4534 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4535 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4536 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4537 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4538 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4539 | up(&fs_info->uuid_tree_rescan_sem); |
| 4540 | return 0; |
| 4541 | } |
| 4542 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4543 | /* |
| 4544 | * Callback for btrfs_uuid_tree_iterate(). |
| 4545 | * returns: |
| 4546 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4547 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4548 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4549 | */ |
| 4550 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4551 | u8 *uuid, u8 type, u64 subid) |
| 4552 | { |
| 4553 | struct btrfs_key key; |
| 4554 | int ret = 0; |
| 4555 | struct btrfs_root *subvol_root; |
| 4556 | |
| 4557 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4558 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4559 | goto out; |
| 4560 | |
| 4561 | key.objectid = subid; |
| 4562 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4563 | key.offset = (u64)-1; |
| 4564 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4565 | if (IS_ERR(subvol_root)) { |
| 4566 | ret = PTR_ERR(subvol_root); |
| 4567 | if (ret == -ENOENT) |
| 4568 | ret = 1; |
| 4569 | goto out; |
| 4570 | } |
| 4571 | |
| 4572 | switch (type) { |
| 4573 | case BTRFS_UUID_KEY_SUBVOL: |
| 4574 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4575 | ret = 1; |
| 4576 | break; |
| 4577 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4578 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4579 | BTRFS_UUID_SIZE)) |
| 4580 | ret = 1; |
| 4581 | break; |
| 4582 | } |
| 4583 | |
| 4584 | out: |
| 4585 | return ret; |
| 4586 | } |
| 4587 | |
| 4588 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4589 | { |
| 4590 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4591 | int ret; |
| 4592 | |
| 4593 | /* |
| 4594 | * 1st step is to iterate through the existing UUID tree and |
| 4595 | * to delete all entries that contain outdated data. |
| 4596 | * 2nd step is to add all missing entries to the UUID tree. |
| 4597 | */ |
| 4598 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4599 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4600 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4601 | up(&fs_info->uuid_tree_rescan_sem); |
| 4602 | return ret; |
| 4603 | } |
| 4604 | return btrfs_uuid_scan_kthread(data); |
| 4605 | } |
| 4606 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4607 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4608 | { |
| 4609 | struct btrfs_trans_handle *trans; |
| 4610 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4611 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4612 | struct task_struct *task; |
| 4613 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4614 | |
| 4615 | /* |
| 4616 | * 1 - root node |
| 4617 | * 1 - root item |
| 4618 | */ |
| 4619 | trans = btrfs_start_transaction(tree_root, 2); |
| 4620 | if (IS_ERR(trans)) |
| 4621 | return PTR_ERR(trans); |
| 4622 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4623 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4624 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4625 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4626 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4627 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4628 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | fs_info->uuid_root = uuid_root; |
| 4632 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4633 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4634 | if (ret) |
| 4635 | return ret; |
| 4636 | |
| 4637 | down(&fs_info->uuid_tree_rescan_sem); |
| 4638 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4639 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4640 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4641 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4642 | up(&fs_info->uuid_tree_rescan_sem); |
| 4643 | return PTR_ERR(task); |
| 4644 | } |
| 4645 | |
| 4646 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4647 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4648 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4649 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4650 | { |
| 4651 | struct task_struct *task; |
| 4652 | |
| 4653 | down(&fs_info->uuid_tree_rescan_sem); |
| 4654 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4655 | if (IS_ERR(task)) { |
| 4656 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4657 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4658 | up(&fs_info->uuid_tree_rescan_sem); |
| 4659 | return PTR_ERR(task); |
| 4660 | } |
| 4661 | |
| 4662 | return 0; |
| 4663 | } |
| 4664 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4665 | /* |
| 4666 | * shrinking a device means finding all of the device extents past |
| 4667 | * the new size, and then following the back refs to the chunks. |
| 4668 | * The chunk relocation code actually frees the device extent |
| 4669 | */ |
| 4670 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4671 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4672 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4673 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4674 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4675 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4676 | struct btrfs_path *path; |
| 4677 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4678 | u64 chunk_offset; |
| 4679 | int ret; |
| 4680 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4681 | int failed = 0; |
| 4682 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4683 | struct extent_buffer *l; |
| 4684 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4685 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4686 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4687 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4688 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4689 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4690 | |
| 4691 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4692 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4693 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4694 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4695 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4696 | return -EINVAL; |
| 4697 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4698 | path = btrfs_alloc_path(); |
| 4699 | if (!path) |
| 4700 | return -ENOMEM; |
| 4701 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4702 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4703 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4704 | trans = btrfs_start_transaction(root, 0); |
| 4705 | if (IS_ERR(trans)) { |
| 4706 | btrfs_free_path(path); |
| 4707 | return PTR_ERR(trans); |
| 4708 | } |
| 4709 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4710 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4711 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4712 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4713 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4714 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4715 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4716 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4717 | |
| 4718 | /* |
| 4719 | * Once the device's size has been set to the new size, ensure all |
| 4720 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4721 | * and relocates them accordingly. |
| 4722 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4723 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4724 | mutex_unlock(&fs_info->chunk_mutex); |
| 4725 | ret = btrfs_commit_transaction(trans); |
| 4726 | if (ret) |
| 4727 | goto done; |
| 4728 | } else { |
| 4729 | mutex_unlock(&fs_info->chunk_mutex); |
| 4730 | btrfs_end_transaction(trans); |
| 4731 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4732 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4733 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4734 | key.objectid = device->devid; |
| 4735 | key.offset = (u64)-1; |
| 4736 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4737 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4738 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4739 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4740 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4741 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4742 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4743 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4744 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4745 | |
| 4746 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4747 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4748 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4749 | if (ret < 0) |
| 4750 | goto done; |
| 4751 | if (ret) { |
| 4752 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4753 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4754 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | l = path->nodes[0]; |
| 4758 | slot = path->slots[0]; |
| 4759 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4760 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4761 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4762 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4763 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4764 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4765 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4766 | |
| 4767 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4768 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4769 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4770 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4771 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4772 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4773 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4774 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4775 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4776 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4777 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4778 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4779 | /* |
| 4780 | * We may be relocating the only data chunk we have, |
| 4781 | * which could potentially end up with losing data's |
| 4782 | * raid profile, so lets allocate an empty one in |
| 4783 | * advance. |
| 4784 | */ |
| 4785 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4786 | if (ret < 0) { |
| 4787 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4788 | goto done; |
| 4789 | } |
| 4790 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4791 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4792 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4793 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4794 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4795 | } else if (ret) { |
| 4796 | if (ret == -ETXTBSY) { |
| 4797 | btrfs_warn(fs_info, |
| 4798 | "could not shrink block group %llu due to active swapfile", |
| 4799 | chunk_offset); |
| 4800 | } |
| 4801 | goto done; |
| 4802 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4803 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4804 | |
| 4805 | if (failed && !retried) { |
| 4806 | failed = 0; |
| 4807 | retried = true; |
| 4808 | goto again; |
| 4809 | } else if (failed && retried) { |
| 4810 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4811 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4812 | } |
| 4813 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4814 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4815 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4816 | if (IS_ERR(trans)) { |
| 4817 | ret = PTR_ERR(trans); |
| 4818 | goto done; |
| 4819 | } |
| 4820 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4821 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4822 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4823 | if (list_empty(&device->post_commit_list)) |
| 4824 | list_add_tail(&device->post_commit_list, |
| 4825 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4826 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4827 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4828 | btrfs_set_super_total_bytes(super_copy, |
| 4829 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4830 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4831 | |
| 4832 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4833 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4834 | if (ret < 0) { |
| 4835 | btrfs_abort_transaction(trans, ret); |
| 4836 | btrfs_end_transaction(trans); |
| 4837 | } else { |
| 4838 | ret = btrfs_commit_transaction(trans); |
| 4839 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4840 | done: |
| 4841 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4842 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4843 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4844 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4845 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4846 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4847 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4848 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4849 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4850 | return ret; |
| 4851 | } |
| 4852 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4853 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4854 | struct btrfs_key *key, |
| 4855 | struct btrfs_chunk *chunk, int item_size) |
| 4856 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4857 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4858 | struct btrfs_disk_key disk_key; |
| 4859 | u32 array_size; |
| 4860 | u8 *ptr; |
| 4861 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4862 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4863 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4864 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4865 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4866 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4867 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4868 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4869 | |
| 4870 | ptr = super_copy->sys_chunk_array + array_size; |
| 4871 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4872 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4873 | ptr += sizeof(disk_key); |
| 4874 | memcpy(ptr, chunk, item_size); |
| 4875 | item_size += sizeof(disk_key); |
| 4876 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4877 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4878 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4879 | return 0; |
| 4880 | } |
| 4881 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4882 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4883 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4884 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4885 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4886 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4887 | const struct btrfs_device_info *di_a = a; |
| 4888 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4889 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4890 | if (di_a->max_avail > di_b->max_avail) |
| 4891 | return -1; |
| 4892 | if (di_a->max_avail < di_b->max_avail) |
| 4893 | return 1; |
| 4894 | if (di_a->total_avail > di_b->total_avail) |
| 4895 | return -1; |
| 4896 | if (di_a->total_avail < di_b->total_avail) |
| 4897 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4898 | return 0; |
| 4899 | } |
| 4900 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4901 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4902 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4903 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4904 | return; |
| 4905 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4906 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4907 | } |
| 4908 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4909 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4910 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4911 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4912 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4913 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4914 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4915 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4916 | struct extent_map_tree *em_tree; |
| 4917 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4918 | struct btrfs_device_info *devices_info = NULL; |
| 4919 | u64 total_avail; |
| 4920 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4921 | int data_stripes; /* number of stripes that count for |
| 4922 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4923 | int sub_stripes; /* sub_stripes info for map */ |
| 4924 | int dev_stripes; /* stripes per dev */ |
| 4925 | int devs_max; /* max devs to use */ |
| 4926 | int devs_min; /* min devs needed */ |
| 4927 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4928 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4929 | int nparity; /* number of stripes worth of bytes to |
| 4930 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4931 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4932 | u64 max_stripe_size; |
| 4933 | u64 max_chunk_size; |
| 4934 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4935 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4936 | int ndevs; |
| 4937 | int i; |
| 4938 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4939 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4940 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4941 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4942 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4943 | if (list_empty(&fs_devices->alloc_list)) { |
| 4944 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4945 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4946 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4947 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4948 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4949 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4950 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4951 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4952 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4953 | devs_max = btrfs_raid_array[index].devs_max; |
| 4954 | devs_min = btrfs_raid_array[index].devs_min; |
| 4955 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4956 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4957 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4958 | |
| 4959 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4960 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4961 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4962 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4963 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4964 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4965 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4966 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4967 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4968 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4969 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4970 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4971 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4972 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4973 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4974 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4975 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4976 | if (!devs_max) |
| 4977 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4978 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4979 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4980 | type); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4981 | BUG(); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4982 | } |
| 4983 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4984 | /* We don't want a chunk larger than 10% of writable space */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4985 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4986 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4987 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4988 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4989 | GFP_NOFS); |
| 4990 | if (!devices_info) |
| 4991 | return -ENOMEM; |
| 4992 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4993 | /* |
| 4994 | * in the first pass through the devices list, we gather information |
| 4995 | * about the available holes on each device. |
| 4996 | */ |
| 4997 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4998 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4999 | u64 max_avail; |
| 5000 | u64 dev_offset; |
| 5001 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5002 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 5003 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5004 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5005 | continue; |
| 5006 | } |
| 5007 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 5008 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 5009 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 5010 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5011 | continue; |
| 5012 | |
| 5013 | if (device->total_bytes > device->bytes_used) |
| 5014 | total_avail = device->total_bytes - device->bytes_used; |
| 5015 | else |
| 5016 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 5017 | |
| 5018 | /* If there is no space on this device, skip it. */ |
| 5019 | if (total_avail == 0) |
| 5020 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5021 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 5022 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5023 | max_stripe_size * dev_stripes, |
| 5024 | &dev_offset, &max_avail); |
| 5025 | if (ret && ret != -ENOSPC) |
| 5026 | goto error; |
| 5027 | |
| 5028 | if (ret == 0) |
| 5029 | max_avail = max_stripe_size * dev_stripes; |
| 5030 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5031 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 5032 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 5033 | btrfs_debug(info, |
| 5034 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 5035 | __func__, device->devid, max_avail, |
| 5036 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5037 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5038 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5039 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 5040 | if (ndevs == fs_devices->rw_devices) { |
| 5041 | WARN(1, "%s: found more than %llu devices\n", |
| 5042 | __func__, fs_devices->rw_devices); |
| 5043 | break; |
| 5044 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5045 | devices_info[ndevs].dev_offset = dev_offset; |
| 5046 | devices_info[ndevs].max_avail = max_avail; |
| 5047 | devices_info[ndevs].total_avail = total_avail; |
| 5048 | devices_info[ndevs].dev = device; |
| 5049 | ++ndevs; |
| 5050 | } |
| 5051 | |
| 5052 | /* |
| 5053 | * now sort the devices by hole size / available space |
| 5054 | */ |
| 5055 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 5056 | btrfs_cmp_device_info, NULL); |
| 5057 | |
| 5058 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 5059 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5060 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5061 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5062 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5063 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5064 | btrfs_debug(info, |
| 5065 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5066 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5067 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5068 | goto error; |
| 5069 | } |
| 5070 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 5071 | ndevs = min(ndevs, devs_max); |
| 5072 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5073 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5074 | * The primary goal is to maximize the number of stripes, so use as |
| 5075 | * many devices as possible, even if the stripes are not maximum sized. |
| 5076 | * |
| 5077 | * The DUP profile stores more than one stripe per device, the |
| 5078 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5079 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5080 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5081 | num_stripes = ndevs * dev_stripes; |
| 5082 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5083 | /* |
| 5084 | * this will have to be fixed for RAID1 and RAID10 over |
| 5085 | * more drives |
| 5086 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 5087 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5088 | |
| 5089 | /* |
| 5090 | * Use the number of data stripes to figure out how big this chunk |
| 5091 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5092 | * and compare that answer with the max chunk size. If it's higher, |
| 5093 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5094 | */ |
| 5095 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5096 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5097 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5098 | * then use it, unless it ends up being even bigger than the |
| 5099 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5100 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5101 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 5102 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5103 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5104 | } |
| 5105 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5106 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5107 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5108 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5109 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 5110 | if (!map) { |
| 5111 | ret = -ENOMEM; |
| 5112 | goto error; |
| 5113 | } |
| 5114 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5115 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5116 | for (i = 0; i < ndevs; ++i) { |
| 5117 | for (j = 0; j < dev_stripes; ++j) { |
| 5118 | int s = i * dev_stripes + j; |
| 5119 | map->stripes[s].dev = devices_info[i].dev; |
| 5120 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5121 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5122 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5123 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5124 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5125 | map->io_align = BTRFS_STRIPE_LEN; |
| 5126 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5127 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5128 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5129 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5130 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5131 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5132 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5133 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5134 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5135 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5136 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5137 | ret = -ENOMEM; |
| 5138 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5139 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5140 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5141 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5142 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5143 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5144 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5145 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5146 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5147 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5148 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5149 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5150 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5151 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5152 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5153 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5154 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5155 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5156 | write_unlock(&em_tree->lock); |
| 5157 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5158 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5159 | if (ret) |
| 5160 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5161 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5162 | for (i = 0; i < map->num_stripes; i++) { |
| 5163 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5164 | |
| 5165 | btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size); |
| 5166 | if (list_empty(&dev->post_commit_list)) |
| 5167 | list_add_tail(&dev->post_commit_list, |
| 5168 | &trans->transaction->dev_update_list); |
| 5169 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5170 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 5171 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5172 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5173 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5174 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5175 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5176 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5177 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5178 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5179 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5180 | write_lock(&em_tree->lock); |
| 5181 | remove_extent_mapping(em_tree, em); |
| 5182 | write_unlock(&em_tree->lock); |
| 5183 | |
| 5184 | /* One for our allocation */ |
| 5185 | free_extent_map(em); |
| 5186 | /* One for the tree reference */ |
| 5187 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5188 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5189 | kfree(devices_info); |
| 5190 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5191 | } |
| 5192 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5193 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5194 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5195 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5196 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5197 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5198 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5199 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5200 | struct btrfs_device *device; |
| 5201 | struct btrfs_chunk *chunk; |
| 5202 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5203 | struct extent_map *em; |
| 5204 | struct map_lookup *map; |
| 5205 | size_t item_size; |
| 5206 | u64 dev_offset; |
| 5207 | u64 stripe_size; |
| 5208 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5209 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5210 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5211 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5212 | if (IS_ERR(em)) |
| 5213 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5214 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5215 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5216 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5217 | stripe_size = em->orig_block_len; |
| 5218 | |
| 5219 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5220 | if (!chunk) { |
| 5221 | ret = -ENOMEM; |
| 5222 | goto out; |
| 5223 | } |
| 5224 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5225 | /* |
| 5226 | * Take the device list mutex to prevent races with the final phase of |
| 5227 | * a device replace operation that replaces the device object associated |
| 5228 | * with the map's stripes, because the device object's id can change |
| 5229 | * at any time during that final phase of the device replace operation |
| 5230 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5231 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5232 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5233 | for (i = 0; i < map->num_stripes; i++) { |
| 5234 | device = map->stripes[i].dev; |
| 5235 | dev_offset = map->stripes[i].physical; |
| 5236 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5237 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5238 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5239 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5240 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5241 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5242 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5243 | break; |
| 5244 | } |
| 5245 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5246 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5247 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5248 | } |
| 5249 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5250 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5251 | for (i = 0; i < map->num_stripes; i++) { |
| 5252 | device = map->stripes[i].dev; |
| 5253 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5254 | |
| 5255 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5256 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5257 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5258 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5259 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5260 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5261 | |
| 5262 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5263 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5264 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5265 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5266 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5267 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5268 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5269 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5270 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5271 | |
| 5272 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5273 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5274 | key.offset = chunk_offset; |
| 5275 | |
| 5276 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5277 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5278 | /* |
| 5279 | * TODO: Cleanup of inserted chunk root in case of |
| 5280 | * failure. |
| 5281 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5282 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5283 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5284 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5285 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5286 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5287 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5288 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5292 | * Chunk allocation falls into two parts. The first part does work |
| 5293 | * that makes the new allocated chunk usable, but does not do any operation |
| 5294 | * that modifies the chunk tree. The second part does the work that |
| 5295 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5296 | * bootstrap process of adding storage to a seed btrfs. |
| 5297 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5298 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5299 | { |
| 5300 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5301 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5302 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5303 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5304 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5305 | } |
| 5306 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5307 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5308 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5309 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5310 | u64 chunk_offset; |
| 5311 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5312 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5313 | int ret; |
| 5314 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5315 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5316 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5317 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5318 | if (ret) |
| 5319 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5320 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5321 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5322 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5323 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5324 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5325 | } |
| 5326 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5327 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5328 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5329 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5330 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5331 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5332 | } |
| 5333 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5334 | 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] | 5335 | { |
| 5336 | struct extent_map *em; |
| 5337 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5338 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5339 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5340 | int i; |
| 5341 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5342 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5343 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5344 | return 1; |
| 5345 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5346 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5347 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5348 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5349 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5350 | miss_ndevs++; |
| 5351 | continue; |
| 5352 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5353 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5354 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5355 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5356 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5357 | } |
| 5358 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5359 | |
| 5360 | /* |
| 5361 | * If the number of missing devices is larger than max errors, |
| 5362 | * we can not write the data into that chunk successfully, so |
| 5363 | * set it readonly. |
| 5364 | */ |
| 5365 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5366 | readonly = 1; |
| 5367 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5368 | free_extent_map(em); |
| 5369 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5370 | } |
| 5371 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5372 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5373 | { |
| 5374 | struct extent_map *em; |
| 5375 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5376 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5377 | write_lock(&tree->lock); |
| 5378 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5379 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5380 | remove_extent_mapping(tree, em); |
| 5381 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5382 | if (!em) |
| 5383 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5384 | /* once for us */ |
| 5385 | free_extent_map(em); |
| 5386 | /* once for the tree */ |
| 5387 | free_extent_map(em); |
| 5388 | } |
| 5389 | } |
| 5390 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5391 | 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] | 5392 | { |
| 5393 | struct extent_map *em; |
| 5394 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5395 | int ret; |
| 5396 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5397 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5398 | if (IS_ERR(em)) |
| 5399 | /* |
| 5400 | * We could return errors for these cases, but that could get |
| 5401 | * ugly and we'd probably do the same thing which is just not do |
| 5402 | * anything else and exit, so return 1 so the callers don't try |
| 5403 | * to use other copies. |
| 5404 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5405 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5406 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5407 | map = em->map_lookup; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5408 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 5409 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5410 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5411 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5412 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5413 | ret = 2; |
| 5414 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5415 | /* |
| 5416 | * There could be two corrupted data stripes, we need |
| 5417 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5418 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5419 | * Fail a stripe at a time on every retry except the |
| 5420 | * stripe under reconstruction. |
| 5421 | */ |
| 5422 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5423 | else |
| 5424 | ret = 1; |
| 5425 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5426 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5427 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5428 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5429 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5430 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5431 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5432 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5433 | return ret; |
| 5434 | } |
| 5435 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5436 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5437 | u64 logical) |
| 5438 | { |
| 5439 | struct extent_map *em; |
| 5440 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5441 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5442 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5443 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5444 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5445 | if (!WARN_ON(IS_ERR(em))) { |
| 5446 | map = em->map_lookup; |
| 5447 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5448 | len = map->stripe_len * nr_data_stripes(map); |
| 5449 | free_extent_map(em); |
| 5450 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5451 | return len; |
| 5452 | } |
| 5453 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5454 | 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] | 5455 | { |
| 5456 | struct extent_map *em; |
| 5457 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5458 | int ret = 0; |
| 5459 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5460 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5461 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5462 | if(!WARN_ON(IS_ERR(em))) { |
| 5463 | map = em->map_lookup; |
| 5464 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5465 | ret = 1; |
| 5466 | free_extent_map(em); |
| 5467 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5468 | return ret; |
| 5469 | } |
| 5470 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5471 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5472 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5473 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5474 | { |
| 5475 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5476 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5477 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5478 | int tolerance; |
| 5479 | struct btrfs_device *srcdev; |
| 5480 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5481 | ASSERT((map->type & |
| 5482 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))); |
| 5483 | |
| 5484 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5485 | num_stripes = map->sub_stripes; |
| 5486 | else |
| 5487 | num_stripes = map->num_stripes; |
| 5488 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5489 | preferred_mirror = first + current->pid % num_stripes; |
| 5490 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5491 | if (dev_replace_is_ongoing && |
| 5492 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5493 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5494 | srcdev = fs_info->dev_replace.srcdev; |
| 5495 | else |
| 5496 | srcdev = NULL; |
| 5497 | |
| 5498 | /* |
| 5499 | * try to avoid the drive that is the source drive for a |
| 5500 | * dev-replace procedure, only choose it if no other non-missing |
| 5501 | * mirror is available |
| 5502 | */ |
| 5503 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5504 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5505 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5506 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5507 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5508 | if (map->stripes[i].dev->bdev && |
| 5509 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5510 | return i; |
| 5511 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5512 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5513 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5514 | /* we couldn't find one that doesn't fail. Just return something |
| 5515 | * and the io error handling code will clean up eventually |
| 5516 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5517 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5518 | } |
| 5519 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5520 | static inline int parity_smaller(u64 a, u64 b) |
| 5521 | { |
| 5522 | return a > b; |
| 5523 | } |
| 5524 | |
| 5525 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5526 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5527 | { |
| 5528 | struct btrfs_bio_stripe s; |
| 5529 | int i; |
| 5530 | u64 l; |
| 5531 | int again = 1; |
| 5532 | |
| 5533 | while (again) { |
| 5534 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5535 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5536 | if (parity_smaller(bbio->raid_map[i], |
| 5537 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5538 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5539 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5540 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5541 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5542 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5543 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5544 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5545 | again = 1; |
| 5546 | } |
| 5547 | } |
| 5548 | } |
| 5549 | } |
| 5550 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5551 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5552 | { |
| 5553 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5554 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5555 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5556 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5557 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5558 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5559 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5560 | /* |
| 5561 | * plus the raid_map, which includes both the tgt dev |
| 5562 | * and the stripes |
| 5563 | */ |
| 5564 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5565 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5566 | |
| 5567 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5568 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5569 | |
| 5570 | return bbio; |
| 5571 | } |
| 5572 | |
| 5573 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5574 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5575 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5576 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5580 | { |
| 5581 | if (!bbio) |
| 5582 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5583 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5584 | kfree(bbio); |
| 5585 | } |
| 5586 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5587 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5588 | /* |
| 5589 | * Please note that, discard won't be sent to target device of device |
| 5590 | * replace. |
| 5591 | */ |
| 5592 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5593 | u64 logical, u64 length, |
| 5594 | struct btrfs_bio **bbio_ret) |
| 5595 | { |
| 5596 | struct extent_map *em; |
| 5597 | struct map_lookup *map; |
| 5598 | struct btrfs_bio *bbio; |
| 5599 | u64 offset; |
| 5600 | u64 stripe_nr; |
| 5601 | u64 stripe_nr_end; |
| 5602 | u64 stripe_end_offset; |
| 5603 | u64 stripe_cnt; |
| 5604 | u64 stripe_len; |
| 5605 | u64 stripe_offset; |
| 5606 | u64 num_stripes; |
| 5607 | u32 stripe_index; |
| 5608 | u32 factor = 0; |
| 5609 | u32 sub_stripes = 0; |
| 5610 | u64 stripes_per_dev = 0; |
| 5611 | u32 remaining_stripes = 0; |
| 5612 | u32 last_stripe = 0; |
| 5613 | int ret = 0; |
| 5614 | int i; |
| 5615 | |
| 5616 | /* discard always return a bbio */ |
| 5617 | ASSERT(bbio_ret); |
| 5618 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5619 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5620 | if (IS_ERR(em)) |
| 5621 | return PTR_ERR(em); |
| 5622 | |
| 5623 | map = em->map_lookup; |
| 5624 | /* we don't discard raid56 yet */ |
| 5625 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5626 | ret = -EOPNOTSUPP; |
| 5627 | goto out; |
| 5628 | } |
| 5629 | |
| 5630 | offset = logical - em->start; |
| 5631 | length = min_t(u64, em->len - offset, length); |
| 5632 | |
| 5633 | stripe_len = map->stripe_len; |
| 5634 | /* |
| 5635 | * stripe_nr counts the total number of stripes we have to stride |
| 5636 | * to get to this block |
| 5637 | */ |
| 5638 | stripe_nr = div64_u64(offset, stripe_len); |
| 5639 | |
| 5640 | /* stripe_offset is the offset of this block in its stripe */ |
| 5641 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5642 | |
| 5643 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5644 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5645 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5646 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5647 | (offset + length); |
| 5648 | /* |
| 5649 | * after this, stripe_nr is the number of stripes on this |
| 5650 | * device we have to walk to find the data, and stripe_index is |
| 5651 | * the number of our device in the stripe array |
| 5652 | */ |
| 5653 | num_stripes = 1; |
| 5654 | stripe_index = 0; |
| 5655 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5656 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5657 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5658 | sub_stripes = 1; |
| 5659 | else |
| 5660 | sub_stripes = map->sub_stripes; |
| 5661 | |
| 5662 | factor = map->num_stripes / sub_stripes; |
| 5663 | num_stripes = min_t(u64, map->num_stripes, |
| 5664 | sub_stripes * stripe_cnt); |
| 5665 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5666 | stripe_index *= sub_stripes; |
| 5667 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5668 | &remaining_stripes); |
| 5669 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5670 | last_stripe *= sub_stripes; |
| 5671 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5672 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5673 | num_stripes = map->num_stripes; |
| 5674 | } else { |
| 5675 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5676 | &stripe_index); |
| 5677 | } |
| 5678 | |
| 5679 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5680 | if (!bbio) { |
| 5681 | ret = -ENOMEM; |
| 5682 | goto out; |
| 5683 | } |
| 5684 | |
| 5685 | for (i = 0; i < num_stripes; i++) { |
| 5686 | bbio->stripes[i].physical = |
| 5687 | map->stripes[stripe_index].physical + |
| 5688 | stripe_offset + stripe_nr * map->stripe_len; |
| 5689 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5690 | |
| 5691 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5692 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5693 | bbio->stripes[i].length = stripes_per_dev * |
| 5694 | map->stripe_len; |
| 5695 | |
| 5696 | if (i / sub_stripes < remaining_stripes) |
| 5697 | bbio->stripes[i].length += |
| 5698 | map->stripe_len; |
| 5699 | |
| 5700 | /* |
| 5701 | * Special for the first stripe and |
| 5702 | * the last stripe: |
| 5703 | * |
| 5704 | * |-------|...|-------| |
| 5705 | * |----------| |
| 5706 | * off end_off |
| 5707 | */ |
| 5708 | if (i < sub_stripes) |
| 5709 | bbio->stripes[i].length -= |
| 5710 | stripe_offset; |
| 5711 | |
| 5712 | if (stripe_index >= last_stripe && |
| 5713 | stripe_index <= (last_stripe + |
| 5714 | sub_stripes - 1)) |
| 5715 | bbio->stripes[i].length -= |
| 5716 | stripe_end_offset; |
| 5717 | |
| 5718 | if (i == sub_stripes - 1) |
| 5719 | stripe_offset = 0; |
| 5720 | } else { |
| 5721 | bbio->stripes[i].length = length; |
| 5722 | } |
| 5723 | |
| 5724 | stripe_index++; |
| 5725 | if (stripe_index == map->num_stripes) { |
| 5726 | stripe_index = 0; |
| 5727 | stripe_nr++; |
| 5728 | } |
| 5729 | } |
| 5730 | |
| 5731 | *bbio_ret = bbio; |
| 5732 | bbio->map_type = map->type; |
| 5733 | bbio->num_stripes = num_stripes; |
| 5734 | out: |
| 5735 | free_extent_map(em); |
| 5736 | return ret; |
| 5737 | } |
| 5738 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5739 | /* |
| 5740 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5741 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5742 | * left cursor can also be read from the target drive. |
| 5743 | * |
| 5744 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5745 | * array of stripes. |
| 5746 | * For READ, it also needs to be supported using the same mirror number. |
| 5747 | * |
| 5748 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5749 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5750 | * case. |
| 5751 | */ |
| 5752 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5753 | u64 logical, u64 length, |
| 5754 | u64 srcdev_devid, int *mirror_num, |
| 5755 | u64 *physical) |
| 5756 | { |
| 5757 | struct btrfs_bio *bbio = NULL; |
| 5758 | int num_stripes; |
| 5759 | int index_srcdev = 0; |
| 5760 | int found = 0; |
| 5761 | u64 physical_of_found = 0; |
| 5762 | int i; |
| 5763 | int ret = 0; |
| 5764 | |
| 5765 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5766 | logical, &length, &bbio, 0, 0); |
| 5767 | if (ret) { |
| 5768 | ASSERT(bbio == NULL); |
| 5769 | return ret; |
| 5770 | } |
| 5771 | |
| 5772 | num_stripes = bbio->num_stripes; |
| 5773 | if (*mirror_num > num_stripes) { |
| 5774 | /* |
| 5775 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5776 | * that means that the requested area is not left of the left |
| 5777 | * cursor |
| 5778 | */ |
| 5779 | btrfs_put_bbio(bbio); |
| 5780 | return -EIO; |
| 5781 | } |
| 5782 | |
| 5783 | /* |
| 5784 | * process the rest of the function using the mirror_num of the source |
| 5785 | * drive. Therefore look it up first. At the end, patch the device |
| 5786 | * pointer to the one of the target drive. |
| 5787 | */ |
| 5788 | for (i = 0; i < num_stripes; i++) { |
| 5789 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5790 | continue; |
| 5791 | |
| 5792 | /* |
| 5793 | * In case of DUP, in order to keep it simple, only add the |
| 5794 | * mirror with the lowest physical address |
| 5795 | */ |
| 5796 | if (found && |
| 5797 | physical_of_found <= bbio->stripes[i].physical) |
| 5798 | continue; |
| 5799 | |
| 5800 | index_srcdev = i; |
| 5801 | found = 1; |
| 5802 | physical_of_found = bbio->stripes[i].physical; |
| 5803 | } |
| 5804 | |
| 5805 | btrfs_put_bbio(bbio); |
| 5806 | |
| 5807 | ASSERT(found); |
| 5808 | if (!found) |
| 5809 | return -EIO; |
| 5810 | |
| 5811 | *mirror_num = index_srcdev + 1; |
| 5812 | *physical = physical_of_found; |
| 5813 | return ret; |
| 5814 | } |
| 5815 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5816 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5817 | struct btrfs_bio **bbio_ret, |
| 5818 | struct btrfs_dev_replace *dev_replace, |
| 5819 | int *num_stripes_ret, int *max_errors_ret) |
| 5820 | { |
| 5821 | struct btrfs_bio *bbio = *bbio_ret; |
| 5822 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5823 | int tgtdev_indexes = 0; |
| 5824 | int num_stripes = *num_stripes_ret; |
| 5825 | int max_errors = *max_errors_ret; |
| 5826 | int i; |
| 5827 | |
| 5828 | if (op == BTRFS_MAP_WRITE) { |
| 5829 | int index_where_to_add; |
| 5830 | |
| 5831 | /* |
| 5832 | * duplicate the write operations while the dev replace |
| 5833 | * procedure is running. Since the copying of the old disk to |
| 5834 | * the new disk takes place at run time while the filesystem is |
| 5835 | * mounted writable, the regular write operations to the old |
| 5836 | * disk have to be duplicated to go to the new disk as well. |
| 5837 | * |
| 5838 | * Note that device->missing is handled by the caller, and that |
| 5839 | * the write to the old disk is already set up in the stripes |
| 5840 | * array. |
| 5841 | */ |
| 5842 | index_where_to_add = num_stripes; |
| 5843 | for (i = 0; i < num_stripes; i++) { |
| 5844 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5845 | /* write to new disk, too */ |
| 5846 | struct btrfs_bio_stripe *new = |
| 5847 | bbio->stripes + index_where_to_add; |
| 5848 | struct btrfs_bio_stripe *old = |
| 5849 | bbio->stripes + i; |
| 5850 | |
| 5851 | new->physical = old->physical; |
| 5852 | new->length = old->length; |
| 5853 | new->dev = dev_replace->tgtdev; |
| 5854 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5855 | index_where_to_add++; |
| 5856 | max_errors++; |
| 5857 | tgtdev_indexes++; |
| 5858 | } |
| 5859 | } |
| 5860 | num_stripes = index_where_to_add; |
| 5861 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5862 | int index_srcdev = 0; |
| 5863 | int found = 0; |
| 5864 | u64 physical_of_found = 0; |
| 5865 | |
| 5866 | /* |
| 5867 | * During the dev-replace procedure, the target drive can also |
| 5868 | * be used to read data in case it is needed to repair a corrupt |
| 5869 | * block elsewhere. This is possible if the requested area is |
| 5870 | * left of the left cursor. In this area, the target drive is a |
| 5871 | * full copy of the source drive. |
| 5872 | */ |
| 5873 | for (i = 0; i < num_stripes; i++) { |
| 5874 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5875 | /* |
| 5876 | * In case of DUP, in order to keep it simple, |
| 5877 | * only add the mirror with the lowest physical |
| 5878 | * address |
| 5879 | */ |
| 5880 | if (found && |
| 5881 | physical_of_found <= |
| 5882 | bbio->stripes[i].physical) |
| 5883 | continue; |
| 5884 | index_srcdev = i; |
| 5885 | found = 1; |
| 5886 | physical_of_found = bbio->stripes[i].physical; |
| 5887 | } |
| 5888 | } |
| 5889 | if (found) { |
| 5890 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5891 | bbio->stripes + num_stripes; |
| 5892 | |
| 5893 | tgtdev_stripe->physical = physical_of_found; |
| 5894 | tgtdev_stripe->length = |
| 5895 | bbio->stripes[index_srcdev].length; |
| 5896 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5897 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5898 | |
| 5899 | tgtdev_indexes++; |
| 5900 | num_stripes++; |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | *num_stripes_ret = num_stripes; |
| 5905 | *max_errors_ret = max_errors; |
| 5906 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5907 | *bbio_ret = bbio; |
| 5908 | } |
| 5909 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5910 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5911 | { |
| 5912 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5913 | } |
| 5914 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5915 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5916 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5917 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5918 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5919 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5920 | { |
| 5921 | struct extent_map *em; |
| 5922 | struct map_lookup *map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5923 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5924 | u64 stripe_offset; |
| 5925 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5926 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5927 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5928 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5929 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5930 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5931 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5932 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5933 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5934 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5935 | int dev_replace_is_ongoing = 0; |
| 5936 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5937 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5938 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5939 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5940 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5941 | if (op == BTRFS_MAP_DISCARD) |
| 5942 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 5943 | *length, bbio_ret); |
| 5944 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5945 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5946 | if (IS_ERR(em)) |
| 5947 | return PTR_ERR(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5948 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5949 | map = em->map_lookup; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5950 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5951 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5952 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5953 | stripe_nr = offset; |
| 5954 | /* |
| 5955 | * stripe_nr counts the total number of stripes we have to stride |
| 5956 | * to get to this block |
| 5957 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5958 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5959 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5960 | stripe_offset = stripe_nr * stripe_len; |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5961 | if (offset < stripe_offset) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5962 | btrfs_crit(fs_info, |
| 5963 | "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu", |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5964 | stripe_offset, offset, em->start, logical, |
| 5965 | stripe_len); |
| 5966 | free_extent_map(em); |
| 5967 | return -EINVAL; |
| 5968 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5969 | |
| 5970 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5971 | stripe_offset = offset - stripe_offset; |
| 5972 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5973 | /* if we're here for raid56, we need to know the stripe aligned start */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5974 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5975 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5976 | raid56_full_stripe_start = offset; |
| 5977 | |
| 5978 | /* allow a write of a full stripe, but make sure we don't |
| 5979 | * allow straddling of stripes |
| 5980 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5981 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5982 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5983 | raid56_full_stripe_start *= full_stripe_len; |
| 5984 | } |
| 5985 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5986 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5987 | u64 max_len; |
| 5988 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5989 | For other RAID types and for RAID[56] reads, just allow a single |
| 5990 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5991 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5992 | (op == BTRFS_MAP_WRITE)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5993 | max_len = stripe_len * nr_data_stripes(map) - |
| 5994 | (offset - raid56_full_stripe_start); |
| 5995 | } else { |
| 5996 | /* we limit the length of each bio to what fits in a stripe */ |
| 5997 | max_len = stripe_len - stripe_offset; |
| 5998 | } |
| 5999 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6000 | } else { |
| 6001 | *length = em->len - offset; |
| 6002 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6003 | |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 6004 | /* |
| 6005 | * This is for when we're called from btrfs_bio_fits_in_stripe and all |
| 6006 | * it cares about is the length |
| 6007 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6008 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6009 | goto out; |
| 6010 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6011 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6012 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6013 | /* |
| 6014 | * Hold the semaphore for read during the whole operation, write is |
| 6015 | * requested at commit time but must wait. |
| 6016 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6017 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6018 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6019 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6020 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6021 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6022 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 6023 | dev_replace->srcdev->devid, |
| 6024 | &mirror_num, |
| 6025 | &physical_to_patch_in_first_stripe); |
| 6026 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6027 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6028 | else |
| 6029 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6030 | } else if (mirror_num > map->num_stripes) { |
| 6031 | mirror_num = 0; |
| 6032 | } |
| 6033 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6034 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6035 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6036 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6037 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6038 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6039 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6040 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6041 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6042 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6043 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6044 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6045 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6046 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6047 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6048 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6049 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6050 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6051 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6052 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6053 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6054 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6055 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6056 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6057 | } else { |
| 6058 | mirror_num = 1; |
| 6059 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6060 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6061 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6062 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6063 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6064 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6065 | stripe_index *= map->sub_stripes; |
| 6066 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6067 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6068 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6069 | else if (mirror_num) |
| 6070 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6071 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6072 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6073 | stripe_index = find_live_mirror(fs_info, map, |
| 6074 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6075 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6076 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6077 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6078 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6079 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6080 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6081 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6082 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6083 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6084 | |
| 6085 | /* RAID[56] write or recovery. Return all stripes */ |
| 6086 | num_stripes = map->num_stripes; |
| 6087 | max_errors = nr_parity_stripes(map); |
| 6088 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6089 | *length = map->stripe_len; |
| 6090 | stripe_index = 0; |
| 6091 | stripe_offset = 0; |
| 6092 | } else { |
| 6093 | /* |
| 6094 | * Mirror #0 or #1 means the original data block. |
| 6095 | * Mirror #2 is RAID5 parity block. |
| 6096 | * Mirror #3 is RAID6 Q block. |
| 6097 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6098 | stripe_nr = div_u64_rem(stripe_nr, |
| 6099 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6100 | if (mirror_num > 1) |
| 6101 | stripe_index = nr_data_stripes(map) + |
| 6102 | mirror_num - 2; |
| 6103 | |
| 6104 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6105 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6106 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6107 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6108 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6109 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6110 | } else { |
| 6111 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6112 | * after this, stripe_nr is the number of stripes on this |
| 6113 | * device we have to walk to find the data, and stripe_index is |
| 6114 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6115 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6116 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6117 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6118 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6119 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6120 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6121 | btrfs_crit(fs_info, |
| 6122 | "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] | 6123 | stripe_index, map->num_stripes); |
| 6124 | ret = -EINVAL; |
| 6125 | goto out; |
| 6126 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6127 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6128 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6129 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6130 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6131 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6132 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6133 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6134 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6135 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6136 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6137 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6138 | if (!bbio) { |
| 6139 | ret = -ENOMEM; |
| 6140 | goto out; |
| 6141 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6142 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6143 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6144 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6145 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6146 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6147 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6148 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6149 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6150 | |
| 6151 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6152 | sizeof(struct btrfs_bio_stripe) * |
| 6153 | num_alloc_stripes + |
| 6154 | sizeof(int) * tgtdev_indexes); |
| 6155 | |
| 6156 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6157 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6158 | |
| 6159 | /* Fill in the logical address of each stripe */ |
| 6160 | tmp = stripe_nr * nr_data_stripes(map); |
| 6161 | for (i = 0; i < nr_data_stripes(map); i++) |
| 6162 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6163 | em->start + (tmp + i) * map->stripe_len; |
| 6164 | |
| 6165 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6166 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6167 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6168 | RAID6_Q_STRIPE; |
| 6169 | } |
| 6170 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6171 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6172 | for (i = 0; i < num_stripes; i++) { |
| 6173 | bbio->stripes[i].physical = |
| 6174 | map->stripes[stripe_index].physical + |
| 6175 | stripe_offset + |
| 6176 | stripe_nr * map->stripe_len; |
| 6177 | bbio->stripes[i].dev = |
| 6178 | map->stripes[stripe_index].dev; |
| 6179 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6180 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6181 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6182 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6183 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6184 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6185 | if (bbio->raid_map) |
| 6186 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6187 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6188 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6189 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6190 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6191 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6192 | } |
| 6193 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6194 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6195 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6196 | bbio->num_stripes = num_stripes; |
| 6197 | bbio->max_errors = max_errors; |
| 6198 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6199 | |
| 6200 | /* |
| 6201 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6202 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6203 | * available as a mirror |
| 6204 | */ |
| 6205 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6206 | WARN_ON(num_stripes > 1); |
| 6207 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6208 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6209 | bbio->mirror_num = map->num_stripes + 1; |
| 6210 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6211 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6212 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6213 | lockdep_assert_held(&dev_replace->rwsem); |
| 6214 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6215 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6216 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6217 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6218 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6219 | } |
| 6220 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6221 | 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] | 6222 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6223 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6224 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6225 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6226 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6227 | } |
| 6228 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6229 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6230 | 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] | 6231 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6232 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6233 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6234 | 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] | 6235 | } |
| 6236 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6237 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6238 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6239 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6240 | struct extent_map *em; |
| 6241 | struct map_lookup *map; |
| 6242 | u64 *buf; |
| 6243 | u64 bytenr; |
| 6244 | u64 length; |
| 6245 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6246 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6247 | int i, j, nr = 0; |
| 6248 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6249 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6250 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6251 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6252 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6253 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6254 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6255 | rmap_len = map->stripe_len; |
| 6256 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6257 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6258 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6259 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6260 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6261 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6262 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6263 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6264 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6265 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6266 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6267 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6268 | |
| 6269 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6270 | if (map->stripes[i].physical > physical || |
| 6271 | map->stripes[i].physical + length <= physical) |
| 6272 | continue; |
| 6273 | |
| 6274 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6275 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6276 | |
| 6277 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6278 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6279 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6280 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6281 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6282 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6283 | * Alternatively, just use rmap_len below instead of |
| 6284 | * map->stripe_len */ |
| 6285 | |
| 6286 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6287 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6288 | for (j = 0; j < nr; j++) { |
| 6289 | if (buf[j] == bytenr) |
| 6290 | break; |
| 6291 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6292 | if (j == nr) { |
| 6293 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6294 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6295 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6296 | } |
| 6297 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6298 | *logical = buf; |
| 6299 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6300 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6301 | |
| 6302 | free_extent_map(em); |
| 6303 | return 0; |
| 6304 | } |
| 6305 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6306 | 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] | 6307 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6308 | bio->bi_private = bbio->private; |
| 6309 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6310 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6311 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6312 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6313 | } |
| 6314 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6315 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6316 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6317 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6318 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6319 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6320 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6321 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6322 | if (bio->bi_status == BLK_STS_IOERR || |
| 6323 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6324 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6325 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6326 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6327 | |
| 6328 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6329 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6330 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6331 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6332 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6333 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6334 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6335 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6336 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6337 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6338 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6339 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6340 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6341 | } |
| 6342 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6343 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6344 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6345 | is_orig_bio = 1; |
| 6346 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6347 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6348 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6349 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6350 | if (!is_orig_bio) { |
| 6351 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6352 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6353 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6354 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6355 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6356 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6357 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6358 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6359 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6360 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6361 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6362 | /* |
| 6363 | * this bio is actually up to date, we didn't |
| 6364 | * go over the max number of errors |
| 6365 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6366 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6367 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6368 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6369 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6370 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6371 | bio_put(bio); |
| 6372 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6373 | } |
| 6374 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6375 | /* |
| 6376 | * see run_scheduled_bios for a description of why bios are collected for |
| 6377 | * async submit. |
| 6378 | * |
| 6379 | * This will add one bio to the pending list for a device and make sure |
| 6380 | * the work struct is scheduled. |
| 6381 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6382 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6383 | struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6384 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6385 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6386 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6387 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6388 | |
| 6389 | /* don't bother with additional async steps for reads, right now */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6390 | if (bio_op(bio) == REQ_OP_READ) { |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6391 | btrfsic_submit_bio(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6392 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6393 | } |
| 6394 | |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 6395 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6396 | bio->bi_next = NULL; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6397 | |
| 6398 | spin_lock(&device->io_lock); |
Christoph Hellwig | 67f055c | 2016-11-01 07:40:06 -0600 | [diff] [blame] | 6399 | if (op_is_sync(bio->bi_opf)) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6400 | pending_bios = &device->pending_sync_bios; |
| 6401 | else |
| 6402 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6403 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6404 | if (pending_bios->tail) |
| 6405 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6406 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6407 | pending_bios->tail = bio; |
| 6408 | if (!pending_bios->head) |
| 6409 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6410 | if (device->running_pending) |
| 6411 | should_queue = 0; |
| 6412 | |
| 6413 | spin_unlock(&device->io_lock); |
| 6414 | |
| 6415 | if (should_queue) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6416 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6417 | } |
| 6418 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6419 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
| 6420 | u64 physical, int dev_nr, int async) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6421 | { |
| 6422 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6423 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6424 | |
| 6425 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6426 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6427 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6428 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6429 | btrfs_debug_in_rcu(fs_info, |
| 6430 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6431 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6432 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6433 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6434 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6435 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6436 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6437 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6438 | if (async) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6439 | btrfs_schedule_bio(dev, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6440 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6441 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6442 | } |
| 6443 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6444 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6445 | { |
| 6446 | atomic_inc(&bbio->error); |
| 6447 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6448 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6449 | WARN_ON(bio != bbio->orig_bio); |
| 6450 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6451 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6452 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6453 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6454 | bio->bi_status = BLK_STS_IOERR; |
| 6455 | else |
| 6456 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6457 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6458 | } |
| 6459 | } |
| 6460 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6461 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
| 6462 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6463 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6464 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6465 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6466 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6467 | u64 length = 0; |
| 6468 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6469 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6470 | int dev_nr; |
| 6471 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6472 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6473 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6474 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6475 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6476 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6477 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6478 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6479 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6480 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6481 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6482 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6483 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6484 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6485 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6486 | bbio->orig_bio = first_bio; |
| 6487 | bbio->private = first_bio->bi_private; |
| 6488 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6489 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6490 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6491 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6492 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6493 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6494 | /* In this case, map_length has been set to the length of |
| 6495 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6496 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6497 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6498 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6499 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6500 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6501 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6502 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6503 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6504 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6505 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6506 | } |
| 6507 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6508 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6509 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6510 | "mapping failed logical %llu bio len %llu len %llu", |
| 6511 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6512 | BUG(); |
| 6513 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6514 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6515 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6516 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6517 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6518 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6519 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6520 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6521 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6522 | continue; |
| 6523 | } |
| 6524 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6525 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6526 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6527 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6528 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6529 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6530 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
| 6531 | dev_nr, async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6532 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6533 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6534 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6535 | } |
| 6536 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6537 | /* |
| 6538 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6539 | * return NULL. |
| 6540 | * |
| 6541 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6542 | * only devid is used. |
| 6543 | * |
| 6544 | * If @seed is true, traverse through the seed devices. |
| 6545 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6546 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6547 | u64 devid, u8 *uuid, u8 *fsid, |
| 6548 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6549 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6550 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6551 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6552 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6553 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6554 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6555 | list_for_each_entry(device, &fs_devices->devices, |
| 6556 | dev_list) { |
| 6557 | if (device->devid == devid && |
| 6558 | (!uuid || memcmp(device->uuid, uuid, |
| 6559 | BTRFS_UUID_SIZE) == 0)) |
| 6560 | return device; |
| 6561 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6562 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6563 | if (seed) |
| 6564 | fs_devices = fs_devices->seed; |
| 6565 | else |
| 6566 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6567 | } |
| 6568 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6569 | } |
| 6570 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6571 | 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] | 6572 | u64 devid, u8 *dev_uuid) |
| 6573 | { |
| 6574 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6575 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6576 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6577 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6578 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6579 | |
| 6580 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6581 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6582 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6583 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6584 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6585 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6586 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6587 | return device; |
| 6588 | } |
| 6589 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6590 | /** |
| 6591 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6592 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6593 | * devid is provided (i.e. @devid != NULL). |
| 6594 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6595 | * is generated. |
| 6596 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6597 | * is generated. |
| 6598 | * |
| 6599 | * 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] | 6600 | * 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] | 6601 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6602 | */ |
| 6603 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6604 | const u64 *devid, |
| 6605 | const u8 *uuid) |
| 6606 | { |
| 6607 | struct btrfs_device *dev; |
| 6608 | u64 tmp; |
| 6609 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6610 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6611 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6612 | |
| 6613 | dev = __alloc_device(); |
| 6614 | if (IS_ERR(dev)) |
| 6615 | return dev; |
| 6616 | |
| 6617 | if (devid) |
| 6618 | tmp = *devid; |
| 6619 | else { |
| 6620 | int ret; |
| 6621 | |
| 6622 | ret = find_next_devid(fs_info, &tmp); |
| 6623 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6624 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6625 | return ERR_PTR(ret); |
| 6626 | } |
| 6627 | } |
| 6628 | dev->devid = tmp; |
| 6629 | |
| 6630 | if (uuid) |
| 6631 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6632 | else |
| 6633 | generate_random_uuid(dev->uuid); |
| 6634 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6635 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6636 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6637 | |
| 6638 | return dev; |
| 6639 | } |
| 6640 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6641 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6642 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6643 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6644 | if (error) |
| 6645 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6646 | devid, uuid); |
| 6647 | else |
| 6648 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6649 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6650 | } |
| 6651 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6652 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6653 | { |
| 6654 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6655 | int ncopies = btrfs_raid_array[index].ncopies; |
| 6656 | int data_stripes; |
| 6657 | |
| 6658 | switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 6659 | case BTRFS_BLOCK_GROUP_RAID5: |
| 6660 | data_stripes = num_stripes - 1; |
| 6661 | break; |
| 6662 | case BTRFS_BLOCK_GROUP_RAID6: |
| 6663 | data_stripes = num_stripes - 2; |
| 6664 | break; |
| 6665 | default: |
| 6666 | data_stripes = num_stripes / ncopies; |
| 6667 | break; |
| 6668 | } |
| 6669 | return div_u64(chunk_len, data_stripes); |
| 6670 | } |
| 6671 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6672 | 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] | 6673 | struct btrfs_chunk *chunk) |
| 6674 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6675 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6676 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6677 | struct map_lookup *map; |
| 6678 | struct extent_map *em; |
| 6679 | u64 logical; |
| 6680 | u64 length; |
| 6681 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6682 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6683 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6684 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6685 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6686 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6687 | logical = key->offset; |
| 6688 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6689 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6690 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6691 | /* |
| 6692 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6693 | * as chunk item in tree block is already verified by tree-checker. |
| 6694 | */ |
| 6695 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6696 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6697 | if (ret) |
| 6698 | return ret; |
| 6699 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6700 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6701 | read_lock(&map_tree->lock); |
| 6702 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6703 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6704 | |
| 6705 | /* already mapped? */ |
| 6706 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6707 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6708 | return 0; |
| 6709 | } else if (em) { |
| 6710 | free_extent_map(em); |
| 6711 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6712 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6713 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6714 | if (!em) |
| 6715 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6716 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6717 | if (!map) { |
| 6718 | free_extent_map(em); |
| 6719 | return -ENOMEM; |
| 6720 | } |
| 6721 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6722 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6723 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6724 | em->start = logical; |
| 6725 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6726 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6727 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6728 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6729 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6730 | map->num_stripes = num_stripes; |
| 6731 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6732 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6733 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6734 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6735 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6736 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6737 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6738 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6739 | for (i = 0; i < num_stripes; i++) { |
| 6740 | map->stripes[i].physical = |
| 6741 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6742 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6743 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6744 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6745 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6746 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6747 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6748 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6749 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6750 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6751 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6752 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6753 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6754 | if (!map->stripes[i].dev) { |
| 6755 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6756 | add_missing_dev(fs_info->fs_devices, devid, |
| 6757 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6758 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6759 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6760 | btrfs_err(fs_info, |
| 6761 | "failed to init missing dev %llu: %ld", |
| 6762 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6763 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6764 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6765 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6766 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6767 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6768 | &(map->stripes[i].dev->dev_state)); |
| 6769 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6770 | } |
| 6771 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6772 | write_lock(&map_tree->lock); |
| 6773 | ret = add_extent_mapping(map_tree, em, 0); |
| 6774 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6775 | if (ret < 0) { |
| 6776 | btrfs_err(fs_info, |
| 6777 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6778 | em->start, em->len, ret); |
| 6779 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6780 | free_extent_map(em); |
| 6781 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6782 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6783 | } |
| 6784 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6785 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6786 | struct btrfs_dev_item *dev_item, |
| 6787 | struct btrfs_device *device) |
| 6788 | { |
| 6789 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6790 | |
| 6791 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6792 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6793 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6794 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6795 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6796 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6797 | device->type = btrfs_device_type(leaf, dev_item); |
| 6798 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6799 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6800 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6801 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6802 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6803 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6804 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6805 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6806 | } |
| 6807 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6808 | 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] | 6809 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6810 | { |
| 6811 | struct btrfs_fs_devices *fs_devices; |
| 6812 | int ret; |
| 6813 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6814 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6815 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6816 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6817 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6818 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6819 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6820 | return fs_devices; |
| 6821 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6822 | fs_devices = fs_devices->seed; |
| 6823 | } |
| 6824 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6825 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6826 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6827 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6828 | return ERR_PTR(-ENOENT); |
| 6829 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6830 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6831 | if (IS_ERR(fs_devices)) |
| 6832 | return fs_devices; |
| 6833 | |
| 6834 | fs_devices->seeding = 1; |
| 6835 | fs_devices->opened = 1; |
| 6836 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6837 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6838 | |
| 6839 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6840 | if (IS_ERR(fs_devices)) |
| 6841 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6842 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6843 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6844 | if (ret) { |
| 6845 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6846 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6847 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6848 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6849 | |
| 6850 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6851 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6852 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6853 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6854 | goto out; |
| 6855 | } |
| 6856 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6857 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6858 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6859 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6860 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6861 | } |
| 6862 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6863 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6864 | struct btrfs_dev_item *dev_item) |
| 6865 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6866 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6867 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6868 | struct btrfs_device *device; |
| 6869 | u64 devid; |
| 6870 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6871 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6872 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6873 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6874 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6875 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6876 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6877 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6878 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6879 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6880 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6881 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6882 | if (IS_ERR(fs_devices)) |
| 6883 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6884 | } |
| 6885 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6886 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6887 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6888 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6889 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6890 | btrfs_report_missing_device(fs_info, devid, |
| 6891 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6892 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6893 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6894 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6895 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6896 | if (IS_ERR(device)) { |
| 6897 | btrfs_err(fs_info, |
| 6898 | "failed to add missing dev %llu: %ld", |
| 6899 | devid, PTR_ERR(device)); |
| 6900 | return PTR_ERR(device); |
| 6901 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6902 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6903 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6904 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6905 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6906 | btrfs_report_missing_device(fs_info, |
| 6907 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6908 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6909 | } |
| 6910 | btrfs_report_missing_device(fs_info, devid, |
| 6911 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6912 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6913 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6914 | if (!device->bdev && |
| 6915 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6916 | /* |
| 6917 | * this happens when a device that was properly setup |
| 6918 | * in the device info lists suddenly goes bad. |
| 6919 | * device->bdev is NULL, and so we have to set |
| 6920 | * device->missing to one here |
| 6921 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6922 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6923 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6924 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6925 | |
| 6926 | /* Move the device to its own fs_devices */ |
| 6927 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6928 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6929 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6930 | |
| 6931 | list_move(&device->dev_list, &fs_devices->devices); |
| 6932 | device->fs_devices->num_devices--; |
| 6933 | fs_devices->num_devices++; |
| 6934 | |
| 6935 | device->fs_devices->missing_devices--; |
| 6936 | fs_devices->missing_devices++; |
| 6937 | |
| 6938 | device->fs_devices = fs_devices; |
| 6939 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6940 | } |
| 6941 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6942 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6943 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6944 | if (device->generation != |
| 6945 | btrfs_device_generation(leaf, dev_item)) |
| 6946 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6947 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6948 | |
| 6949 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6950 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6951 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6952 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6953 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6954 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6955 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6956 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6957 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6958 | return ret; |
| 6959 | } |
| 6960 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6961 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6962 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6963 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6964 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6965 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6966 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6967 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6968 | u8 *array_ptr; |
| 6969 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6970 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6971 | u32 num_stripes; |
| 6972 | u32 array_size; |
| 6973 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6974 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6975 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6976 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6977 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6978 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6979 | /* |
| 6980 | * This will create extent buffer of nodesize, superblock size is |
| 6981 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6982 | * overallocate but we can keep it as-is, only the first page is used. |
| 6983 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6984 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6985 | if (IS_ERR(sb)) |
| 6986 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6987 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6988 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6989 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6990 | * 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] | 6991 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6992 | * pages up-to-date when the page is larger: extent does not cover the |
| 6993 | * whole page and consequently check_page_uptodate does not find all |
| 6994 | * the page's extents up-to-date (the hole beyond sb), |
| 6995 | * write_extent_buffer then triggers a WARN_ON. |
| 6996 | * |
| 6997 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6998 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6999 | * to silence the warning eg. on PowerPC 64. |
| 7000 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7001 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7002 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 7003 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7004 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7005 | array_size = btrfs_super_sys_array_size(super_copy); |
| 7006 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7007 | array_ptr = super_copy->sys_chunk_array; |
| 7008 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 7009 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7010 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7011 | while (cur_offset < array_size) { |
| 7012 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7013 | len = sizeof(*disk_key); |
| 7014 | if (cur_offset + len > array_size) |
| 7015 | goto out_short_read; |
| 7016 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7017 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 7018 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7019 | array_ptr += len; |
| 7020 | sb_array_offset += len; |
| 7021 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7022 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7023 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7024 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7025 | /* |
| 7026 | * At least one btrfs_chunk with one stripe must be |
| 7027 | * present, exact stripe count check comes afterwards |
| 7028 | */ |
| 7029 | len = btrfs_chunk_item_size(1); |
| 7030 | if (cur_offset + len > array_size) |
| 7031 | goto out_short_read; |
| 7032 | |
| 7033 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7034 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7035 | btrfs_err(fs_info, |
| 7036 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7037 | num_stripes, cur_offset); |
| 7038 | ret = -EIO; |
| 7039 | break; |
| 7040 | } |
| 7041 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7042 | type = btrfs_chunk_type(sb, chunk); |
| 7043 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7044 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7045 | "invalid chunk type %llu in sys_array at offset %u", |
| 7046 | type, cur_offset); |
| 7047 | ret = -EIO; |
| 7048 | break; |
| 7049 | } |
| 7050 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7051 | len = btrfs_chunk_item_size(num_stripes); |
| 7052 | if (cur_offset + len > array_size) |
| 7053 | goto out_short_read; |
| 7054 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7055 | ret = read_one_chunk(&key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7056 | if (ret) |
| 7057 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7058 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7059 | btrfs_err(fs_info, |
| 7060 | "unexpected item type %u in sys_array at offset %u", |
| 7061 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7062 | ret = -EIO; |
| 7063 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 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 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7069 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7070 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7071 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7072 | |
| 7073 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7074 | 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] | 7075 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7076 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7077 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7078 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7079 | } |
| 7080 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7081 | /* |
| 7082 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 7083 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7084 | * If the @failing_dev is specified, it's accounted as missing. |
| 7085 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7086 | * Return true if all chunks meet the minimal RW mount requirements. |
| 7087 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 7088 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7089 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 7090 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7091 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7092 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7093 | struct extent_map *em; |
| 7094 | u64 next_start = 0; |
| 7095 | bool ret = true; |
| 7096 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7097 | read_lock(&map_tree->lock); |
| 7098 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 7099 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7100 | /* No chunk at all? Return false anyway */ |
| 7101 | if (!em) { |
| 7102 | ret = false; |
| 7103 | goto out; |
| 7104 | } |
| 7105 | while (em) { |
| 7106 | struct map_lookup *map; |
| 7107 | int missing = 0; |
| 7108 | int max_tolerated; |
| 7109 | int i; |
| 7110 | |
| 7111 | map = em->map_lookup; |
| 7112 | max_tolerated = |
| 7113 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 7114 | map->type); |
| 7115 | for (i = 0; i < map->num_stripes; i++) { |
| 7116 | struct btrfs_device *dev = map->stripes[i].dev; |
| 7117 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 7118 | if (!dev || !dev->bdev || |
| 7119 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7120 | dev->last_flush_error) |
| 7121 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7122 | else if (failing_dev && failing_dev == dev) |
| 7123 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7124 | } |
| 7125 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7126 | if (!failing_dev) |
| 7127 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7128 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7129 | em->start, missing, max_tolerated); |
| 7130 | free_extent_map(em); |
| 7131 | ret = false; |
| 7132 | goto out; |
| 7133 | } |
| 7134 | next_start = extent_map_end(em); |
| 7135 | free_extent_map(em); |
| 7136 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7137 | read_lock(&map_tree->lock); |
| 7138 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7139 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7140 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7141 | } |
| 7142 | out: |
| 7143 | return ret; |
| 7144 | } |
| 7145 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7146 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7147 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7148 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7149 | struct btrfs_path *path; |
| 7150 | struct extent_buffer *leaf; |
| 7151 | struct btrfs_key key; |
| 7152 | struct btrfs_key found_key; |
| 7153 | int ret; |
| 7154 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7155 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7156 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7157 | path = btrfs_alloc_path(); |
| 7158 | if (!path) |
| 7159 | return -ENOMEM; |
| 7160 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7161 | /* |
| 7162 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7163 | * otherwise we don't need it. |
| 7164 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7165 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7166 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7167 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7168 | /* |
| 7169 | * Read all device items, and then all the chunk items. All |
| 7170 | * device items are found before any chunk item (their object id |
| 7171 | * is smaller than the lowest possible object id for a chunk |
| 7172 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7173 | */ |
| 7174 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7175 | key.offset = 0; |
| 7176 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7177 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7178 | if (ret < 0) |
| 7179 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7180 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7181 | leaf = path->nodes[0]; |
| 7182 | slot = path->slots[0]; |
| 7183 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7184 | ret = btrfs_next_leaf(root, path); |
| 7185 | if (ret == 0) |
| 7186 | continue; |
| 7187 | if (ret < 0) |
| 7188 | goto error; |
| 7189 | break; |
| 7190 | } |
| 7191 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7192 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7193 | struct btrfs_dev_item *dev_item; |
| 7194 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7195 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7196 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7197 | if (ret) |
| 7198 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7199 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7200 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7201 | struct btrfs_chunk *chunk; |
| 7202 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7203 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7204 | if (ret) |
| 7205 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7206 | } |
| 7207 | path->slots[0]++; |
| 7208 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7209 | |
| 7210 | /* |
| 7211 | * After loading chunk tree, we've got all device information, |
| 7212 | * do another round of validation checks. |
| 7213 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7214 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7215 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7216 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7217 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7218 | total_dev); |
| 7219 | ret = -EINVAL; |
| 7220 | goto error; |
| 7221 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7222 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7223 | fs_info->fs_devices->total_rw_bytes) { |
| 7224 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7225 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7226 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7227 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7228 | ret = -EINVAL; |
| 7229 | goto error; |
| 7230 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7231 | ret = 0; |
| 7232 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7233 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7234 | mutex_unlock(&uuid_mutex); |
| 7235 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7236 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7237 | return ret; |
| 7238 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7239 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7240 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7241 | { |
| 7242 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7243 | struct btrfs_device *device; |
| 7244 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7245 | while (fs_devices) { |
| 7246 | mutex_lock(&fs_devices->device_list_mutex); |
| 7247 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7248 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7249 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7250 | |
| 7251 | fs_devices = fs_devices->seed; |
| 7252 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7255 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 7256 | { |
| 7257 | int i; |
| 7258 | |
| 7259 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7260 | btrfs_dev_stat_reset(dev, i); |
| 7261 | } |
| 7262 | |
| 7263 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7264 | { |
| 7265 | struct btrfs_key key; |
| 7266 | struct btrfs_key found_key; |
| 7267 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7268 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7269 | struct extent_buffer *eb; |
| 7270 | int slot; |
| 7271 | int ret = 0; |
| 7272 | struct btrfs_device *device; |
| 7273 | struct btrfs_path *path = NULL; |
| 7274 | int i; |
| 7275 | |
| 7276 | path = btrfs_alloc_path(); |
| 7277 | if (!path) { |
| 7278 | ret = -ENOMEM; |
| 7279 | goto out; |
| 7280 | } |
| 7281 | |
| 7282 | mutex_lock(&fs_devices->device_list_mutex); |
| 7283 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7284 | int item_size; |
| 7285 | struct btrfs_dev_stats_item *ptr; |
| 7286 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7287 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7288 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7289 | key.offset = device->devid; |
| 7290 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7291 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7292 | __btrfs_reset_dev_stats(device); |
| 7293 | device->dev_stats_valid = 1; |
| 7294 | btrfs_release_path(path); |
| 7295 | continue; |
| 7296 | } |
| 7297 | slot = path->slots[0]; |
| 7298 | eb = path->nodes[0]; |
| 7299 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 7300 | item_size = btrfs_item_size_nr(eb, slot); |
| 7301 | |
| 7302 | ptr = btrfs_item_ptr(eb, slot, |
| 7303 | struct btrfs_dev_stats_item); |
| 7304 | |
| 7305 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7306 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7307 | btrfs_dev_stat_set(device, i, |
| 7308 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7309 | else |
| 7310 | btrfs_dev_stat_reset(device, i); |
| 7311 | } |
| 7312 | |
| 7313 | device->dev_stats_valid = 1; |
| 7314 | btrfs_dev_stat_print_on_load(device); |
| 7315 | btrfs_release_path(path); |
| 7316 | } |
| 7317 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7318 | |
| 7319 | out: |
| 7320 | btrfs_free_path(path); |
| 7321 | return ret < 0 ? ret : 0; |
| 7322 | } |
| 7323 | |
| 7324 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7325 | struct btrfs_device *device) |
| 7326 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7327 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7328 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7329 | struct btrfs_path *path; |
| 7330 | struct btrfs_key key; |
| 7331 | struct extent_buffer *eb; |
| 7332 | struct btrfs_dev_stats_item *ptr; |
| 7333 | int ret; |
| 7334 | int i; |
| 7335 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7336 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7337 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7338 | key.offset = device->devid; |
| 7339 | |
| 7340 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7341 | if (!path) |
| 7342 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7343 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7344 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7345 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7346 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7347 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7348 | goto out; |
| 7349 | } |
| 7350 | |
| 7351 | if (ret == 0 && |
| 7352 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7353 | /* need to delete old one and insert a new one */ |
| 7354 | ret = btrfs_del_item(trans, dev_root, path); |
| 7355 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7356 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7357 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7358 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7359 | goto out; |
| 7360 | } |
| 7361 | ret = 1; |
| 7362 | } |
| 7363 | |
| 7364 | if (ret == 1) { |
| 7365 | /* need to insert a new item */ |
| 7366 | btrfs_release_path(path); |
| 7367 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7368 | &key, sizeof(*ptr)); |
| 7369 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7370 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7371 | "insert dev_stats item for device %s failed %d", |
| 7372 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7373 | goto out; |
| 7374 | } |
| 7375 | } |
| 7376 | |
| 7377 | eb = path->nodes[0]; |
| 7378 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7379 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7380 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7381 | btrfs_dev_stat_read(device, i)); |
| 7382 | btrfs_mark_buffer_dirty(eb); |
| 7383 | |
| 7384 | out: |
| 7385 | btrfs_free_path(path); |
| 7386 | return ret; |
| 7387 | } |
| 7388 | |
| 7389 | /* |
| 7390 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7391 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7392 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7393 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7394 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7395 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7396 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7397 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7398 | int ret = 0; |
| 7399 | |
| 7400 | mutex_lock(&fs_devices->device_list_mutex); |
| 7401 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7402 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7403 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7404 | continue; |
| 7405 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7406 | |
| 7407 | /* |
| 7408 | * There is a LOAD-LOAD control dependency between the value of |
| 7409 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7410 | * reading the in-memory counters. Such control dependencies |
| 7411 | * require explicit read memory barriers. |
| 7412 | * |
| 7413 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7414 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7415 | * barrier implied by atomic_xchg in |
| 7416 | * btrfs_dev_stats_read_and_reset |
| 7417 | */ |
| 7418 | smp_rmb(); |
| 7419 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7420 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7421 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7422 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7423 | } |
| 7424 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7425 | |
| 7426 | return ret; |
| 7427 | } |
| 7428 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7429 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7430 | { |
| 7431 | btrfs_dev_stat_inc(dev, index); |
| 7432 | btrfs_dev_stat_print_on_error(dev); |
| 7433 | } |
| 7434 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7435 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7436 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7437 | if (!dev->dev_stats_valid) |
| 7438 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7439 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7440 | "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] | 7441 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7442 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7443 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7444 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7445 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7446 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7447 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7448 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7449 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7450 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7451 | int i; |
| 7452 | |
| 7453 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7454 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7455 | break; |
| 7456 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7457 | return; /* all values == 0, suppress message */ |
| 7458 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7459 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7460 | "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] | 7461 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7462 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7463 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7464 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7465 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7466 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7467 | } |
| 7468 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7469 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7470 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7471 | { |
| 7472 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7473 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7474 | int i; |
| 7475 | |
| 7476 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7477 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7478 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7479 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7480 | |
| 7481 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7482 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7483 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7484 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7485 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7486 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7487 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7488 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7489 | if (stats->nr_items > i) |
| 7490 | stats->values[i] = |
| 7491 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7492 | else |
| 7493 | btrfs_dev_stat_reset(dev, i); |
| 7494 | } |
| 7495 | } else { |
| 7496 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7497 | if (stats->nr_items > i) |
| 7498 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7499 | } |
| 7500 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7501 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7502 | return 0; |
| 7503 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7504 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7505 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7506 | { |
| 7507 | struct buffer_head *bh; |
| 7508 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7509 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7510 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7511 | if (!bdev) |
| 7512 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7513 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7514 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7515 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7516 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7517 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7518 | continue; |
| 7519 | |
| 7520 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7521 | |
| 7522 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7523 | set_buffer_dirty(bh); |
| 7524 | sync_dirty_buffer(bh); |
| 7525 | brelse(bh); |
| 7526 | } |
| 7527 | |
| 7528 | /* Notify udev that device has changed */ |
| 7529 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7530 | |
| 7531 | /* Update ctime/mtime for device path for libblkid */ |
| 7532 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7533 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7534 | |
| 7535 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7536 | * Update the size and bytes used for each device where it changed. This is |
| 7537 | * delayed since we would otherwise get errors while writing out the |
| 7538 | * superblocks. |
| 7539 | * |
| 7540 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7541 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7542 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7543 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7544 | struct btrfs_device *curr, *next; |
| 7545 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7546 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7547 | |
| 7548 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7549 | return; |
| 7550 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7551 | /* |
| 7552 | * We don't need the device_list_mutex here. This list is owned by the |
| 7553 | * transaction and the transaction must complete before the device is |
| 7554 | * released. |
| 7555 | */ |
| 7556 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7557 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7558 | post_commit_list) { |
| 7559 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7560 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7561 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7562 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7563 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7564 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7565 | |
| 7566 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7567 | { |
| 7568 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7569 | while (fs_devices) { |
| 7570 | fs_devices->fs_info = fs_info; |
| 7571 | fs_devices = fs_devices->seed; |
| 7572 | } |
| 7573 | } |
| 7574 | |
| 7575 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7576 | { |
| 7577 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7578 | while (fs_devices) { |
| 7579 | fs_devices->fs_info = NULL; |
| 7580 | fs_devices = fs_devices->seed; |
| 7581 | } |
| 7582 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7583 | |
| 7584 | /* |
| 7585 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7586 | */ |
| 7587 | int btrfs_bg_type_to_factor(u64 flags) |
| 7588 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame^] | 7589 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7590 | |
| 7591 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7592 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7593 | |
| 7594 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7595 | |
| 7596 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7597 | u64 chunk_offset, u64 devid, |
| 7598 | u64 physical_offset, u64 physical_len) |
| 7599 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7600 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7601 | struct extent_map *em; |
| 7602 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7603 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7604 | u64 stripe_len; |
| 7605 | bool found = false; |
| 7606 | int ret = 0; |
| 7607 | int i; |
| 7608 | |
| 7609 | read_lock(&em_tree->lock); |
| 7610 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7611 | read_unlock(&em_tree->lock); |
| 7612 | |
| 7613 | if (!em) { |
| 7614 | btrfs_err(fs_info, |
| 7615 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7616 | physical_offset, devid); |
| 7617 | ret = -EUCLEAN; |
| 7618 | goto out; |
| 7619 | } |
| 7620 | |
| 7621 | map = em->map_lookup; |
| 7622 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7623 | if (physical_len != stripe_len) { |
| 7624 | btrfs_err(fs_info, |
| 7625 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7626 | physical_offset, devid, em->start, physical_len, |
| 7627 | stripe_len); |
| 7628 | ret = -EUCLEAN; |
| 7629 | goto out; |
| 7630 | } |
| 7631 | |
| 7632 | for (i = 0; i < map->num_stripes; i++) { |
| 7633 | if (map->stripes[i].dev->devid == devid && |
| 7634 | map->stripes[i].physical == physical_offset) { |
| 7635 | found = true; |
| 7636 | if (map->verified_stripes >= map->num_stripes) { |
| 7637 | btrfs_err(fs_info, |
| 7638 | "too many dev extents for chunk %llu found", |
| 7639 | em->start); |
| 7640 | ret = -EUCLEAN; |
| 7641 | goto out; |
| 7642 | } |
| 7643 | map->verified_stripes++; |
| 7644 | break; |
| 7645 | } |
| 7646 | } |
| 7647 | if (!found) { |
| 7648 | btrfs_err(fs_info, |
| 7649 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7650 | physical_offset, devid); |
| 7651 | ret = -EUCLEAN; |
| 7652 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7653 | |
| 7654 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7655 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7656 | if (!dev) { |
| 7657 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7658 | ret = -EUCLEAN; |
| 7659 | goto out; |
| 7660 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7661 | |
| 7662 | /* It's possible this device is a dummy for seed device */ |
| 7663 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7664 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7665 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7666 | if (!dev) { |
| 7667 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7668 | devid); |
| 7669 | ret = -EUCLEAN; |
| 7670 | goto out; |
| 7671 | } |
| 7672 | } |
| 7673 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7674 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7675 | btrfs_err(fs_info, |
| 7676 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7677 | devid, physical_offset, physical_len, |
| 7678 | dev->disk_total_bytes); |
| 7679 | ret = -EUCLEAN; |
| 7680 | goto out; |
| 7681 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7682 | out: |
| 7683 | free_extent_map(em); |
| 7684 | return ret; |
| 7685 | } |
| 7686 | |
| 7687 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7688 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7689 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7690 | struct extent_map *em; |
| 7691 | struct rb_node *node; |
| 7692 | int ret = 0; |
| 7693 | |
| 7694 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7695 | 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] | 7696 | em = rb_entry(node, struct extent_map, rb_node); |
| 7697 | if (em->map_lookup->num_stripes != |
| 7698 | em->map_lookup->verified_stripes) { |
| 7699 | btrfs_err(fs_info, |
| 7700 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7701 | em->start, em->map_lookup->verified_stripes, |
| 7702 | em->map_lookup->num_stripes); |
| 7703 | ret = -EUCLEAN; |
| 7704 | goto out; |
| 7705 | } |
| 7706 | } |
| 7707 | out: |
| 7708 | read_unlock(&em_tree->lock); |
| 7709 | return ret; |
| 7710 | } |
| 7711 | |
| 7712 | /* |
| 7713 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7714 | * later chunk allocation/free would cause unexpected behavior. |
| 7715 | * |
| 7716 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7717 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7718 | */ |
| 7719 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7720 | { |
| 7721 | struct btrfs_path *path; |
| 7722 | struct btrfs_root *root = fs_info->dev_root; |
| 7723 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7724 | u64 prev_devid = 0; |
| 7725 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7726 | int ret = 0; |
| 7727 | |
| 7728 | key.objectid = 1; |
| 7729 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7730 | key.offset = 0; |
| 7731 | |
| 7732 | path = btrfs_alloc_path(); |
| 7733 | if (!path) |
| 7734 | return -ENOMEM; |
| 7735 | |
| 7736 | path->reada = READA_FORWARD; |
| 7737 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7738 | if (ret < 0) |
| 7739 | goto out; |
| 7740 | |
| 7741 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7742 | ret = btrfs_next_item(root, path); |
| 7743 | if (ret < 0) |
| 7744 | goto out; |
| 7745 | /* No dev extents at all? Not good */ |
| 7746 | if (ret > 0) { |
| 7747 | ret = -EUCLEAN; |
| 7748 | goto out; |
| 7749 | } |
| 7750 | } |
| 7751 | while (1) { |
| 7752 | struct extent_buffer *leaf = path->nodes[0]; |
| 7753 | struct btrfs_dev_extent *dext; |
| 7754 | int slot = path->slots[0]; |
| 7755 | u64 chunk_offset; |
| 7756 | u64 physical_offset; |
| 7757 | u64 physical_len; |
| 7758 | u64 devid; |
| 7759 | |
| 7760 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7761 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7762 | break; |
| 7763 | devid = key.objectid; |
| 7764 | physical_offset = key.offset; |
| 7765 | |
| 7766 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7767 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7768 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7769 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7770 | /* Check if this dev extent overlaps with the previous one */ |
| 7771 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7772 | btrfs_err(fs_info, |
| 7773 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7774 | devid, physical_offset, prev_dev_ext_end); |
| 7775 | ret = -EUCLEAN; |
| 7776 | goto out; |
| 7777 | } |
| 7778 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7779 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7780 | physical_offset, physical_len); |
| 7781 | if (ret < 0) |
| 7782 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7783 | prev_devid = devid; |
| 7784 | prev_dev_ext_end = physical_offset + physical_len; |
| 7785 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7786 | ret = btrfs_next_item(root, path); |
| 7787 | if (ret < 0) |
| 7788 | goto out; |
| 7789 | if (ret > 0) { |
| 7790 | ret = 0; |
| 7791 | break; |
| 7792 | } |
| 7793 | } |
| 7794 | |
| 7795 | /* Ensure all chunks have corresponding dev extents */ |
| 7796 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7797 | out: |
| 7798 | btrfs_free_path(path); |
| 7799 | return ret; |
| 7800 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7801 | |
| 7802 | /* |
| 7803 | * Check whether the given block group or device is pinned by any inode being |
| 7804 | * used as a swapfile. |
| 7805 | */ |
| 7806 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7807 | { |
| 7808 | struct btrfs_swapfile_pin *sp; |
| 7809 | struct rb_node *node; |
| 7810 | |
| 7811 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7812 | node = fs_info->swapfile_pins.rb_node; |
| 7813 | while (node) { |
| 7814 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7815 | if (ptr < sp->ptr) |
| 7816 | node = node->rb_left; |
| 7817 | else if (ptr > sp->ptr) |
| 7818 | node = node->rb_right; |
| 7819 | else |
| 7820 | break; |
| 7821 | } |
| 7822 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7823 | return node != NULL; |
| 7824 | } |