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" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 30 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 31 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 32 | [BTRFS_RAID_RAID10] = { |
| 33 | .sub_stripes = 2, |
| 34 | .dev_stripes = 1, |
| 35 | .devs_max = 0, /* 0 == as many as possible */ |
| 36 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 37 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 38 | .devs_increment = 2, |
| 39 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 40 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 41 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 42 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 43 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 44 | }, |
| 45 | [BTRFS_RAID_RAID1] = { |
| 46 | .sub_stripes = 1, |
| 47 | .dev_stripes = 1, |
| 48 | .devs_max = 2, |
| 49 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 50 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 51 | .devs_increment = 2, |
| 52 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 53 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 54 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 55 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 56 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 57 | }, |
| 58 | [BTRFS_RAID_DUP] = { |
| 59 | .sub_stripes = 1, |
| 60 | .dev_stripes = 2, |
| 61 | .devs_max = 1, |
| 62 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 63 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 64 | .devs_increment = 1, |
| 65 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 66 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 67 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 68 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 69 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 70 | }, |
| 71 | [BTRFS_RAID_RAID0] = { |
| 72 | .sub_stripes = 1, |
| 73 | .dev_stripes = 1, |
| 74 | .devs_max = 0, |
| 75 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 76 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 77 | .devs_increment = 1, |
| 78 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 79 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 80 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 81 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 82 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 83 | }, |
| 84 | [BTRFS_RAID_SINGLE] = { |
| 85 | .sub_stripes = 1, |
| 86 | .dev_stripes = 1, |
| 87 | .devs_max = 1, |
| 88 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 89 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 90 | .devs_increment = 1, |
| 91 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 92 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 93 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 94 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 95 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 96 | }, |
| 97 | [BTRFS_RAID_RAID5] = { |
| 98 | .sub_stripes = 1, |
| 99 | .dev_stripes = 1, |
| 100 | .devs_max = 0, |
| 101 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 102 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 103 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 104 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 105 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 106 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 107 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 108 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 109 | }, |
| 110 | [BTRFS_RAID_RAID6] = { |
| 111 | .sub_stripes = 1, |
| 112 | .dev_stripes = 1, |
| 113 | .devs_max = 0, |
| 114 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 115 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 116 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 117 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 118 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 119 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 120 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 121 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 122 | }, |
| 123 | }; |
| 124 | |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 125 | const char *get_raid_name(enum btrfs_raid_types type) |
| 126 | { |
| 127 | if (type >= BTRFS_NR_RAID_TYPES) |
| 128 | return NULL; |
| 129 | |
| 130 | return btrfs_raid_array[type].raid_name; |
| 131 | } |
| 132 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame^] | 133 | /* |
| 134 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 135 | * bytes including terminating null byte. |
| 136 | */ |
| 137 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 138 | { |
| 139 | int i; |
| 140 | int ret; |
| 141 | char *bp = buf; |
| 142 | u64 flags = bg_flags; |
| 143 | u32 size_bp = size_buf; |
| 144 | |
| 145 | if (!flags) { |
| 146 | strcpy(bp, "NONE"); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | #define DESCRIBE_FLAG(flag, desc) \ |
| 151 | do { \ |
| 152 | if (flags & (flag)) { \ |
| 153 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 154 | if (ret < 0 || ret >= size_bp) \ |
| 155 | goto out_overflow; \ |
| 156 | size_bp -= ret; \ |
| 157 | bp += ret; \ |
| 158 | flags &= ~(flag); \ |
| 159 | } \ |
| 160 | } while (0) |
| 161 | |
| 162 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 163 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 164 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 165 | |
| 166 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 167 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 168 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 169 | btrfs_raid_array[i].raid_name); |
| 170 | #undef DESCRIBE_FLAG |
| 171 | |
| 172 | if (flags) { |
| 173 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 174 | size_bp -= ret; |
| 175 | } |
| 176 | |
| 177 | if (size_bp < size_buf) |
| 178 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 179 | |
| 180 | /* |
| 181 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 182 | * large buffer |
| 183 | */ |
| 184 | out_overflow:; |
| 185 | } |
| 186 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 187 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 188 | struct btrfs_fs_info *fs_info); |
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, |
| 215 | * seeding, structure cloning, openning/closing devices at mount/umount time |
| 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); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 321 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 322 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 323 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 324 | if (fsid) |
| 325 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 326 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 327 | if (metadata_fsid) |
| 328 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 329 | else if (fsid) |
| 330 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 331 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 332 | return fs_devs; |
| 333 | } |
| 334 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 335 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 336 | { |
| 337 | rcu_string_free(device->name); |
| 338 | bio_put(device->flush_bio); |
| 339 | kfree(device); |
| 340 | } |
| 341 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 342 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 343 | { |
| 344 | struct btrfs_device *device; |
| 345 | WARN_ON(fs_devices->opened); |
| 346 | while (!list_empty(&fs_devices->devices)) { |
| 347 | device = list_entry(fs_devices->devices.next, |
| 348 | struct btrfs_device, dev_list); |
| 349 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 350 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 351 | } |
| 352 | kfree(fs_devices); |
| 353 | } |
| 354 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 355 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 356 | enum kobject_action action) |
| 357 | { |
| 358 | int ret; |
| 359 | |
| 360 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 361 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 362 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 363 | action, |
| 364 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 365 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 366 | } |
| 367 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 368 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 369 | { |
| 370 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 371 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 372 | while (!list_empty(&fs_uuids)) { |
| 373 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 374 | struct btrfs_fs_devices, fs_list); |
| 375 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 376 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 377 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 378 | } |
| 379 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 380 | /* |
| 381 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 382 | * 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] | 383 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 384 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 385 | static struct btrfs_device *__alloc_device(void) |
| 386 | { |
| 387 | struct btrfs_device *dev; |
| 388 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 389 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 390 | if (!dev) |
| 391 | return ERR_PTR(-ENOMEM); |
| 392 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 393 | /* |
| 394 | * Preallocate a bio that's always going to be used for flushing device |
| 395 | * barriers and matches the device lifespan |
| 396 | */ |
| 397 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 398 | if (!dev->flush_bio) { |
| 399 | kfree(dev); |
| 400 | return ERR_PTR(-ENOMEM); |
| 401 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 402 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 403 | INIT_LIST_HEAD(&dev->dev_list); |
| 404 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 405 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 406 | |
| 407 | spin_lock_init(&dev->io_lock); |
| 408 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 409 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 410 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 411 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 412 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 413 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 414 | |
| 415 | return dev; |
| 416 | } |
| 417 | |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 418 | /* |
| 419 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 420 | * return NULL. |
| 421 | * |
| 422 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 423 | * only devid is used. |
| 424 | */ |
| 425 | static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices, |
| 426 | u64 devid, const u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 427 | { |
| 428 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 429 | |
Anand Jain | 636d2c9 | 2018-04-12 10:29:29 +0800 | [diff] [blame] | 430 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 431 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 432 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 433 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 434 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 439 | static noinline struct btrfs_fs_devices *find_fsid( |
| 440 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 441 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 442 | struct btrfs_fs_devices *fs_devices; |
| 443 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 444 | ASSERT(fsid); |
| 445 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 446 | if (metadata_fsid) { |
| 447 | /* |
| 448 | * Handle scanned device having completed its fsid change but |
| 449 | * belonging to a fs_devices that was created by first scanning |
| 450 | * a device which didn't have its fsid/metadata_uuid changed |
| 451 | * at all and the CHANGING_FSID_V2 flag set. |
| 452 | */ |
| 453 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 454 | if (fs_devices->fsid_change && |
| 455 | memcmp(metadata_fsid, fs_devices->fsid, |
| 456 | BTRFS_FSID_SIZE) == 0 && |
| 457 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 458 | BTRFS_FSID_SIZE) == 0) { |
| 459 | return fs_devices; |
| 460 | } |
| 461 | } |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 462 | /* |
| 463 | * Handle scanned device having completed its fsid change but |
| 464 | * belonging to a fs_devices that was created by a device that |
| 465 | * has an outdated pair of fsid/metadata_uuid and |
| 466 | * CHANGING_FSID_V2 flag set. |
| 467 | */ |
| 468 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 469 | if (fs_devices->fsid_change && |
| 470 | memcmp(fs_devices->metadata_uuid, |
| 471 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 472 | memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 473 | BTRFS_FSID_SIZE) == 0) { |
| 474 | return fs_devices; |
| 475 | } |
| 476 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 480 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 481 | if (metadata_fsid) { |
| 482 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 483 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 484 | BTRFS_FSID_SIZE) == 0) |
| 485 | return fs_devices; |
| 486 | } else { |
| 487 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 488 | return fs_devices; |
| 489 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 490 | } |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 494 | static int |
| 495 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 496 | int flush, struct block_device **bdev, |
| 497 | struct buffer_head **bh) |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 502 | |
| 503 | if (IS_ERR(*bdev)) { |
| 504 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 505 | goto error; |
| 506 | } |
| 507 | |
| 508 | if (flush) |
| 509 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 510 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 511 | if (ret) { |
| 512 | blkdev_put(*bdev, flags); |
| 513 | goto error; |
| 514 | } |
| 515 | invalidate_bdev(*bdev); |
| 516 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 517 | if (IS_ERR(*bh)) { |
| 518 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 519 | blkdev_put(*bdev, flags); |
| 520 | goto error; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | |
| 525 | error: |
| 526 | *bdev = NULL; |
| 527 | *bh = NULL; |
| 528 | return ret; |
| 529 | } |
| 530 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 531 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 532 | struct bio *head, struct bio *tail) |
| 533 | { |
| 534 | |
| 535 | struct bio *old_head; |
| 536 | |
| 537 | old_head = pending_bios->head; |
| 538 | pending_bios->head = head; |
| 539 | if (pending_bios->tail) |
| 540 | tail->bi_next = old_head; |
| 541 | else |
| 542 | pending_bios->tail = tail; |
| 543 | } |
| 544 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 545 | /* |
| 546 | * we try to collect pending bios for a device so we don't get a large |
| 547 | * number of procs sending bios down to the same device. This greatly |
| 548 | * improves the schedulers ability to collect and merge the bios. |
| 549 | * |
| 550 | * But, it also turns into a long list of bios to process and that is sure |
| 551 | * to eventually make the worker thread block. The solution here is to |
| 552 | * make some progress and then put this work struct back at the end of |
| 553 | * the list if the block device is congested. This way, multiple devices |
| 554 | * can make progress from a single worker thread. |
| 555 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 556 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 557 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 558 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 559 | struct bio *pending; |
| 560 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 561 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 562 | struct bio *tail; |
| 563 | struct bio *cur; |
| 564 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 565 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 566 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 567 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 568 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 569 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 570 | struct blk_plug plug; |
| 571 | |
| 572 | /* |
| 573 | * this function runs all the bios we've collected for |
| 574 | * a particular device. We don't want to wander off to |
| 575 | * another device without first sending all of these down. |
| 576 | * So, setup a plug here and finish it off before we return |
| 577 | */ |
| 578 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 579 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 580 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 581 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 582 | loop: |
| 583 | spin_lock(&device->io_lock); |
| 584 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 585 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 586 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 587 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 588 | /* take all the bios off the list at once and process them |
| 589 | * later on (without the lock held). But, remember the |
| 590 | * tail and other pointers so the bios can be properly reinserted |
| 591 | * into the list if we hit congestion |
| 592 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 593 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 594 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 595 | force_reg = 1; |
| 596 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 597 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 598 | force_reg = 0; |
| 599 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 600 | |
| 601 | pending = pending_bios->head; |
| 602 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 603 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * if pending was null this time around, no bios need processing |
| 607 | * at all and we can stop. Otherwise it'll loop back up again |
| 608 | * and do an additional check so no bios are missed. |
| 609 | * |
| 610 | * device->running_pending is used to synchronize with the |
| 611 | * schedule_bio code. |
| 612 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 613 | if (device->pending_sync_bios.head == NULL && |
| 614 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 615 | again = 0; |
| 616 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 617 | } else { |
| 618 | again = 1; |
| 619 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 620 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 621 | |
| 622 | pending_bios->head = NULL; |
| 623 | pending_bios->tail = NULL; |
| 624 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 625 | spin_unlock(&device->io_lock); |
| 626 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 627 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 628 | |
| 629 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 630 | /* we want to work on both lists, but do more bios on the |
| 631 | * sync list than the regular list |
| 632 | */ |
| 633 | if ((num_run > 32 && |
| 634 | pending_bios != &device->pending_sync_bios && |
| 635 | device->pending_sync_bios.head) || |
| 636 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 637 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 638 | spin_lock(&device->io_lock); |
| 639 | requeue_list(pending_bios, pending, tail); |
| 640 | goto loop_lock; |
| 641 | } |
| 642 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 643 | cur = pending; |
| 644 | pending = pending->bi_next; |
| 645 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 646 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 647 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 648 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 649 | /* |
| 650 | * if we're doing the sync list, record that our |
| 651 | * plug has some sync requests on it |
| 652 | * |
| 653 | * If we're doing the regular list and there are |
| 654 | * sync requests sitting around, unplug before |
| 655 | * we add more |
| 656 | */ |
| 657 | if (pending_bios == &device->pending_sync_bios) { |
| 658 | sync_pending = 1; |
| 659 | } else if (sync_pending) { |
| 660 | blk_finish_plug(&plug); |
| 661 | blk_start_plug(&plug); |
| 662 | sync_pending = 0; |
| 663 | } |
| 664 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 665 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 666 | num_run++; |
| 667 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 668 | |
| 669 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * we made progress, there is more work to do and the bdi |
| 673 | * is now congested. Back off and let other work structs |
| 674 | * run instead |
| 675 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 676 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 677 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 678 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 679 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 680 | ioc = current->io_context; |
| 681 | |
| 682 | /* |
| 683 | * the main goal here is that we don't want to |
| 684 | * block if we're going to be able to submit |
| 685 | * more requests without blocking. |
| 686 | * |
| 687 | * This code does two great things, it pokes into |
| 688 | * the elevator code from a filesystem _and_ |
| 689 | * it makes assumptions about how batching works. |
| 690 | */ |
| 691 | if (ioc && ioc->nr_batch_requests > 0 && |
| 692 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 693 | (last_waited == 0 || |
| 694 | ioc->last_waited == last_waited)) { |
| 695 | /* |
| 696 | * we want to go through our batch of |
| 697 | * requests and stop. So, we copy out |
| 698 | * the ioc->last_waited time and test |
| 699 | * against it before looping |
| 700 | */ |
| 701 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 702 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 703 | continue; |
| 704 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 705 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 706 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 707 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 708 | |
| 709 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 710 | btrfs_queue_work(fs_info->submit_workers, |
| 711 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 712 | goto done; |
| 713 | } |
| 714 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 715 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 716 | cond_resched(); |
| 717 | if (again) |
| 718 | goto loop; |
| 719 | |
| 720 | spin_lock(&device->io_lock); |
| 721 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 722 | goto loop_lock; |
| 723 | spin_unlock(&device->io_lock); |
| 724 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 725 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 726 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 729 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 730 | { |
| 731 | struct btrfs_device *device; |
| 732 | |
| 733 | device = container_of(work, struct btrfs_device, work); |
| 734 | run_scheduled_bios(device); |
| 735 | } |
| 736 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 737 | /* |
| 738 | * Search and remove all stale (devices which are not mounted) devices. |
| 739 | * When both inputs are NULL, it will search and release all stale devices. |
| 740 | * path: Optional. When provided will it release all unmounted devices |
| 741 | * matching this path only. |
| 742 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 743 | * devices. |
| 744 | */ |
| 745 | static void btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 746 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 747 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 748 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 749 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 750 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 751 | list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 752 | mutex_lock(&fs_devices->device_list_mutex); |
| 753 | if (fs_devices->opened) { |
| 754 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 755 | continue; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 756 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 757 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 758 | list_for_each_entry_safe(device, tmp_device, |
| 759 | &fs_devices->devices, dev_list) { |
Anand Jain | 522f1b4 | 2018-01-18 22:00:35 +0800 | [diff] [blame] | 760 | int not_found = 0; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 761 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 762 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 763 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 764 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 765 | continue; |
| 766 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 767 | rcu_read_lock(); |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 768 | if (path) |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 769 | not_found = strcmp(rcu_str_deref(device->name), |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 770 | path); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 771 | rcu_read_unlock(); |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 772 | if (not_found) |
| 773 | continue; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 774 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 775 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 776 | fs_devices->num_devices--; |
| 777 | list_del(&device->dev_list); |
| 778 | btrfs_free_device(device); |
| 779 | |
| 780 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 781 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 782 | } |
| 783 | mutex_unlock(&fs_devices->device_list_mutex); |
| 784 | if (fs_devices->num_devices == 0) { |
| 785 | btrfs_sysfs_remove_fsid(fs_devices); |
| 786 | list_del(&fs_devices->fs_list); |
| 787 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 792 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 793 | struct btrfs_device *device, fmode_t flags, |
| 794 | void *holder) |
| 795 | { |
| 796 | struct request_queue *q; |
| 797 | struct block_device *bdev; |
| 798 | struct buffer_head *bh; |
| 799 | struct btrfs_super_block *disk_super; |
| 800 | u64 devid; |
| 801 | int ret; |
| 802 | |
| 803 | if (device->bdev) |
| 804 | return -EINVAL; |
| 805 | if (!device->name) |
| 806 | return -EINVAL; |
| 807 | |
| 808 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 809 | &bdev, &bh); |
| 810 | if (ret) |
| 811 | return ret; |
| 812 | |
| 813 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 814 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 815 | if (devid != device->devid) |
| 816 | goto error_brelse; |
| 817 | |
| 818 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 819 | goto error_brelse; |
| 820 | |
| 821 | device->generation = btrfs_super_generation(disk_super); |
| 822 | |
| 823 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 824 | if (btrfs_super_incompat_flags(disk_super) & |
| 825 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 826 | pr_err( |
| 827 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 828 | goto error_brelse; |
| 829 | } |
| 830 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 831 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 832 | fs_devices->seeding = 1; |
| 833 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 834 | if (bdev_read_only(bdev)) |
| 835 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 836 | else |
| 837 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 841 | if (!blk_queue_nonrot(q)) |
| 842 | fs_devices->rotating = 1; |
| 843 | |
| 844 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 845 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 846 | device->mode = flags; |
| 847 | |
| 848 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 849 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 850 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 851 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 852 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 853 | } |
| 854 | brelse(bh); |
| 855 | |
| 856 | return 0; |
| 857 | |
| 858 | error_brelse: |
| 859 | brelse(bh); |
| 860 | blkdev_put(bdev, flags); |
| 861 | |
| 862 | return -EINVAL; |
| 863 | } |
| 864 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 865 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 866 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| 867 | * being created with a disk that has already completed its fsid change. |
| 868 | */ |
| 869 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 870 | struct btrfs_super_block *disk_super) |
| 871 | { |
| 872 | struct btrfs_fs_devices *fs_devices; |
| 873 | |
| 874 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 875 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 876 | BTRFS_FSID_SIZE) != 0 && |
| 877 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 878 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 879 | return fs_devices; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return NULL; |
| 884 | } |
| 885 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 886 | |
| 887 | static struct btrfs_fs_devices *find_fsid_changed( |
| 888 | struct btrfs_super_block *disk_super) |
| 889 | { |
| 890 | struct btrfs_fs_devices *fs_devices; |
| 891 | |
| 892 | /* |
| 893 | * Handles the case where scanned device is part of an fs that had |
| 894 | * multiple successful changes of FSID but curently device didn't |
| 895 | * observe it. Meaning our fsid will be different than theirs. |
| 896 | */ |
| 897 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 898 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 899 | BTRFS_FSID_SIZE) != 0 && |
| 900 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 901 | BTRFS_FSID_SIZE) == 0 && |
| 902 | memcmp(fs_devices->fsid, disk_super->fsid, |
| 903 | BTRFS_FSID_SIZE) != 0) { |
| 904 | return fs_devices; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | return NULL; |
| 909 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 910 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 911 | * Add new device to list of registered devices |
| 912 | * |
| 913 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 914 | * device pointer which was just added or updated when successful |
| 915 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 916 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 917 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 918 | struct btrfs_super_block *disk_super, |
| 919 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 920 | { |
| 921 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 922 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 923 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 924 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 925 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 926 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 927 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 928 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 929 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 930 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 931 | if (fsid_change_in_progress) { |
| 932 | if (!has_metadata_uuid) { |
| 933 | /* |
| 934 | * When we have an image which has CHANGING_FSID_V2 set |
| 935 | * it might belong to either a filesystem which has |
| 936 | * disks with completed fsid change or it might belong |
| 937 | * to fs with no UUID changes in effect, handle both. |
| 938 | */ |
| 939 | fs_devices = find_fsid_inprogress(disk_super); |
| 940 | if (!fs_devices) |
| 941 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 942 | } else { |
| 943 | fs_devices = find_fsid_changed(disk_super); |
| 944 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 945 | } else if (has_metadata_uuid) { |
| 946 | fs_devices = find_fsid(disk_super->fsid, |
| 947 | disk_super->metadata_uuid); |
| 948 | } else { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 949 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 952 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 953 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 954 | if (has_metadata_uuid) |
| 955 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 956 | disk_super->metadata_uuid); |
| 957 | else |
| 958 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 959 | |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 960 | fs_devices->fsid_change = fsid_change_in_progress; |
| 961 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 962 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 963 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 964 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 965 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 966 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 967 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 968 | device = NULL; |
| 969 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 970 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 971 | device = find_device(fs_devices, devid, |
| 972 | disk_super->dev_item.uuid); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 973 | |
| 974 | /* |
| 975 | * If this disk has been pulled into an fs devices created by |
| 976 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 977 | * metadata_uuid/fsid values of the fs_devices. |
| 978 | */ |
| 979 | if (has_metadata_uuid && fs_devices->fsid_change && |
| 980 | found_transid > fs_devices->latest_generation) { |
| 981 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 982 | BTRFS_FSID_SIZE); |
| 983 | memcpy(fs_devices->metadata_uuid, |
| 984 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 985 | |
| 986 | fs_devices->fsid_change = false; |
| 987 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 988 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 989 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 990 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 991 | if (fs_devices->opened) { |
| 992 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 993 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 994 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 995 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 996 | device = btrfs_alloc_device(NULL, &devid, |
| 997 | disk_super->dev_item.uuid); |
| 998 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 999 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1000 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1001 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1002 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1003 | |
| 1004 | name = rcu_string_strdup(path, GFP_NOFS); |
| 1005 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1006 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1007 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1008 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1009 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1010 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 1011 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1012 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1013 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1014 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1015 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1016 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1017 | |
| 1018 | if (disk_super->label[0]) |
| 1019 | pr_info("BTRFS: device label %s devid %llu transid %llu %s\n", |
| 1020 | disk_super->label, devid, found_transid, path); |
| 1021 | else |
| 1022 | pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n", |
| 1023 | disk_super->fsid, devid, found_transid, path); |
| 1024 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1025 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1026 | /* |
| 1027 | * When FS is already mounted. |
| 1028 | * 1. If you are here and if the device->name is NULL that |
| 1029 | * means this device was missing at time of FS mount. |
| 1030 | * 2. If you are here and if the device->name is different |
| 1031 | * from 'path' that means either |
| 1032 | * a. The same device disappeared and reappeared with |
| 1033 | * different name. or |
| 1034 | * b. The missing-disk-which-was-replaced, has |
| 1035 | * reappeared now. |
| 1036 | * |
| 1037 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 1038 | * and unintentional. |
| 1039 | * |
| 1040 | * Further in case of 1 and 2a above, the disk at 'path' |
| 1041 | * would have missed some transaction when it was away and |
| 1042 | * in case of 2a the stale bdev has to be updated as well. |
| 1043 | * 2b must not be allowed at all time. |
| 1044 | */ |
| 1045 | |
| 1046 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1047 | * For now, we do allow update to btrfs_fs_device through the |
| 1048 | * btrfs dev scan cli after FS has been mounted. We're still |
| 1049 | * tracking a problem where systems fail mount by subvolume id |
| 1050 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1051 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1052 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1053 | /* |
| 1054 | * That is if the FS is _not_ mounted and if you |
| 1055 | * are here, that means there is more than one |
| 1056 | * disk with same uuid and devid.We keep the one |
| 1057 | * with larger generation number or the last-in if |
| 1058 | * generation are equal. |
| 1059 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1060 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1061 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1062 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1063 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 1064 | /* |
| 1065 | * We are going to replace the device path for a given devid, |
| 1066 | * make sure it's the same device if the device is mounted |
| 1067 | */ |
| 1068 | if (device->bdev) { |
| 1069 | struct block_device *path_bdev; |
| 1070 | |
| 1071 | path_bdev = lookup_bdev(path); |
| 1072 | if (IS_ERR(path_bdev)) { |
| 1073 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1074 | return ERR_CAST(path_bdev); |
| 1075 | } |
| 1076 | |
| 1077 | if (device->bdev != path_bdev) { |
| 1078 | bdput(path_bdev); |
| 1079 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1080 | btrfs_warn_in_rcu(device->fs_info, |
| 1081 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 1082 | disk_super->fsid, devid, |
| 1083 | rcu_str_deref(device->name), path); |
| 1084 | return ERR_PTR(-EEXIST); |
| 1085 | } |
| 1086 | bdput(path_bdev); |
| 1087 | btrfs_info_in_rcu(device->fs_info, |
| 1088 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 1089 | disk_super->fsid, devid, |
| 1090 | rcu_str_deref(device->name), path); |
| 1091 | } |
| 1092 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1093 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1094 | if (!name) { |
| 1095 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1096 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1097 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1098 | rcu_string_free(device->name); |
| 1099 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1100 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1101 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1102 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1103 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1104 | } |
| 1105 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1106 | /* |
| 1107 | * Unmount does not free the btrfs_device struct but would zero |
| 1108 | * generation along with most of the other members. So just update |
| 1109 | * it back. We need it to pick the disk with largest generation |
| 1110 | * (as above). |
| 1111 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1112 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1113 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1114 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 1115 | fs_devices->latest_generation); |
| 1116 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1117 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 1118 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 1119 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1120 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1121 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1122 | } |
| 1123 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1124 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 1125 | { |
| 1126 | struct btrfs_fs_devices *fs_devices; |
| 1127 | struct btrfs_device *device; |
| 1128 | struct btrfs_device *orig_dev; |
| 1129 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 1130 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1131 | if (IS_ERR(fs_devices)) |
| 1132 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1133 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1134 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1135 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1136 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1137 | /* We have held the volume lock, it is safe to get the devices. */ |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1138 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1139 | struct rcu_string *name; |
| 1140 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 1141 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 1142 | orig_dev->uuid); |
| 1143 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1144 | goto error; |
| 1145 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1146 | /* |
| 1147 | * This is ok to do without rcu read locked because we hold the |
| 1148 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1149 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1150 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1151 | name = rcu_string_strdup(orig_dev->name->str, |
| 1152 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1153 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1154 | btrfs_free_device(device); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1155 | goto error; |
| 1156 | } |
| 1157 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1158 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1159 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1160 | list_add(&device->dev_list, &fs_devices->devices); |
| 1161 | device->fs_devices = fs_devices; |
| 1162 | fs_devices->num_devices++; |
| 1163 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1164 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1165 | return fs_devices; |
| 1166 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1167 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1168 | free_fs_devices(fs_devices); |
| 1169 | return ERR_PTR(-ENOMEM); |
| 1170 | } |
| 1171 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1172 | /* |
| 1173 | * After we have read the system tree and know devids belonging to |
| 1174 | * this filesystem, remove the device which does not belong there. |
| 1175 | */ |
| 1176 | 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] | 1177 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1178 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1179 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1180 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1181 | mutex_lock(&uuid_mutex); |
| 1182 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1183 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1184 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1185 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1186 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1187 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1188 | &device->dev_state) && |
| 1189 | (!latest_dev || |
| 1190 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1191 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1192 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1193 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1194 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1195 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1196 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1197 | /* |
| 1198 | * In the first step, keep the device which has |
| 1199 | * the correct fsid and the devid that is used |
| 1200 | * for the dev_replace procedure. |
| 1201 | * In the second step, the dev_replace state is |
| 1202 | * read from the device tree and it is known |
| 1203 | * whether the procedure is really active or |
| 1204 | * not, which means whether this device is |
| 1205 | * used or whether it should be removed. |
| 1206 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1207 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1208 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1209 | continue; |
| 1210 | } |
| 1211 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1212 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1213 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1214 | device->bdev = NULL; |
| 1215 | fs_devices->open_devices--; |
| 1216 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1217 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1218 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1219 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1220 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1221 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1222 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1223 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1224 | list_del_init(&device->dev_list); |
| 1225 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1226 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1227 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1228 | |
| 1229 | if (fs_devices->seed) { |
| 1230 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1231 | goto again; |
| 1232 | } |
| 1233 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1234 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1235 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1236 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1237 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1238 | |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 1239 | static void free_device_rcu(struct rcu_head *head) |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1240 | { |
| 1241 | struct btrfs_device *device; |
| 1242 | |
| 1243 | device = container_of(head, struct btrfs_device, rcu); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1244 | btrfs_free_device(device); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1247 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1248 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1249 | if (!device->bdev) |
| 1250 | return; |
| 1251 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1252 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1253 | sync_blockdev(device->bdev); |
| 1254 | invalidate_bdev(device->bdev); |
| 1255 | } |
| 1256 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1257 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1258 | } |
| 1259 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1260 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1261 | { |
| 1262 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1263 | struct btrfs_device *new_device; |
| 1264 | struct rcu_string *name; |
| 1265 | |
| 1266 | if (device->bdev) |
| 1267 | fs_devices->open_devices--; |
| 1268 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1269 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1270 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1271 | list_del_init(&device->dev_alloc_list); |
| 1272 | fs_devices->rw_devices--; |
| 1273 | } |
| 1274 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1275 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1276 | fs_devices->missing_devices--; |
| 1277 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1278 | btrfs_close_bdev(device); |
| 1279 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1280 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1281 | device->uuid); |
| 1282 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1283 | |
| 1284 | /* Safe because we are under uuid_mutex */ |
| 1285 | if (device->name) { |
| 1286 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1287 | BUG_ON(!name); /* -ENOMEM */ |
| 1288 | rcu_assign_pointer(new_device->name, name); |
| 1289 | } |
| 1290 | |
| 1291 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1292 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1293 | |
| 1294 | call_rcu(&device->rcu, free_device_rcu); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1295 | } |
| 1296 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1297 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1298 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1299 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1300 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1301 | if (--fs_devices->opened > 0) |
| 1302 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1303 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1304 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1305 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1306 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1307 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1308 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1309 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1310 | WARN_ON(fs_devices->open_devices); |
| 1311 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1312 | fs_devices->opened = 0; |
| 1313 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1314 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1315 | return 0; |
| 1316 | } |
| 1317 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1318 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1319 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1320 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1321 | int ret; |
| 1322 | |
| 1323 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1324 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1325 | if (!fs_devices->opened) { |
| 1326 | seed_devices = fs_devices->seed; |
| 1327 | fs_devices->seed = NULL; |
| 1328 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1329 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1330 | |
| 1331 | while (seed_devices) { |
| 1332 | fs_devices = seed_devices; |
| 1333 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1334 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1335 | free_fs_devices(fs_devices); |
| 1336 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1340 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1341 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1342 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1343 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1344 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1345 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1346 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1347 | flags |= FMODE_EXCL; |
| 1348 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1349 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1350 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1351 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1352 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1353 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1354 | if (!latest_dev || |
| 1355 | device->generation > latest_dev->generation) |
| 1356 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1357 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1358 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1359 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1360 | goto out; |
| 1361 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1362 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1363 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1364 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1365 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1366 | return ret; |
| 1367 | } |
| 1368 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1369 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1370 | { |
| 1371 | struct btrfs_device *dev1, *dev2; |
| 1372 | |
| 1373 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1374 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1375 | |
| 1376 | if (dev1->devid < dev2->devid) |
| 1377 | return -1; |
| 1378 | else if (dev1->devid > dev2->devid) |
| 1379 | return 1; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1383 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1384 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1385 | { |
| 1386 | int ret; |
| 1387 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1388 | lockdep_assert_held(&uuid_mutex); |
| 1389 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1390 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1391 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1392 | fs_devices->opened++; |
| 1393 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1394 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1395 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1396 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1397 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1398 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1399 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1400 | return ret; |
| 1401 | } |
| 1402 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1403 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1404 | { |
| 1405 | kunmap(page); |
| 1406 | put_page(page); |
| 1407 | } |
| 1408 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1409 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1410 | struct page **page, |
| 1411 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1412 | { |
| 1413 | void *p; |
| 1414 | pgoff_t index; |
| 1415 | |
| 1416 | /* make sure our super fits in the device */ |
| 1417 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1418 | return 1; |
| 1419 | |
| 1420 | /* make sure our super fits in the page */ |
| 1421 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1422 | return 1; |
| 1423 | |
| 1424 | /* make sure our super doesn't straddle pages on disk */ |
| 1425 | index = bytenr >> PAGE_SHIFT; |
| 1426 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1427 | return 1; |
| 1428 | |
| 1429 | /* pull in the page with our super */ |
| 1430 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1431 | index, GFP_KERNEL); |
| 1432 | |
| 1433 | if (IS_ERR_OR_NULL(*page)) |
| 1434 | return 1; |
| 1435 | |
| 1436 | p = kmap(*page); |
| 1437 | |
| 1438 | /* align our pointer to the offset of the super block */ |
| 1439 | *disk_super = p + (bytenr & ~PAGE_MASK); |
| 1440 | |
| 1441 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1442 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1443 | btrfs_release_disk_super(*page); |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | if ((*disk_super)->label[0] && |
| 1448 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1449 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1450 | |
| 1451 | return 0; |
| 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 | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1503 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1504 | struct btrfs_device *device, |
| 1505 | u64 *start, u64 len) |
| 1506 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1507 | struct btrfs_fs_info *fs_info = device->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1508 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1509 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1510 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1511 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1512 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1513 | if (transaction) |
| 1514 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1515 | again: |
| 1516 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1517 | struct map_lookup *map; |
| 1518 | int i; |
| 1519 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 1520 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1521 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1522 | u64 end; |
| 1523 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1524 | if (map->stripes[i].dev != device) |
| 1525 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1526 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1527 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1528 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1529 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1530 | /* |
| 1531 | * Make sure that while processing the pinned list we do |
| 1532 | * not override our *start with a lower value, because |
| 1533 | * we can have pinned chunks that fall within this |
| 1534 | * device hole and that have lower physical addresses |
| 1535 | * than the pending chunks we processed before. If we |
| 1536 | * do not take this special care we can end up getting |
| 1537 | * 2 pending chunks that start at the same physical |
| 1538 | * device offsets because the end offset of a pinned |
| 1539 | * chunk can be equal to the start offset of some |
| 1540 | * pending chunk. |
| 1541 | */ |
| 1542 | end = map->stripes[i].physical + em->orig_block_len; |
| 1543 | if (end > *start) { |
| 1544 | *start = end; |
| 1545 | ret = 1; |
| 1546 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1547 | } |
| 1548 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1549 | if (search_list != &fs_info->pinned_chunks) { |
| 1550 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1551 | goto again; |
| 1552 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1553 | |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
| 1557 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1558 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1559 | * find_free_dev_extent_start - find free space in the specified device |
| 1560 | * @device: the device which we search the free space in |
| 1561 | * @num_bytes: the size of the free space that we need |
| 1562 | * @search_start: the position from which to begin the search |
| 1563 | * @start: store the start of the free space. |
| 1564 | * @len: the size of the free space. that we find, or the size |
| 1565 | * 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] | 1566 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1567 | * this uses a pretty simple search, the expectation is that it is |
| 1568 | * called very infrequently and that a given device has a small number |
| 1569 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * @start is used to store the start of the free space if we find. But if we |
| 1572 | * don't find suitable free space, it will be used to store the start position |
| 1573 | * of the max free space. |
| 1574 | * |
| 1575 | * @len is used to store the size of the free space that we find. |
| 1576 | * But if we don't find suitable free space, it is used to store the size of |
| 1577 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1579 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1580 | struct btrfs_device *device, u64 num_bytes, |
| 1581 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1582 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1583 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1584 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1585 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1586 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1587 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1588 | u64 hole_size; |
| 1589 | u64 max_hole_start; |
| 1590 | u64 max_hole_size; |
| 1591 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1592 | u64 search_end = device->total_bytes; |
| 1593 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1594 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1595 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1596 | |
| 1597 | /* |
| 1598 | * We don't want to overwrite the superblock on the drive nor any area |
| 1599 | * used by the boot loader (grub for example), so we make sure to start |
| 1600 | * at an offset of at least 1MB. |
| 1601 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1602 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1603 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1604 | path = btrfs_alloc_path(); |
| 1605 | if (!path) |
| 1606 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1607 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1608 | max_hole_start = search_start; |
| 1609 | max_hole_size = 0; |
| 1610 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1611 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1612 | if (search_start >= search_end || |
| 1613 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1614 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1615 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1618 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1619 | path->search_commit_root = 1; |
| 1620 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1621 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1622 | key.objectid = device->devid; |
| 1623 | key.offset = search_start; |
| 1624 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1625 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1626 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1627 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1628 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1629 | if (ret > 0) { |
| 1630 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1631 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1632 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1633 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1634 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1635 | while (1) { |
| 1636 | l = path->nodes[0]; |
| 1637 | slot = path->slots[0]; |
| 1638 | if (slot >= btrfs_header_nritems(l)) { |
| 1639 | ret = btrfs_next_leaf(root, path); |
| 1640 | if (ret == 0) |
| 1641 | continue; |
| 1642 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1643 | goto out; |
| 1644 | |
| 1645 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1646 | } |
| 1647 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1648 | |
| 1649 | if (key.objectid < device->devid) |
| 1650 | goto next; |
| 1651 | |
| 1652 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1653 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1654 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1655 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1656 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1657 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1658 | if (key.offset > search_start) { |
| 1659 | hole_size = key.offset - search_start; |
| 1660 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1661 | /* |
| 1662 | * Have to check before we set max_hole_start, otherwise |
| 1663 | * we could end up sending back this offset anyway. |
| 1664 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1665 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1666 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1667 | hole_size)) { |
| 1668 | if (key.offset >= search_start) { |
| 1669 | hole_size = key.offset - search_start; |
| 1670 | } else { |
| 1671 | WARN_ON_ONCE(1); |
| 1672 | hole_size = 0; |
| 1673 | } |
| 1674 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1675 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1676 | if (hole_size > max_hole_size) { |
| 1677 | max_hole_start = search_start; |
| 1678 | max_hole_size = hole_size; |
| 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * If this free space is greater than which we need, |
| 1683 | * it must be the max free space that we have found |
| 1684 | * until now, so max_hole_start must point to the start |
| 1685 | * of this free space and the length of this free space |
| 1686 | * is stored in max_hole_size. Thus, we return |
| 1687 | * max_hole_start and max_hole_size and go back to the |
| 1688 | * caller. |
| 1689 | */ |
| 1690 | if (hole_size >= num_bytes) { |
| 1691 | ret = 0; |
| 1692 | goto out; |
| 1693 | } |
| 1694 | } |
| 1695 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1696 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1697 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1698 | dev_extent); |
| 1699 | if (extent_end > search_start) |
| 1700 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1701 | next: |
| 1702 | path->slots[0]++; |
| 1703 | cond_resched(); |
| 1704 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1705 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1706 | /* |
| 1707 | * At this point, search_start should be the end of |
| 1708 | * allocated dev extents, and when shrinking the device, |
| 1709 | * search_end may be smaller than search_start. |
| 1710 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1711 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1712 | hole_size = search_end - search_start; |
| 1713 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1714 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1715 | hole_size)) { |
| 1716 | btrfs_release_path(path); |
| 1717 | goto again; |
| 1718 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1719 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1720 | if (hole_size > max_hole_size) { |
| 1721 | max_hole_start = search_start; |
| 1722 | max_hole_size = hole_size; |
| 1723 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1724 | } |
| 1725 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1726 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1727 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1728 | ret = -ENOSPC; |
| 1729 | else |
| 1730 | ret = 0; |
| 1731 | |
| 1732 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1733 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1734 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1735 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1736 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1737 | return ret; |
| 1738 | } |
| 1739 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1740 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1741 | struct btrfs_device *device, u64 num_bytes, |
| 1742 | u64 *start, u64 *len) |
| 1743 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1744 | /* FIXME use last free of some kind */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1745 | return find_free_dev_extent_start(trans->transaction, device, |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1746 | num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1747 | } |
| 1748 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1749 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1750 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1751 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1752 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1753 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1754 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1755 | int ret; |
| 1756 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1757 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1758 | struct btrfs_key found_key; |
| 1759 | struct extent_buffer *leaf = NULL; |
| 1760 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1761 | |
| 1762 | path = btrfs_alloc_path(); |
| 1763 | if (!path) |
| 1764 | return -ENOMEM; |
| 1765 | |
| 1766 | key.objectid = device->devid; |
| 1767 | key.offset = start; |
| 1768 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1769 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1770 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1771 | if (ret > 0) { |
| 1772 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1773 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1774 | if (ret) |
| 1775 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1776 | leaf = path->nodes[0]; |
| 1777 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1778 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1779 | struct btrfs_dev_extent); |
| 1780 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1781 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1782 | key = found_key; |
| 1783 | btrfs_release_path(path); |
| 1784 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1785 | } else if (ret == 0) { |
| 1786 | leaf = path->nodes[0]; |
| 1787 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1788 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1789 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1790 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1791 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1792 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1793 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1794 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1795 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1796 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1797 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1798 | btrfs_handle_fs_error(fs_info, ret, |
| 1799 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1800 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1801 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1802 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1803 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1804 | btrfs_free_path(path); |
| 1805 | return ret; |
| 1806 | } |
| 1807 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1808 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1809 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1810 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1811 | { |
| 1812 | int ret; |
| 1813 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1814 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1815 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1816 | struct btrfs_dev_extent *extent; |
| 1817 | struct extent_buffer *leaf; |
| 1818 | struct btrfs_key key; |
| 1819 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1820 | 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] | 1821 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1822 | path = btrfs_alloc_path(); |
| 1823 | if (!path) |
| 1824 | return -ENOMEM; |
| 1825 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1826 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1827 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1828 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1829 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1830 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1831 | if (ret) |
| 1832 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1833 | |
| 1834 | leaf = path->nodes[0]; |
| 1835 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1836 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1837 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1838 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1839 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1840 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1841 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1842 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1843 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1844 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1845 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1846 | btrfs_free_path(path); |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1850 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1851 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1852 | struct extent_map_tree *em_tree; |
| 1853 | struct extent_map *em; |
| 1854 | struct rb_node *n; |
| 1855 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1856 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1857 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1858 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1859 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1860 | if (n) { |
| 1861 | em = rb_entry(n, struct extent_map, rb_node); |
| 1862 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1863 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1864 | read_unlock(&em_tree->lock); |
| 1865 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1866 | return ret; |
| 1867 | } |
| 1868 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1869 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1870 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1871 | { |
| 1872 | int ret; |
| 1873 | struct btrfs_key key; |
| 1874 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1875 | struct btrfs_path *path; |
| 1876 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1877 | path = btrfs_alloc_path(); |
| 1878 | if (!path) |
| 1879 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1880 | |
| 1881 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1882 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1883 | key.offset = (u64)-1; |
| 1884 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1885 | 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] | 1886 | if (ret < 0) |
| 1887 | goto error; |
| 1888 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1889 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1890 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1891 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1892 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1893 | BTRFS_DEV_ITEM_KEY); |
| 1894 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1895 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1896 | } else { |
| 1897 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1898 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1899 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1900 | } |
| 1901 | ret = 0; |
| 1902 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1903 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * the device information is stored in the chunk root |
| 1909 | * the btrfs_device struct should be fully filled in |
| 1910 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1911 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1912 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1913 | { |
| 1914 | int ret; |
| 1915 | struct btrfs_path *path; |
| 1916 | struct btrfs_dev_item *dev_item; |
| 1917 | struct extent_buffer *leaf; |
| 1918 | struct btrfs_key key; |
| 1919 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1920 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1921 | path = btrfs_alloc_path(); |
| 1922 | if (!path) |
| 1923 | return -ENOMEM; |
| 1924 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1925 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1926 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1927 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1928 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1929 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1930 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1931 | if (ret) |
| 1932 | goto out; |
| 1933 | |
| 1934 | leaf = path->nodes[0]; |
| 1935 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1936 | |
| 1937 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1938 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1939 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1940 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1941 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1942 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1943 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1944 | btrfs_device_get_disk_total_bytes(device)); |
| 1945 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1946 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1947 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1948 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1949 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1950 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1951 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1952 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1953 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1954 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1955 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1956 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1957 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1958 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1959 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1960 | out: |
| 1961 | btrfs_free_path(path); |
| 1962 | return ret; |
| 1963 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1964 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1965 | /* |
| 1966 | * Function to update ctime/mtime for a given device path. |
| 1967 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1968 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1969 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1970 | { |
| 1971 | struct file *filp; |
| 1972 | |
| 1973 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1974 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1975 | return; |
| 1976 | file_update_time(filp); |
| 1977 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1980 | static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1981 | struct btrfs_device *device) |
| 1982 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1983 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1984 | int ret; |
| 1985 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1986 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1987 | struct btrfs_trans_handle *trans; |
| 1988 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1989 | path = btrfs_alloc_path(); |
| 1990 | if (!path) |
| 1991 | return -ENOMEM; |
| 1992 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1993 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1994 | if (IS_ERR(trans)) { |
| 1995 | btrfs_free_path(path); |
| 1996 | return PTR_ERR(trans); |
| 1997 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1998 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1999 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2000 | key.offset = device->devid; |
| 2001 | |
| 2002 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 2003 | if (ret) { |
| 2004 | if (ret > 0) |
| 2005 | ret = -ENOENT; |
| 2006 | btrfs_abort_transaction(trans, ret); |
| 2007 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2008 | goto out; |
| 2009 | } |
| 2010 | |
| 2011 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 2012 | if (ret) { |
| 2013 | btrfs_abort_transaction(trans, ret); |
| 2014 | btrfs_end_transaction(trans); |
| 2015 | } |
| 2016 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2017 | out: |
| 2018 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 2019 | if (!ret) |
| 2020 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2021 | return ret; |
| 2022 | } |
| 2023 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 2026 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 2027 | * replace. |
| 2028 | */ |
| 2029 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 2030 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2031 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2032 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2033 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2034 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2035 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2036 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2037 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2038 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2039 | all_avail = fs_info->avail_data_alloc_bits | |
| 2040 | fs_info->avail_system_alloc_bits | |
| 2041 | fs_info->avail_metadata_alloc_bits; |
| 2042 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2043 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2044 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 2045 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2046 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2047 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2048 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 2049 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2050 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2051 | if (ret) |
| 2052 | return ret; |
| 2053 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 2059 | static struct btrfs_device * btrfs_find_next_active_device( |
| 2060 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2061 | { |
| 2062 | struct btrfs_device *next_device; |
| 2063 | |
| 2064 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 2065 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2066 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 2067 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2068 | return next_device; |
| 2069 | } |
| 2070 | |
| 2071 | return NULL; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 2076 | * and replace it with the provided or the next active device, in the context |
| 2077 | * where this function called, there should be always be another device (or |
| 2078 | * this_dev) which is active. |
| 2079 | */ |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2080 | void btrfs_assign_next_active_device(struct btrfs_device *device, |
| 2081 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2082 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2083 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2084 | struct btrfs_device *next_device; |
| 2085 | |
| 2086 | if (this_dev) |
| 2087 | next_device = this_dev; |
| 2088 | else |
| 2089 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 2090 | device); |
| 2091 | ASSERT(next_device); |
| 2092 | |
| 2093 | if (fs_info->sb->s_bdev && |
| 2094 | (fs_info->sb->s_bdev == device->bdev)) |
| 2095 | fs_info->sb->s_bdev = next_device->bdev; |
| 2096 | |
| 2097 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 2098 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2099 | } |
| 2100 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2101 | /* |
| 2102 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 2103 | * currently replaced. |
| 2104 | */ |
| 2105 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 2106 | { |
| 2107 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 2108 | |
| 2109 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
| 2110 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 2111 | ASSERT(num_devices > 1); |
| 2112 | num_devices--; |
| 2113 | } |
| 2114 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
| 2115 | |
| 2116 | return num_devices; |
| 2117 | } |
| 2118 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2119 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2120 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2121 | { |
| 2122 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2123 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2124 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2125 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2126 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2127 | |
| 2128 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2129 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2130 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2131 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2132 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2133 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2134 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2135 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2136 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2137 | |
| 2138 | if (IS_ERR(device)) { |
| 2139 | if (PTR_ERR(device) == -ENOENT && |
| 2140 | strcmp(device_path, "missing") == 0) |
| 2141 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2142 | else |
| 2143 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2144 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2145 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2146 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2147 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2148 | btrfs_warn_in_rcu(fs_info, |
| 2149 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2150 | rcu_str_deref(device->name), device->devid); |
| 2151 | ret = -ETXTBSY; |
| 2152 | goto out; |
| 2153 | } |
| 2154 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2155 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2156 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2157 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2160 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2161 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2162 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2163 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2166 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2167 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2168 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2169 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2170 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2171 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2172 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2173 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2174 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2175 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2176 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2177 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2178 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2179 | /* |
| 2180 | * TODO: the superblock still includes this device in its num_devices |
| 2181 | * counter although write_all_supers() is not locked out. This |
| 2182 | * could give a filesystem state which requires a degraded mount. |
| 2183 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2184 | ret = btrfs_rm_dev_item(fs_info, device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2185 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2186 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2187 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2188 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2189 | btrfs_scrub_cancel_dev(fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2190 | |
| 2191 | /* |
| 2192 | * the device list mutex makes sure that we don't change |
| 2193 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2194 | * the device supers. Whoever is writing all supers, should |
| 2195 | * lock the device list mutex before getting the number of |
| 2196 | * devices in the super block (super_copy). Conversely, |
| 2197 | * whoever updates the number of devices in the super block |
| 2198 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2199 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2200 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2201 | /* |
| 2202 | * In normal cases the cur_devices == fs_devices. But in case |
| 2203 | * of deleting a seed device, the cur_devices should point to |
| 2204 | * its own fs_devices listed under the fs_devices->seed. |
| 2205 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2206 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2207 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2208 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2209 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2210 | cur_devices->num_devices--; |
| 2211 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2212 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2213 | if (cur_devices != fs_devices) |
| 2214 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2215 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2216 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2217 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2218 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2219 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2220 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2221 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2222 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2223 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2224 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2225 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2226 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2227 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2228 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2229 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2230 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2231 | /* |
| 2232 | * at this point, the device is zero sized and detached from |
| 2233 | * the devices list. All that's left is to zero out the old |
| 2234 | * supers and free the device. |
| 2235 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2236 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2237 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2238 | |
| 2239 | btrfs_close_bdev(device); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2240 | call_rcu(&device->rcu, free_device_rcu); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2241 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2242 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2243 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2244 | if (fs_devices->seed == cur_devices) { |
| 2245 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2246 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2247 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2248 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2250 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2251 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2252 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2253 | } |
| 2254 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2255 | out: |
| 2256 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2257 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2258 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2259 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2260 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2261 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2262 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2263 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2264 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2265 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2266 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2267 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2268 | } |
| 2269 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2270 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2271 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2272 | struct btrfs_fs_devices *fs_devices; |
| 2273 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2274 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2275 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2276 | /* |
| 2277 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2278 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2279 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2280 | * srcdev will have its correct fs_devices in both the cases. |
| 2281 | */ |
| 2282 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2283 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2284 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2285 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2286 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2287 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2288 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2289 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2290 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2291 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2292 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2293 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2294 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 2298 | struct btrfs_device *srcdev) |
| 2299 | { |
| 2300 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2301 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2302 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2303 | /* zero out the old super if it is writable */ |
| 2304 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2305 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2306 | |
| 2307 | btrfs_close_bdev(srcdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2308 | call_rcu(&srcdev->rcu, free_device_rcu); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2309 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2310 | /* if this is no devs we rather delete the fs_devices */ |
| 2311 | if (!fs_devices->num_devices) { |
| 2312 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2313 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2314 | /* |
| 2315 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2316 | * seed. In case of a seed device being replaced, the replace |
| 2317 | * target added to the sprout FS, so there will be no more |
| 2318 | * device left under the seed FS. |
| 2319 | */ |
| 2320 | ASSERT(fs_devices->seeding); |
| 2321 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2322 | tmp_fs_devices = fs_info->fs_devices; |
| 2323 | while (tmp_fs_devices) { |
| 2324 | if (tmp_fs_devices->seed == fs_devices) { |
| 2325 | tmp_fs_devices->seed = fs_devices->seed; |
| 2326 | break; |
| 2327 | } |
| 2328 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2329 | } |
| 2330 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2331 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2332 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2333 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2334 | } |
| 2335 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2336 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2337 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2338 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2339 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2340 | WARN_ON(!tgtdev); |
| 2341 | mutex_lock(&fs_devices->device_list_mutex); |
| 2342 | |
| 2343 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2344 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2345 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2346 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2347 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2348 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2349 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2350 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2351 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2352 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2353 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2354 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2355 | |
| 2356 | /* |
| 2357 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2358 | * may lead to a call to btrfs_show_devname() which will try |
| 2359 | * to hold device_list_mutex. And here this device |
| 2360 | * is already out of device list, so we don't have to hold |
| 2361 | * the device_list_mutex lock. |
| 2362 | */ |
| 2363 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2364 | |
| 2365 | btrfs_close_bdev(tgtdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2366 | call_rcu(&tgtdev->rcu, free_device_rcu); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2369 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2370 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2371 | { |
| 2372 | int ret = 0; |
| 2373 | struct btrfs_super_block *disk_super; |
| 2374 | u64 devid; |
| 2375 | u8 *dev_uuid; |
| 2376 | struct block_device *bdev; |
| 2377 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2378 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2379 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2380 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2381 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2382 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2383 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2384 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2385 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2386 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2387 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
| 2388 | device = btrfs_find_device(fs_info, devid, dev_uuid, |
| 2389 | disk_super->metadata_uuid); |
| 2390 | else |
| 2391 | device = btrfs_find_device(fs_info, devid, |
| 2392 | dev_uuid, disk_super->fsid); |
| 2393 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2394 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2395 | if (!device) |
| 2396 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2397 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2398 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2399 | } |
| 2400 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2401 | static struct btrfs_device *btrfs_find_device_missing_or_by_path( |
| 2402 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2403 | { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2404 | struct btrfs_device *device = NULL; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2405 | if (strcmp(device_path, "missing") == 0) { |
| 2406 | struct list_head *devices; |
| 2407 | struct btrfs_device *tmp; |
| 2408 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2409 | devices = &fs_info->fs_devices->devices; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2410 | list_for_each_entry(tmp, devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2411 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2412 | &tmp->dev_state) && !tmp->bdev) { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2413 | device = tmp; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2414 | break; |
| 2415 | } |
| 2416 | } |
| 2417 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2418 | if (!device) |
| 2419 | return ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2420 | } else { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2421 | device = btrfs_find_device_by_path(fs_info, device_path); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2422 | } |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2423 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2424 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2427 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2428 | * Lookup a device given by device id, or the path if the id is 0. |
| 2429 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2430 | struct btrfs_device *btrfs_find_device_by_devspec( |
| 2431 | struct btrfs_fs_info *fs_info, u64 devid, const char *devpath) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2432 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2433 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2434 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2435 | if (devid) { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2436 | device = btrfs_find_device(fs_info, devid, NULL, NULL); |
| 2437 | if (!device) |
| 2438 | return ERR_PTR(-ENOENT); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2439 | } else { |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2440 | if (!devpath || !devpath[0]) |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2441 | return ERR_PTR(-EINVAL); |
| 2442 | device = btrfs_find_device_missing_or_by_path(fs_info, devpath); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2443 | } |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2444 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2445 | } |
| 2446 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2447 | /* |
| 2448 | * does all the dirty work required for changing file system's UUID. |
| 2449 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2450 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2451 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2452 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2453 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2454 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2455 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2456 | struct btrfs_device *device; |
| 2457 | u64 super_flags; |
| 2458 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2459 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2460 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2461 | return -EINVAL; |
| 2462 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2463 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2464 | if (IS_ERR(seed_devices)) |
| 2465 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2466 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2467 | old_devices = clone_fs_devices(fs_devices); |
| 2468 | if (IS_ERR(old_devices)) { |
| 2469 | kfree(seed_devices); |
| 2470 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2471 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2472 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2473 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2474 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2475 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2476 | seed_devices->opened = 1; |
| 2477 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2478 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2479 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2480 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2481 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2482 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2483 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2484 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2485 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2486 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2487 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2488 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2489 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2490 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2491 | fs_devices->seeding = 0; |
| 2492 | fs_devices->num_devices = 0; |
| 2493 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2494 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2495 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2496 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2497 | |
| 2498 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2499 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2500 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2501 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2502 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2503 | super_flags = btrfs_super_flags(disk_super) & |
| 2504 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2505 | btrfs_set_super_flags(disk_super, super_flags); |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2511 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2512 | */ |
| 2513 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2514 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2515 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2516 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2517 | struct btrfs_path *path; |
| 2518 | struct extent_buffer *leaf; |
| 2519 | struct btrfs_dev_item *dev_item; |
| 2520 | struct btrfs_device *device; |
| 2521 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2522 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2523 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2524 | u64 devid; |
| 2525 | int ret; |
| 2526 | |
| 2527 | path = btrfs_alloc_path(); |
| 2528 | if (!path) |
| 2529 | return -ENOMEM; |
| 2530 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2531 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2532 | key.offset = 0; |
| 2533 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2534 | |
| 2535 | while (1) { |
| 2536 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2537 | if (ret < 0) |
| 2538 | goto error; |
| 2539 | |
| 2540 | leaf = path->nodes[0]; |
| 2541 | next_slot: |
| 2542 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2543 | ret = btrfs_next_leaf(root, path); |
| 2544 | if (ret > 0) |
| 2545 | break; |
| 2546 | if (ret < 0) |
| 2547 | goto error; |
| 2548 | leaf = path->nodes[0]; |
| 2549 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2550 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2551 | continue; |
| 2552 | } |
| 2553 | |
| 2554 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2555 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2556 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2557 | break; |
| 2558 | |
| 2559 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2560 | struct btrfs_dev_item); |
| 2561 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2562 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2563 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2564 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2565 | BTRFS_FSID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2566 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2567 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2568 | |
| 2569 | if (device->fs_devices->seeding) { |
| 2570 | btrfs_set_device_generation(leaf, dev_item, |
| 2571 | device->generation); |
| 2572 | btrfs_mark_buffer_dirty(leaf); |
| 2573 | } |
| 2574 | |
| 2575 | path->slots[0]++; |
| 2576 | goto next_slot; |
| 2577 | } |
| 2578 | ret = 0; |
| 2579 | error: |
| 2580 | btrfs_free_path(path); |
| 2581 | return ret; |
| 2582 | } |
| 2583 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2584 | 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] | 2585 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2586 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2587 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2588 | struct btrfs_trans_handle *trans; |
| 2589 | struct btrfs_device *device; |
| 2590 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2591 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2592 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2593 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2594 | u64 orig_super_total_bytes; |
| 2595 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2596 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2597 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2598 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2599 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2600 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2601 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2602 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2603 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2604 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2605 | if (IS_ERR(bdev)) |
| 2606 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2607 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2608 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2609 | seeding_dev = 1; |
| 2610 | down_write(&sb->s_umount); |
| 2611 | mutex_lock(&uuid_mutex); |
| 2612 | } |
| 2613 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2614 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2615 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2616 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2617 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2618 | if (device->bdev == bdev) { |
| 2619 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2620 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2621 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2622 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2623 | } |
| 2624 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2625 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2626 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2627 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2628 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2629 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2630 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2631 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2632 | } |
| 2633 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2634 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2635 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2636 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2637 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2638 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2639 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2640 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2641 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2642 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2643 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2644 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2647 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2648 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2649 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2650 | device->io_width = fs_info->sectorsize; |
| 2651 | device->io_align = fs_info->sectorsize; |
| 2652 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2653 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2654 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2655 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2656 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2657 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2658 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2659 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2660 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2661 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2662 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2663 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2664 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2665 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2666 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2667 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2668 | if (ret) { |
| 2669 | btrfs_abort_transaction(trans, ret); |
| 2670 | goto error_trans; |
| 2671 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2672 | } |
| 2673 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2674 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2675 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2676 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2677 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2678 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2679 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2680 | fs_devices->num_devices++; |
| 2681 | fs_devices->open_devices++; |
| 2682 | fs_devices->rw_devices++; |
| 2683 | fs_devices->total_devices++; |
| 2684 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2685 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2686 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2687 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2688 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2689 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2690 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2691 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2692 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2693 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2694 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2695 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2696 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2697 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2698 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2699 | |
| 2700 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2701 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2702 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2703 | /* |
| 2704 | * we've got more storage, clear any full flags on the space |
| 2705 | * infos |
| 2706 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2707 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2708 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2709 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2710 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2711 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2712 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2713 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 2714 | ret = init_first_rw_device(trans, fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2715 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2716 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2717 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2718 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2719 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2720 | } |
| 2721 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2722 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2723 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2724 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2725 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | if (seeding_dev) { |
| 2729 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2730 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2731 | ret = btrfs_finish_sprout(trans, fs_info); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2732 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2733 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2734 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2735 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2736 | |
| 2737 | /* Sprouting would change fsid of the mounted root, |
| 2738 | * so rename the fsid on the sysfs |
| 2739 | */ |
| 2740 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 2741 | fs_info->fs_devices->fsid); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2742 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2743 | btrfs_warn(fs_info, |
| 2744 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2745 | } |
| 2746 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2747 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2748 | |
| 2749 | if (seeding_dev) { |
| 2750 | mutex_unlock(&uuid_mutex); |
| 2751 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2752 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2753 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2754 | if (ret) /* transaction commit */ |
| 2755 | return ret; |
| 2756 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2757 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2758 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2759 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2760 | "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] | 2761 | trans = btrfs_attach_transaction(root); |
| 2762 | if (IS_ERR(trans)) { |
| 2763 | if (PTR_ERR(trans) == -ENOENT) |
| 2764 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2765 | ret = PTR_ERR(trans); |
| 2766 | trans = NULL; |
| 2767 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2768 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2769 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2770 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2771 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2772 | /* Update ctime/mtime for libblkid */ |
| 2773 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2774 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2775 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2776 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2777 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2778 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2779 | mutex_lock(&fs_info->chunk_mutex); |
| 2780 | list_del_rcu(&device->dev_list); |
| 2781 | list_del(&device->dev_alloc_list); |
| 2782 | fs_info->fs_devices->num_devices--; |
| 2783 | fs_info->fs_devices->open_devices--; |
| 2784 | fs_info->fs_devices->rw_devices--; |
| 2785 | fs_info->fs_devices->total_devices--; |
| 2786 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2787 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2788 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2789 | orig_super_total_bytes); |
| 2790 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2791 | orig_super_num_devices); |
| 2792 | mutex_unlock(&fs_info->chunk_mutex); |
| 2793 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2794 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2795 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2796 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2797 | if (trans) |
| 2798 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2799 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2800 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2801 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2802 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2803 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2804 | mutex_unlock(&uuid_mutex); |
| 2805 | up_write(&sb->s_umount); |
| 2806 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2807 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2810 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2811 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2812 | { |
| 2813 | int ret; |
| 2814 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2815 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2816 | struct btrfs_dev_item *dev_item; |
| 2817 | struct extent_buffer *leaf; |
| 2818 | struct btrfs_key key; |
| 2819 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2820 | path = btrfs_alloc_path(); |
| 2821 | if (!path) |
| 2822 | return -ENOMEM; |
| 2823 | |
| 2824 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2825 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2826 | key.offset = device->devid; |
| 2827 | |
| 2828 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2829 | if (ret < 0) |
| 2830 | goto out; |
| 2831 | |
| 2832 | if (ret > 0) { |
| 2833 | ret = -ENOENT; |
| 2834 | goto out; |
| 2835 | } |
| 2836 | |
| 2837 | leaf = path->nodes[0]; |
| 2838 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2839 | |
| 2840 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2841 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2842 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2843 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2844 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2845 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2846 | btrfs_device_get_disk_total_bytes(device)); |
| 2847 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2848 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2849 | btrfs_mark_buffer_dirty(leaf); |
| 2850 | |
| 2851 | out: |
| 2852 | btrfs_free_path(path); |
| 2853 | return ret; |
| 2854 | } |
| 2855 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2856 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2857 | struct btrfs_device *device, u64 new_size) |
| 2858 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2859 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2860 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2861 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2862 | u64 old_total; |
| 2863 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2864 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2865 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2866 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2867 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2868 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2869 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2870 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2871 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2872 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2873 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2874 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2875 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2876 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2877 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2878 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2879 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2880 | fs_devices = fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2881 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2882 | btrfs_set_super_total_bytes(super_copy, |
| 2883 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2884 | device->fs_devices->total_rw_bytes += diff; |
| 2885 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2886 | btrfs_device_set_total_bytes(device, new_size); |
| 2887 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2888 | btrfs_clear_space_info_full(device->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2889 | if (list_empty(&device->resized_list)) |
| 2890 | list_add_tail(&device->resized_list, |
| 2891 | &fs_devices->resized_devices); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2892 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2893 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2894 | return btrfs_update_device(trans, device); |
| 2895 | } |
| 2896 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2897 | 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] | 2898 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2899 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2900 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2901 | int ret; |
| 2902 | struct btrfs_path *path; |
| 2903 | struct btrfs_key key; |
| 2904 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2905 | path = btrfs_alloc_path(); |
| 2906 | if (!path) |
| 2907 | return -ENOMEM; |
| 2908 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2909 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2910 | key.offset = chunk_offset; |
| 2911 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2912 | |
| 2913 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2914 | if (ret < 0) |
| 2915 | goto out; |
| 2916 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2917 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2918 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2919 | ret = -ENOENT; |
| 2920 | goto out; |
| 2921 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2922 | |
| 2923 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2924 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2925 | btrfs_handle_fs_error(fs_info, ret, |
| 2926 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2927 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2928 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2929 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2930 | } |
| 2931 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2932 | 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] | 2933 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2934 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2935 | struct btrfs_disk_key *disk_key; |
| 2936 | struct btrfs_chunk *chunk; |
| 2937 | u8 *ptr; |
| 2938 | int ret = 0; |
| 2939 | u32 num_stripes; |
| 2940 | u32 array_size; |
| 2941 | u32 len = 0; |
| 2942 | u32 cur; |
| 2943 | struct btrfs_key key; |
| 2944 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2945 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2946 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2947 | |
| 2948 | ptr = super_copy->sys_chunk_array; |
| 2949 | cur = 0; |
| 2950 | |
| 2951 | while (cur < array_size) { |
| 2952 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2953 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2954 | |
| 2955 | len = sizeof(*disk_key); |
| 2956 | |
| 2957 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2958 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2959 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2960 | len += btrfs_chunk_item_size(num_stripes); |
| 2961 | } else { |
| 2962 | ret = -EIO; |
| 2963 | break; |
| 2964 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2965 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2966 | key.offset == chunk_offset) { |
| 2967 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2968 | array_size -= len; |
| 2969 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2970 | } else { |
| 2971 | ptr += len; |
| 2972 | cur += len; |
| 2973 | } |
| 2974 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2975 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2976 | return ret; |
| 2977 | } |
| 2978 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2979 | /* |
| 2980 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2981 | * @logical: Logical block offset in bytes. |
| 2982 | * @length: Length of extent in bytes. |
| 2983 | * |
| 2984 | * Return: Chunk mapping or ERR_PTR. |
| 2985 | */ |
| 2986 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2987 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2988 | { |
| 2989 | struct extent_map_tree *em_tree; |
| 2990 | struct extent_map *em; |
| 2991 | |
| 2992 | em_tree = &fs_info->mapping_tree.map_tree; |
| 2993 | read_lock(&em_tree->lock); |
| 2994 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2995 | read_unlock(&em_tree->lock); |
| 2996 | |
| 2997 | if (!em) { |
| 2998 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2999 | logical, length); |
| 3000 | return ERR_PTR(-EINVAL); |
| 3001 | } |
| 3002 | |
| 3003 | if (em->start > logical || em->start + em->len < logical) { |
| 3004 | btrfs_crit(fs_info, |
| 3005 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 3006 | logical, length, em->start, em->start + em->len); |
| 3007 | free_extent_map(em); |
| 3008 | return ERR_PTR(-EINVAL); |
| 3009 | } |
| 3010 | |
| 3011 | /* callers are responsible for dropping em's ref. */ |
| 3012 | return em; |
| 3013 | } |
| 3014 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3015 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3016 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3017 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3018 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3019 | struct map_lookup *map; |
| 3020 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3021 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3022 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3023 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 3024 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 3025 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3026 | /* |
| 3027 | * 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] | 3028 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3029 | * do anything we still error out. |
| 3030 | */ |
| 3031 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 3032 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3033 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 3034 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3035 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 3036 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3037 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3038 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3039 | /* |
| 3040 | * Take the device list mutex to prevent races with the final phase of |
| 3041 | * a device replace operation that replaces the device object associated |
| 3042 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 3043 | */ |
| 3044 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3045 | for (i = 0; i < map->num_stripes; i++) { |
| 3046 | struct btrfs_device *device = map->stripes[i].dev; |
| 3047 | ret = btrfs_free_dev_extent(trans, device, |
| 3048 | map->stripes[i].physical, |
| 3049 | &dev_extent_len); |
| 3050 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3051 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3052 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3053 | goto out; |
| 3054 | } |
| 3055 | |
| 3056 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3057 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3058 | btrfs_device_set_bytes_used(device, |
| 3059 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 3060 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3061 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3062 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3063 | } |
| 3064 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 3065 | ret = btrfs_update_device(trans, device); |
| 3066 | if (ret) { |
| 3067 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3068 | btrfs_abort_transaction(trans, ret); |
| 3069 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3070 | } |
| 3071 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3072 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3073 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 3074 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3075 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3076 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3077 | goto out; |
| 3078 | } |
| 3079 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3080 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3081 | |
| 3082 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 3083 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3084 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3085 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3086 | goto out; |
| 3087 | } |
| 3088 | } |
| 3089 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 3090 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3091 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3092 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3093 | goto out; |
| 3094 | } |
| 3095 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3096 | out: |
| 3097 | /* once for us */ |
| 3098 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3099 | return ret; |
| 3100 | } |
| 3101 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3102 | 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] | 3103 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3104 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3105 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3106 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3107 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3108 | /* |
| 3109 | * Prevent races with automatic removal of unused block groups. |
| 3110 | * After we relocate and before we remove the chunk with offset |
| 3111 | * chunk_offset, automatic removal of the block group can kick in, |
| 3112 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3113 | * |
| 3114 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3115 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3116 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3117 | * we release the path used to search the chunk/dev tree and before |
| 3118 | * the current task acquires this mutex and calls us. |
| 3119 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3120 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3121 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3122 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3123 | if (ret) |
| 3124 | return -ENOSPC; |
| 3125 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3126 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3127 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3128 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3129 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3130 | if (ret) |
| 3131 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3132 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 3133 | /* |
| 3134 | * We add the kobjects here (and after forcing data chunk creation) |
| 3135 | * since relocation is the only place we'll create chunks of a new |
| 3136 | * type at runtime. The only place where we'll remove the last |
| 3137 | * chunk of a type is the call immediately below this one. Even |
| 3138 | * so, we're protected against races with the cleaner thread since |
| 3139 | * we're covered by the delete_unused_bgs_mutex. |
| 3140 | */ |
| 3141 | btrfs_add_raid_kobjects(fs_info); |
| 3142 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3143 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3144 | chunk_offset); |
| 3145 | if (IS_ERR(trans)) { |
| 3146 | ret = PTR_ERR(trans); |
| 3147 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3148 | return ret; |
| 3149 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3150 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3151 | /* |
| 3152 | * step two, delete the device extents and the |
| 3153 | * chunk tree entries |
| 3154 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3155 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3156 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3157 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3158 | } |
| 3159 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3160 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3161 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3162 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3163 | struct btrfs_path *path; |
| 3164 | struct extent_buffer *leaf; |
| 3165 | struct btrfs_chunk *chunk; |
| 3166 | struct btrfs_key key; |
| 3167 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3168 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3169 | bool retried = false; |
| 3170 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3171 | int ret; |
| 3172 | |
| 3173 | path = btrfs_alloc_path(); |
| 3174 | if (!path) |
| 3175 | return -ENOMEM; |
| 3176 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3177 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3178 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3179 | key.offset = (u64)-1; |
| 3180 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3181 | |
| 3182 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3183 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3184 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3185 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3186 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3187 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3188 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3189 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3190 | |
| 3191 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3192 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3193 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3194 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3195 | if (ret < 0) |
| 3196 | goto error; |
| 3197 | if (ret > 0) |
| 3198 | break; |
| 3199 | |
| 3200 | leaf = path->nodes[0]; |
| 3201 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3202 | |
| 3203 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3204 | struct btrfs_chunk); |
| 3205 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3206 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3207 | |
| 3208 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3209 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3210 | if (ret == -ENOSPC) |
| 3211 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3212 | else |
| 3213 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3214 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3215 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3216 | |
| 3217 | if (found_key.offset == 0) |
| 3218 | break; |
| 3219 | key.offset = found_key.offset - 1; |
| 3220 | } |
| 3221 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3222 | if (failed && !retried) { |
| 3223 | failed = 0; |
| 3224 | retried = true; |
| 3225 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3226 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3227 | ret = -ENOSPC; |
| 3228 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3229 | error: |
| 3230 | btrfs_free_path(path); |
| 3231 | return ret; |
| 3232 | } |
| 3233 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3234 | /* |
| 3235 | * return 1 : allocate a data chunk successfully, |
| 3236 | * return <0: errors during allocating a data chunk, |
| 3237 | * return 0 : no need to allocate a data chunk. |
| 3238 | */ |
| 3239 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3240 | u64 chunk_offset) |
| 3241 | { |
| 3242 | struct btrfs_block_group_cache *cache; |
| 3243 | u64 bytes_used; |
| 3244 | u64 chunk_type; |
| 3245 | |
| 3246 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3247 | ASSERT(cache); |
| 3248 | chunk_type = cache->flags; |
| 3249 | btrfs_put_block_group(cache); |
| 3250 | |
| 3251 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 3252 | spin_lock(&fs_info->data_sinfo->lock); |
| 3253 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3254 | spin_unlock(&fs_info->data_sinfo->lock); |
| 3255 | |
| 3256 | if (!bytes_used) { |
| 3257 | struct btrfs_trans_handle *trans; |
| 3258 | int ret; |
| 3259 | |
| 3260 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3261 | if (IS_ERR(trans)) |
| 3262 | return PTR_ERR(trans); |
| 3263 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 3264 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3265 | BTRFS_BLOCK_GROUP_DATA); |
| 3266 | btrfs_end_transaction(trans); |
| 3267 | if (ret < 0) |
| 3268 | return ret; |
| 3269 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 3270 | btrfs_add_raid_kobjects(fs_info); |
| 3271 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3272 | return 1; |
| 3273 | } |
| 3274 | } |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3278 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3279 | struct btrfs_balance_control *bctl) |
| 3280 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3281 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3282 | struct btrfs_trans_handle *trans; |
| 3283 | struct btrfs_balance_item *item; |
| 3284 | struct btrfs_disk_balance_args disk_bargs; |
| 3285 | struct btrfs_path *path; |
| 3286 | struct extent_buffer *leaf; |
| 3287 | struct btrfs_key key; |
| 3288 | int ret, err; |
| 3289 | |
| 3290 | path = btrfs_alloc_path(); |
| 3291 | if (!path) |
| 3292 | return -ENOMEM; |
| 3293 | |
| 3294 | trans = btrfs_start_transaction(root, 0); |
| 3295 | if (IS_ERR(trans)) { |
| 3296 | btrfs_free_path(path); |
| 3297 | return PTR_ERR(trans); |
| 3298 | } |
| 3299 | |
| 3300 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3301 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3302 | key.offset = 0; |
| 3303 | |
| 3304 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3305 | sizeof(*item)); |
| 3306 | if (ret) |
| 3307 | goto out; |
| 3308 | |
| 3309 | leaf = path->nodes[0]; |
| 3310 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3311 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3312 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3313 | |
| 3314 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3315 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3316 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3317 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3318 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3319 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3320 | |
| 3321 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3322 | |
| 3323 | btrfs_mark_buffer_dirty(leaf); |
| 3324 | out: |
| 3325 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3326 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3327 | if (err && !ret) |
| 3328 | ret = err; |
| 3329 | return ret; |
| 3330 | } |
| 3331 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3332 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3333 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3334 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3335 | struct btrfs_trans_handle *trans; |
| 3336 | struct btrfs_path *path; |
| 3337 | struct btrfs_key key; |
| 3338 | int ret, err; |
| 3339 | |
| 3340 | path = btrfs_alloc_path(); |
| 3341 | if (!path) |
| 3342 | return -ENOMEM; |
| 3343 | |
| 3344 | trans = btrfs_start_transaction(root, 0); |
| 3345 | if (IS_ERR(trans)) { |
| 3346 | btrfs_free_path(path); |
| 3347 | return PTR_ERR(trans); |
| 3348 | } |
| 3349 | |
| 3350 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3351 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3352 | key.offset = 0; |
| 3353 | |
| 3354 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3355 | if (ret < 0) |
| 3356 | goto out; |
| 3357 | if (ret > 0) { |
| 3358 | ret = -ENOENT; |
| 3359 | goto out; |
| 3360 | } |
| 3361 | |
| 3362 | ret = btrfs_del_item(trans, root, path); |
| 3363 | out: |
| 3364 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3365 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3366 | if (err && !ret) |
| 3367 | ret = err; |
| 3368 | return ret; |
| 3369 | } |
| 3370 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3371 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3372 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3373 | * resume after balance was interrupted. |
| 3374 | */ |
| 3375 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3376 | { |
| 3377 | /* |
| 3378 | * Turn on soft mode for chunk types that were being converted. |
| 3379 | */ |
| 3380 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3381 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3382 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3383 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3384 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3385 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3386 | |
| 3387 | /* |
| 3388 | * Turn on usage filter if is not already used. The idea is |
| 3389 | * that chunks that we have already balanced should be |
| 3390 | * reasonably full. Don't do it for chunks that are being |
| 3391 | * converted - that will keep us from relocating unconverted |
| 3392 | * (albeit full) chunks. |
| 3393 | */ |
| 3394 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3395 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3396 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3397 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3398 | bctl->data.usage = 90; |
| 3399 | } |
| 3400 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3401 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3402 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3403 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3404 | bctl->sys.usage = 90; |
| 3405 | } |
| 3406 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3407 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3408 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3409 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3410 | bctl->meta.usage = 90; |
| 3411 | } |
| 3412 | } |
| 3413 | |
| 3414 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3415 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3416 | */ |
| 3417 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3418 | { |
| 3419 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3420 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3421 | |
| 3422 | BUG_ON(!fs_info->balance_ctl); |
| 3423 | |
| 3424 | spin_lock(&fs_info->balance_lock); |
| 3425 | fs_info->balance_ctl = NULL; |
| 3426 | spin_unlock(&fs_info->balance_lock); |
| 3427 | |
| 3428 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3429 | ret = del_balance_item(fs_info); |
| 3430 | if (ret) |
| 3431 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3434 | /* |
| 3435 | * Balance filters. Return 1 if chunk should be filtered out |
| 3436 | * (should not be balanced). |
| 3437 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3438 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3439 | struct btrfs_balance_args *bargs) |
| 3440 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3441 | chunk_type = chunk_to_extended(chunk_type) & |
| 3442 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3443 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3444 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3445 | return 0; |
| 3446 | |
| 3447 | return 1; |
| 3448 | } |
| 3449 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3450 | 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] | 3451 | struct btrfs_balance_args *bargs) |
| 3452 | { |
| 3453 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3454 | u64 chunk_used; |
| 3455 | u64 user_thresh_min; |
| 3456 | u64 user_thresh_max; |
| 3457 | int ret = 1; |
| 3458 | |
| 3459 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3460 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3461 | |
| 3462 | if (bargs->usage_min == 0) |
| 3463 | user_thresh_min = 0; |
| 3464 | else |
| 3465 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3466 | bargs->usage_min); |
| 3467 | |
| 3468 | if (bargs->usage_max == 0) |
| 3469 | user_thresh_max = 1; |
| 3470 | else if (bargs->usage_max > 100) |
| 3471 | user_thresh_max = cache->key.offset; |
| 3472 | else |
| 3473 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3474 | bargs->usage_max); |
| 3475 | |
| 3476 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3477 | ret = 0; |
| 3478 | |
| 3479 | btrfs_put_block_group(cache); |
| 3480 | return ret; |
| 3481 | } |
| 3482 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3483 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3484 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3485 | { |
| 3486 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3487 | u64 chunk_used, user_thresh; |
| 3488 | int ret = 1; |
| 3489 | |
| 3490 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3491 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3492 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3493 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3494 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3495 | else if (bargs->usage > 100) |
| 3496 | user_thresh = cache->key.offset; |
| 3497 | else |
| 3498 | user_thresh = div_factor_fine(cache->key.offset, |
| 3499 | bargs->usage); |
| 3500 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3501 | if (chunk_used < user_thresh) |
| 3502 | ret = 0; |
| 3503 | |
| 3504 | btrfs_put_block_group(cache); |
| 3505 | return ret; |
| 3506 | } |
| 3507 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3508 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3509 | struct btrfs_chunk *chunk, |
| 3510 | struct btrfs_balance_args *bargs) |
| 3511 | { |
| 3512 | struct btrfs_stripe *stripe; |
| 3513 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3514 | int i; |
| 3515 | |
| 3516 | for (i = 0; i < num_stripes; i++) { |
| 3517 | stripe = btrfs_stripe_nr(chunk, i); |
| 3518 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | return 1; |
| 3523 | } |
| 3524 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3525 | /* [pstart, pend) */ |
| 3526 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3527 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3528 | struct btrfs_balance_args *bargs) |
| 3529 | { |
| 3530 | struct btrfs_stripe *stripe; |
| 3531 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3532 | u64 stripe_offset; |
| 3533 | u64 stripe_length; |
| 3534 | int factor; |
| 3535 | int i; |
| 3536 | |
| 3537 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3538 | return 0; |
| 3539 | |
| 3540 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3541 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3542 | factor = num_stripes / 2; |
| 3543 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3544 | factor = num_stripes - 1; |
| 3545 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3546 | factor = num_stripes - 2; |
| 3547 | } else { |
| 3548 | factor = num_stripes; |
| 3549 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3550 | |
| 3551 | for (i = 0; i < num_stripes; i++) { |
| 3552 | stripe = btrfs_stripe_nr(chunk, i); |
| 3553 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3554 | continue; |
| 3555 | |
| 3556 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3557 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3558 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3559 | |
| 3560 | if (stripe_offset < bargs->pend && |
| 3561 | stripe_offset + stripe_length > bargs->pstart) |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
| 3565 | return 1; |
| 3566 | } |
| 3567 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3568 | /* [vstart, vend) */ |
| 3569 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3570 | struct btrfs_chunk *chunk, |
| 3571 | u64 chunk_offset, |
| 3572 | struct btrfs_balance_args *bargs) |
| 3573 | { |
| 3574 | if (chunk_offset < bargs->vend && |
| 3575 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3576 | /* at least part of the chunk is inside this vrange */ |
| 3577 | return 0; |
| 3578 | |
| 3579 | return 1; |
| 3580 | } |
| 3581 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3582 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3583 | struct btrfs_chunk *chunk, |
| 3584 | struct btrfs_balance_args *bargs) |
| 3585 | { |
| 3586 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3587 | |
| 3588 | if (bargs->stripes_min <= num_stripes |
| 3589 | && num_stripes <= bargs->stripes_max) |
| 3590 | return 0; |
| 3591 | |
| 3592 | return 1; |
| 3593 | } |
| 3594 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3595 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3596 | struct btrfs_balance_args *bargs) |
| 3597 | { |
| 3598 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3599 | return 0; |
| 3600 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3601 | chunk_type = chunk_to_extended(chunk_type) & |
| 3602 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3603 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3604 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3605 | return 1; |
| 3606 | |
| 3607 | return 0; |
| 3608 | } |
| 3609 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3610 | static int should_balance_chunk(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3611 | struct extent_buffer *leaf, |
| 3612 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3613 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3614 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3615 | struct btrfs_balance_args *bargs = NULL; |
| 3616 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3617 | |
| 3618 | /* type filter */ |
| 3619 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3620 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
| 3624 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3625 | bargs = &bctl->data; |
| 3626 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3627 | bargs = &bctl->sys; |
| 3628 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3629 | bargs = &bctl->meta; |
| 3630 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3631 | /* profiles filter */ |
| 3632 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3633 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3637 | /* usage filter */ |
| 3638 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3639 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3640 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3641 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3642 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3643 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3644 | } |
| 3645 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3646 | /* devid filter */ |
| 3647 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3648 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3649 | return 0; |
| 3650 | } |
| 3651 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3652 | /* drange filter, makes sense only with devid filter */ |
| 3653 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3654 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3655 | return 0; |
| 3656 | } |
| 3657 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3658 | /* vrange filter */ |
| 3659 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3660 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3664 | /* stripes filter */ |
| 3665 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3666 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3670 | /* soft profile changing mode */ |
| 3671 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3672 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3676 | /* |
| 3677 | * limited by count, must be the last filter |
| 3678 | */ |
| 3679 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3680 | if (bargs->limit == 0) |
| 3681 | return 0; |
| 3682 | else |
| 3683 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3684 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3685 | /* |
| 3686 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3687 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3688 | * about the count of all chunks that satisfy the filters. |
| 3689 | */ |
| 3690 | if (bargs->limit_max == 0) |
| 3691 | return 0; |
| 3692 | else |
| 3693 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3696 | return 1; |
| 3697 | } |
| 3698 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3699 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3700 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3701 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3702 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3703 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3704 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3705 | struct btrfs_device *device; |
| 3706 | u64 old_size; |
| 3707 | u64 size_to_free; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3708 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3709 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3710 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3711 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3712 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3713 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3714 | struct extent_buffer *leaf; |
| 3715 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3716 | int ret; |
| 3717 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3718 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3719 | /* 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] | 3720 | u64 limit_data = bctl->data.limit; |
| 3721 | u64 limit_meta = bctl->meta.limit; |
| 3722 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3723 | u32 count_data = 0; |
| 3724 | u32 count_meta = 0; |
| 3725 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3726 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3727 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3728 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3729 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3730 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3731 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3732 | size_to_free = div_factor(old_size, 1); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3733 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 3734 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3735 | btrfs_device_get_total_bytes(device) - |
| 3736 | btrfs_device_get_bytes_used(device) > size_to_free || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 3737 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3738 | continue; |
| 3739 | |
| 3740 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3741 | if (ret == -ENOSPC) |
| 3742 | break; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3743 | if (ret) { |
| 3744 | /* btrfs_shrink_device never returns ret > 0 */ |
| 3745 | WARN_ON(ret > 0); |
| 3746 | goto error; |
| 3747 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3748 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3749 | trans = btrfs_start_transaction(dev_root, 0); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3750 | if (IS_ERR(trans)) { |
| 3751 | ret = PTR_ERR(trans); |
| 3752 | btrfs_info_in_rcu(fs_info, |
| 3753 | "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3754 | rcu_str_deref(device->name), ret, |
| 3755 | old_size, old_size - size_to_free); |
| 3756 | goto error; |
| 3757 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3758 | |
| 3759 | ret = btrfs_grow_device(trans, device, old_size); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3760 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3761 | btrfs_end_transaction(trans); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3762 | /* btrfs_grow_device never returns ret > 0 */ |
| 3763 | WARN_ON(ret > 0); |
| 3764 | btrfs_info_in_rcu(fs_info, |
| 3765 | "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3766 | rcu_str_deref(device->name), ret, |
| 3767 | old_size, old_size - size_to_free); |
| 3768 | goto error; |
| 3769 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3770 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3771 | btrfs_end_transaction(trans); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | /* step two, relocate all the chunks */ |
| 3775 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3776 | if (!path) { |
| 3777 | ret = -ENOMEM; |
| 3778 | goto error; |
| 3779 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3780 | |
| 3781 | /* zero out stat counters */ |
| 3782 | spin_lock(&fs_info->balance_lock); |
| 3783 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3784 | spin_unlock(&fs_info->balance_lock); |
| 3785 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3786 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3787 | /* |
| 3788 | * The single value limit and min/max limits use the same bytes |
| 3789 | * in the |
| 3790 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3791 | bctl->data.limit = limit_data; |
| 3792 | bctl->meta.limit = limit_meta; |
| 3793 | bctl->sys.limit = limit_sys; |
| 3794 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3795 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3796 | key.offset = (u64)-1; |
| 3797 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3798 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3799 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3800 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3801 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3802 | ret = -ECANCELED; |
| 3803 | goto error; |
| 3804 | } |
| 3805 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3806 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3807 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3808 | if (ret < 0) { |
| 3809 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3810 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3811 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3812 | |
| 3813 | /* |
| 3814 | * this shouldn't happen, it means the last relocate |
| 3815 | * failed |
| 3816 | */ |
| 3817 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3818 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3819 | |
| 3820 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3821 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3822 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3823 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3824 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3825 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3826 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3827 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3828 | leaf = path->nodes[0]; |
| 3829 | slot = path->slots[0]; |
| 3830 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3831 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3832 | if (found_key.objectid != key.objectid) { |
| 3833 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3834 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3835 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3836 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3837 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3838 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3839 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3840 | if (!counting) { |
| 3841 | spin_lock(&fs_info->balance_lock); |
| 3842 | bctl->stat.considered++; |
| 3843 | spin_unlock(&fs_info->balance_lock); |
| 3844 | } |
| 3845 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3846 | ret = should_balance_chunk(fs_info, leaf, chunk, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3847 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3848 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3849 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3850 | if (!ret) { |
| 3851 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3852 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3853 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3854 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3855 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3856 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3857 | spin_lock(&fs_info->balance_lock); |
| 3858 | bctl->stat.expected++; |
| 3859 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3860 | |
| 3861 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3862 | count_data++; |
| 3863 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3864 | count_sys++; |
| 3865 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3866 | count_meta++; |
| 3867 | |
| 3868 | goto loop; |
| 3869 | } |
| 3870 | |
| 3871 | /* |
| 3872 | * Apply limit_min filter, no need to check if the LIMITS |
| 3873 | * filter is used, limit_min is 0 by default |
| 3874 | */ |
| 3875 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3876 | count_data < bctl->data.limit_min) |
| 3877 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3878 | count_meta < bctl->meta.limit_min) |
| 3879 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3880 | count_sys < bctl->sys.limit_min)) { |
| 3881 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3882 | goto loop; |
| 3883 | } |
| 3884 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3885 | if (!chunk_reserved) { |
| 3886 | /* |
| 3887 | * We may be relocating the only data chunk we have, |
| 3888 | * which could potentially end up with losing data's |
| 3889 | * raid profile, so lets allocate an empty one in |
| 3890 | * advance. |
| 3891 | */ |
| 3892 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3893 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3894 | if (ret < 0) { |
| 3895 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3896 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3897 | } else if (ret == 1) { |
| 3898 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3899 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3900 | } |
| 3901 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3902 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3903 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3904 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3905 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3906 | } else if (ret == -ETXTBSY) { |
| 3907 | btrfs_info(fs_info, |
| 3908 | "skipping relocation of block group %llu due to active swapfile", |
| 3909 | found_key.offset); |
| 3910 | ret = 0; |
| 3911 | } else if (ret) { |
| 3912 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3913 | } else { |
| 3914 | spin_lock(&fs_info->balance_lock); |
| 3915 | bctl->stat.completed++; |
| 3916 | spin_unlock(&fs_info->balance_lock); |
| 3917 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3918 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3919 | if (found_key.offset == 0) |
| 3920 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3921 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3922 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3923 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3924 | if (counting) { |
| 3925 | btrfs_release_path(path); |
| 3926 | counting = false; |
| 3927 | goto again; |
| 3928 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3929 | error: |
| 3930 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3931 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3932 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3933 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3934 | if (!ret) |
| 3935 | ret = -ENOSPC; |
| 3936 | } |
| 3937 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3938 | return ret; |
| 3939 | } |
| 3940 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3941 | /** |
| 3942 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3943 | * @flags: profile to validate |
| 3944 | * @extended: if true @flags is treated as an extended profile |
| 3945 | */ |
| 3946 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3947 | { |
| 3948 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3949 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3950 | |
| 3951 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3952 | |
| 3953 | /* 1) check that all other bits are zeroed */ |
| 3954 | if (flags & ~mask) |
| 3955 | return 0; |
| 3956 | |
| 3957 | /* 2) see if profile is reduced */ |
| 3958 | if (flags == 0) |
| 3959 | return !extended; /* "0" is valid for usual profiles */ |
| 3960 | |
| 3961 | /* true if exactly one bit set */ |
David Sterba | 818255f | 2018-09-21 14:26:34 +0200 | [diff] [blame] | 3962 | return is_power_of_2(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3963 | } |
| 3964 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3965 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3966 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3967 | /* cancel requested || normal exit path */ |
| 3968 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3969 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3970 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3973 | /* Non-zero return value signifies invalidity */ |
| 3974 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3975 | u64 allowed) |
| 3976 | { |
| 3977 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3978 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3979 | (bctl_arg->target & ~allowed))); |
| 3980 | } |
| 3981 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3982 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3983 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3984 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3985 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3986 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3987 | struct btrfs_ioctl_balance_args *bargs) |
| 3988 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3989 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3990 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3991 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3992 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3993 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3994 | unsigned seq; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3995 | bool reducing_integrity; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3996 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3997 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3998 | atomic_read(&fs_info->balance_pause_req) || |
| 3999 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4000 | ret = -EINVAL; |
| 4001 | goto out; |
| 4002 | } |
| 4003 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4004 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 4005 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 4006 | mixed = 1; |
| 4007 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4008 | /* |
| 4009 | * In case of mixed groups both data and meta should be picked, |
| 4010 | * and identical options should be given for both of them. |
| 4011 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4012 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 4013 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4014 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4015 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4016 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4017 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4018 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4019 | ret = -EINVAL; |
| 4020 | goto out; |
| 4021 | } |
| 4022 | } |
| 4023 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4024 | num_devices = btrfs_num_devices(fs_info); |
| 4025 | |
Austin S. Hemmelgarn | 88be159 | 2016-03-23 14:22:59 -0400 | [diff] [blame] | 4026 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP; |
| 4027 | if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4028 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 4029 | if (num_devices > 2) |
| 4030 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 4031 | if (num_devices > 3) |
| 4032 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 4033 | BTRFS_BLOCK_GROUP_RAID6); |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4034 | if (validate_convert_profile(&bctl->data, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4035 | int index = btrfs_bg_flags_to_raid_index(bctl->data.target); |
| 4036 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4037 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4038 | "balance: invalid convert data profile %s", |
| 4039 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4040 | ret = -EINVAL; |
| 4041 | goto out; |
| 4042 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4043 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4044 | int index = btrfs_bg_flags_to_raid_index(bctl->meta.target); |
| 4045 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4046 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4047 | "balance: invalid convert metadata profile %s", |
| 4048 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4049 | ret = -EINVAL; |
| 4050 | goto out; |
| 4051 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4052 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4053 | int index = btrfs_bg_flags_to_raid_index(bctl->sys.target); |
| 4054 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4055 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4056 | "balance: invalid convert system profile %s", |
| 4057 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4058 | ret = -EINVAL; |
| 4059 | goto out; |
| 4060 | } |
| 4061 | |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4062 | /* allow to reduce meta or sys integrity only if force set */ |
| 4063 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4064 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4065 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4066 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4067 | do { |
| 4068 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4069 | |
| 4070 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4071 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4072 | !(bctl->sys.target & allowed)) || |
| 4073 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4074 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4075 | !(bctl->meta.target & allowed))) |
| 4076 | reducing_integrity = true; |
| 4077 | else |
| 4078 | reducing_integrity = false; |
| 4079 | |
| 4080 | /* if we're not converting, the target field is uninitialized */ |
| 4081 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4082 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4083 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4084 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4085 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4086 | |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4087 | if (reducing_integrity) { |
| 4088 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4089 | btrfs_info(fs_info, |
| 4090 | "balance: force reducing metadata integrity"); |
| 4091 | } else { |
| 4092 | btrfs_err(fs_info, |
| 4093 | "balance: reduces metadata integrity, use --force if you want this"); |
| 4094 | ret = -EINVAL; |
| 4095 | goto out; |
| 4096 | } |
| 4097 | } |
| 4098 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4099 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4100 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4101 | int meta_index = btrfs_bg_flags_to_raid_index(meta_target); |
| 4102 | int data_index = btrfs_bg_flags_to_raid_index(data_target); |
| 4103 | |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4104 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4105 | "balance: metadata profile %s has lower redundancy than data profile %s", |
| 4106 | get_raid_name(meta_index), get_raid_name(data_index)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4109 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4110 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4111 | goto out; |
| 4112 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4113 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4114 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4115 | BUG_ON(fs_info->balance_ctl); |
| 4116 | spin_lock(&fs_info->balance_lock); |
| 4117 | fs_info->balance_ctl = bctl; |
| 4118 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4119 | } else { |
| 4120 | BUG_ON(ret != -EEXIST); |
| 4121 | spin_lock(&fs_info->balance_lock); |
| 4122 | update_balance_args(bctl); |
| 4123 | spin_unlock(&fs_info->balance_lock); |
| 4124 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4125 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4126 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4127 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4128 | mutex_unlock(&fs_info->balance_mutex); |
| 4129 | |
| 4130 | ret = __btrfs_balance(fs_info); |
| 4131 | |
| 4132 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4133 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4134 | |
| 4135 | if (bargs) { |
| 4136 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4137 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4140 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4141 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4142 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4143 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4146 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4147 | |
| 4148 | return ret; |
| 4149 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4150 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4151 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4152 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4153 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4154 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4155 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4156 | return ret; |
| 4157 | } |
| 4158 | |
| 4159 | static int balance_kthread(void *data) |
| 4160 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4161 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4162 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4163 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4164 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4165 | if (fs_info->balance_ctl) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4166 | btrfs_info(fs_info, "balance: resuming"); |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4167 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4168 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4169 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4170 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4171 | return ret; |
| 4172 | } |
| 4173 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4174 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4175 | { |
| 4176 | struct task_struct *tsk; |
| 4177 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4178 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4179 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4180 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4181 | return 0; |
| 4182 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4183 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4184 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4185 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4186 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
| 4189 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4190 | /* |
| 4191 | * A ro->rw remount sequence should continue with the paused balance |
| 4192 | * regardless of who pauses it, system or the user as of now, so set |
| 4193 | * the resume flag. |
| 4194 | */ |
| 4195 | spin_lock(&fs_info->balance_lock); |
| 4196 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4197 | spin_unlock(&fs_info->balance_lock); |
| 4198 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4199 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4200 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4201 | } |
| 4202 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4203 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4204 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4205 | struct btrfs_balance_control *bctl; |
| 4206 | struct btrfs_balance_item *item; |
| 4207 | struct btrfs_disk_balance_args disk_bargs; |
| 4208 | struct btrfs_path *path; |
| 4209 | struct extent_buffer *leaf; |
| 4210 | struct btrfs_key key; |
| 4211 | int ret; |
| 4212 | |
| 4213 | path = btrfs_alloc_path(); |
| 4214 | if (!path) |
| 4215 | return -ENOMEM; |
| 4216 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4217 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4218 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4219 | key.offset = 0; |
| 4220 | |
| 4221 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4222 | if (ret < 0) |
| 4223 | goto out; |
| 4224 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4225 | ret = 0; |
| 4226 | goto out; |
| 4227 | } |
| 4228 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4229 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4230 | if (!bctl) { |
| 4231 | ret = -ENOMEM; |
| 4232 | goto out; |
| 4233 | } |
| 4234 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4235 | leaf = path->nodes[0]; |
| 4236 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4237 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4238 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4239 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4240 | |
| 4241 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4242 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4243 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4244 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4245 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4246 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4247 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4248 | /* |
| 4249 | * This should never happen, as the paused balance state is recovered |
| 4250 | * during mount without any chance of other exclusive ops to collide. |
| 4251 | * |
| 4252 | * This gives the exclusive op status to balance and keeps in paused |
| 4253 | * state until user intervention (cancel or umount). If the ownership |
| 4254 | * cannot be assigned, show a message but do not fail. The balance |
| 4255 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4256 | * set up. |
| 4257 | */ |
| 4258 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4259 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4260 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4261 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4262 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4263 | BUG_ON(fs_info->balance_ctl); |
| 4264 | spin_lock(&fs_info->balance_lock); |
| 4265 | fs_info->balance_ctl = bctl; |
| 4266 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4267 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4268 | out: |
| 4269 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4270 | return ret; |
| 4271 | } |
| 4272 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4273 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4274 | { |
| 4275 | int ret = 0; |
| 4276 | |
| 4277 | mutex_lock(&fs_info->balance_mutex); |
| 4278 | if (!fs_info->balance_ctl) { |
| 4279 | mutex_unlock(&fs_info->balance_mutex); |
| 4280 | return -ENOTCONN; |
| 4281 | } |
| 4282 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4283 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4284 | atomic_inc(&fs_info->balance_pause_req); |
| 4285 | mutex_unlock(&fs_info->balance_mutex); |
| 4286 | |
| 4287 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4288 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4289 | |
| 4290 | mutex_lock(&fs_info->balance_mutex); |
| 4291 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4292 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4293 | atomic_dec(&fs_info->balance_pause_req); |
| 4294 | } else { |
| 4295 | ret = -ENOTCONN; |
| 4296 | } |
| 4297 | |
| 4298 | mutex_unlock(&fs_info->balance_mutex); |
| 4299 | return ret; |
| 4300 | } |
| 4301 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4302 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4303 | { |
| 4304 | mutex_lock(&fs_info->balance_mutex); |
| 4305 | if (!fs_info->balance_ctl) { |
| 4306 | mutex_unlock(&fs_info->balance_mutex); |
| 4307 | return -ENOTCONN; |
| 4308 | } |
| 4309 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4310 | /* |
| 4311 | * A paused balance with the item stored on disk can be resumed at |
| 4312 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4313 | * and we must not allow cancelling as it deletes the item. |
| 4314 | */ |
| 4315 | if (sb_rdonly(fs_info->sb)) { |
| 4316 | mutex_unlock(&fs_info->balance_mutex); |
| 4317 | return -EROFS; |
| 4318 | } |
| 4319 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4320 | atomic_inc(&fs_info->balance_cancel_req); |
| 4321 | /* |
| 4322 | * if we are running just wait and return, balance item is |
| 4323 | * deleted in btrfs_balance in this case |
| 4324 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4325 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4326 | mutex_unlock(&fs_info->balance_mutex); |
| 4327 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4328 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4329 | mutex_lock(&fs_info->balance_mutex); |
| 4330 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4331 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4332 | /* |
| 4333 | * Lock released to allow other waiters to continue, we'll |
| 4334 | * reexamine the status again. |
| 4335 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4336 | mutex_lock(&fs_info->balance_mutex); |
| 4337 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4338 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4339 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4340 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4341 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4342 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4343 | } |
| 4344 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4345 | BUG_ON(fs_info->balance_ctl || |
| 4346 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4347 | atomic_dec(&fs_info->balance_cancel_req); |
| 4348 | mutex_unlock(&fs_info->balance_mutex); |
| 4349 | return 0; |
| 4350 | } |
| 4351 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4352 | static int btrfs_uuid_scan_kthread(void *data) |
| 4353 | { |
| 4354 | struct btrfs_fs_info *fs_info = data; |
| 4355 | struct btrfs_root *root = fs_info->tree_root; |
| 4356 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4357 | struct btrfs_path *path = NULL; |
| 4358 | int ret = 0; |
| 4359 | struct extent_buffer *eb; |
| 4360 | int slot; |
| 4361 | struct btrfs_root_item root_item; |
| 4362 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4363 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4364 | |
| 4365 | path = btrfs_alloc_path(); |
| 4366 | if (!path) { |
| 4367 | ret = -ENOMEM; |
| 4368 | goto out; |
| 4369 | } |
| 4370 | |
| 4371 | key.objectid = 0; |
| 4372 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4373 | key.offset = 0; |
| 4374 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4375 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4376 | ret = btrfs_search_forward(root, &key, path, |
| 4377 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4378 | if (ret) { |
| 4379 | if (ret > 0) |
| 4380 | ret = 0; |
| 4381 | break; |
| 4382 | } |
| 4383 | |
| 4384 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4385 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4386 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4387 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4388 | goto skip; |
| 4389 | |
| 4390 | eb = path->nodes[0]; |
| 4391 | slot = path->slots[0]; |
| 4392 | item_size = btrfs_item_size_nr(eb, slot); |
| 4393 | if (item_size < sizeof(root_item)) |
| 4394 | goto skip; |
| 4395 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4396 | read_extent_buffer(eb, &root_item, |
| 4397 | btrfs_item_ptr_offset(eb, slot), |
| 4398 | (int)sizeof(root_item)); |
| 4399 | if (btrfs_root_refs(&root_item) == 0) |
| 4400 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4401 | |
| 4402 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4403 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4404 | if (trans) |
| 4405 | goto update_tree; |
| 4406 | |
| 4407 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4408 | /* |
| 4409 | * 1 - subvol uuid item |
| 4410 | * 1 - received_subvol uuid item |
| 4411 | */ |
| 4412 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4413 | if (IS_ERR(trans)) { |
| 4414 | ret = PTR_ERR(trans); |
| 4415 | break; |
| 4416 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4417 | continue; |
| 4418 | } else { |
| 4419 | goto skip; |
| 4420 | } |
| 4421 | update_tree: |
| 4422 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4423 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4424 | BTRFS_UUID_KEY_SUBVOL, |
| 4425 | key.objectid); |
| 4426 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4427 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4428 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4429 | break; |
| 4430 | } |
| 4431 | } |
| 4432 | |
| 4433 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4434 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4435 | root_item.received_uuid, |
| 4436 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4437 | key.objectid); |
| 4438 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4439 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4440 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4441 | break; |
| 4442 | } |
| 4443 | } |
| 4444 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4445 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4446 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4447 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4448 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4449 | if (ret) |
| 4450 | break; |
| 4451 | } |
| 4452 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4453 | btrfs_release_path(path); |
| 4454 | if (key.offset < (u64)-1) { |
| 4455 | key.offset++; |
| 4456 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4457 | key.offset = 0; |
| 4458 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4459 | } else if (key.objectid < (u64)-1) { |
| 4460 | key.offset = 0; |
| 4461 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4462 | key.objectid++; |
| 4463 | } else { |
| 4464 | break; |
| 4465 | } |
| 4466 | cond_resched(); |
| 4467 | } |
| 4468 | |
| 4469 | out: |
| 4470 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4471 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4472 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4473 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4474 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4475 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4476 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4477 | up(&fs_info->uuid_tree_rescan_sem); |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4481 | /* |
| 4482 | * Callback for btrfs_uuid_tree_iterate(). |
| 4483 | * returns: |
| 4484 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4485 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4486 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4487 | */ |
| 4488 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4489 | u8 *uuid, u8 type, u64 subid) |
| 4490 | { |
| 4491 | struct btrfs_key key; |
| 4492 | int ret = 0; |
| 4493 | struct btrfs_root *subvol_root; |
| 4494 | |
| 4495 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4496 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4497 | goto out; |
| 4498 | |
| 4499 | key.objectid = subid; |
| 4500 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4501 | key.offset = (u64)-1; |
| 4502 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4503 | if (IS_ERR(subvol_root)) { |
| 4504 | ret = PTR_ERR(subvol_root); |
| 4505 | if (ret == -ENOENT) |
| 4506 | ret = 1; |
| 4507 | goto out; |
| 4508 | } |
| 4509 | |
| 4510 | switch (type) { |
| 4511 | case BTRFS_UUID_KEY_SUBVOL: |
| 4512 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4513 | ret = 1; |
| 4514 | break; |
| 4515 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4516 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4517 | BTRFS_UUID_SIZE)) |
| 4518 | ret = 1; |
| 4519 | break; |
| 4520 | } |
| 4521 | |
| 4522 | out: |
| 4523 | return ret; |
| 4524 | } |
| 4525 | |
| 4526 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4527 | { |
| 4528 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4529 | int ret; |
| 4530 | |
| 4531 | /* |
| 4532 | * 1st step is to iterate through the existing UUID tree and |
| 4533 | * to delete all entries that contain outdated data. |
| 4534 | * 2nd step is to add all missing entries to the UUID tree. |
| 4535 | */ |
| 4536 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4537 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4538 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4539 | up(&fs_info->uuid_tree_rescan_sem); |
| 4540 | return ret; |
| 4541 | } |
| 4542 | return btrfs_uuid_scan_kthread(data); |
| 4543 | } |
| 4544 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4545 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4546 | { |
| 4547 | struct btrfs_trans_handle *trans; |
| 4548 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4549 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4550 | struct task_struct *task; |
| 4551 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4552 | |
| 4553 | /* |
| 4554 | * 1 - root node |
| 4555 | * 1 - root item |
| 4556 | */ |
| 4557 | trans = btrfs_start_transaction(tree_root, 2); |
| 4558 | if (IS_ERR(trans)) |
| 4559 | return PTR_ERR(trans); |
| 4560 | |
| 4561 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4562 | BTRFS_UUID_TREE_OBJECTID); |
| 4563 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4564 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4565 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4566 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4567 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | fs_info->uuid_root = uuid_root; |
| 4571 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4572 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4573 | if (ret) |
| 4574 | return ret; |
| 4575 | |
| 4576 | down(&fs_info->uuid_tree_rescan_sem); |
| 4577 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4578 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4579 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4580 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4581 | up(&fs_info->uuid_tree_rescan_sem); |
| 4582 | return PTR_ERR(task); |
| 4583 | } |
| 4584 | |
| 4585 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4586 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4587 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4588 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4589 | { |
| 4590 | struct task_struct *task; |
| 4591 | |
| 4592 | down(&fs_info->uuid_tree_rescan_sem); |
| 4593 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4594 | if (IS_ERR(task)) { |
| 4595 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4596 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4597 | up(&fs_info->uuid_tree_rescan_sem); |
| 4598 | return PTR_ERR(task); |
| 4599 | } |
| 4600 | |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4604 | /* |
| 4605 | * shrinking a device means finding all of the device extents past |
| 4606 | * the new size, and then following the back refs to the chunks. |
| 4607 | * The chunk relocation code actually frees the device extent |
| 4608 | */ |
| 4609 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4610 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4611 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4612 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4613 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4614 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4615 | struct btrfs_path *path; |
| 4616 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4617 | u64 chunk_offset; |
| 4618 | int ret; |
| 4619 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4620 | int failed = 0; |
| 4621 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4622 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4623 | struct extent_buffer *l; |
| 4624 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4625 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4626 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4627 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4628 | u64 diff; |
| 4629 | |
| 4630 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4631 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4632 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4633 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4634 | return -EINVAL; |
| 4635 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4636 | path = btrfs_alloc_path(); |
| 4637 | if (!path) |
| 4638 | return -ENOMEM; |
| 4639 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4640 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4641 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4642 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4643 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4644 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4645 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4646 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4647 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4648 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4649 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4650 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4651 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4652 | key.objectid = device->devid; |
| 4653 | key.offset = (u64)-1; |
| 4654 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4655 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4656 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4657 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4658 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4659 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4660 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4661 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4662 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4663 | |
| 4664 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4665 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4666 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4667 | if (ret < 0) |
| 4668 | goto done; |
| 4669 | if (ret) { |
| 4670 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4671 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4672 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | l = path->nodes[0]; |
| 4676 | slot = path->slots[0]; |
| 4677 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4678 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4679 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4680 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4681 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4682 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4683 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4684 | |
| 4685 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4686 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4687 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4688 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4689 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4690 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4691 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4692 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4693 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4694 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4695 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4696 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4697 | /* |
| 4698 | * We may be relocating the only data chunk we have, |
| 4699 | * which could potentially end up with losing data's |
| 4700 | * raid profile, so lets allocate an empty one in |
| 4701 | * advance. |
| 4702 | */ |
| 4703 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4704 | if (ret < 0) { |
| 4705 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4706 | goto done; |
| 4707 | } |
| 4708 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4709 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4710 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4711 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4712 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4713 | } else if (ret) { |
| 4714 | if (ret == -ETXTBSY) { |
| 4715 | btrfs_warn(fs_info, |
| 4716 | "could not shrink block group %llu due to active swapfile", |
| 4717 | chunk_offset); |
| 4718 | } |
| 4719 | goto done; |
| 4720 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4721 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4722 | |
| 4723 | if (failed && !retried) { |
| 4724 | failed = 0; |
| 4725 | retried = true; |
| 4726 | goto again; |
| 4727 | } else if (failed && retried) { |
| 4728 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4729 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4730 | } |
| 4731 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4732 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4733 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4734 | if (IS_ERR(trans)) { |
| 4735 | ret = PTR_ERR(trans); |
| 4736 | goto done; |
| 4737 | } |
| 4738 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4739 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4740 | |
| 4741 | /* |
| 4742 | * We checked in the above loop all device extents that were already in |
| 4743 | * the device tree. However before we have updated the device's |
| 4744 | * total_bytes to the new size, we might have had chunk allocations that |
| 4745 | * have not complete yet (new block groups attached to transaction |
| 4746 | * handles), and therefore their device extents were not yet in the |
| 4747 | * device tree and we missed them in the loop above. So if we have any |
| 4748 | * pending chunk using a device extent that overlaps the device range |
| 4749 | * that we can not use anymore, commit the current transaction and |
| 4750 | * repeat the search on the device tree - this way we guarantee we will |
| 4751 | * not have chunks using device extents that end beyond 'new_size'. |
| 4752 | */ |
| 4753 | if (!checked_pending_chunks) { |
| 4754 | u64 start = new_size; |
| 4755 | u64 len = old_size - new_size; |
| 4756 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4757 | if (contains_pending_extent(trans->transaction, device, |
| 4758 | &start, len)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4759 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4760 | checked_pending_chunks = true; |
| 4761 | failed = 0; |
| 4762 | retried = false; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4763 | ret = btrfs_commit_transaction(trans); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4764 | if (ret) |
| 4765 | goto done; |
| 4766 | goto again; |
| 4767 | } |
| 4768 | } |
| 4769 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4770 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4771 | if (list_empty(&device->resized_list)) |
| 4772 | list_add_tail(&device->resized_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4773 | &fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4774 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4775 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4776 | btrfs_set_super_total_bytes(super_copy, |
| 4777 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4778 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4779 | |
| 4780 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4781 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4782 | if (ret < 0) { |
| 4783 | btrfs_abort_transaction(trans, ret); |
| 4784 | btrfs_end_transaction(trans); |
| 4785 | } else { |
| 4786 | ret = btrfs_commit_transaction(trans); |
| 4787 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4788 | done: |
| 4789 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4790 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4791 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4792 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4793 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4794 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4795 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4796 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4797 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4798 | return ret; |
| 4799 | } |
| 4800 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4801 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4802 | struct btrfs_key *key, |
| 4803 | struct btrfs_chunk *chunk, int item_size) |
| 4804 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4805 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4806 | struct btrfs_disk_key disk_key; |
| 4807 | u32 array_size; |
| 4808 | u8 *ptr; |
| 4809 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4810 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4811 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4812 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4813 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4814 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4815 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4816 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4817 | |
| 4818 | ptr = super_copy->sys_chunk_array + array_size; |
| 4819 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4820 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4821 | ptr += sizeof(disk_key); |
| 4822 | memcpy(ptr, chunk, item_size); |
| 4823 | item_size += sizeof(disk_key); |
| 4824 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4825 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4826 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
| 4829 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4830 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4831 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4832 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4833 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4834 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4835 | const struct btrfs_device_info *di_a = a; |
| 4836 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4837 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4838 | if (di_a->max_avail > di_b->max_avail) |
| 4839 | return -1; |
| 4840 | if (di_a->max_avail < di_b->max_avail) |
| 4841 | return 1; |
| 4842 | if (di_a->total_avail > di_b->total_avail) |
| 4843 | return -1; |
| 4844 | if (di_a->total_avail < di_b->total_avail) |
| 4845 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4846 | return 0; |
| 4847 | } |
| 4848 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4849 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4850 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4851 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4852 | return; |
| 4853 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4854 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4855 | } |
| 4856 | |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4857 | #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \ |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4858 | - sizeof(struct btrfs_chunk)) \ |
| 4859 | / sizeof(struct btrfs_stripe) + 1) |
| 4860 | |
| 4861 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4862 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4863 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4864 | / sizeof(struct btrfs_stripe) + 1) |
| 4865 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4866 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4867 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4868 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4869 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4870 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4871 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4872 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4873 | struct extent_map_tree *em_tree; |
| 4874 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4875 | struct btrfs_device_info *devices_info = NULL; |
| 4876 | u64 total_avail; |
| 4877 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4878 | int data_stripes; /* number of stripes that count for |
| 4879 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4880 | int sub_stripes; /* sub_stripes info for map */ |
| 4881 | int dev_stripes; /* stripes per dev */ |
| 4882 | int devs_max; /* max devs to use */ |
| 4883 | int devs_min; /* min devs needed */ |
| 4884 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4885 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4886 | int nparity; /* number of stripes worth of bytes to |
| 4887 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4888 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4889 | u64 max_stripe_size; |
| 4890 | u64 max_chunk_size; |
| 4891 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4892 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4893 | int ndevs; |
| 4894 | int i; |
| 4895 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4896 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4897 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4898 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4899 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4900 | if (list_empty(&fs_devices->alloc_list)) { |
| 4901 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4902 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4903 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4904 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4905 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4906 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4907 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4908 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4909 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4910 | devs_max = btrfs_raid_array[index].devs_max; |
| 4911 | devs_min = btrfs_raid_array[index].devs_min; |
| 4912 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4913 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4914 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4915 | |
| 4916 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4917 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4918 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4919 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4920 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4921 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4922 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4923 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4924 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4925 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4926 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4927 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4928 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4929 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4930 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4931 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4932 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4933 | if (!devs_max) |
| 4934 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4935 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4936 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4937 | type); |
| 4938 | BUG_ON(1); |
| 4939 | } |
| 4940 | |
| 4941 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4942 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4943 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4944 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4945 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4946 | GFP_NOFS); |
| 4947 | if (!devices_info) |
| 4948 | return -ENOMEM; |
| 4949 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4950 | /* |
| 4951 | * in the first pass through the devices list, we gather information |
| 4952 | * about the available holes on each device. |
| 4953 | */ |
| 4954 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4955 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4956 | u64 max_avail; |
| 4957 | u64 dev_offset; |
| 4958 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4959 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4960 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4961 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4962 | continue; |
| 4963 | } |
| 4964 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4965 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4966 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4967 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4968 | continue; |
| 4969 | |
| 4970 | if (device->total_bytes > device->bytes_used) |
| 4971 | total_avail = device->total_bytes - device->bytes_used; |
| 4972 | else |
| 4973 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4974 | |
| 4975 | /* If there is no space on this device, skip it. */ |
| 4976 | if (total_avail == 0) |
| 4977 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4978 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4979 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4980 | max_stripe_size * dev_stripes, |
| 4981 | &dev_offset, &max_avail); |
| 4982 | if (ret && ret != -ENOSPC) |
| 4983 | goto error; |
| 4984 | |
| 4985 | if (ret == 0) |
| 4986 | max_avail = max_stripe_size * dev_stripes; |
| 4987 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4988 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 4989 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4990 | btrfs_debug(info, |
| 4991 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 4992 | __func__, device->devid, max_avail, |
| 4993 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4994 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4995 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4996 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4997 | if (ndevs == fs_devices->rw_devices) { |
| 4998 | WARN(1, "%s: found more than %llu devices\n", |
| 4999 | __func__, fs_devices->rw_devices); |
| 5000 | break; |
| 5001 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5002 | devices_info[ndevs].dev_offset = dev_offset; |
| 5003 | devices_info[ndevs].max_avail = max_avail; |
| 5004 | devices_info[ndevs].total_avail = total_avail; |
| 5005 | devices_info[ndevs].dev = device; |
| 5006 | ++ndevs; |
| 5007 | } |
| 5008 | |
| 5009 | /* |
| 5010 | * now sort the devices by hole size / available space |
| 5011 | */ |
| 5012 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 5013 | btrfs_cmp_device_info, NULL); |
| 5014 | |
| 5015 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 5016 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5017 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5018 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5019 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5020 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5021 | btrfs_debug(info, |
| 5022 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5023 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5024 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5025 | goto error; |
| 5026 | } |
| 5027 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 5028 | ndevs = min(ndevs, devs_max); |
| 5029 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5030 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5031 | * The primary goal is to maximize the number of stripes, so use as |
| 5032 | * many devices as possible, even if the stripes are not maximum sized. |
| 5033 | * |
| 5034 | * The DUP profile stores more than one stripe per device, the |
| 5035 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5036 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5037 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5038 | num_stripes = ndevs * dev_stripes; |
| 5039 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5040 | /* |
| 5041 | * this will have to be fixed for RAID1 and RAID10 over |
| 5042 | * more drives |
| 5043 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 5044 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5045 | |
| 5046 | /* |
| 5047 | * Use the number of data stripes to figure out how big this chunk |
| 5048 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5049 | * and compare that answer with the max chunk size. If it's higher, |
| 5050 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5051 | */ |
| 5052 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5053 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5054 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5055 | * then use it, unless it ends up being even bigger than the |
| 5056 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5057 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5058 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 5059 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5060 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5061 | } |
| 5062 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5063 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5064 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5065 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5066 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 5067 | if (!map) { |
| 5068 | ret = -ENOMEM; |
| 5069 | goto error; |
| 5070 | } |
| 5071 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5072 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5073 | for (i = 0; i < ndevs; ++i) { |
| 5074 | for (j = 0; j < dev_stripes; ++j) { |
| 5075 | int s = i * dev_stripes + j; |
| 5076 | map->stripes[s].dev = devices_info[i].dev; |
| 5077 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5078 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5079 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5080 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5081 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5082 | map->io_align = BTRFS_STRIPE_LEN; |
| 5083 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5084 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5085 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5086 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5087 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5088 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5089 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5090 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5091 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5092 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5093 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5094 | ret = -ENOMEM; |
| 5095 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5096 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5097 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5098 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5099 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5100 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5101 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5102 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5103 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5104 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5105 | em_tree = &info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5106 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5107 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5108 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5109 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5110 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5111 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5112 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5113 | |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5114 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 5115 | refcount_inc(&em->refs); |
| 5116 | write_unlock(&em_tree->lock); |
| 5117 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5118 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5119 | if (ret) |
| 5120 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5121 | |
Hans van Kranenburg | 2f29df4 | 2018-10-04 23:24:38 +0200 | [diff] [blame] | 5122 | for (i = 0; i < map->num_stripes; i++) |
| 5123 | btrfs_device_set_bytes_used(map->stripes[i].dev, |
| 5124 | map->stripes[i].dev->bytes_used + stripe_size); |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5125 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 5126 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5127 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5128 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5129 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5130 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5131 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5132 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5133 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5134 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5135 | write_lock(&em_tree->lock); |
| 5136 | remove_extent_mapping(em_tree, em); |
| 5137 | write_unlock(&em_tree->lock); |
| 5138 | |
| 5139 | /* One for our allocation */ |
| 5140 | free_extent_map(em); |
| 5141 | /* One for the tree reference */ |
| 5142 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 5143 | /* One for the pending_chunks list reference */ |
| 5144 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5145 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5146 | kfree(devices_info); |
| 5147 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5148 | } |
| 5149 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5150 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5151 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5152 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5153 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5154 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5155 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5156 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5157 | struct btrfs_device *device; |
| 5158 | struct btrfs_chunk *chunk; |
| 5159 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5160 | struct extent_map *em; |
| 5161 | struct map_lookup *map; |
| 5162 | size_t item_size; |
| 5163 | u64 dev_offset; |
| 5164 | u64 stripe_size; |
| 5165 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5166 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5167 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5168 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5169 | if (IS_ERR(em)) |
| 5170 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5171 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5172 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5173 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5174 | stripe_size = em->orig_block_len; |
| 5175 | |
| 5176 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5177 | if (!chunk) { |
| 5178 | ret = -ENOMEM; |
| 5179 | goto out; |
| 5180 | } |
| 5181 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5182 | /* |
| 5183 | * Take the device list mutex to prevent races with the final phase of |
| 5184 | * a device replace operation that replaces the device object associated |
| 5185 | * with the map's stripes, because the device object's id can change |
| 5186 | * at any time during that final phase of the device replace operation |
| 5187 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5188 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5189 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5190 | for (i = 0; i < map->num_stripes; i++) { |
| 5191 | device = map->stripes[i].dev; |
| 5192 | dev_offset = map->stripes[i].physical; |
| 5193 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5194 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5195 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5196 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5197 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5198 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5199 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5200 | break; |
| 5201 | } |
| 5202 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5203 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5204 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5205 | } |
| 5206 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5207 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5208 | for (i = 0; i < map->num_stripes; i++) { |
| 5209 | device = map->stripes[i].dev; |
| 5210 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5211 | |
| 5212 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5213 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5214 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5215 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5216 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5217 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5218 | |
| 5219 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5220 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5221 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5222 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5223 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5224 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5225 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5226 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5227 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5228 | |
| 5229 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5230 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5231 | key.offset = chunk_offset; |
| 5232 | |
| 5233 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5234 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5235 | /* |
| 5236 | * TODO: Cleanup of inserted chunk root in case of |
| 5237 | * failure. |
| 5238 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5239 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5240 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5241 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5242 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5243 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5244 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5245 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | /* |
| 5249 | * Chunk allocation falls into two parts. The first part does works |
| 5250 | * that make the new allocated chunk useable, but not do any operation |
| 5251 | * that modifies the chunk tree. The second part does the works that |
| 5252 | * require modifying the chunk tree. This division is important for the |
| 5253 | * bootstrap process of adding storage to a seed btrfs. |
| 5254 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5255 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5256 | { |
| 5257 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5258 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5259 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5260 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5261 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5262 | } |
| 5263 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5264 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 5265 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5266 | { |
| 5267 | u64 chunk_offset; |
| 5268 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5269 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5270 | int ret; |
| 5271 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5272 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5273 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5274 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5275 | if (ret) |
| 5276 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5277 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5278 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5279 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5280 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5281 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5282 | } |
| 5283 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5284 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5285 | { |
| 5286 | int max_errors; |
| 5287 | |
| 5288 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5289 | BTRFS_BLOCK_GROUP_RAID10 | |
| 5290 | BTRFS_BLOCK_GROUP_RAID5 | |
| 5291 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5292 | max_errors = 1; |
| 5293 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 5294 | max_errors = 2; |
| 5295 | } else { |
| 5296 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5297 | } |
| 5298 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5299 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5300 | } |
| 5301 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5302 | 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] | 5303 | { |
| 5304 | struct extent_map *em; |
| 5305 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5306 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5307 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5308 | int i; |
| 5309 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5310 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5311 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5312 | return 1; |
| 5313 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5314 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5315 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5316 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5317 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5318 | miss_ndevs++; |
| 5319 | continue; |
| 5320 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5321 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5322 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5323 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5324 | goto end; |
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 | |
| 5328 | /* |
| 5329 | * If the number of missing devices is larger than max errors, |
| 5330 | * we can not write the data into that chunk successfully, so |
| 5331 | * set it readonly. |
| 5332 | */ |
| 5333 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5334 | readonly = 1; |
| 5335 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5336 | free_extent_map(em); |
| 5337 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 5341 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 5342 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 5346 | { |
| 5347 | struct extent_map *em; |
| 5348 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5349 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5350 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5351 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 5352 | if (em) |
| 5353 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5354 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5355 | if (!em) |
| 5356 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5357 | /* once for us */ |
| 5358 | free_extent_map(em); |
| 5359 | /* once for the tree */ |
| 5360 | free_extent_map(em); |
| 5361 | } |
| 5362 | } |
| 5363 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5364 | 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] | 5365 | { |
| 5366 | struct extent_map *em; |
| 5367 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5368 | int ret; |
| 5369 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5370 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5371 | if (IS_ERR(em)) |
| 5372 | /* |
| 5373 | * We could return errors for these cases, but that could get |
| 5374 | * ugly and we'd probably do the same thing which is just not do |
| 5375 | * anything else and exit, so return 1 so the callers don't try |
| 5376 | * to use other copies. |
| 5377 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5378 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5379 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5380 | map = em->map_lookup; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5381 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 5382 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5383 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5384 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5385 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5386 | ret = 2; |
| 5387 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5388 | /* |
| 5389 | * There could be two corrupted data stripes, we need |
| 5390 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5391 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5392 | * Fail a stripe at a time on every retry except the |
| 5393 | * stripe under reconstruction. |
| 5394 | */ |
| 5395 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5396 | else |
| 5397 | ret = 1; |
| 5398 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5399 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5400 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5401 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5402 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5403 | ret++; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5404 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5405 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5406 | return ret; |
| 5407 | } |
| 5408 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5409 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5410 | u64 logical) |
| 5411 | { |
| 5412 | struct extent_map *em; |
| 5413 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5414 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5415 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5416 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5417 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5418 | if (!WARN_ON(IS_ERR(em))) { |
| 5419 | map = em->map_lookup; |
| 5420 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5421 | len = map->stripe_len * nr_data_stripes(map); |
| 5422 | free_extent_map(em); |
| 5423 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5424 | return len; |
| 5425 | } |
| 5426 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5427 | 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] | 5428 | { |
| 5429 | struct extent_map *em; |
| 5430 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5431 | int ret = 0; |
| 5432 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5433 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5434 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5435 | if(!WARN_ON(IS_ERR(em))) { |
| 5436 | map = em->map_lookup; |
| 5437 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5438 | ret = 1; |
| 5439 | free_extent_map(em); |
| 5440 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5441 | return ret; |
| 5442 | } |
| 5443 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5444 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5445 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5446 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5447 | { |
| 5448 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5449 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5450 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5451 | int tolerance; |
| 5452 | struct btrfs_device *srcdev; |
| 5453 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5454 | ASSERT((map->type & |
| 5455 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))); |
| 5456 | |
| 5457 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5458 | num_stripes = map->sub_stripes; |
| 5459 | else |
| 5460 | num_stripes = map->num_stripes; |
| 5461 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5462 | preferred_mirror = first + current->pid % num_stripes; |
| 5463 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5464 | if (dev_replace_is_ongoing && |
| 5465 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5466 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5467 | srcdev = fs_info->dev_replace.srcdev; |
| 5468 | else |
| 5469 | srcdev = NULL; |
| 5470 | |
| 5471 | /* |
| 5472 | * try to avoid the drive that is the source drive for a |
| 5473 | * dev-replace procedure, only choose it if no other non-missing |
| 5474 | * mirror is available |
| 5475 | */ |
| 5476 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5477 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5478 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5479 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5480 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5481 | if (map->stripes[i].dev->bdev && |
| 5482 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5483 | return i; |
| 5484 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5485 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5486 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5487 | /* we couldn't find one that doesn't fail. Just return something |
| 5488 | * and the io error handling code will clean up eventually |
| 5489 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5490 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5491 | } |
| 5492 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5493 | static inline int parity_smaller(u64 a, u64 b) |
| 5494 | { |
| 5495 | return a > b; |
| 5496 | } |
| 5497 | |
| 5498 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5499 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5500 | { |
| 5501 | struct btrfs_bio_stripe s; |
| 5502 | int i; |
| 5503 | u64 l; |
| 5504 | int again = 1; |
| 5505 | |
| 5506 | while (again) { |
| 5507 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5508 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5509 | if (parity_smaller(bbio->raid_map[i], |
| 5510 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5511 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5512 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5513 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5514 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5515 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5516 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5517 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5518 | again = 1; |
| 5519 | } |
| 5520 | } |
| 5521 | } |
| 5522 | } |
| 5523 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5524 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5525 | { |
| 5526 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5527 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5528 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5529 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5530 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5531 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5532 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5533 | /* |
| 5534 | * plus the raid_map, which includes both the tgt dev |
| 5535 | * and the stripes |
| 5536 | */ |
| 5537 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5538 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5539 | |
| 5540 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5541 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5542 | |
| 5543 | return bbio; |
| 5544 | } |
| 5545 | |
| 5546 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5547 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5548 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5549 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5553 | { |
| 5554 | if (!bbio) |
| 5555 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5556 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5557 | kfree(bbio); |
| 5558 | } |
| 5559 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5560 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5561 | /* |
| 5562 | * Please note that, discard won't be sent to target device of device |
| 5563 | * replace. |
| 5564 | */ |
| 5565 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5566 | u64 logical, u64 length, |
| 5567 | struct btrfs_bio **bbio_ret) |
| 5568 | { |
| 5569 | struct extent_map *em; |
| 5570 | struct map_lookup *map; |
| 5571 | struct btrfs_bio *bbio; |
| 5572 | u64 offset; |
| 5573 | u64 stripe_nr; |
| 5574 | u64 stripe_nr_end; |
| 5575 | u64 stripe_end_offset; |
| 5576 | u64 stripe_cnt; |
| 5577 | u64 stripe_len; |
| 5578 | u64 stripe_offset; |
| 5579 | u64 num_stripes; |
| 5580 | u32 stripe_index; |
| 5581 | u32 factor = 0; |
| 5582 | u32 sub_stripes = 0; |
| 5583 | u64 stripes_per_dev = 0; |
| 5584 | u32 remaining_stripes = 0; |
| 5585 | u32 last_stripe = 0; |
| 5586 | int ret = 0; |
| 5587 | int i; |
| 5588 | |
| 5589 | /* discard always return a bbio */ |
| 5590 | ASSERT(bbio_ret); |
| 5591 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5592 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5593 | if (IS_ERR(em)) |
| 5594 | return PTR_ERR(em); |
| 5595 | |
| 5596 | map = em->map_lookup; |
| 5597 | /* we don't discard raid56 yet */ |
| 5598 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5599 | ret = -EOPNOTSUPP; |
| 5600 | goto out; |
| 5601 | } |
| 5602 | |
| 5603 | offset = logical - em->start; |
| 5604 | length = min_t(u64, em->len - offset, length); |
| 5605 | |
| 5606 | stripe_len = map->stripe_len; |
| 5607 | /* |
| 5608 | * stripe_nr counts the total number of stripes we have to stride |
| 5609 | * to get to this block |
| 5610 | */ |
| 5611 | stripe_nr = div64_u64(offset, stripe_len); |
| 5612 | |
| 5613 | /* stripe_offset is the offset of this block in its stripe */ |
| 5614 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5615 | |
| 5616 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5617 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5618 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5619 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5620 | (offset + length); |
| 5621 | /* |
| 5622 | * after this, stripe_nr is the number of stripes on this |
| 5623 | * device we have to walk to find the data, and stripe_index is |
| 5624 | * the number of our device in the stripe array |
| 5625 | */ |
| 5626 | num_stripes = 1; |
| 5627 | stripe_index = 0; |
| 5628 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5629 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5630 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5631 | sub_stripes = 1; |
| 5632 | else |
| 5633 | sub_stripes = map->sub_stripes; |
| 5634 | |
| 5635 | factor = map->num_stripes / sub_stripes; |
| 5636 | num_stripes = min_t(u64, map->num_stripes, |
| 5637 | sub_stripes * stripe_cnt); |
| 5638 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5639 | stripe_index *= sub_stripes; |
| 5640 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5641 | &remaining_stripes); |
| 5642 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5643 | last_stripe *= sub_stripes; |
| 5644 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5645 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5646 | num_stripes = map->num_stripes; |
| 5647 | } else { |
| 5648 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5649 | &stripe_index); |
| 5650 | } |
| 5651 | |
| 5652 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5653 | if (!bbio) { |
| 5654 | ret = -ENOMEM; |
| 5655 | goto out; |
| 5656 | } |
| 5657 | |
| 5658 | for (i = 0; i < num_stripes; i++) { |
| 5659 | bbio->stripes[i].physical = |
| 5660 | map->stripes[stripe_index].physical + |
| 5661 | stripe_offset + stripe_nr * map->stripe_len; |
| 5662 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5663 | |
| 5664 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5665 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5666 | bbio->stripes[i].length = stripes_per_dev * |
| 5667 | map->stripe_len; |
| 5668 | |
| 5669 | if (i / sub_stripes < remaining_stripes) |
| 5670 | bbio->stripes[i].length += |
| 5671 | map->stripe_len; |
| 5672 | |
| 5673 | /* |
| 5674 | * Special for the first stripe and |
| 5675 | * the last stripe: |
| 5676 | * |
| 5677 | * |-------|...|-------| |
| 5678 | * |----------| |
| 5679 | * off end_off |
| 5680 | */ |
| 5681 | if (i < sub_stripes) |
| 5682 | bbio->stripes[i].length -= |
| 5683 | stripe_offset; |
| 5684 | |
| 5685 | if (stripe_index >= last_stripe && |
| 5686 | stripe_index <= (last_stripe + |
| 5687 | sub_stripes - 1)) |
| 5688 | bbio->stripes[i].length -= |
| 5689 | stripe_end_offset; |
| 5690 | |
| 5691 | if (i == sub_stripes - 1) |
| 5692 | stripe_offset = 0; |
| 5693 | } else { |
| 5694 | bbio->stripes[i].length = length; |
| 5695 | } |
| 5696 | |
| 5697 | stripe_index++; |
| 5698 | if (stripe_index == map->num_stripes) { |
| 5699 | stripe_index = 0; |
| 5700 | stripe_nr++; |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | *bbio_ret = bbio; |
| 5705 | bbio->map_type = map->type; |
| 5706 | bbio->num_stripes = num_stripes; |
| 5707 | out: |
| 5708 | free_extent_map(em); |
| 5709 | return ret; |
| 5710 | } |
| 5711 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5712 | /* |
| 5713 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5714 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5715 | * left cursor can also be read from the target drive. |
| 5716 | * |
| 5717 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5718 | * array of stripes. |
| 5719 | * For READ, it also needs to be supported using the same mirror number. |
| 5720 | * |
| 5721 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5722 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5723 | * case. |
| 5724 | */ |
| 5725 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5726 | u64 logical, u64 length, |
| 5727 | u64 srcdev_devid, int *mirror_num, |
| 5728 | u64 *physical) |
| 5729 | { |
| 5730 | struct btrfs_bio *bbio = NULL; |
| 5731 | int num_stripes; |
| 5732 | int index_srcdev = 0; |
| 5733 | int found = 0; |
| 5734 | u64 physical_of_found = 0; |
| 5735 | int i; |
| 5736 | int ret = 0; |
| 5737 | |
| 5738 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5739 | logical, &length, &bbio, 0, 0); |
| 5740 | if (ret) { |
| 5741 | ASSERT(bbio == NULL); |
| 5742 | return ret; |
| 5743 | } |
| 5744 | |
| 5745 | num_stripes = bbio->num_stripes; |
| 5746 | if (*mirror_num > num_stripes) { |
| 5747 | /* |
| 5748 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5749 | * that means that the requested area is not left of the left |
| 5750 | * cursor |
| 5751 | */ |
| 5752 | btrfs_put_bbio(bbio); |
| 5753 | return -EIO; |
| 5754 | } |
| 5755 | |
| 5756 | /* |
| 5757 | * process the rest of the function using the mirror_num of the source |
| 5758 | * drive. Therefore look it up first. At the end, patch the device |
| 5759 | * pointer to the one of the target drive. |
| 5760 | */ |
| 5761 | for (i = 0; i < num_stripes; i++) { |
| 5762 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5763 | continue; |
| 5764 | |
| 5765 | /* |
| 5766 | * In case of DUP, in order to keep it simple, only add the |
| 5767 | * mirror with the lowest physical address |
| 5768 | */ |
| 5769 | if (found && |
| 5770 | physical_of_found <= bbio->stripes[i].physical) |
| 5771 | continue; |
| 5772 | |
| 5773 | index_srcdev = i; |
| 5774 | found = 1; |
| 5775 | physical_of_found = bbio->stripes[i].physical; |
| 5776 | } |
| 5777 | |
| 5778 | btrfs_put_bbio(bbio); |
| 5779 | |
| 5780 | ASSERT(found); |
| 5781 | if (!found) |
| 5782 | return -EIO; |
| 5783 | |
| 5784 | *mirror_num = index_srcdev + 1; |
| 5785 | *physical = physical_of_found; |
| 5786 | return ret; |
| 5787 | } |
| 5788 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5789 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5790 | struct btrfs_bio **bbio_ret, |
| 5791 | struct btrfs_dev_replace *dev_replace, |
| 5792 | int *num_stripes_ret, int *max_errors_ret) |
| 5793 | { |
| 5794 | struct btrfs_bio *bbio = *bbio_ret; |
| 5795 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5796 | int tgtdev_indexes = 0; |
| 5797 | int num_stripes = *num_stripes_ret; |
| 5798 | int max_errors = *max_errors_ret; |
| 5799 | int i; |
| 5800 | |
| 5801 | if (op == BTRFS_MAP_WRITE) { |
| 5802 | int index_where_to_add; |
| 5803 | |
| 5804 | /* |
| 5805 | * duplicate the write operations while the dev replace |
| 5806 | * procedure is running. Since the copying of the old disk to |
| 5807 | * the new disk takes place at run time while the filesystem is |
| 5808 | * mounted writable, the regular write operations to the old |
| 5809 | * disk have to be duplicated to go to the new disk as well. |
| 5810 | * |
| 5811 | * Note that device->missing is handled by the caller, and that |
| 5812 | * the write to the old disk is already set up in the stripes |
| 5813 | * array. |
| 5814 | */ |
| 5815 | index_where_to_add = num_stripes; |
| 5816 | for (i = 0; i < num_stripes; i++) { |
| 5817 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5818 | /* write to new disk, too */ |
| 5819 | struct btrfs_bio_stripe *new = |
| 5820 | bbio->stripes + index_where_to_add; |
| 5821 | struct btrfs_bio_stripe *old = |
| 5822 | bbio->stripes + i; |
| 5823 | |
| 5824 | new->physical = old->physical; |
| 5825 | new->length = old->length; |
| 5826 | new->dev = dev_replace->tgtdev; |
| 5827 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5828 | index_where_to_add++; |
| 5829 | max_errors++; |
| 5830 | tgtdev_indexes++; |
| 5831 | } |
| 5832 | } |
| 5833 | num_stripes = index_where_to_add; |
| 5834 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5835 | int index_srcdev = 0; |
| 5836 | int found = 0; |
| 5837 | u64 physical_of_found = 0; |
| 5838 | |
| 5839 | /* |
| 5840 | * During the dev-replace procedure, the target drive can also |
| 5841 | * be used to read data in case it is needed to repair a corrupt |
| 5842 | * block elsewhere. This is possible if the requested area is |
| 5843 | * left of the left cursor. In this area, the target drive is a |
| 5844 | * full copy of the source drive. |
| 5845 | */ |
| 5846 | for (i = 0; i < num_stripes; i++) { |
| 5847 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5848 | /* |
| 5849 | * In case of DUP, in order to keep it simple, |
| 5850 | * only add the mirror with the lowest physical |
| 5851 | * address |
| 5852 | */ |
| 5853 | if (found && |
| 5854 | physical_of_found <= |
| 5855 | bbio->stripes[i].physical) |
| 5856 | continue; |
| 5857 | index_srcdev = i; |
| 5858 | found = 1; |
| 5859 | physical_of_found = bbio->stripes[i].physical; |
| 5860 | } |
| 5861 | } |
| 5862 | if (found) { |
| 5863 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5864 | bbio->stripes + num_stripes; |
| 5865 | |
| 5866 | tgtdev_stripe->physical = physical_of_found; |
| 5867 | tgtdev_stripe->length = |
| 5868 | bbio->stripes[index_srcdev].length; |
| 5869 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5870 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5871 | |
| 5872 | tgtdev_indexes++; |
| 5873 | num_stripes++; |
| 5874 | } |
| 5875 | } |
| 5876 | |
| 5877 | *num_stripes_ret = num_stripes; |
| 5878 | *max_errors_ret = max_errors; |
| 5879 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5880 | *bbio_ret = bbio; |
| 5881 | } |
| 5882 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5883 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5884 | { |
| 5885 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5886 | } |
| 5887 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5888 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5889 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5890 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5891 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5892 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5893 | { |
| 5894 | struct extent_map *em; |
| 5895 | struct map_lookup *map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5896 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5897 | u64 stripe_offset; |
| 5898 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5899 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5900 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5901 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5902 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5903 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5904 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5905 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5906 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5907 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5908 | int dev_replace_is_ongoing = 0; |
| 5909 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5910 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5911 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5912 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5913 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5914 | if (op == BTRFS_MAP_DISCARD) |
| 5915 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 5916 | *length, bbio_ret); |
| 5917 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5918 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5919 | if (IS_ERR(em)) |
| 5920 | return PTR_ERR(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5921 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5922 | map = em->map_lookup; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5923 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5924 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5925 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5926 | stripe_nr = offset; |
| 5927 | /* |
| 5928 | * stripe_nr counts the total number of stripes we have to stride |
| 5929 | * to get to this block |
| 5930 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5931 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5932 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5933 | stripe_offset = stripe_nr * stripe_len; |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5934 | if (offset < stripe_offset) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5935 | btrfs_crit(fs_info, |
| 5936 | "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] | 5937 | stripe_offset, offset, em->start, logical, |
| 5938 | stripe_len); |
| 5939 | free_extent_map(em); |
| 5940 | return -EINVAL; |
| 5941 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5942 | |
| 5943 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5944 | stripe_offset = offset - stripe_offset; |
| 5945 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5946 | /* 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] | 5947 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5948 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5949 | raid56_full_stripe_start = offset; |
| 5950 | |
| 5951 | /* allow a write of a full stripe, but make sure we don't |
| 5952 | * allow straddling of stripes |
| 5953 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5954 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5955 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5956 | raid56_full_stripe_start *= full_stripe_len; |
| 5957 | } |
| 5958 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5959 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5960 | u64 max_len; |
| 5961 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5962 | For other RAID types and for RAID[56] reads, just allow a single |
| 5963 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5964 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5965 | (op == BTRFS_MAP_WRITE)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5966 | max_len = stripe_len * nr_data_stripes(map) - |
| 5967 | (offset - raid56_full_stripe_start); |
| 5968 | } else { |
| 5969 | /* we limit the length of each bio to what fits in a stripe */ |
| 5970 | max_len = stripe_len - stripe_offset; |
| 5971 | } |
| 5972 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5973 | } else { |
| 5974 | *length = em->len - offset; |
| 5975 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5976 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5977 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5978 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5979 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5980 | goto out; |
| 5981 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5982 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5983 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5984 | if (!dev_replace_is_ongoing) |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5985 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5986 | else |
| 5987 | btrfs_dev_replace_set_lock_blocking(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5988 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5989 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5990 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5991 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 5992 | dev_replace->srcdev->devid, |
| 5993 | &mirror_num, |
| 5994 | &physical_to_patch_in_first_stripe); |
| 5995 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5996 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5997 | else |
| 5998 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5999 | } else if (mirror_num > map->num_stripes) { |
| 6000 | mirror_num = 0; |
| 6001 | } |
| 6002 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6003 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6004 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6005 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6006 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6007 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6008 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6009 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6010 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6011 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6012 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6013 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6014 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6015 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6016 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6017 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6018 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6019 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6020 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6021 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6022 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6023 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6024 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6025 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6026 | } else { |
| 6027 | mirror_num = 1; |
| 6028 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6029 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6030 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6031 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6032 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6033 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6034 | stripe_index *= map->sub_stripes; |
| 6035 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6036 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6037 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6038 | else if (mirror_num) |
| 6039 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6040 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6041 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6042 | stripe_index = find_live_mirror(fs_info, map, |
| 6043 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6044 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6045 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6046 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6047 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6048 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6049 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6050 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6051 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6052 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6053 | |
| 6054 | /* RAID[56] write or recovery. Return all stripes */ |
| 6055 | num_stripes = map->num_stripes; |
| 6056 | max_errors = nr_parity_stripes(map); |
| 6057 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6058 | *length = map->stripe_len; |
| 6059 | stripe_index = 0; |
| 6060 | stripe_offset = 0; |
| 6061 | } else { |
| 6062 | /* |
| 6063 | * Mirror #0 or #1 means the original data block. |
| 6064 | * Mirror #2 is RAID5 parity block. |
| 6065 | * Mirror #3 is RAID6 Q block. |
| 6066 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6067 | stripe_nr = div_u64_rem(stripe_nr, |
| 6068 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6069 | if (mirror_num > 1) |
| 6070 | stripe_index = nr_data_stripes(map) + |
| 6071 | mirror_num - 2; |
| 6072 | |
| 6073 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6074 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6075 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6076 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6077 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6078 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6079 | } else { |
| 6080 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6081 | * after this, stripe_nr is the number of stripes on this |
| 6082 | * device we have to walk to find the data, and stripe_index is |
| 6083 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6084 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6085 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6086 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6087 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6088 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6089 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6090 | btrfs_crit(fs_info, |
| 6091 | "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] | 6092 | stripe_index, map->num_stripes); |
| 6093 | ret = -EINVAL; |
| 6094 | goto out; |
| 6095 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6096 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6097 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6098 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6099 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6100 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6101 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6102 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6103 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6104 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6105 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6106 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6107 | if (!bbio) { |
| 6108 | ret = -ENOMEM; |
| 6109 | goto out; |
| 6110 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6111 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6112 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6113 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6114 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6115 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6116 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6117 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6118 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6119 | |
| 6120 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6121 | sizeof(struct btrfs_bio_stripe) * |
| 6122 | num_alloc_stripes + |
| 6123 | sizeof(int) * tgtdev_indexes); |
| 6124 | |
| 6125 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6126 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6127 | |
| 6128 | /* Fill in the logical address of each stripe */ |
| 6129 | tmp = stripe_nr * nr_data_stripes(map); |
| 6130 | for (i = 0; i < nr_data_stripes(map); i++) |
| 6131 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6132 | em->start + (tmp + i) * map->stripe_len; |
| 6133 | |
| 6134 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6135 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6136 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6137 | RAID6_Q_STRIPE; |
| 6138 | } |
| 6139 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6140 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6141 | for (i = 0; i < num_stripes; i++) { |
| 6142 | bbio->stripes[i].physical = |
| 6143 | map->stripes[stripe_index].physical + |
| 6144 | stripe_offset + |
| 6145 | stripe_nr * map->stripe_len; |
| 6146 | bbio->stripes[i].dev = |
| 6147 | map->stripes[stripe_index].dev; |
| 6148 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6149 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6150 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6151 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6152 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6153 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6154 | if (bbio->raid_map) |
| 6155 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6156 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6157 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6158 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6159 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6160 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6161 | } |
| 6162 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6163 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6164 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6165 | bbio->num_stripes = num_stripes; |
| 6166 | bbio->max_errors = max_errors; |
| 6167 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6168 | |
| 6169 | /* |
| 6170 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6171 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6172 | * available as a mirror |
| 6173 | */ |
| 6174 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6175 | WARN_ON(num_stripes > 1); |
| 6176 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6177 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6178 | bbio->mirror_num = map->num_stripes + 1; |
| 6179 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6180 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6181 | if (dev_replace_is_ongoing) { |
David Sterba | 7fb2ece | 2018-08-24 17:33:58 +0200 | [diff] [blame] | 6182 | ASSERT(atomic_read(&dev_replace->blocking_readers) > 0); |
| 6183 | btrfs_dev_replace_read_lock(dev_replace); |
| 6184 | /* Barrier implied by atomic_dec_and_test */ |
| 6185 | if (atomic_dec_and_test(&dev_replace->blocking_readers)) |
| 6186 | cond_wake_up_nomb(&dev_replace->read_lock_wq); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 6187 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6188 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6189 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6190 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6191 | } |
| 6192 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6193 | 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] | 6194 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6195 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6196 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6197 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6198 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6199 | } |
| 6200 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6201 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6202 | 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] | 6203 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6204 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6205 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6206 | 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] | 6207 | } |
| 6208 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6209 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6210 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6211 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6212 | struct extent_map *em; |
| 6213 | struct map_lookup *map; |
| 6214 | u64 *buf; |
| 6215 | u64 bytenr; |
| 6216 | u64 length; |
| 6217 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6218 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6219 | int i, j, nr = 0; |
| 6220 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6221 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6222 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6223 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6224 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6225 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6226 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6227 | rmap_len = map->stripe_len; |
| 6228 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6229 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6230 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6231 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6232 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6233 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6234 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6235 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6236 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6237 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6238 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6239 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6240 | |
| 6241 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6242 | if (map->stripes[i].physical > physical || |
| 6243 | map->stripes[i].physical + length <= physical) |
| 6244 | continue; |
| 6245 | |
| 6246 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6247 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6248 | |
| 6249 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6250 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6251 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6252 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6253 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6254 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6255 | * Alternatively, just use rmap_len below instead of |
| 6256 | * map->stripe_len */ |
| 6257 | |
| 6258 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6259 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6260 | for (j = 0; j < nr; j++) { |
| 6261 | if (buf[j] == bytenr) |
| 6262 | break; |
| 6263 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6264 | if (j == nr) { |
| 6265 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6266 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6267 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6268 | } |
| 6269 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6270 | *logical = buf; |
| 6271 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6272 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6273 | |
| 6274 | free_extent_map(em); |
| 6275 | return 0; |
| 6276 | } |
| 6277 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6278 | 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] | 6279 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6280 | bio->bi_private = bbio->private; |
| 6281 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6282 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6283 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6284 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6285 | } |
| 6286 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6287 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6288 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6289 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6290 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6291 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6292 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6293 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6294 | if (bio->bi_status == BLK_STS_IOERR || |
| 6295 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6296 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6297 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6298 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6299 | |
| 6300 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6301 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6302 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6303 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6304 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6305 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 6306 | else |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6307 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6308 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6309 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6310 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6311 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6312 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6313 | } |
| 6314 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6315 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6316 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6317 | is_orig_bio = 1; |
| 6318 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6319 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6320 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6321 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6322 | if (!is_orig_bio) { |
| 6323 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6324 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6325 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6326 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6327 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6328 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6329 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6330 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6331 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6332 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6333 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6334 | /* |
| 6335 | * this bio is actually up to date, we didn't |
| 6336 | * go over the max number of errors |
| 6337 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6338 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6339 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6340 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6341 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6342 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6343 | bio_put(bio); |
| 6344 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6345 | } |
| 6346 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6347 | /* |
| 6348 | * see run_scheduled_bios for a description of why bios are collected for |
| 6349 | * async submit. |
| 6350 | * |
| 6351 | * This will add one bio to the pending list for a device and make sure |
| 6352 | * the work struct is scheduled. |
| 6353 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6354 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6355 | struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6356 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6357 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6358 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6359 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6360 | |
| 6361 | /* don't bother with additional async steps for reads, right now */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6362 | if (bio_op(bio) == REQ_OP_READ) { |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6363 | btrfsic_submit_bio(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6364 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6365 | } |
| 6366 | |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 6367 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6368 | bio->bi_next = NULL; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6369 | |
| 6370 | spin_lock(&device->io_lock); |
Christoph Hellwig | 67f055c | 2016-11-01 07:40:06 -0600 | [diff] [blame] | 6371 | if (op_is_sync(bio->bi_opf)) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6372 | pending_bios = &device->pending_sync_bios; |
| 6373 | else |
| 6374 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6375 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6376 | if (pending_bios->tail) |
| 6377 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6378 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6379 | pending_bios->tail = bio; |
| 6380 | if (!pending_bios->head) |
| 6381 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6382 | if (device->running_pending) |
| 6383 | should_queue = 0; |
| 6384 | |
| 6385 | spin_unlock(&device->io_lock); |
| 6386 | |
| 6387 | if (should_queue) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6388 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6389 | } |
| 6390 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6391 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
| 6392 | u64 physical, int dev_nr, int async) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6393 | { |
| 6394 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6395 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6396 | |
| 6397 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6398 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6399 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6400 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6401 | btrfs_debug_in_rcu(fs_info, |
| 6402 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6403 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6404 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6405 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6406 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6407 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6408 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6409 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6410 | if (async) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6411 | btrfs_schedule_bio(dev, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6412 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6413 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6414 | } |
| 6415 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6416 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6417 | { |
| 6418 | atomic_inc(&bbio->error); |
| 6419 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6420 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6421 | WARN_ON(bio != bbio->orig_bio); |
| 6422 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6423 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6424 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6425 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6426 | bio->bi_status = BLK_STS_IOERR; |
| 6427 | else |
| 6428 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6429 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6430 | } |
| 6431 | } |
| 6432 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6433 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
| 6434 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6435 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6436 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6437 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6438 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6439 | u64 length = 0; |
| 6440 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6441 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6442 | int dev_nr; |
| 6443 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6444 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6445 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6446 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6447 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6448 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6449 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6450 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6451 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6452 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6453 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6454 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6455 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6456 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6457 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6458 | bbio->orig_bio = first_bio; |
| 6459 | bbio->private = first_bio->bi_private; |
| 6460 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6461 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6462 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6463 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6464 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6465 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6466 | /* In this case, map_length has been set to the length of |
| 6467 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6468 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6469 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6470 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6471 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6472 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6473 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6474 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6475 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6476 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6477 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6478 | } |
| 6479 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6480 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6481 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6482 | "mapping failed logical %llu bio len %llu len %llu", |
| 6483 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6484 | BUG(); |
| 6485 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6486 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6487 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6488 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6489 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6490 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6491 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6492 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6493 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6494 | continue; |
| 6495 | } |
| 6496 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6497 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6498 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6499 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6500 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6501 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6502 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
| 6503 | dev_nr, async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6504 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6505 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6506 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6507 | } |
| 6508 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6509 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6510 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6511 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6512 | struct btrfs_device *device; |
| 6513 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6514 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6515 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6516 | while (cur_devices) { |
| 6517 | if (!fsid || |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6518 | !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 6519 | device = find_device(cur_devices, devid, uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6520 | if (device) |
| 6521 | return device; |
| 6522 | } |
| 6523 | cur_devices = cur_devices->seed; |
| 6524 | } |
| 6525 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6526 | } |
| 6527 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6528 | 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] | 6529 | u64 devid, u8 *dev_uuid) |
| 6530 | { |
| 6531 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6532 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6533 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6534 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6535 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6536 | |
| 6537 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6538 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6539 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6540 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6541 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6542 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6543 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6544 | return device; |
| 6545 | } |
| 6546 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6547 | /** |
| 6548 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6549 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6550 | * devid is provided (i.e. @devid != NULL). |
| 6551 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6552 | * is generated. |
| 6553 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6554 | * is generated. |
| 6555 | * |
| 6556 | * 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] | 6557 | * 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] | 6558 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6559 | */ |
| 6560 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6561 | const u64 *devid, |
| 6562 | const u8 *uuid) |
| 6563 | { |
| 6564 | struct btrfs_device *dev; |
| 6565 | u64 tmp; |
| 6566 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6567 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6568 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6569 | |
| 6570 | dev = __alloc_device(); |
| 6571 | if (IS_ERR(dev)) |
| 6572 | return dev; |
| 6573 | |
| 6574 | if (devid) |
| 6575 | tmp = *devid; |
| 6576 | else { |
| 6577 | int ret; |
| 6578 | |
| 6579 | ret = find_next_devid(fs_info, &tmp); |
| 6580 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6581 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6582 | return ERR_PTR(ret); |
| 6583 | } |
| 6584 | } |
| 6585 | dev->devid = tmp; |
| 6586 | |
| 6587 | if (uuid) |
| 6588 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6589 | else |
| 6590 | generate_random_uuid(dev->uuid); |
| 6591 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6592 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6593 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6594 | |
| 6595 | return dev; |
| 6596 | } |
| 6597 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6598 | /* Return -EIO if any error, otherwise return 0. */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6599 | static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6600 | struct extent_buffer *leaf, |
| 6601 | struct btrfs_chunk *chunk, u64 logical) |
| 6602 | { |
| 6603 | u64 length; |
| 6604 | u64 stripe_len; |
| 6605 | u16 num_stripes; |
| 6606 | u16 sub_stripes; |
| 6607 | u64 type; |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6608 | u64 features; |
| 6609 | bool mixed = false; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6610 | |
| 6611 | length = btrfs_chunk_length(leaf, chunk); |
| 6612 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6613 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6614 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 6615 | type = btrfs_chunk_type(leaf, chunk); |
| 6616 | |
| 6617 | if (!num_stripes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6618 | btrfs_err(fs_info, "invalid chunk num_stripes: %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6619 | num_stripes); |
| 6620 | return -EIO; |
| 6621 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6622 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
| 6623 | btrfs_err(fs_info, "invalid chunk logical %llu", logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6624 | return -EIO; |
| 6625 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6626 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
| 6627 | btrfs_err(fs_info, "invalid chunk sectorsize %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6628 | btrfs_chunk_sector_size(leaf, chunk)); |
| 6629 | return -EIO; |
| 6630 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6631 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
| 6632 | btrfs_err(fs_info, "invalid chunk length %llu", length); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6633 | return -EIO; |
| 6634 | } |
| 6635 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6636 | btrfs_err(fs_info, "invalid chunk stripe length: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6637 | stripe_len); |
| 6638 | return -EIO; |
| 6639 | } |
| 6640 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6641 | type) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6642 | btrfs_err(fs_info, "unrecognized chunk type: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6643 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 6644 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6645 | btrfs_chunk_type(leaf, chunk)); |
| 6646 | return -EIO; |
| 6647 | } |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6648 | |
| 6649 | if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { |
| 6650 | btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type); |
| 6651 | return -EIO; |
| 6652 | } |
| 6653 | |
| 6654 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 6655 | (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { |
| 6656 | btrfs_err(fs_info, |
| 6657 | "system chunk with data or metadata type: 0x%llx", type); |
| 6658 | return -EIO; |
| 6659 | } |
| 6660 | |
| 6661 | features = btrfs_super_incompat_flags(fs_info->super_copy); |
| 6662 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 6663 | mixed = true; |
| 6664 | |
| 6665 | if (!mixed) { |
| 6666 | if ((type & BTRFS_BLOCK_GROUP_METADATA) && |
| 6667 | (type & BTRFS_BLOCK_GROUP_DATA)) { |
| 6668 | btrfs_err(fs_info, |
| 6669 | "mixed chunk type in non-mixed mode: 0x%llx", type); |
| 6670 | return -EIO; |
| 6671 | } |
| 6672 | } |
| 6673 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6674 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
| 6675 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || |
| 6676 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
| 6677 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || |
| 6678 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || |
| 6679 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && |
| 6680 | num_stripes != 1)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6681 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6682 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
| 6683 | num_stripes, sub_stripes, |
| 6684 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 6685 | return -EIO; |
| 6686 | } |
| 6687 | |
| 6688 | return 0; |
| 6689 | } |
| 6690 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6691 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6692 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6693 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6694 | if (error) |
| 6695 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6696 | devid, uuid); |
| 6697 | else |
| 6698 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6699 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6700 | } |
| 6701 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6702 | static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6703 | struct extent_buffer *leaf, |
| 6704 | struct btrfs_chunk *chunk) |
| 6705 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6706 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6707 | struct map_lookup *map; |
| 6708 | struct extent_map *em; |
| 6709 | u64 logical; |
| 6710 | u64 length; |
| 6711 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6712 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6713 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6714 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6715 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6716 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6717 | logical = key->offset; |
| 6718 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6719 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6720 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6721 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6722 | if (ret) |
| 6723 | return ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6724 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6725 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6726 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6727 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6728 | |
| 6729 | /* already mapped? */ |
| 6730 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6731 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6732 | return 0; |
| 6733 | } else if (em) { |
| 6734 | free_extent_map(em); |
| 6735 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6736 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6737 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6738 | if (!em) |
| 6739 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6740 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6741 | if (!map) { |
| 6742 | free_extent_map(em); |
| 6743 | return -ENOMEM; |
| 6744 | } |
| 6745 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6746 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6747 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6748 | em->start = logical; |
| 6749 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6750 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6751 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6752 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6753 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6754 | map->num_stripes = num_stripes; |
| 6755 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6756 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6757 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6758 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6759 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6760 | map->verified_stripes = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6761 | for (i = 0; i < num_stripes; i++) { |
| 6762 | map->stripes[i].physical = |
| 6763 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6764 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6765 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6766 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6767 | BTRFS_UUID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6768 | map->stripes[i].dev = btrfs_find_device(fs_info, devid, |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6769 | uuid, NULL); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6770 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6771 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6772 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6773 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6774 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6775 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6776 | if (!map->stripes[i].dev) { |
| 6777 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6778 | add_missing_dev(fs_info->fs_devices, devid, |
| 6779 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6780 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6781 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6782 | btrfs_err(fs_info, |
| 6783 | "failed to init missing dev %llu: %ld", |
| 6784 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6785 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6786 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6787 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6788 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6789 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6790 | &(map->stripes[i].dev->dev_state)); |
| 6791 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6792 | } |
| 6793 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6794 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6795 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6796 | write_unlock(&map_tree->map_tree.lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6797 | if (ret < 0) { |
| 6798 | btrfs_err(fs_info, |
| 6799 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6800 | em->start, em->len, ret); |
| 6801 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6802 | free_extent_map(em); |
| 6803 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6804 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6805 | } |
| 6806 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6807 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6808 | struct btrfs_dev_item *dev_item, |
| 6809 | struct btrfs_device *device) |
| 6810 | { |
| 6811 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6812 | |
| 6813 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6814 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6815 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6816 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6817 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6818 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6819 | device->type = btrfs_device_type(leaf, dev_item); |
| 6820 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6821 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6822 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6823 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6824 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6825 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6826 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6827 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6828 | } |
| 6829 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6830 | 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] | 6831 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6832 | { |
| 6833 | struct btrfs_fs_devices *fs_devices; |
| 6834 | int ret; |
| 6835 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6836 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6837 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6838 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6839 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6840 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6841 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6842 | return fs_devices; |
| 6843 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6844 | fs_devices = fs_devices->seed; |
| 6845 | } |
| 6846 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6847 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6848 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6849 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6850 | return ERR_PTR(-ENOENT); |
| 6851 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6852 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6853 | if (IS_ERR(fs_devices)) |
| 6854 | return fs_devices; |
| 6855 | |
| 6856 | fs_devices->seeding = 1; |
| 6857 | fs_devices->opened = 1; |
| 6858 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6859 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6860 | |
| 6861 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6862 | if (IS_ERR(fs_devices)) |
| 6863 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6864 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6865 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6866 | if (ret) { |
| 6867 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6868 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6869 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6870 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6871 | |
| 6872 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6873 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6874 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6875 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6876 | goto out; |
| 6877 | } |
| 6878 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6879 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6880 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6881 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6882 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6883 | } |
| 6884 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6885 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6886 | struct extent_buffer *leaf, |
| 6887 | struct btrfs_dev_item *dev_item) |
| 6888 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6889 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6890 | struct btrfs_device *device; |
| 6891 | u64 devid; |
| 6892 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6893 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6894 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6895 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6896 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6897 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6898 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6899 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6900 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6901 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6902 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6903 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6904 | if (IS_ERR(fs_devices)) |
| 6905 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6906 | } |
| 6907 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6908 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6909 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6910 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6911 | btrfs_report_missing_device(fs_info, devid, |
| 6912 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6913 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6914 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6915 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6916 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6917 | if (IS_ERR(device)) { |
| 6918 | btrfs_err(fs_info, |
| 6919 | "failed to add missing dev %llu: %ld", |
| 6920 | devid, PTR_ERR(device)); |
| 6921 | return PTR_ERR(device); |
| 6922 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6923 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6924 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6925 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6926 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6927 | btrfs_report_missing_device(fs_info, |
| 6928 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6929 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6930 | } |
| 6931 | btrfs_report_missing_device(fs_info, devid, |
| 6932 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6933 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6934 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6935 | if (!device->bdev && |
| 6936 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6937 | /* |
| 6938 | * this happens when a device that was properly setup |
| 6939 | * in the device info lists suddenly goes bad. |
| 6940 | * device->bdev is NULL, and so we have to set |
| 6941 | * device->missing to one here |
| 6942 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6943 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6944 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6945 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6946 | |
| 6947 | /* Move the device to its own fs_devices */ |
| 6948 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6949 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6950 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6951 | |
| 6952 | list_move(&device->dev_list, &fs_devices->devices); |
| 6953 | device->fs_devices->num_devices--; |
| 6954 | fs_devices->num_devices++; |
| 6955 | |
| 6956 | device->fs_devices->missing_devices--; |
| 6957 | fs_devices->missing_devices++; |
| 6958 | |
| 6959 | device->fs_devices = fs_devices; |
| 6960 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6961 | } |
| 6962 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6963 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6964 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6965 | if (device->generation != |
| 6966 | btrfs_device_generation(leaf, dev_item)) |
| 6967 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6968 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6969 | |
| 6970 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6971 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6972 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6973 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6974 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6975 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6976 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6977 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6978 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6979 | return ret; |
| 6980 | } |
| 6981 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6982 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6983 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6984 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6985 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6986 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6987 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6988 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6989 | u8 *array_ptr; |
| 6990 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6991 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6992 | u32 num_stripes; |
| 6993 | u32 array_size; |
| 6994 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6995 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6996 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6997 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6998 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6999 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 7000 | /* |
| 7001 | * This will create extent buffer of nodesize, superblock size is |
| 7002 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 7003 | * overallocate but we can keep it as-is, only the first page is used. |
| 7004 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7005 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 7006 | if (IS_ERR(sb)) |
| 7007 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 7008 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 7009 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 7010 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7011 | * 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] | 7012 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 7013 | * pages up-to-date when the page is larger: extent does not cover the |
| 7014 | * whole page and consequently check_page_uptodate does not find all |
| 7015 | * the page's extents up-to-date (the hole beyond sb), |
| 7016 | * write_extent_buffer then triggers a WARN_ON. |
| 7017 | * |
| 7018 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 7019 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 7020 | * to silence the warning eg. on PowerPC 64. |
| 7021 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7022 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7023 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 7024 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7025 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7026 | array_size = btrfs_super_sys_array_size(super_copy); |
| 7027 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7028 | array_ptr = super_copy->sys_chunk_array; |
| 7029 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 7030 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7031 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7032 | while (cur_offset < array_size) { |
| 7033 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7034 | len = sizeof(*disk_key); |
| 7035 | if (cur_offset + len > array_size) |
| 7036 | goto out_short_read; |
| 7037 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7038 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 7039 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7040 | array_ptr += len; |
| 7041 | sb_array_offset += len; |
| 7042 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7043 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7044 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7045 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7046 | /* |
| 7047 | * At least one btrfs_chunk with one stripe must be |
| 7048 | * present, exact stripe count check comes afterwards |
| 7049 | */ |
| 7050 | len = btrfs_chunk_item_size(1); |
| 7051 | if (cur_offset + len > array_size) |
| 7052 | goto out_short_read; |
| 7053 | |
| 7054 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7055 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7056 | btrfs_err(fs_info, |
| 7057 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7058 | num_stripes, cur_offset); |
| 7059 | ret = -EIO; |
| 7060 | break; |
| 7061 | } |
| 7062 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7063 | type = btrfs_chunk_type(sb, chunk); |
| 7064 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7065 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7066 | "invalid chunk type %llu in sys_array at offset %u", |
| 7067 | type, cur_offset); |
| 7068 | ret = -EIO; |
| 7069 | break; |
| 7070 | } |
| 7071 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7072 | len = btrfs_chunk_item_size(num_stripes); |
| 7073 | if (cur_offset + len > array_size) |
| 7074 | goto out_short_read; |
| 7075 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7076 | ret = read_one_chunk(fs_info, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7077 | if (ret) |
| 7078 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7079 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7080 | btrfs_err(fs_info, |
| 7081 | "unexpected item type %u in sys_array at offset %u", |
| 7082 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7083 | ret = -EIO; |
| 7084 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7085 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7086 | array_ptr += len; |
| 7087 | sb_array_offset += len; |
| 7088 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7089 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7090 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7091 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7092 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7093 | |
| 7094 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7095 | 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] | 7096 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7097 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7098 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7099 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7100 | } |
| 7101 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7102 | /* |
| 7103 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 7104 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7105 | * If the @failing_dev is specified, it's accounted as missing. |
| 7106 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7107 | * Return true if all chunks meet the minimal RW mount requirements. |
| 7108 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 7109 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7110 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 7111 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7112 | { |
| 7113 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 7114 | struct extent_map *em; |
| 7115 | u64 next_start = 0; |
| 7116 | bool ret = true; |
| 7117 | |
| 7118 | read_lock(&map_tree->map_tree.lock); |
| 7119 | em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1); |
| 7120 | read_unlock(&map_tree->map_tree.lock); |
| 7121 | /* No chunk at all? Return false anyway */ |
| 7122 | if (!em) { |
| 7123 | ret = false; |
| 7124 | goto out; |
| 7125 | } |
| 7126 | while (em) { |
| 7127 | struct map_lookup *map; |
| 7128 | int missing = 0; |
| 7129 | int max_tolerated; |
| 7130 | int i; |
| 7131 | |
| 7132 | map = em->map_lookup; |
| 7133 | max_tolerated = |
| 7134 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 7135 | map->type); |
| 7136 | for (i = 0; i < map->num_stripes; i++) { |
| 7137 | struct btrfs_device *dev = map->stripes[i].dev; |
| 7138 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 7139 | if (!dev || !dev->bdev || |
| 7140 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7141 | dev->last_flush_error) |
| 7142 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7143 | else if (failing_dev && failing_dev == dev) |
| 7144 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7145 | } |
| 7146 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7147 | if (!failing_dev) |
| 7148 | btrfs_warn(fs_info, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7149 | "chunk %llu missing %d devices, max tolerance is %d for writeable mount", |
| 7150 | em->start, missing, max_tolerated); |
| 7151 | free_extent_map(em); |
| 7152 | ret = false; |
| 7153 | goto out; |
| 7154 | } |
| 7155 | next_start = extent_map_end(em); |
| 7156 | free_extent_map(em); |
| 7157 | |
| 7158 | read_lock(&map_tree->map_tree.lock); |
| 7159 | em = lookup_extent_mapping(&map_tree->map_tree, next_start, |
| 7160 | (u64)(-1) - next_start); |
| 7161 | read_unlock(&map_tree->map_tree.lock); |
| 7162 | } |
| 7163 | out: |
| 7164 | return ret; |
| 7165 | } |
| 7166 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7167 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7168 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7169 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7170 | struct btrfs_path *path; |
| 7171 | struct extent_buffer *leaf; |
| 7172 | struct btrfs_key key; |
| 7173 | struct btrfs_key found_key; |
| 7174 | int ret; |
| 7175 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7176 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7177 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7178 | path = btrfs_alloc_path(); |
| 7179 | if (!path) |
| 7180 | return -ENOMEM; |
| 7181 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7182 | /* |
| 7183 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7184 | * otherwise we don't need it. |
| 7185 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7186 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7187 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7188 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7189 | /* |
| 7190 | * Read all device items, and then all the chunk items. All |
| 7191 | * device items are found before any chunk item (their object id |
| 7192 | * is smaller than the lowest possible object id for a chunk |
| 7193 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7194 | */ |
| 7195 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7196 | key.offset = 0; |
| 7197 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7198 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7199 | if (ret < 0) |
| 7200 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7201 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7202 | leaf = path->nodes[0]; |
| 7203 | slot = path->slots[0]; |
| 7204 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7205 | ret = btrfs_next_leaf(root, path); |
| 7206 | if (ret == 0) |
| 7207 | continue; |
| 7208 | if (ret < 0) |
| 7209 | goto error; |
| 7210 | break; |
| 7211 | } |
| 7212 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7213 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7214 | struct btrfs_dev_item *dev_item; |
| 7215 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7216 | struct btrfs_dev_item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7217 | ret = read_one_dev(fs_info, leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7218 | if (ret) |
| 7219 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7220 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7221 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7222 | struct btrfs_chunk *chunk; |
| 7223 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7224 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7225 | if (ret) |
| 7226 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7227 | } |
| 7228 | path->slots[0]++; |
| 7229 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7230 | |
| 7231 | /* |
| 7232 | * After loading chunk tree, we've got all device information, |
| 7233 | * do another round of validation checks. |
| 7234 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7235 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7236 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7237 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7238 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7239 | total_dev); |
| 7240 | ret = -EINVAL; |
| 7241 | goto error; |
| 7242 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7243 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7244 | fs_info->fs_devices->total_rw_bytes) { |
| 7245 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7246 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7247 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7248 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7249 | ret = -EINVAL; |
| 7250 | goto error; |
| 7251 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7252 | ret = 0; |
| 7253 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7254 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7255 | mutex_unlock(&uuid_mutex); |
| 7256 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7257 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7258 | return ret; |
| 7259 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7260 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7261 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7262 | { |
| 7263 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7264 | struct btrfs_device *device; |
| 7265 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7266 | while (fs_devices) { |
| 7267 | mutex_lock(&fs_devices->device_list_mutex); |
| 7268 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7269 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7270 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7271 | |
| 7272 | fs_devices = fs_devices->seed; |
| 7273 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7274 | } |
| 7275 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7276 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 7277 | { |
| 7278 | int i; |
| 7279 | |
| 7280 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7281 | btrfs_dev_stat_reset(dev, i); |
| 7282 | } |
| 7283 | |
| 7284 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7285 | { |
| 7286 | struct btrfs_key key; |
| 7287 | struct btrfs_key found_key; |
| 7288 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7289 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7290 | struct extent_buffer *eb; |
| 7291 | int slot; |
| 7292 | int ret = 0; |
| 7293 | struct btrfs_device *device; |
| 7294 | struct btrfs_path *path = NULL; |
| 7295 | int i; |
| 7296 | |
| 7297 | path = btrfs_alloc_path(); |
| 7298 | if (!path) { |
| 7299 | ret = -ENOMEM; |
| 7300 | goto out; |
| 7301 | } |
| 7302 | |
| 7303 | mutex_lock(&fs_devices->device_list_mutex); |
| 7304 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7305 | int item_size; |
| 7306 | struct btrfs_dev_stats_item *ptr; |
| 7307 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7308 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7309 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7310 | key.offset = device->devid; |
| 7311 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7312 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7313 | __btrfs_reset_dev_stats(device); |
| 7314 | device->dev_stats_valid = 1; |
| 7315 | btrfs_release_path(path); |
| 7316 | continue; |
| 7317 | } |
| 7318 | slot = path->slots[0]; |
| 7319 | eb = path->nodes[0]; |
| 7320 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 7321 | item_size = btrfs_item_size_nr(eb, slot); |
| 7322 | |
| 7323 | ptr = btrfs_item_ptr(eb, slot, |
| 7324 | struct btrfs_dev_stats_item); |
| 7325 | |
| 7326 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7327 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7328 | btrfs_dev_stat_set(device, i, |
| 7329 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7330 | else |
| 7331 | btrfs_dev_stat_reset(device, i); |
| 7332 | } |
| 7333 | |
| 7334 | device->dev_stats_valid = 1; |
| 7335 | btrfs_dev_stat_print_on_load(device); |
| 7336 | btrfs_release_path(path); |
| 7337 | } |
| 7338 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7339 | |
| 7340 | out: |
| 7341 | btrfs_free_path(path); |
| 7342 | return ret < 0 ? ret : 0; |
| 7343 | } |
| 7344 | |
| 7345 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7346 | struct btrfs_device *device) |
| 7347 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7348 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7349 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7350 | struct btrfs_path *path; |
| 7351 | struct btrfs_key key; |
| 7352 | struct extent_buffer *eb; |
| 7353 | struct btrfs_dev_stats_item *ptr; |
| 7354 | int ret; |
| 7355 | int i; |
| 7356 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7357 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7358 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7359 | key.offset = device->devid; |
| 7360 | |
| 7361 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7362 | if (!path) |
| 7363 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7364 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7365 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7366 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7367 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7368 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7369 | goto out; |
| 7370 | } |
| 7371 | |
| 7372 | if (ret == 0 && |
| 7373 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7374 | /* need to delete old one and insert a new one */ |
| 7375 | ret = btrfs_del_item(trans, dev_root, path); |
| 7376 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7377 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7378 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7379 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7380 | goto out; |
| 7381 | } |
| 7382 | ret = 1; |
| 7383 | } |
| 7384 | |
| 7385 | if (ret == 1) { |
| 7386 | /* need to insert a new item */ |
| 7387 | btrfs_release_path(path); |
| 7388 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7389 | &key, sizeof(*ptr)); |
| 7390 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7391 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7392 | "insert dev_stats item for device %s failed %d", |
| 7393 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7394 | goto out; |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | eb = path->nodes[0]; |
| 7399 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7400 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7401 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7402 | btrfs_dev_stat_read(device, i)); |
| 7403 | btrfs_mark_buffer_dirty(eb); |
| 7404 | |
| 7405 | out: |
| 7406 | btrfs_free_path(path); |
| 7407 | return ret; |
| 7408 | } |
| 7409 | |
| 7410 | /* |
| 7411 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7412 | */ |
| 7413 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 7414 | struct btrfs_fs_info *fs_info) |
| 7415 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7416 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7417 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7418 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7419 | int ret = 0; |
| 7420 | |
| 7421 | mutex_lock(&fs_devices->device_list_mutex); |
| 7422 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7423 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7424 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7425 | continue; |
| 7426 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7427 | |
| 7428 | /* |
| 7429 | * There is a LOAD-LOAD control dependency between the value of |
| 7430 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7431 | * reading the in-memory counters. Such control dependencies |
| 7432 | * require explicit read memory barriers. |
| 7433 | * |
| 7434 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7435 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7436 | * barrier implied by atomic_xchg in |
| 7437 | * btrfs_dev_stats_read_and_reset |
| 7438 | */ |
| 7439 | smp_rmb(); |
| 7440 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7441 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7442 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7443 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7444 | } |
| 7445 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7446 | |
| 7447 | return ret; |
| 7448 | } |
| 7449 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7450 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7451 | { |
| 7452 | btrfs_dev_stat_inc(dev, index); |
| 7453 | btrfs_dev_stat_print_on_error(dev); |
| 7454 | } |
| 7455 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7456 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7457 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7458 | if (!dev->dev_stats_valid) |
| 7459 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7460 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7461 | "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] | 7462 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7463 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7464 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7465 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7466 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7467 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7468 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7469 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7470 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7471 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7472 | int i; |
| 7473 | |
| 7474 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7475 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7476 | break; |
| 7477 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7478 | return; /* all values == 0, suppress message */ |
| 7479 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7480 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7481 | "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] | 7482 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7483 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7484 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7485 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7486 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7487 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7488 | } |
| 7489 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7490 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7491 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7492 | { |
| 7493 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7494 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7495 | int i; |
| 7496 | |
| 7497 | mutex_lock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7498 | dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7499 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7500 | |
| 7501 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7502 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7503 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7504 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7505 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7506 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7507 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7508 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7509 | if (stats->nr_items > i) |
| 7510 | stats->values[i] = |
| 7511 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7512 | else |
| 7513 | btrfs_dev_stat_reset(dev, i); |
| 7514 | } |
| 7515 | } else { |
| 7516 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7517 | if (stats->nr_items > i) |
| 7518 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7519 | } |
| 7520 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7521 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7522 | return 0; |
| 7523 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7524 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7525 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7526 | { |
| 7527 | struct buffer_head *bh; |
| 7528 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7529 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7530 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7531 | if (!bdev) |
| 7532 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7533 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7534 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7535 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7536 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7537 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7538 | continue; |
| 7539 | |
| 7540 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7541 | |
| 7542 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7543 | set_buffer_dirty(bh); |
| 7544 | sync_dirty_buffer(bh); |
| 7545 | brelse(bh); |
| 7546 | } |
| 7547 | |
| 7548 | /* Notify udev that device has changed */ |
| 7549 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7550 | |
| 7551 | /* Update ctime/mtime for device path for libblkid */ |
| 7552 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7553 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7554 | |
| 7555 | /* |
| 7556 | * Update the size of all devices, which is used for writing out the |
| 7557 | * super blocks. |
| 7558 | */ |
| 7559 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 7560 | { |
| 7561 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7562 | struct btrfs_device *curr, *next; |
| 7563 | |
| 7564 | if (list_empty(&fs_devices->resized_devices)) |
| 7565 | return; |
| 7566 | |
| 7567 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7568 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7569 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 7570 | resized_list) { |
| 7571 | list_del_init(&curr->resized_list); |
| 7572 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 7573 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7574 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7575 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7576 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7577 | |
| 7578 | /* Must be invoked during the transaction commit */ |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7579 | void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7580 | { |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7581 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7582 | struct extent_map *em; |
| 7583 | struct map_lookup *map; |
| 7584 | struct btrfs_device *dev; |
| 7585 | int i; |
| 7586 | |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7587 | if (list_empty(&trans->pending_chunks)) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7588 | return; |
| 7589 | |
| 7590 | /* In order to kick the device replace finish process */ |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7591 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7592 | list_for_each_entry(em, &trans->pending_chunks, list) { |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 7593 | map = em->map_lookup; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7594 | |
| 7595 | for (i = 0; i < map->num_stripes; i++) { |
| 7596 | dev = map->stripes[i].dev; |
| 7597 | dev->commit_bytes_used = dev->bytes_used; |
| 7598 | } |
| 7599 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7600 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7601 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7602 | |
| 7603 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7604 | { |
| 7605 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7606 | while (fs_devices) { |
| 7607 | fs_devices->fs_info = fs_info; |
| 7608 | fs_devices = fs_devices->seed; |
| 7609 | } |
| 7610 | } |
| 7611 | |
| 7612 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7613 | { |
| 7614 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7615 | while (fs_devices) { |
| 7616 | fs_devices->fs_info = NULL; |
| 7617 | fs_devices = fs_devices->seed; |
| 7618 | } |
| 7619 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7620 | |
| 7621 | /* |
| 7622 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7623 | */ |
| 7624 | int btrfs_bg_type_to_factor(u64 flags) |
| 7625 | { |
| 7626 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 7627 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7628 | return 2; |
| 7629 | return 1; |
| 7630 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7631 | |
| 7632 | |
| 7633 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 7634 | { |
| 7635 | int index = btrfs_bg_flags_to_raid_index(type); |
| 7636 | int ncopies = btrfs_raid_array[index].ncopies; |
| 7637 | int data_stripes; |
| 7638 | |
| 7639 | switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 7640 | case BTRFS_BLOCK_GROUP_RAID5: |
| 7641 | data_stripes = num_stripes - 1; |
| 7642 | break; |
| 7643 | case BTRFS_BLOCK_GROUP_RAID6: |
| 7644 | data_stripes = num_stripes - 2; |
| 7645 | break; |
| 7646 | default: |
| 7647 | data_stripes = num_stripes / ncopies; |
| 7648 | break; |
| 7649 | } |
| 7650 | return div_u64(chunk_len, data_stripes); |
| 7651 | } |
| 7652 | |
| 7653 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7654 | u64 chunk_offset, u64 devid, |
| 7655 | u64 physical_offset, u64 physical_len) |
| 7656 | { |
| 7657 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 7658 | struct extent_map *em; |
| 7659 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7660 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7661 | u64 stripe_len; |
| 7662 | bool found = false; |
| 7663 | int ret = 0; |
| 7664 | int i; |
| 7665 | |
| 7666 | read_lock(&em_tree->lock); |
| 7667 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7668 | read_unlock(&em_tree->lock); |
| 7669 | |
| 7670 | if (!em) { |
| 7671 | btrfs_err(fs_info, |
| 7672 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7673 | physical_offset, devid); |
| 7674 | ret = -EUCLEAN; |
| 7675 | goto out; |
| 7676 | } |
| 7677 | |
| 7678 | map = em->map_lookup; |
| 7679 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7680 | if (physical_len != stripe_len) { |
| 7681 | btrfs_err(fs_info, |
| 7682 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7683 | physical_offset, devid, em->start, physical_len, |
| 7684 | stripe_len); |
| 7685 | ret = -EUCLEAN; |
| 7686 | goto out; |
| 7687 | } |
| 7688 | |
| 7689 | for (i = 0; i < map->num_stripes; i++) { |
| 7690 | if (map->stripes[i].dev->devid == devid && |
| 7691 | map->stripes[i].physical == physical_offset) { |
| 7692 | found = true; |
| 7693 | if (map->verified_stripes >= map->num_stripes) { |
| 7694 | btrfs_err(fs_info, |
| 7695 | "too many dev extents for chunk %llu found", |
| 7696 | em->start); |
| 7697 | ret = -EUCLEAN; |
| 7698 | goto out; |
| 7699 | } |
| 7700 | map->verified_stripes++; |
| 7701 | break; |
| 7702 | } |
| 7703 | } |
| 7704 | if (!found) { |
| 7705 | btrfs_err(fs_info, |
| 7706 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7707 | physical_offset, devid); |
| 7708 | ret = -EUCLEAN; |
| 7709 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7710 | |
| 7711 | /* Make sure no dev extent is beyond device bondary */ |
| 7712 | dev = btrfs_find_device(fs_info, devid, NULL, NULL); |
| 7713 | if (!dev) { |
| 7714 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7715 | ret = -EUCLEAN; |
| 7716 | goto out; |
| 7717 | } |
| 7718 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7719 | btrfs_err(fs_info, |
| 7720 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7721 | devid, physical_offset, physical_len, |
| 7722 | dev->disk_total_bytes); |
| 7723 | ret = -EUCLEAN; |
| 7724 | goto out; |
| 7725 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7726 | out: |
| 7727 | free_extent_map(em); |
| 7728 | return ret; |
| 7729 | } |
| 7730 | |
| 7731 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7732 | { |
| 7733 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 7734 | struct extent_map *em; |
| 7735 | struct rb_node *node; |
| 7736 | int ret = 0; |
| 7737 | |
| 7738 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7739 | 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] | 7740 | em = rb_entry(node, struct extent_map, rb_node); |
| 7741 | if (em->map_lookup->num_stripes != |
| 7742 | em->map_lookup->verified_stripes) { |
| 7743 | btrfs_err(fs_info, |
| 7744 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7745 | em->start, em->map_lookup->verified_stripes, |
| 7746 | em->map_lookup->num_stripes); |
| 7747 | ret = -EUCLEAN; |
| 7748 | goto out; |
| 7749 | } |
| 7750 | } |
| 7751 | out: |
| 7752 | read_unlock(&em_tree->lock); |
| 7753 | return ret; |
| 7754 | } |
| 7755 | |
| 7756 | /* |
| 7757 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7758 | * later chunk allocation/free would cause unexpected behavior. |
| 7759 | * |
| 7760 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7761 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7762 | */ |
| 7763 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7764 | { |
| 7765 | struct btrfs_path *path; |
| 7766 | struct btrfs_root *root = fs_info->dev_root; |
| 7767 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7768 | u64 prev_devid = 0; |
| 7769 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7770 | int ret = 0; |
| 7771 | |
| 7772 | key.objectid = 1; |
| 7773 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7774 | key.offset = 0; |
| 7775 | |
| 7776 | path = btrfs_alloc_path(); |
| 7777 | if (!path) |
| 7778 | return -ENOMEM; |
| 7779 | |
| 7780 | path->reada = READA_FORWARD; |
| 7781 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7782 | if (ret < 0) |
| 7783 | goto out; |
| 7784 | |
| 7785 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7786 | ret = btrfs_next_item(root, path); |
| 7787 | if (ret < 0) |
| 7788 | goto out; |
| 7789 | /* No dev extents at all? Not good */ |
| 7790 | if (ret > 0) { |
| 7791 | ret = -EUCLEAN; |
| 7792 | goto out; |
| 7793 | } |
| 7794 | } |
| 7795 | while (1) { |
| 7796 | struct extent_buffer *leaf = path->nodes[0]; |
| 7797 | struct btrfs_dev_extent *dext; |
| 7798 | int slot = path->slots[0]; |
| 7799 | u64 chunk_offset; |
| 7800 | u64 physical_offset; |
| 7801 | u64 physical_len; |
| 7802 | u64 devid; |
| 7803 | |
| 7804 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7805 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7806 | break; |
| 7807 | devid = key.objectid; |
| 7808 | physical_offset = key.offset; |
| 7809 | |
| 7810 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7811 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7812 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7813 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7814 | /* Check if this dev extent overlaps with the previous one */ |
| 7815 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7816 | btrfs_err(fs_info, |
| 7817 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7818 | devid, physical_offset, prev_dev_ext_end); |
| 7819 | ret = -EUCLEAN; |
| 7820 | goto out; |
| 7821 | } |
| 7822 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7823 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7824 | physical_offset, physical_len); |
| 7825 | if (ret < 0) |
| 7826 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7827 | prev_devid = devid; |
| 7828 | prev_dev_ext_end = physical_offset + physical_len; |
| 7829 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7830 | ret = btrfs_next_item(root, path); |
| 7831 | if (ret < 0) |
| 7832 | goto out; |
| 7833 | if (ret > 0) { |
| 7834 | ret = 0; |
| 7835 | break; |
| 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | /* Ensure all chunks have corresponding dev extents */ |
| 7840 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7841 | out: |
| 7842 | btrfs_free_path(path); |
| 7843 | return ret; |
| 7844 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7845 | |
| 7846 | /* |
| 7847 | * Check whether the given block group or device is pinned by any inode being |
| 7848 | * used as a swapfile. |
| 7849 | */ |
| 7850 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7851 | { |
| 7852 | struct btrfs_swapfile_pin *sp; |
| 7853 | struct rb_node *node; |
| 7854 | |
| 7855 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7856 | node = fs_info->swapfile_pins.rb_node; |
| 7857 | while (node) { |
| 7858 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7859 | if (ptr < sp->ptr) |
| 7860 | node = node->rb_left; |
| 7861 | else if (ptr > sp->ptr) |
| 7862 | node = node->rb_right; |
| 7863 | else |
| 7864 | break; |
| 7865 | } |
| 7866 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7867 | return node != NULL; |
| 7868 | } |