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> |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 17 | #include "misc.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "extent_map.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "transaction.h" |
| 22 | #include "print-tree.h" |
| 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 25 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 26 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 27 | #include "rcu-string.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 28 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 29 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 30 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 32 | #include "block-group.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 34 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 35 | [BTRFS_RAID_RAID10] = { |
| 36 | .sub_stripes = 2, |
| 37 | .dev_stripes = 1, |
| 38 | .devs_max = 0, /* 0 == as many as possible */ |
| 39 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 40 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 41 | .devs_increment = 2, |
| 42 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 43 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 44 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 45 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 46 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 47 | }, |
| 48 | [BTRFS_RAID_RAID1] = { |
| 49 | .sub_stripes = 1, |
| 50 | .dev_stripes = 1, |
| 51 | .devs_max = 2, |
| 52 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 53 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 54 | .devs_increment = 2, |
| 55 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 56 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 57 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 58 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 59 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 60 | }, |
| 61 | [BTRFS_RAID_DUP] = { |
| 62 | .sub_stripes = 1, |
| 63 | .dev_stripes = 2, |
| 64 | .devs_max = 1, |
| 65 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 66 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 67 | .devs_increment = 1, |
| 68 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 69 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 70 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 71 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 72 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 73 | }, |
| 74 | [BTRFS_RAID_RAID0] = { |
| 75 | .sub_stripes = 1, |
| 76 | .dev_stripes = 1, |
| 77 | .devs_max = 0, |
| 78 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 79 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 80 | .devs_increment = 1, |
| 81 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 82 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 83 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 84 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 85 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 86 | }, |
| 87 | [BTRFS_RAID_SINGLE] = { |
| 88 | .sub_stripes = 1, |
| 89 | .dev_stripes = 1, |
| 90 | .devs_max = 1, |
| 91 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 92 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 93 | .devs_increment = 1, |
| 94 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 95 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 96 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 97 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 98 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 99 | }, |
| 100 | [BTRFS_RAID_RAID5] = { |
| 101 | .sub_stripes = 1, |
| 102 | .dev_stripes = 1, |
| 103 | .devs_max = 0, |
| 104 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 105 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 106 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 107 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 108 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 109 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 110 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 111 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 112 | }, |
| 113 | [BTRFS_RAID_RAID6] = { |
| 114 | .sub_stripes = 1, |
| 115 | .dev_stripes = 1, |
| 116 | .devs_max = 0, |
| 117 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 118 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 119 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 120 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 121 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 122 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 123 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 124 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 125 | }, |
| 126 | }; |
| 127 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 128 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 129 | { |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 130 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 131 | |
| 132 | if (index >= BTRFS_NR_RAID_TYPES) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 133 | return NULL; |
| 134 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 135 | return btrfs_raid_array[index].raid_name; |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 138 | /* |
| 139 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 140 | * bytes including terminating null byte. |
| 141 | */ |
| 142 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 143 | { |
| 144 | int i; |
| 145 | int ret; |
| 146 | char *bp = buf; |
| 147 | u64 flags = bg_flags; |
| 148 | u32 size_bp = size_buf; |
| 149 | |
| 150 | if (!flags) { |
| 151 | strcpy(bp, "NONE"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | #define DESCRIBE_FLAG(flag, desc) \ |
| 156 | do { \ |
| 157 | if (flags & (flag)) { \ |
| 158 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 159 | if (ret < 0 || ret >= size_bp) \ |
| 160 | goto out_overflow; \ |
| 161 | size_bp -= ret; \ |
| 162 | bp += ret; \ |
| 163 | flags &= ~(flag); \ |
| 164 | } \ |
| 165 | } while (0) |
| 166 | |
| 167 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 168 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 169 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 170 | |
| 171 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 172 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 173 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 174 | btrfs_raid_array[i].raid_name); |
| 175 | #undef DESCRIBE_FLAG |
| 176 | |
| 177 | if (flags) { |
| 178 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 179 | size_bp -= ret; |
| 180 | } |
| 181 | |
| 182 | if (size_bp < size_buf) |
| 183 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 184 | |
| 185 | /* |
| 186 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 187 | * large buffer |
| 188 | */ |
| 189 | out_overflow:; |
| 190 | } |
| 191 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 192 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 193 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 194 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 195 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 196 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 197 | enum btrfs_map_op op, |
| 198 | u64 logical, u64 *length, |
| 199 | struct btrfs_bio **bbio_ret, |
| 200 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 201 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Device locking |
| 204 | * ============== |
| 205 | * |
| 206 | * There are several mutexes that protect manipulation of devices and low-level |
| 207 | * structures like chunks but not block groups, extents or files |
| 208 | * |
| 209 | * uuid_mutex (global lock) |
| 210 | * ------------------------ |
| 211 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 212 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 213 | * device) or requested by the device= mount option |
| 214 | * |
| 215 | * the mutex can be very coarse and can cover long-running operations |
| 216 | * |
| 217 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 218 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 219 | * |
| 220 | * global::fs_devs - add, remove, updates to the global list |
| 221 | * |
| 222 | * does not protect: manipulation of the fs_devices::devices list! |
| 223 | * |
| 224 | * btrfs_device::name - renames (write side), read is RCU |
| 225 | * |
| 226 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 227 | * ------------------------------------------------ |
| 228 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 229 | * |
| 230 | * simple list traversal with read-only actions can be done with RCU protection |
| 231 | * |
| 232 | * may be used to exclude some operations from running concurrently without any |
| 233 | * modifications to the list (see write_all_supers) |
| 234 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 235 | * balance_mutex |
| 236 | * ------------- |
| 237 | * protects balance structures (status, state) and context accessed from |
| 238 | * several places (internally, ioctl) |
| 239 | * |
| 240 | * chunk_mutex |
| 241 | * ----------- |
| 242 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 243 | * device is added/removed. Additionally it also protects post_commit_list of |
| 244 | * individual devices, since they can be added to the transaction's |
| 245 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 246 | * |
| 247 | * cleaner_mutex |
| 248 | * ------------- |
| 249 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 250 | * cleaning together with relocation or delayed iputs |
| 251 | * |
| 252 | * |
| 253 | * Lock nesting |
| 254 | * ============ |
| 255 | * |
| 256 | * uuid_mutex |
| 257 | * volume_mutex |
| 258 | * device_list_mutex |
| 259 | * chunk_mutex |
| 260 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 261 | * |
| 262 | * |
| 263 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 264 | * ====================================== |
| 265 | * |
| 266 | * Maintains the exclusivity of the following operations that apply to the |
| 267 | * whole filesystem and cannot run in parallel. |
| 268 | * |
| 269 | * - Balance (*) |
| 270 | * - Device add |
| 271 | * - Device remove |
| 272 | * - Device replace (*) |
| 273 | * - Resize |
| 274 | * |
| 275 | * The device operations (as above) can be in one of the following states: |
| 276 | * |
| 277 | * - Running state |
| 278 | * - Paused state |
| 279 | * - Completed state |
| 280 | * |
| 281 | * Only device operations marked with (*) can go into the Paused state for the |
| 282 | * following reasons: |
| 283 | * |
| 284 | * - ioctl (only Balance can be Paused through ioctl) |
| 285 | * - filesystem remounted as read-only |
| 286 | * - filesystem unmounted and mounted as read-only |
| 287 | * - system power-cycle and filesystem mounted as read-only |
| 288 | * - filesystem or device errors leading to forced read-only |
| 289 | * |
| 290 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 291 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 292 | * A device operation in Paused or Running state can be canceled or resumed |
| 293 | * either by ioctl (Balance only) or when remounted as read-write. |
| 294 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 295 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 296 | */ |
| 297 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 298 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 299 | static LIST_HEAD(fs_uuids); |
David Sterba | 4143cb8 | 2019-10-01 19:57:37 +0200 | [diff] [blame] | 300 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 301 | { |
| 302 | return &fs_uuids; |
| 303 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 304 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 305 | /* |
| 306 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 307 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 308 | * @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] | 309 | * |
| 310 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 311 | * The returned struct is not linked onto any lists and can be destroyed with |
| 312 | * kfree() right away. |
| 313 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 314 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 315 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 316 | { |
| 317 | struct btrfs_fs_devices *fs_devs; |
| 318 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 319 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 320 | if (!fs_devs) |
| 321 | return ERR_PTR(-ENOMEM); |
| 322 | |
| 323 | mutex_init(&fs_devs->device_list_mutex); |
| 324 | |
| 325 | INIT_LIST_HEAD(&fs_devs->devices); |
| 326 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 327 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 328 | if (fsid) |
| 329 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 330 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 331 | if (metadata_fsid) |
| 332 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 333 | else if (fsid) |
| 334 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 335 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 336 | return fs_devs; |
| 337 | } |
| 338 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 339 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 340 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 341 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 342 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 343 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 344 | bio_put(device->flush_bio); |
| 345 | kfree(device); |
| 346 | } |
| 347 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 348 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 349 | { |
| 350 | struct btrfs_device *device; |
| 351 | WARN_ON(fs_devices->opened); |
| 352 | while (!list_empty(&fs_devices->devices)) { |
| 353 | device = list_entry(fs_devices->devices.next, |
| 354 | struct btrfs_device, dev_list); |
| 355 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 356 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 357 | } |
| 358 | kfree(fs_devices); |
| 359 | } |
| 360 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 361 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 362 | { |
| 363 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 364 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 365 | while (!list_empty(&fs_uuids)) { |
| 366 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 367 | struct btrfs_fs_devices, fs_list); |
| 368 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 369 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 370 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 371 | } |
| 372 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 373 | /* |
| 374 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 375 | * 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] | 376 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 377 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 378 | static struct btrfs_device *__alloc_device(void) |
| 379 | { |
| 380 | struct btrfs_device *dev; |
| 381 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 382 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 383 | if (!dev) |
| 384 | return ERR_PTR(-ENOMEM); |
| 385 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 386 | /* |
| 387 | * Preallocate a bio that's always going to be used for flushing device |
| 388 | * barriers and matches the device lifespan |
| 389 | */ |
| 390 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 391 | if (!dev->flush_bio) { |
| 392 | kfree(dev); |
| 393 | return ERR_PTR(-ENOMEM); |
| 394 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 395 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 396 | INIT_LIST_HEAD(&dev->dev_list); |
| 397 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 398 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 399 | |
| 400 | spin_lock_init(&dev->io_lock); |
| 401 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 402 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 403 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 404 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 405 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 406 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 407 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 408 | |
| 409 | return dev; |
| 410 | } |
| 411 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 412 | static noinline struct btrfs_fs_devices *find_fsid( |
| 413 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 414 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 415 | struct btrfs_fs_devices *fs_devices; |
| 416 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 417 | ASSERT(fsid); |
| 418 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 419 | if (metadata_fsid) { |
| 420 | /* |
| 421 | * Handle scanned device having completed its fsid change but |
| 422 | * belonging to a fs_devices that was created by first scanning |
| 423 | * a device which didn't have its fsid/metadata_uuid changed |
| 424 | * at all and the CHANGING_FSID_V2 flag set. |
| 425 | */ |
| 426 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 427 | if (fs_devices->fsid_change && |
| 428 | memcmp(metadata_fsid, fs_devices->fsid, |
| 429 | BTRFS_FSID_SIZE) == 0 && |
| 430 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 431 | BTRFS_FSID_SIZE) == 0) { |
| 432 | return fs_devices; |
| 433 | } |
| 434 | } |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 435 | /* |
| 436 | * Handle scanned device having completed its fsid change but |
| 437 | * belonging to a fs_devices that was created by a device that |
| 438 | * has an outdated pair of fsid/metadata_uuid and |
| 439 | * CHANGING_FSID_V2 flag set. |
| 440 | */ |
| 441 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 442 | if (fs_devices->fsid_change && |
| 443 | memcmp(fs_devices->metadata_uuid, |
| 444 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 445 | memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 446 | BTRFS_FSID_SIZE) == 0) { |
| 447 | return fs_devices; |
| 448 | } |
| 449 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 453 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 454 | if (metadata_fsid) { |
| 455 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 456 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 457 | BTRFS_FSID_SIZE) == 0) |
| 458 | return fs_devices; |
| 459 | } else { |
| 460 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 461 | return fs_devices; |
| 462 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 463 | } |
| 464 | return NULL; |
| 465 | } |
| 466 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 467 | static int |
| 468 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 469 | int flush, struct block_device **bdev, |
| 470 | struct buffer_head **bh) |
| 471 | { |
| 472 | int ret; |
| 473 | |
| 474 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 475 | |
| 476 | if (IS_ERR(*bdev)) { |
| 477 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 478 | goto error; |
| 479 | } |
| 480 | |
| 481 | if (flush) |
| 482 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 483 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 484 | if (ret) { |
| 485 | blkdev_put(*bdev, flags); |
| 486 | goto error; |
| 487 | } |
| 488 | invalidate_bdev(*bdev); |
| 489 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 490 | if (IS_ERR(*bh)) { |
| 491 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 492 | blkdev_put(*bdev, flags); |
| 493 | goto error; |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | |
| 498 | error: |
| 499 | *bdev = NULL; |
| 500 | *bh = NULL; |
| 501 | return ret; |
| 502 | } |
| 503 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 504 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 505 | struct bio *head, struct bio *tail) |
| 506 | { |
| 507 | |
| 508 | struct bio *old_head; |
| 509 | |
| 510 | old_head = pending_bios->head; |
| 511 | pending_bios->head = head; |
| 512 | if (pending_bios->tail) |
| 513 | tail->bi_next = old_head; |
| 514 | else |
| 515 | pending_bios->tail = tail; |
| 516 | } |
| 517 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 518 | /* |
| 519 | * we try to collect pending bios for a device so we don't get a large |
| 520 | * number of procs sending bios down to the same device. This greatly |
| 521 | * improves the schedulers ability to collect and merge the bios. |
| 522 | * |
| 523 | * But, it also turns into a long list of bios to process and that is sure |
| 524 | * to eventually make the worker thread block. The solution here is to |
| 525 | * make some progress and then put this work struct back at the end of |
| 526 | * the list if the block device is congested. This way, multiple devices |
| 527 | * can make progress from a single worker thread. |
| 528 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 529 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 530 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 531 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 532 | struct bio *pending; |
| 533 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 534 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 535 | struct bio *tail; |
| 536 | struct bio *cur; |
| 537 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 538 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 539 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 540 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 541 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 542 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 543 | struct blk_plug plug; |
| 544 | |
| 545 | /* |
| 546 | * this function runs all the bios we've collected for |
| 547 | * a particular device. We don't want to wander off to |
| 548 | * another device without first sending all of these down. |
| 549 | * So, setup a plug here and finish it off before we return |
| 550 | */ |
| 551 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 552 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 553 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 554 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 555 | loop: |
| 556 | spin_lock(&device->io_lock); |
| 557 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 558 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 559 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 560 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 561 | /* take all the bios off the list at once and process them |
| 562 | * later on (without the lock held). But, remember the |
| 563 | * tail and other pointers so the bios can be properly reinserted |
| 564 | * into the list if we hit congestion |
| 565 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 566 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 567 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 568 | force_reg = 1; |
| 569 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 570 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 571 | force_reg = 0; |
| 572 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 573 | |
| 574 | pending = pending_bios->head; |
| 575 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 576 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * if pending was null this time around, no bios need processing |
| 580 | * at all and we can stop. Otherwise it'll loop back up again |
| 581 | * and do an additional check so no bios are missed. |
| 582 | * |
| 583 | * device->running_pending is used to synchronize with the |
| 584 | * schedule_bio code. |
| 585 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 586 | if (device->pending_sync_bios.head == NULL && |
| 587 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 588 | again = 0; |
| 589 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 590 | } else { |
| 591 | again = 1; |
| 592 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 593 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 594 | |
| 595 | pending_bios->head = NULL; |
| 596 | pending_bios->tail = NULL; |
| 597 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 598 | spin_unlock(&device->io_lock); |
| 599 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 600 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 601 | |
| 602 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 603 | /* we want to work on both lists, but do more bios on the |
| 604 | * sync list than the regular list |
| 605 | */ |
| 606 | if ((num_run > 32 && |
| 607 | pending_bios != &device->pending_sync_bios && |
| 608 | device->pending_sync_bios.head) || |
| 609 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 610 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 611 | spin_lock(&device->io_lock); |
| 612 | requeue_list(pending_bios, pending, tail); |
| 613 | goto loop_lock; |
| 614 | } |
| 615 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 616 | cur = pending; |
| 617 | pending = pending->bi_next; |
| 618 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 619 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 620 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 621 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 622 | /* |
| 623 | * if we're doing the sync list, record that our |
| 624 | * plug has some sync requests on it |
| 625 | * |
| 626 | * If we're doing the regular list and there are |
| 627 | * sync requests sitting around, unplug before |
| 628 | * we add more |
| 629 | */ |
| 630 | if (pending_bios == &device->pending_sync_bios) { |
| 631 | sync_pending = 1; |
| 632 | } else if (sync_pending) { |
| 633 | blk_finish_plug(&plug); |
| 634 | blk_start_plug(&plug); |
| 635 | sync_pending = 0; |
| 636 | } |
| 637 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 638 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 639 | num_run++; |
| 640 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 641 | |
| 642 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * we made progress, there is more work to do and the bdi |
| 646 | * is now congested. Back off and let other work structs |
| 647 | * run instead |
| 648 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 649 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 650 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 651 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 652 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 653 | ioc = current->io_context; |
| 654 | |
| 655 | /* |
| 656 | * the main goal here is that we don't want to |
| 657 | * block if we're going to be able to submit |
| 658 | * more requests without blocking. |
| 659 | * |
| 660 | * This code does two great things, it pokes into |
| 661 | * the elevator code from a filesystem _and_ |
| 662 | * it makes assumptions about how batching works. |
| 663 | */ |
| 664 | if (ioc && ioc->nr_batch_requests > 0 && |
| 665 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 666 | (last_waited == 0 || |
| 667 | ioc->last_waited == last_waited)) { |
| 668 | /* |
| 669 | * we want to go through our batch of |
| 670 | * requests and stop. So, we copy out |
| 671 | * the ioc->last_waited time and test |
| 672 | * against it before looping |
| 673 | */ |
| 674 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 675 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 676 | continue; |
| 677 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 678 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 679 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 680 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 681 | |
| 682 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 683 | btrfs_queue_work(fs_info->submit_workers, |
| 684 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 685 | goto done; |
| 686 | } |
| 687 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 688 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 689 | cond_resched(); |
| 690 | if (again) |
| 691 | goto loop; |
| 692 | |
| 693 | spin_lock(&device->io_lock); |
| 694 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 695 | goto loop_lock; |
| 696 | spin_unlock(&device->io_lock); |
| 697 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 698 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 699 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 700 | } |
| 701 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 702 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 703 | { |
| 704 | struct btrfs_device *device; |
| 705 | |
| 706 | device = container_of(work, struct btrfs_device, work); |
| 707 | run_scheduled_bios(device); |
| 708 | } |
| 709 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 710 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 711 | { |
| 712 | int found; |
| 713 | |
| 714 | rcu_read_lock(); |
| 715 | found = strcmp(rcu_str_deref(device->name), path); |
| 716 | rcu_read_unlock(); |
| 717 | |
| 718 | return found == 0; |
| 719 | } |
| 720 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 721 | /* |
| 722 | * Search and remove all stale (devices which are not mounted) devices. |
| 723 | * When both inputs are NULL, it will search and release all stale devices. |
| 724 | * path: Optional. When provided will it release all unmounted devices |
| 725 | * matching this path only. |
| 726 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 727 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 728 | * Return: 0 for success or if @path is NULL. |
| 729 | * -EBUSY if @path is a mounted device. |
| 730 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 731 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 732 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 733 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 734 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 735 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 736 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 737 | int ret = 0; |
| 738 | |
| 739 | if (path) |
| 740 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 741 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 742 | list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 743 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 744 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 745 | list_for_each_entry_safe(device, tmp_device, |
| 746 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 747 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 748 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 749 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 750 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 751 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 752 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 753 | if (fs_devices->opened) { |
| 754 | /* for an already deleted device return 0 */ |
| 755 | if (path && ret != 0) |
| 756 | ret = -EBUSY; |
| 757 | break; |
| 758 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 759 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 760 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 761 | fs_devices->num_devices--; |
| 762 | list_del(&device->dev_list); |
| 763 | btrfs_free_device(device); |
| 764 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 765 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 766 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 767 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 768 | } |
| 769 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 770 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 771 | if (fs_devices->num_devices == 0) { |
| 772 | btrfs_sysfs_remove_fsid(fs_devices); |
| 773 | list_del(&fs_devices->fs_list); |
| 774 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 775 | } |
| 776 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 777 | |
| 778 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 779 | } |
| 780 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 781 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 782 | struct btrfs_device *device, fmode_t flags, |
| 783 | void *holder) |
| 784 | { |
| 785 | struct request_queue *q; |
| 786 | struct block_device *bdev; |
| 787 | struct buffer_head *bh; |
| 788 | struct btrfs_super_block *disk_super; |
| 789 | u64 devid; |
| 790 | int ret; |
| 791 | |
| 792 | if (device->bdev) |
| 793 | return -EINVAL; |
| 794 | if (!device->name) |
| 795 | return -EINVAL; |
| 796 | |
| 797 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 798 | &bdev, &bh); |
| 799 | if (ret) |
| 800 | return ret; |
| 801 | |
| 802 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 803 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 804 | if (devid != device->devid) |
| 805 | goto error_brelse; |
| 806 | |
| 807 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 808 | goto error_brelse; |
| 809 | |
| 810 | device->generation = btrfs_super_generation(disk_super); |
| 811 | |
| 812 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 813 | if (btrfs_super_incompat_flags(disk_super) & |
| 814 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 815 | pr_err( |
| 816 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 817 | goto error_brelse; |
| 818 | } |
| 819 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 820 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 821 | fs_devices->seeding = 1; |
| 822 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 823 | if (bdev_read_only(bdev)) |
| 824 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 825 | else |
| 826 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 830 | if (!blk_queue_nonrot(q)) |
| 831 | fs_devices->rotating = 1; |
| 832 | |
| 833 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 834 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 835 | device->mode = flags; |
| 836 | |
| 837 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 838 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 839 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 840 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 841 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 842 | } |
| 843 | brelse(bh); |
| 844 | |
| 845 | return 0; |
| 846 | |
| 847 | error_brelse: |
| 848 | brelse(bh); |
| 849 | blkdev_put(bdev, flags); |
| 850 | |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 854 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 855 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| 856 | * being created with a disk that has already completed its fsid change. |
| 857 | */ |
| 858 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 859 | struct btrfs_super_block *disk_super) |
| 860 | { |
| 861 | struct btrfs_fs_devices *fs_devices; |
| 862 | |
| 863 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 864 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 865 | BTRFS_FSID_SIZE) != 0 && |
| 866 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 867 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 868 | return fs_devices; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | return NULL; |
| 873 | } |
| 874 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 875 | |
| 876 | static struct btrfs_fs_devices *find_fsid_changed( |
| 877 | struct btrfs_super_block *disk_super) |
| 878 | { |
| 879 | struct btrfs_fs_devices *fs_devices; |
| 880 | |
| 881 | /* |
| 882 | * Handles the case where scanned device is part of an fs that had |
| 883 | * multiple successful changes of FSID but curently device didn't |
| 884 | * observe it. Meaning our fsid will be different than theirs. |
| 885 | */ |
| 886 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 887 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 888 | BTRFS_FSID_SIZE) != 0 && |
| 889 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 890 | BTRFS_FSID_SIZE) == 0 && |
| 891 | memcmp(fs_devices->fsid, disk_super->fsid, |
| 892 | BTRFS_FSID_SIZE) != 0) { |
| 893 | return fs_devices; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | return NULL; |
| 898 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 899 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 900 | * Add new device to list of registered devices |
| 901 | * |
| 902 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 903 | * device pointer which was just added or updated when successful |
| 904 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 905 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 906 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 907 | struct btrfs_super_block *disk_super, |
| 908 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 909 | { |
| 910 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 911 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 912 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 913 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 914 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 915 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 916 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 917 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 918 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 919 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 920 | if (fsid_change_in_progress) { |
| 921 | if (!has_metadata_uuid) { |
| 922 | /* |
| 923 | * When we have an image which has CHANGING_FSID_V2 set |
| 924 | * it might belong to either a filesystem which has |
| 925 | * disks with completed fsid change or it might belong |
| 926 | * to fs with no UUID changes in effect, handle both. |
| 927 | */ |
| 928 | fs_devices = find_fsid_inprogress(disk_super); |
| 929 | if (!fs_devices) |
| 930 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 931 | } else { |
| 932 | fs_devices = find_fsid_changed(disk_super); |
| 933 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 934 | } else if (has_metadata_uuid) { |
| 935 | fs_devices = find_fsid(disk_super->fsid, |
| 936 | disk_super->metadata_uuid); |
| 937 | } else { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 938 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 941 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 942 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 943 | if (has_metadata_uuid) |
| 944 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 945 | disk_super->metadata_uuid); |
| 946 | else |
| 947 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 948 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 949 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 950 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 951 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 952 | fs_devices->fsid_change = fsid_change_in_progress; |
| 953 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 954 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 955 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 956 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 957 | device = NULL; |
| 958 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 959 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 960 | device = btrfs_find_device(fs_devices, devid, |
| 961 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 962 | |
| 963 | /* |
| 964 | * If this disk has been pulled into an fs devices created by |
| 965 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 966 | * metadata_uuid/fsid values of the fs_devices. |
| 967 | */ |
| 968 | if (has_metadata_uuid && fs_devices->fsid_change && |
| 969 | found_transid > fs_devices->latest_generation) { |
| 970 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 971 | BTRFS_FSID_SIZE); |
| 972 | memcpy(fs_devices->metadata_uuid, |
| 973 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 974 | |
| 975 | fs_devices->fsid_change = false; |
| 976 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 977 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 978 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 979 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 980 | if (fs_devices->opened) { |
| 981 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 982 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 983 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 984 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 985 | device = btrfs_alloc_device(NULL, &devid, |
| 986 | disk_super->dev_item.uuid); |
| 987 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 988 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 989 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 990 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 991 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 992 | |
| 993 | name = rcu_string_strdup(path, GFP_NOFS); |
| 994 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 995 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 996 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 997 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 998 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 999 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 1000 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1001 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1002 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1003 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1004 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1005 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1006 | |
| 1007 | if (disk_super->label[0]) |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 1008 | pr_info( |
| 1009 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| 1010 | disk_super->label, devid, found_transid, path, |
| 1011 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1012 | else |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 1013 | pr_info( |
| 1014 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| 1015 | disk_super->fsid, devid, found_transid, path, |
| 1016 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 1017 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1018 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1019 | /* |
| 1020 | * When FS is already mounted. |
| 1021 | * 1. If you are here and if the device->name is NULL that |
| 1022 | * means this device was missing at time of FS mount. |
| 1023 | * 2. If you are here and if the device->name is different |
| 1024 | * from 'path' that means either |
| 1025 | * a. The same device disappeared and reappeared with |
| 1026 | * different name. or |
| 1027 | * b. The missing-disk-which-was-replaced, has |
| 1028 | * reappeared now. |
| 1029 | * |
| 1030 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 1031 | * and unintentional. |
| 1032 | * |
| 1033 | * Further in case of 1 and 2a above, the disk at 'path' |
| 1034 | * would have missed some transaction when it was away and |
| 1035 | * in case of 2a the stale bdev has to be updated as well. |
| 1036 | * 2b must not be allowed at all time. |
| 1037 | */ |
| 1038 | |
| 1039 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1040 | * For now, we do allow update to btrfs_fs_device through the |
| 1041 | * btrfs dev scan cli after FS has been mounted. We're still |
| 1042 | * tracking a problem where systems fail mount by subvolume id |
| 1043 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1044 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 1045 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1046 | /* |
| 1047 | * That is if the FS is _not_ mounted and if you |
| 1048 | * are here, that means there is more than one |
| 1049 | * disk with same uuid and devid.We keep the one |
| 1050 | * with larger generation number or the last-in if |
| 1051 | * generation are equal. |
| 1052 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1053 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1054 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1055 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 1056 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 1057 | /* |
| 1058 | * We are going to replace the device path for a given devid, |
| 1059 | * make sure it's the same device if the device is mounted |
| 1060 | */ |
| 1061 | if (device->bdev) { |
| 1062 | struct block_device *path_bdev; |
| 1063 | |
| 1064 | path_bdev = lookup_bdev(path); |
| 1065 | if (IS_ERR(path_bdev)) { |
| 1066 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1067 | return ERR_CAST(path_bdev); |
| 1068 | } |
| 1069 | |
| 1070 | if (device->bdev != path_bdev) { |
| 1071 | bdput(path_bdev); |
| 1072 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1073 | btrfs_warn_in_rcu(device->fs_info, |
| 1074 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 1075 | disk_super->fsid, devid, |
| 1076 | rcu_str_deref(device->name), path); |
| 1077 | return ERR_PTR(-EEXIST); |
| 1078 | } |
| 1079 | bdput(path_bdev); |
| 1080 | btrfs_info_in_rcu(device->fs_info, |
| 1081 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 1082 | disk_super->fsid, devid, |
| 1083 | rcu_str_deref(device->name), path); |
| 1084 | } |
| 1085 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1086 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1087 | if (!name) { |
| 1088 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1089 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1090 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1091 | rcu_string_free(device->name); |
| 1092 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1093 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1094 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1095 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1096 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1099 | /* |
| 1100 | * Unmount does not free the btrfs_device struct but would zero |
| 1101 | * generation along with most of the other members. So just update |
| 1102 | * it back. We need it to pick the disk with largest generation |
| 1103 | * (as above). |
| 1104 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1105 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1106 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 1107 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 1108 | fs_devices->latest_generation); |
| 1109 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 1110 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 1111 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 1112 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 1113 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 1114 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1117 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 1118 | { |
| 1119 | struct btrfs_fs_devices *fs_devices; |
| 1120 | struct btrfs_device *device; |
| 1121 | struct btrfs_device *orig_dev; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1122 | int ret = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1123 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 1124 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1125 | if (IS_ERR(fs_devices)) |
| 1126 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1127 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1128 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1129 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1130 | |
| 1131 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1132 | struct rcu_string *name; |
| 1133 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 1134 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 1135 | orig_dev->uuid); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1136 | if (IS_ERR(device)) { |
| 1137 | ret = PTR_ERR(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1138 | goto error; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1139 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1140 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1141 | /* |
| 1142 | * This is ok to do without rcu read locked because we hold the |
| 1143 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1144 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1145 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1146 | name = rcu_string_strdup(orig_dev->name->str, |
| 1147 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1148 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1149 | btrfs_free_device(device); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1150 | ret = -ENOMEM; |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1151 | goto error; |
| 1152 | } |
| 1153 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1154 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1155 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1156 | list_add(&device->dev_list, &fs_devices->devices); |
| 1157 | device->fs_devices = fs_devices; |
| 1158 | fs_devices->num_devices++; |
| 1159 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1160 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1161 | return fs_devices; |
| 1162 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1163 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1164 | free_fs_devices(fs_devices); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1165 | return ERR_PTR(ret); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1166 | } |
| 1167 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1168 | /* |
| 1169 | * After we have read the system tree and know devids belonging to |
| 1170 | * this filesystem, remove the device which does not belong there. |
| 1171 | */ |
| 1172 | void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1173 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1174 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1175 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1176 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1177 | mutex_lock(&uuid_mutex); |
| 1178 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1179 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1180 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1181 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1182 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1183 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1184 | &device->dev_state) && |
| 1185 | (!latest_dev || |
| 1186 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1187 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1188 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1189 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1190 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1191 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1192 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1193 | /* |
| 1194 | * In the first step, keep the device which has |
| 1195 | * the correct fsid and the devid that is used |
| 1196 | * for the dev_replace procedure. |
| 1197 | * In the second step, the dev_replace state is |
| 1198 | * read from the device tree and it is known |
| 1199 | * whether the procedure is really active or |
| 1200 | * not, which means whether this device is |
| 1201 | * used or whether it should be removed. |
| 1202 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1203 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1204 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1205 | continue; |
| 1206 | } |
| 1207 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1208 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1209 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1210 | device->bdev = NULL; |
| 1211 | fs_devices->open_devices--; |
| 1212 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1213 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1214 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1215 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1216 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1217 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1218 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1219 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1220 | list_del_init(&device->dev_list); |
| 1221 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1222 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1223 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1224 | |
| 1225 | if (fs_devices->seed) { |
| 1226 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1227 | goto again; |
| 1228 | } |
| 1229 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1230 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1231 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1232 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1233 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1234 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1235 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1236 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1237 | if (!device->bdev) |
| 1238 | return; |
| 1239 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1240 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1241 | sync_blockdev(device->bdev); |
| 1242 | invalidate_bdev(device->bdev); |
| 1243 | } |
| 1244 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1245 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1248 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1249 | { |
| 1250 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1251 | struct btrfs_device *new_device; |
| 1252 | struct rcu_string *name; |
| 1253 | |
| 1254 | if (device->bdev) |
| 1255 | fs_devices->open_devices--; |
| 1256 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1257 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1258 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1259 | list_del_init(&device->dev_alloc_list); |
| 1260 | fs_devices->rw_devices--; |
| 1261 | } |
| 1262 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1263 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1264 | fs_devices->missing_devices--; |
| 1265 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1266 | btrfs_close_bdev(device); |
| 1267 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1268 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1269 | device->uuid); |
| 1270 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1271 | |
| 1272 | /* Safe because we are under uuid_mutex */ |
| 1273 | if (device->name) { |
| 1274 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1275 | BUG_ON(!name); /* -ENOMEM */ |
| 1276 | rcu_assign_pointer(new_device->name, name); |
| 1277 | } |
| 1278 | |
| 1279 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1280 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1281 | |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 1282 | synchronize_rcu(); |
| 1283 | btrfs_free_device(device); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1286 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1287 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1288 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1289 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1290 | if (--fs_devices->opened > 0) |
| 1291 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1292 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1293 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1294 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1295 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1296 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1297 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1298 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1299 | WARN_ON(fs_devices->open_devices); |
| 1300 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1301 | fs_devices->opened = 0; |
| 1302 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1303 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1307 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1308 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1309 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1310 | int ret; |
| 1311 | |
| 1312 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1313 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1314 | if (!fs_devices->opened) { |
| 1315 | seed_devices = fs_devices->seed; |
| 1316 | fs_devices->seed = NULL; |
| 1317 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1318 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1319 | |
| 1320 | while (seed_devices) { |
| 1321 | fs_devices = seed_devices; |
| 1322 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1323 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1324 | free_fs_devices(fs_devices); |
| 1325 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1326 | return ret; |
| 1327 | } |
| 1328 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1329 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1330 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1331 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1332 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1333 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1334 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1335 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1336 | flags |= FMODE_EXCL; |
| 1337 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1338 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1339 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1340 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1341 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1342 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1343 | if (!latest_dev || |
| 1344 | device->generation > latest_dev->generation) |
| 1345 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1346 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1347 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1348 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1349 | goto out; |
| 1350 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1351 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1352 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1353 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1354 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1355 | return ret; |
| 1356 | } |
| 1357 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1358 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1359 | { |
| 1360 | struct btrfs_device *dev1, *dev2; |
| 1361 | |
| 1362 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1363 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1364 | |
| 1365 | if (dev1->devid < dev2->devid) |
| 1366 | return -1; |
| 1367 | else if (dev1->devid > dev2->devid) |
| 1368 | return 1; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1372 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1373 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1374 | { |
| 1375 | int ret; |
| 1376 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1377 | lockdep_assert_held(&uuid_mutex); |
| 1378 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1379 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1380 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1381 | fs_devices->opened++; |
| 1382 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1383 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1384 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1385 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1386 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1387 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1388 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1389 | return ret; |
| 1390 | } |
| 1391 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1392 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1393 | { |
| 1394 | kunmap(page); |
| 1395 | put_page(page); |
| 1396 | } |
| 1397 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1398 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1399 | struct page **page, |
| 1400 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1401 | { |
| 1402 | void *p; |
| 1403 | pgoff_t index; |
| 1404 | |
| 1405 | /* make sure our super fits in the device */ |
| 1406 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1407 | return 1; |
| 1408 | |
| 1409 | /* make sure our super fits in the page */ |
| 1410 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1411 | return 1; |
| 1412 | |
| 1413 | /* make sure our super doesn't straddle pages on disk */ |
| 1414 | index = bytenr >> PAGE_SHIFT; |
| 1415 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1416 | return 1; |
| 1417 | |
| 1418 | /* pull in the page with our super */ |
| 1419 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1420 | index, GFP_KERNEL); |
| 1421 | |
| 1422 | if (IS_ERR_OR_NULL(*page)) |
| 1423 | return 1; |
| 1424 | |
| 1425 | p = kmap(*page); |
| 1426 | |
| 1427 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1428 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1429 | |
| 1430 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1431 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1432 | btrfs_release_disk_super(*page); |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
| 1436 | if ((*disk_super)->label[0] && |
| 1437 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1438 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1443 | int btrfs_forget_devices(const char *path) |
| 1444 | { |
| 1445 | int ret; |
| 1446 | |
| 1447 | mutex_lock(&uuid_mutex); |
| 1448 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1449 | mutex_unlock(&uuid_mutex); |
| 1450 | |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1456 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1457 | * is read via pagecache |
| 1458 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1459 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1460 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1461 | { |
| 1462 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1463 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1464 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1465 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1466 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1467 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1468 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1469 | lockdep_assert_held(&uuid_mutex); |
| 1470 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1471 | /* |
| 1472 | * we would like to check all the supers, but that would make |
| 1473 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1474 | * So, we need to add a special mount option to scan for |
| 1475 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1476 | */ |
| 1477 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1478 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1479 | |
| 1480 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1481 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1482 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1483 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1484 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1485 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1486 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1487 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1488 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1489 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1490 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1491 | if (new_device_added) |
| 1492 | btrfs_free_stale_devices(path, device); |
| 1493 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1494 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1495 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1496 | |
| 1497 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1498 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1499 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1500 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1501 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1502 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1503 | /* |
| 1504 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1505 | * such is found, record the end of it in *start |
| 1506 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1507 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1508 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1509 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1510 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1511 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1512 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1513 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1514 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1515 | &physical_start, &physical_end, |
| 1516 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1517 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1518 | if (in_range(physical_start, *start, len) || |
| 1519 | in_range(*start, physical_start, |
| 1520 | physical_end - physical_start)) { |
| 1521 | *start = physical_end + 1; |
| 1522 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1523 | } |
| 1524 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1525 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1529 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1530 | * find_free_dev_extent_start - find free space in the specified device |
| 1531 | * @device: the device which we search the free space in |
| 1532 | * @num_bytes: the size of the free space that we need |
| 1533 | * @search_start: the position from which to begin the search |
| 1534 | * @start: store the start of the free space. |
| 1535 | * @len: the size of the free space. that we find, or the size |
| 1536 | * of the max free space if we don't find suitable free space |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1537 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1538 | * this uses a pretty simple search, the expectation is that it is |
| 1539 | * called very infrequently and that a given device has a small number |
| 1540 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1541 | * |
| 1542 | * @start is used to store the start of the free space if we find. But if we |
| 1543 | * don't find suitable free space, it will be used to store the start position |
| 1544 | * of the max free space. |
| 1545 | * |
| 1546 | * @len is used to store the size of the free space that we find. |
| 1547 | * But if we don't find suitable free space, it is used to store the size of |
| 1548 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1549 | * |
| 1550 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1551 | * check to ensure dev extents are not double allocated. |
| 1552 | * This makes the function safe to allocate dev extents but may not report |
| 1553 | * correct usable device space, as device extent freed in current transaction |
| 1554 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1555 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1556 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1557 | u64 num_bytes, u64 search_start, u64 *start, |
| 1558 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1559 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1560 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1561 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1562 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1563 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1564 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1565 | u64 hole_size; |
| 1566 | u64 max_hole_start; |
| 1567 | u64 max_hole_size; |
| 1568 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1569 | u64 search_end = device->total_bytes; |
| 1570 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1571 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1572 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1573 | |
| 1574 | /* |
| 1575 | * We don't want to overwrite the superblock on the drive nor any area |
| 1576 | * used by the boot loader (grub for example), so we make sure to start |
| 1577 | * at an offset of at least 1MB. |
| 1578 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1579 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1580 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1581 | path = btrfs_alloc_path(); |
| 1582 | if (!path) |
| 1583 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1584 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1585 | max_hole_start = search_start; |
| 1586 | max_hole_size = 0; |
| 1587 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1588 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1589 | if (search_start >= search_end || |
| 1590 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1591 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1592 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1595 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1596 | path->search_commit_root = 1; |
| 1597 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1598 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1599 | key.objectid = device->devid; |
| 1600 | key.offset = search_start; |
| 1601 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1602 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1603 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1604 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1605 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1606 | if (ret > 0) { |
| 1607 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1608 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1609 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1610 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1611 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1612 | while (1) { |
| 1613 | l = path->nodes[0]; |
| 1614 | slot = path->slots[0]; |
| 1615 | if (slot >= btrfs_header_nritems(l)) { |
| 1616 | ret = btrfs_next_leaf(root, path); |
| 1617 | if (ret == 0) |
| 1618 | continue; |
| 1619 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1620 | goto out; |
| 1621 | |
| 1622 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1623 | } |
| 1624 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1625 | |
| 1626 | if (key.objectid < device->devid) |
| 1627 | goto next; |
| 1628 | |
| 1629 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1630 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1631 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1632 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1633 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1634 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1635 | if (key.offset > search_start) { |
| 1636 | hole_size = key.offset - search_start; |
| 1637 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1638 | /* |
| 1639 | * Have to check before we set max_hole_start, otherwise |
| 1640 | * we could end up sending back this offset anyway. |
| 1641 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1642 | if (contains_pending_extent(device, &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1643 | hole_size)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1644 | if (key.offset >= search_start) |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1645 | hole_size = key.offset - search_start; |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1646 | else |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1647 | hole_size = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1648 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1649 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1650 | if (hole_size > max_hole_size) { |
| 1651 | max_hole_start = search_start; |
| 1652 | max_hole_size = hole_size; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * If this free space is greater than which we need, |
| 1657 | * it must be the max free space that we have found |
| 1658 | * until now, so max_hole_start must point to the start |
| 1659 | * of this free space and the length of this free space |
| 1660 | * is stored in max_hole_size. Thus, we return |
| 1661 | * max_hole_start and max_hole_size and go back to the |
| 1662 | * caller. |
| 1663 | */ |
| 1664 | if (hole_size >= num_bytes) { |
| 1665 | ret = 0; |
| 1666 | goto out; |
| 1667 | } |
| 1668 | } |
| 1669 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1670 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1671 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1672 | dev_extent); |
| 1673 | if (extent_end > search_start) |
| 1674 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1675 | next: |
| 1676 | path->slots[0]++; |
| 1677 | cond_resched(); |
| 1678 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1679 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1680 | /* |
| 1681 | * At this point, search_start should be the end of |
| 1682 | * allocated dev extents, and when shrinking the device, |
| 1683 | * search_end may be smaller than search_start. |
| 1684 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1685 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1686 | hole_size = search_end - search_start; |
| 1687 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1688 | if (contains_pending_extent(device, &search_start, hole_size)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1689 | btrfs_release_path(path); |
| 1690 | goto again; |
| 1691 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1692 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1693 | if (hole_size > max_hole_size) { |
| 1694 | max_hole_start = search_start; |
| 1695 | max_hole_size = hole_size; |
| 1696 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1697 | } |
| 1698 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1699 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1700 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1701 | ret = -ENOSPC; |
| 1702 | else |
| 1703 | ret = 0; |
| 1704 | |
| 1705 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1706 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1707 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1708 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1709 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1710 | return ret; |
| 1711 | } |
| 1712 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1713 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1714 | u64 *start, u64 *len) |
| 1715 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1716 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1717 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1718 | } |
| 1719 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1720 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1721 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1722 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1723 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1724 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1725 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1726 | int ret; |
| 1727 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1728 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1729 | struct btrfs_key found_key; |
| 1730 | struct extent_buffer *leaf = NULL; |
| 1731 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1732 | |
| 1733 | path = btrfs_alloc_path(); |
| 1734 | if (!path) |
| 1735 | return -ENOMEM; |
| 1736 | |
| 1737 | key.objectid = device->devid; |
| 1738 | key.offset = start; |
| 1739 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1740 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1741 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1742 | if (ret > 0) { |
| 1743 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1744 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1745 | if (ret) |
| 1746 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1747 | leaf = path->nodes[0]; |
| 1748 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1749 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1750 | struct btrfs_dev_extent); |
| 1751 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1752 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1753 | key = found_key; |
| 1754 | btrfs_release_path(path); |
| 1755 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1756 | } else if (ret == 0) { |
| 1757 | leaf = path->nodes[0]; |
| 1758 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1759 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1760 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1761 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1762 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1763 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1764 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1765 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1766 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1767 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1768 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1769 | btrfs_handle_fs_error(fs_info, ret, |
| 1770 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1771 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1772 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1773 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1774 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1775 | btrfs_free_path(path); |
| 1776 | return ret; |
| 1777 | } |
| 1778 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1779 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1780 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1781 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1782 | { |
| 1783 | int ret; |
| 1784 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1785 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1786 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1787 | struct btrfs_dev_extent *extent; |
| 1788 | struct extent_buffer *leaf; |
| 1789 | struct btrfs_key key; |
| 1790 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1791 | 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] | 1792 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1793 | path = btrfs_alloc_path(); |
| 1794 | if (!path) |
| 1795 | return -ENOMEM; |
| 1796 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1797 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1798 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1799 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1800 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1801 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1802 | if (ret) |
| 1803 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1804 | |
| 1805 | leaf = path->nodes[0]; |
| 1806 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1807 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1808 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1809 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1810 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1811 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1812 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1813 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1814 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1815 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1816 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1817 | btrfs_free_path(path); |
| 1818 | return ret; |
| 1819 | } |
| 1820 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1821 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1822 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1823 | struct extent_map_tree *em_tree; |
| 1824 | struct extent_map *em; |
| 1825 | struct rb_node *n; |
| 1826 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1827 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1828 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1829 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1830 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1831 | if (n) { |
| 1832 | em = rb_entry(n, struct extent_map, rb_node); |
| 1833 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1834 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1835 | read_unlock(&em_tree->lock); |
| 1836 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1837 | return ret; |
| 1838 | } |
| 1839 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1840 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1841 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1842 | { |
| 1843 | int ret; |
| 1844 | struct btrfs_key key; |
| 1845 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1846 | struct btrfs_path *path; |
| 1847 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1848 | path = btrfs_alloc_path(); |
| 1849 | if (!path) |
| 1850 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1851 | |
| 1852 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1853 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1854 | key.offset = (u64)-1; |
| 1855 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1856 | 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] | 1857 | if (ret < 0) |
| 1858 | goto error; |
| 1859 | |
Anand Jain | a06dee4d | 2019-08-27 15:40:44 +0800 | [diff] [blame] | 1860 | if (ret == 0) { |
| 1861 | /* Corruption */ |
| 1862 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| 1863 | ret = -EUCLEAN; |
| 1864 | goto error; |
| 1865 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1866 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1867 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1868 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1869 | BTRFS_DEV_ITEM_KEY); |
| 1870 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1871 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1872 | } else { |
| 1873 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1874 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1875 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1876 | } |
| 1877 | ret = 0; |
| 1878 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1879 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1880 | return ret; |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * the device information is stored in the chunk root |
| 1885 | * the btrfs_device struct should be fully filled in |
| 1886 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1887 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1888 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1889 | { |
| 1890 | int ret; |
| 1891 | struct btrfs_path *path; |
| 1892 | struct btrfs_dev_item *dev_item; |
| 1893 | struct extent_buffer *leaf; |
| 1894 | struct btrfs_key key; |
| 1895 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1896 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1897 | path = btrfs_alloc_path(); |
| 1898 | if (!path) |
| 1899 | return -ENOMEM; |
| 1900 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1901 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1902 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1903 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1904 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1905 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1906 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1907 | if (ret) |
| 1908 | goto out; |
| 1909 | |
| 1910 | leaf = path->nodes[0]; |
| 1911 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1912 | |
| 1913 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1914 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1915 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1916 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1917 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1918 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1919 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1920 | btrfs_device_get_disk_total_bytes(device)); |
| 1921 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1922 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1923 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1924 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1925 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1926 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1927 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1928 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1929 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1930 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1931 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1932 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1933 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1934 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1935 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1936 | out: |
| 1937 | btrfs_free_path(path); |
| 1938 | return ret; |
| 1939 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1940 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1941 | /* |
| 1942 | * Function to update ctime/mtime for a given device path. |
| 1943 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1944 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1945 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1946 | { |
| 1947 | struct file *filp; |
| 1948 | |
| 1949 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1950 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1951 | return; |
| 1952 | file_update_time(filp); |
| 1953 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1954 | } |
| 1955 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1956 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1957 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1958 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1959 | int ret; |
| 1960 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1961 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1962 | struct btrfs_trans_handle *trans; |
| 1963 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1964 | path = btrfs_alloc_path(); |
| 1965 | if (!path) |
| 1966 | return -ENOMEM; |
| 1967 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1968 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1969 | if (IS_ERR(trans)) { |
| 1970 | btrfs_free_path(path); |
| 1971 | return PTR_ERR(trans); |
| 1972 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1973 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1974 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1975 | key.offset = device->devid; |
| 1976 | |
| 1977 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1978 | if (ret) { |
| 1979 | if (ret > 0) |
| 1980 | ret = -ENOENT; |
| 1981 | btrfs_abort_transaction(trans, ret); |
| 1982 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1983 | goto out; |
| 1984 | } |
| 1985 | |
| 1986 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1987 | if (ret) { |
| 1988 | btrfs_abort_transaction(trans, ret); |
| 1989 | btrfs_end_transaction(trans); |
| 1990 | } |
| 1991 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1992 | out: |
| 1993 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1994 | if (!ret) |
| 1995 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1996 | return ret; |
| 1997 | } |
| 1998 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1999 | /* |
| 2000 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 2001 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 2002 | * replace. |
| 2003 | */ |
| 2004 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 2005 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2006 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2007 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2008 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2009 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2010 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2011 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2012 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 2013 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2014 | all_avail = fs_info->avail_data_alloc_bits | |
| 2015 | fs_info->avail_system_alloc_bits | |
| 2016 | fs_info->avail_metadata_alloc_bits; |
| 2017 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2018 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2019 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 2020 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2021 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2022 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2023 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 2024 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2025 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 2026 | if (ret) |
| 2027 | return ret; |
| 2028 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2032 | } |
| 2033 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 2034 | static struct btrfs_device * btrfs_find_next_active_device( |
| 2035 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2036 | { |
| 2037 | struct btrfs_device *next_device; |
| 2038 | |
| 2039 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 2040 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2041 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 2042 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2043 | return next_device; |
| 2044 | } |
| 2045 | |
| 2046 | return NULL; |
| 2047 | } |
| 2048 | |
| 2049 | /* |
| 2050 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 2051 | * and replace it with the provided or the next active device, in the context |
| 2052 | * where this function called, there should be always be another device (or |
| 2053 | * this_dev) which is active. |
| 2054 | */ |
David Sterba | b105e92 | 2019-10-01 19:57:35 +0200 | [diff] [blame] | 2055 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2056 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2057 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2058 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 2059 | struct btrfs_device *next_device; |
| 2060 | |
| 2061 | if (this_dev) |
| 2062 | next_device = this_dev; |
| 2063 | else |
| 2064 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 2065 | device); |
| 2066 | ASSERT(next_device); |
| 2067 | |
| 2068 | if (fs_info->sb->s_bdev && |
| 2069 | (fs_info->sb->s_bdev == device->bdev)) |
| 2070 | fs_info->sb->s_bdev = next_device->bdev; |
| 2071 | |
| 2072 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 2073 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2074 | } |
| 2075 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2076 | /* |
| 2077 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 2078 | * currently replaced. |
| 2079 | */ |
| 2080 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 2081 | { |
| 2082 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 2083 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2084 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2085 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 2086 | ASSERT(num_devices > 1); |
| 2087 | num_devices--; |
| 2088 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 2089 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2090 | |
| 2091 | return num_devices; |
| 2092 | } |
| 2093 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2094 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2095 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2096 | { |
| 2097 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2098 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2099 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2100 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2101 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2102 | |
| 2103 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2104 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2105 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2106 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2107 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2108 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2109 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2110 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2111 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2112 | |
| 2113 | if (IS_ERR(device)) { |
| 2114 | if (PTR_ERR(device) == -ENOENT && |
| 2115 | strcmp(device_path, "missing") == 0) |
| 2116 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2117 | else |
| 2118 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2119 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2120 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2121 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2122 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2123 | btrfs_warn_in_rcu(fs_info, |
| 2124 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2125 | rcu_str_deref(device->name), device->devid); |
| 2126 | ret = -ETXTBSY; |
| 2127 | goto out; |
| 2128 | } |
| 2129 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2130 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2131 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2132 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2135 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2136 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2137 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2138 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2139 | } |
| 2140 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2141 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2142 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2143 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2144 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2145 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2146 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2147 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2148 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2149 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2150 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2151 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2152 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2153 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2154 | /* |
| 2155 | * TODO: the superblock still includes this device in its num_devices |
| 2156 | * counter although write_all_supers() is not locked out. This |
| 2157 | * could give a filesystem state which requires a degraded mount. |
| 2158 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2159 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2160 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2161 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2162 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2163 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2164 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2165 | |
| 2166 | /* |
| 2167 | * the device list mutex makes sure that we don't change |
| 2168 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2169 | * the device supers. Whoever is writing all supers, should |
| 2170 | * lock the device list mutex before getting the number of |
| 2171 | * devices in the super block (super_copy). Conversely, |
| 2172 | * whoever updates the number of devices in the super block |
| 2173 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2174 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2175 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2176 | /* |
| 2177 | * In normal cases the cur_devices == fs_devices. But in case |
| 2178 | * of deleting a seed device, the cur_devices should point to |
| 2179 | * its own fs_devices listed under the fs_devices->seed. |
| 2180 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2181 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2182 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2183 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2184 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2185 | cur_devices->num_devices--; |
| 2186 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2187 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2188 | if (cur_devices != fs_devices) |
| 2189 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2190 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2191 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2192 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2193 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2194 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2195 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2196 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2197 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2198 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2199 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2200 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2201 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2202 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2203 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2204 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2205 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2206 | /* |
| 2207 | * at this point, the device is zero sized and detached from |
| 2208 | * the devices list. All that's left is to zero out the old |
| 2209 | * supers and free the device. |
| 2210 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2211 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2212 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2213 | |
| 2214 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2215 | synchronize_rcu(); |
| 2216 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2217 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2218 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2219 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2220 | if (fs_devices->seed == cur_devices) { |
| 2221 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2222 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2223 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2224 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2225 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2226 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2227 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2228 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2231 | out: |
| 2232 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2233 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2234 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2235 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2236 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2237 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2238 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2239 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2240 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2241 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2242 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2243 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2244 | } |
| 2245 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2246 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2247 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2248 | struct btrfs_fs_devices *fs_devices; |
| 2249 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2250 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2251 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2252 | /* |
| 2253 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2254 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2255 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2256 | * srcdev will have its correct fs_devices in both the cases. |
| 2257 | */ |
| 2258 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2259 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2260 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2261 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2262 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2263 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2264 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2265 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2266 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2267 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2268 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2269 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2270 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2271 | } |
| 2272 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2273 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2274 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2275 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2276 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2277 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2278 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2279 | /* zero out the old super if it is writable */ |
| 2280 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2281 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2282 | |
| 2283 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2284 | synchronize_rcu(); |
| 2285 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2286 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2287 | /* if this is no devs we rather delete the fs_devices */ |
| 2288 | if (!fs_devices->num_devices) { |
| 2289 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2290 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2291 | /* |
| 2292 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2293 | * seed. In case of a seed device being replaced, the replace |
| 2294 | * target added to the sprout FS, so there will be no more |
| 2295 | * device left under the seed FS. |
| 2296 | */ |
| 2297 | ASSERT(fs_devices->seeding); |
| 2298 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2299 | tmp_fs_devices = fs_info->fs_devices; |
| 2300 | while (tmp_fs_devices) { |
| 2301 | if (tmp_fs_devices->seed == fs_devices) { |
| 2302 | tmp_fs_devices->seed = fs_devices->seed; |
| 2303 | break; |
| 2304 | } |
| 2305 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2306 | } |
| 2307 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2308 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2309 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2310 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2313 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2314 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2315 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2316 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2317 | WARN_ON(!tgtdev); |
| 2318 | mutex_lock(&fs_devices->device_list_mutex); |
| 2319 | |
| 2320 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2321 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2322 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2323 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2324 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2325 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2326 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2327 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2328 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2329 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2330 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2331 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2332 | |
| 2333 | /* |
| 2334 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2335 | * may lead to a call to btrfs_show_devname() which will try |
| 2336 | * to hold device_list_mutex. And here this device |
| 2337 | * is already out of device list, so we don't have to hold |
| 2338 | * the device_list_mutex lock. |
| 2339 | */ |
| 2340 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2341 | |
| 2342 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2343 | synchronize_rcu(); |
| 2344 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2347 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2348 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2349 | { |
| 2350 | int ret = 0; |
| 2351 | struct btrfs_super_block *disk_super; |
| 2352 | u64 devid; |
| 2353 | u8 *dev_uuid; |
| 2354 | struct block_device *bdev; |
| 2355 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2356 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2357 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2358 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2359 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2360 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2361 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2362 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2363 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2364 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2365 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2366 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2367 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2368 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2369 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2370 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2371 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2372 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2373 | if (!device) |
| 2374 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2375 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2376 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2379 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2380 | * Lookup a device given by device id, or the path if the id is 0. |
| 2381 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2382 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2383 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2384 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2385 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2386 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2387 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2388 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2389 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2390 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2391 | if (!device) |
| 2392 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2393 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2394 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2395 | |
| 2396 | if (!device_path || !device_path[0]) |
| 2397 | return ERR_PTR(-EINVAL); |
| 2398 | |
| 2399 | if (strcmp(device_path, "missing") == 0) { |
| 2400 | /* Find first missing device */ |
| 2401 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2402 | dev_list) { |
| 2403 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2404 | &device->dev_state) && !device->bdev) |
| 2405 | return device; |
| 2406 | } |
| 2407 | return ERR_PTR(-ENOENT); |
| 2408 | } |
| 2409 | |
| 2410 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2413 | /* |
| 2414 | * does all the dirty work required for changing file system's UUID. |
| 2415 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2416 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2417 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2418 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2419 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2420 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2421 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2422 | struct btrfs_device *device; |
| 2423 | u64 super_flags; |
| 2424 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2425 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2426 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2427 | return -EINVAL; |
| 2428 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2429 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2430 | if (IS_ERR(seed_devices)) |
| 2431 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2432 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2433 | old_devices = clone_fs_devices(fs_devices); |
| 2434 | if (IS_ERR(old_devices)) { |
| 2435 | kfree(seed_devices); |
| 2436 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2437 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2438 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2439 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2440 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2441 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2442 | seed_devices->opened = 1; |
| 2443 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2444 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2445 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2446 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2447 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2448 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2449 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2450 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2451 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2452 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2453 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2454 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2455 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2456 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2457 | fs_devices->seeding = 0; |
| 2458 | fs_devices->num_devices = 0; |
| 2459 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2460 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2461 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2462 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2463 | |
| 2464 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2465 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2466 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2467 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2468 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2469 | super_flags = btrfs_super_flags(disk_super) & |
| 2470 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2471 | btrfs_set_super_flags(disk_super, super_flags); |
| 2472 | |
| 2473 | return 0; |
| 2474 | } |
| 2475 | |
| 2476 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2477 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2478 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2479 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2480 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2481 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2482 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2483 | struct btrfs_path *path; |
| 2484 | struct extent_buffer *leaf; |
| 2485 | struct btrfs_dev_item *dev_item; |
| 2486 | struct btrfs_device *device; |
| 2487 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2488 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2489 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2490 | u64 devid; |
| 2491 | int ret; |
| 2492 | |
| 2493 | path = btrfs_alloc_path(); |
| 2494 | if (!path) |
| 2495 | return -ENOMEM; |
| 2496 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2497 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2498 | key.offset = 0; |
| 2499 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2500 | |
| 2501 | while (1) { |
| 2502 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2503 | if (ret < 0) |
| 2504 | goto error; |
| 2505 | |
| 2506 | leaf = path->nodes[0]; |
| 2507 | next_slot: |
| 2508 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2509 | ret = btrfs_next_leaf(root, path); |
| 2510 | if (ret > 0) |
| 2511 | break; |
| 2512 | if (ret < 0) |
| 2513 | goto error; |
| 2514 | leaf = path->nodes[0]; |
| 2515 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2516 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2517 | continue; |
| 2518 | } |
| 2519 | |
| 2520 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2521 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2522 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2523 | break; |
| 2524 | |
| 2525 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2526 | struct btrfs_dev_item); |
| 2527 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2528 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2529 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2530 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2531 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2532 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2533 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2534 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2535 | |
| 2536 | if (device->fs_devices->seeding) { |
| 2537 | btrfs_set_device_generation(leaf, dev_item, |
| 2538 | device->generation); |
| 2539 | btrfs_mark_buffer_dirty(leaf); |
| 2540 | } |
| 2541 | |
| 2542 | path->slots[0]++; |
| 2543 | goto next_slot; |
| 2544 | } |
| 2545 | ret = 0; |
| 2546 | error: |
| 2547 | btrfs_free_path(path); |
| 2548 | return ret; |
| 2549 | } |
| 2550 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2551 | 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] | 2552 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2553 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2554 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2555 | struct btrfs_trans_handle *trans; |
| 2556 | struct btrfs_device *device; |
| 2557 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2558 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2559 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2560 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2561 | u64 orig_super_total_bytes; |
| 2562 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2563 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2564 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2565 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2566 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2567 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2568 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2569 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2570 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2571 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2572 | if (IS_ERR(bdev)) |
| 2573 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2574 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2575 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2576 | seeding_dev = 1; |
| 2577 | down_write(&sb->s_umount); |
| 2578 | mutex_lock(&uuid_mutex); |
| 2579 | } |
| 2580 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2581 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2582 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2583 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2584 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2585 | if (device->bdev == bdev) { |
| 2586 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2587 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2588 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2589 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2590 | } |
| 2591 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2592 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2593 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2594 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2595 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2596 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2597 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2598 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2599 | } |
| 2600 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2601 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2602 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2603 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2604 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2605 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2606 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2607 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2608 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2609 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2610 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2611 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2614 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2615 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2616 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2617 | device->io_width = fs_info->sectorsize; |
| 2618 | device->io_align = fs_info->sectorsize; |
| 2619 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2620 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2621 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2622 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2623 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2624 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2625 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2626 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2627 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2628 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2629 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2630 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2631 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2632 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2633 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2634 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2635 | if (ret) { |
| 2636 | btrfs_abort_transaction(trans, ret); |
| 2637 | goto error_trans; |
| 2638 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2639 | } |
| 2640 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2641 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2642 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2643 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2644 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2645 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2646 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2647 | fs_devices->num_devices++; |
| 2648 | fs_devices->open_devices++; |
| 2649 | fs_devices->rw_devices++; |
| 2650 | fs_devices->total_devices++; |
| 2651 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2652 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2653 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2654 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2655 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2656 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2657 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2658 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2659 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2660 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2661 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2662 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2663 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2664 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2665 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2666 | |
| 2667 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2668 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2669 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2670 | /* |
| 2671 | * we've got more storage, clear any full flags on the space |
| 2672 | * infos |
| 2673 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2674 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2675 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2676 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2677 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2678 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2679 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2680 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2681 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2682 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2683 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2684 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2685 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2686 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2689 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2690 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2691 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2692 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | if (seeding_dev) { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2696 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2697 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2698 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2699 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2700 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2701 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 2702 | btrfs_sysfs_update_sprout_fsid(fs_devices, |
| 2703 | fs_info->fs_devices->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2704 | } |
| 2705 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2706 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2707 | |
| 2708 | if (seeding_dev) { |
| 2709 | mutex_unlock(&uuid_mutex); |
| 2710 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2711 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2712 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2713 | if (ret) /* transaction commit */ |
| 2714 | return ret; |
| 2715 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2716 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2717 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2718 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2719 | "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] | 2720 | trans = btrfs_attach_transaction(root); |
| 2721 | if (IS_ERR(trans)) { |
| 2722 | if (PTR_ERR(trans) == -ENOENT) |
| 2723 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2724 | ret = PTR_ERR(trans); |
| 2725 | trans = NULL; |
| 2726 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2727 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2728 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2729 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2730 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2731 | /* Update ctime/mtime for libblkid */ |
| 2732 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2733 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2734 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2735 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2736 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2737 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2738 | mutex_lock(&fs_info->chunk_mutex); |
| 2739 | list_del_rcu(&device->dev_list); |
| 2740 | list_del(&device->dev_alloc_list); |
| 2741 | fs_info->fs_devices->num_devices--; |
| 2742 | fs_info->fs_devices->open_devices--; |
| 2743 | fs_info->fs_devices->rw_devices--; |
| 2744 | fs_info->fs_devices->total_devices--; |
| 2745 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2746 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2747 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2748 | orig_super_total_bytes); |
| 2749 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2750 | orig_super_num_devices); |
| 2751 | mutex_unlock(&fs_info->chunk_mutex); |
| 2752 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2753 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2754 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2755 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2756 | if (trans) |
| 2757 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2758 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2759 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2760 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2761 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2762 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2763 | mutex_unlock(&uuid_mutex); |
| 2764 | up_write(&sb->s_umount); |
| 2765 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2766 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2767 | } |
| 2768 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2769 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2770 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2771 | { |
| 2772 | int ret; |
| 2773 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2774 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2775 | struct btrfs_dev_item *dev_item; |
| 2776 | struct extent_buffer *leaf; |
| 2777 | struct btrfs_key key; |
| 2778 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2779 | path = btrfs_alloc_path(); |
| 2780 | if (!path) |
| 2781 | return -ENOMEM; |
| 2782 | |
| 2783 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2784 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2785 | key.offset = device->devid; |
| 2786 | |
| 2787 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2788 | if (ret < 0) |
| 2789 | goto out; |
| 2790 | |
| 2791 | if (ret > 0) { |
| 2792 | ret = -ENOENT; |
| 2793 | goto out; |
| 2794 | } |
| 2795 | |
| 2796 | leaf = path->nodes[0]; |
| 2797 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2798 | |
| 2799 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2800 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2801 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2802 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2803 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2804 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2805 | btrfs_device_get_disk_total_bytes(device)); |
| 2806 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2807 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2808 | btrfs_mark_buffer_dirty(leaf); |
| 2809 | |
| 2810 | out: |
| 2811 | btrfs_free_path(path); |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2815 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2816 | struct btrfs_device *device, u64 new_size) |
| 2817 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2818 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2819 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2820 | u64 old_total; |
| 2821 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2822 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2823 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2824 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2825 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2826 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2827 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2828 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2829 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2830 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2831 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2832 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2833 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2834 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2835 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2836 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2837 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2838 | btrfs_set_super_total_bytes(super_copy, |
| 2839 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2840 | device->fs_devices->total_rw_bytes += diff; |
| 2841 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2842 | btrfs_device_set_total_bytes(device, new_size); |
| 2843 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2844 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2845 | if (list_empty(&device->post_commit_list)) |
| 2846 | list_add_tail(&device->post_commit_list, |
| 2847 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2848 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2849 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2850 | return btrfs_update_device(trans, device); |
| 2851 | } |
| 2852 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2853 | 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] | 2854 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2855 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2856 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2857 | int ret; |
| 2858 | struct btrfs_path *path; |
| 2859 | struct btrfs_key key; |
| 2860 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2861 | path = btrfs_alloc_path(); |
| 2862 | if (!path) |
| 2863 | return -ENOMEM; |
| 2864 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2865 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2866 | key.offset = chunk_offset; |
| 2867 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2868 | |
| 2869 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2870 | if (ret < 0) |
| 2871 | goto out; |
| 2872 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2873 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2874 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2875 | ret = -ENOENT; |
| 2876 | goto out; |
| 2877 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2878 | |
| 2879 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2880 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2881 | btrfs_handle_fs_error(fs_info, ret, |
| 2882 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2883 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2884 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2885 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2886 | } |
| 2887 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2888 | 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] | 2889 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2890 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2891 | struct btrfs_disk_key *disk_key; |
| 2892 | struct btrfs_chunk *chunk; |
| 2893 | u8 *ptr; |
| 2894 | int ret = 0; |
| 2895 | u32 num_stripes; |
| 2896 | u32 array_size; |
| 2897 | u32 len = 0; |
| 2898 | u32 cur; |
| 2899 | struct btrfs_key key; |
| 2900 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2901 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2902 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2903 | |
| 2904 | ptr = super_copy->sys_chunk_array; |
| 2905 | cur = 0; |
| 2906 | |
| 2907 | while (cur < array_size) { |
| 2908 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2909 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2910 | |
| 2911 | len = sizeof(*disk_key); |
| 2912 | |
| 2913 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2914 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2915 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2916 | len += btrfs_chunk_item_size(num_stripes); |
| 2917 | } else { |
| 2918 | ret = -EIO; |
| 2919 | break; |
| 2920 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2921 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2922 | key.offset == chunk_offset) { |
| 2923 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2924 | array_size -= len; |
| 2925 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2926 | } else { |
| 2927 | ptr += len; |
| 2928 | cur += len; |
| 2929 | } |
| 2930 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2931 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2932 | return ret; |
| 2933 | } |
| 2934 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2935 | /* |
| 2936 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2937 | * @logical: Logical block offset in bytes. |
| 2938 | * @length: Length of extent in bytes. |
| 2939 | * |
| 2940 | * Return: Chunk mapping or ERR_PTR. |
| 2941 | */ |
| 2942 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2943 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2944 | { |
| 2945 | struct extent_map_tree *em_tree; |
| 2946 | struct extent_map *em; |
| 2947 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2948 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2949 | read_lock(&em_tree->lock); |
| 2950 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2951 | read_unlock(&em_tree->lock); |
| 2952 | |
| 2953 | if (!em) { |
| 2954 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2955 | logical, length); |
| 2956 | return ERR_PTR(-EINVAL); |
| 2957 | } |
| 2958 | |
| 2959 | if (em->start > logical || em->start + em->len < logical) { |
| 2960 | btrfs_crit(fs_info, |
| 2961 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2962 | logical, length, em->start, em->start + em->len); |
| 2963 | free_extent_map(em); |
| 2964 | return ERR_PTR(-EINVAL); |
| 2965 | } |
| 2966 | |
| 2967 | /* callers are responsible for dropping em's ref. */ |
| 2968 | return em; |
| 2969 | } |
| 2970 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2971 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2972 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2973 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2974 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2975 | struct map_lookup *map; |
| 2976 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2977 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2978 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2979 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2980 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2981 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2982 | /* |
| 2983 | * 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] | 2984 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2985 | * do anything we still error out. |
| 2986 | */ |
| 2987 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2988 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2989 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2990 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2991 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2992 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2993 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2994 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2995 | /* |
| 2996 | * Take the device list mutex to prevent races with the final phase of |
| 2997 | * a device replace operation that replaces the device object associated |
| 2998 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2999 | */ |
| 3000 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3001 | for (i = 0; i < map->num_stripes; i++) { |
| 3002 | struct btrfs_device *device = map->stripes[i].dev; |
| 3003 | ret = btrfs_free_dev_extent(trans, device, |
| 3004 | map->stripes[i].physical, |
| 3005 | &dev_extent_len); |
| 3006 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3007 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3008 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3009 | goto out; |
| 3010 | } |
| 3011 | |
| 3012 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3013 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3014 | btrfs_device_set_bytes_used(device, |
| 3015 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 3016 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3017 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 3018 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3019 | } |
| 3020 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 3021 | ret = btrfs_update_device(trans, device); |
| 3022 | if (ret) { |
| 3023 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3024 | btrfs_abort_transaction(trans, ret); |
| 3025 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3026 | } |
| 3027 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 3028 | mutex_unlock(&fs_devices->device_list_mutex); |
| 3029 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 3030 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3031 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3032 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3033 | goto out; |
| 3034 | } |
| 3035 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3036 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3037 | |
| 3038 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 3039 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3040 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3041 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3042 | goto out; |
| 3043 | } |
| 3044 | } |
| 3045 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 3046 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3047 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3048 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3049 | goto out; |
| 3050 | } |
| 3051 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3052 | out: |
| 3053 | /* once for us */ |
| 3054 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3055 | return ret; |
| 3056 | } |
| 3057 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3058 | 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] | 3059 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3060 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3061 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3062 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3063 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3064 | /* |
| 3065 | * Prevent races with automatic removal of unused block groups. |
| 3066 | * After we relocate and before we remove the chunk with offset |
| 3067 | * chunk_offset, automatic removal of the block group can kick in, |
| 3068 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3069 | * |
| 3070 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3071 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3072 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3073 | * we release the path used to search the chunk/dev tree and before |
| 3074 | * the current task acquires this mutex and calls us. |
| 3075 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3076 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3077 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3078 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3079 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3080 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3081 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3082 | if (ret) |
| 3083 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3084 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3085 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3086 | chunk_offset); |
| 3087 | if (IS_ERR(trans)) { |
| 3088 | ret = PTR_ERR(trans); |
| 3089 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3090 | return ret; |
| 3091 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3092 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3093 | /* |
| 3094 | * step two, delete the device extents and the |
| 3095 | * chunk tree entries |
| 3096 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3097 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3098 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3099 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3100 | } |
| 3101 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3102 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3103 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3104 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3105 | struct btrfs_path *path; |
| 3106 | struct extent_buffer *leaf; |
| 3107 | struct btrfs_chunk *chunk; |
| 3108 | struct btrfs_key key; |
| 3109 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3110 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3111 | bool retried = false; |
| 3112 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3113 | int ret; |
| 3114 | |
| 3115 | path = btrfs_alloc_path(); |
| 3116 | if (!path) |
| 3117 | return -ENOMEM; |
| 3118 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3119 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3120 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3121 | key.offset = (u64)-1; |
| 3122 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3123 | |
| 3124 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3125 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3126 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3127 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3128 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3129 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3130 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3131 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3132 | |
| 3133 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3134 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3135 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3136 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3137 | if (ret < 0) |
| 3138 | goto error; |
| 3139 | if (ret > 0) |
| 3140 | break; |
| 3141 | |
| 3142 | leaf = path->nodes[0]; |
| 3143 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3144 | |
| 3145 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3146 | struct btrfs_chunk); |
| 3147 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3148 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3149 | |
| 3150 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3151 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3152 | if (ret == -ENOSPC) |
| 3153 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3154 | else |
| 3155 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3156 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3157 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3158 | |
| 3159 | if (found_key.offset == 0) |
| 3160 | break; |
| 3161 | key.offset = found_key.offset - 1; |
| 3162 | } |
| 3163 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3164 | if (failed && !retried) { |
| 3165 | failed = 0; |
| 3166 | retried = true; |
| 3167 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3168 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3169 | ret = -ENOSPC; |
| 3170 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3171 | error: |
| 3172 | btrfs_free_path(path); |
| 3173 | return ret; |
| 3174 | } |
| 3175 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3176 | /* |
| 3177 | * return 1 : allocate a data chunk successfully, |
| 3178 | * return <0: errors during allocating a data chunk, |
| 3179 | * return 0 : no need to allocate a data chunk. |
| 3180 | */ |
| 3181 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3182 | u64 chunk_offset) |
| 3183 | { |
| 3184 | struct btrfs_block_group_cache *cache; |
| 3185 | u64 bytes_used; |
| 3186 | u64 chunk_type; |
| 3187 | |
| 3188 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3189 | ASSERT(cache); |
| 3190 | chunk_type = cache->flags; |
| 3191 | btrfs_put_block_group(cache); |
| 3192 | |
| 3193 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 3194 | spin_lock(&fs_info->data_sinfo->lock); |
| 3195 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3196 | spin_unlock(&fs_info->data_sinfo->lock); |
| 3197 | |
| 3198 | if (!bytes_used) { |
| 3199 | struct btrfs_trans_handle *trans; |
| 3200 | int ret; |
| 3201 | |
| 3202 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3203 | if (IS_ERR(trans)) |
| 3204 | return PTR_ERR(trans); |
| 3205 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 3206 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3207 | BTRFS_BLOCK_GROUP_DATA); |
| 3208 | btrfs_end_transaction(trans); |
| 3209 | if (ret < 0) |
| 3210 | return ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3211 | return 1; |
| 3212 | } |
| 3213 | } |
| 3214 | return 0; |
| 3215 | } |
| 3216 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3217 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3218 | struct btrfs_balance_control *bctl) |
| 3219 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3220 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3221 | struct btrfs_trans_handle *trans; |
| 3222 | struct btrfs_balance_item *item; |
| 3223 | struct btrfs_disk_balance_args disk_bargs; |
| 3224 | struct btrfs_path *path; |
| 3225 | struct extent_buffer *leaf; |
| 3226 | struct btrfs_key key; |
| 3227 | int ret, err; |
| 3228 | |
| 3229 | path = btrfs_alloc_path(); |
| 3230 | if (!path) |
| 3231 | return -ENOMEM; |
| 3232 | |
| 3233 | trans = btrfs_start_transaction(root, 0); |
| 3234 | if (IS_ERR(trans)) { |
| 3235 | btrfs_free_path(path); |
| 3236 | return PTR_ERR(trans); |
| 3237 | } |
| 3238 | |
| 3239 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3240 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3241 | key.offset = 0; |
| 3242 | |
| 3243 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3244 | sizeof(*item)); |
| 3245 | if (ret) |
| 3246 | goto out; |
| 3247 | |
| 3248 | leaf = path->nodes[0]; |
| 3249 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3250 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3251 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3252 | |
| 3253 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3254 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3255 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3256 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3257 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3258 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3259 | |
| 3260 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3261 | |
| 3262 | btrfs_mark_buffer_dirty(leaf); |
| 3263 | out: |
| 3264 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3265 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3266 | if (err && !ret) |
| 3267 | ret = err; |
| 3268 | return ret; |
| 3269 | } |
| 3270 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3271 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3272 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3273 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3274 | struct btrfs_trans_handle *trans; |
| 3275 | struct btrfs_path *path; |
| 3276 | struct btrfs_key key; |
| 3277 | int ret, err; |
| 3278 | |
| 3279 | path = btrfs_alloc_path(); |
| 3280 | if (!path) |
| 3281 | return -ENOMEM; |
| 3282 | |
| 3283 | trans = btrfs_start_transaction(root, 0); |
| 3284 | if (IS_ERR(trans)) { |
| 3285 | btrfs_free_path(path); |
| 3286 | return PTR_ERR(trans); |
| 3287 | } |
| 3288 | |
| 3289 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3290 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3291 | key.offset = 0; |
| 3292 | |
| 3293 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3294 | if (ret < 0) |
| 3295 | goto out; |
| 3296 | if (ret > 0) { |
| 3297 | ret = -ENOENT; |
| 3298 | goto out; |
| 3299 | } |
| 3300 | |
| 3301 | ret = btrfs_del_item(trans, root, path); |
| 3302 | out: |
| 3303 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3304 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3305 | if (err && !ret) |
| 3306 | ret = err; |
| 3307 | return ret; |
| 3308 | } |
| 3309 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3310 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3311 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3312 | * resume after balance was interrupted. |
| 3313 | */ |
| 3314 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3315 | { |
| 3316 | /* |
| 3317 | * Turn on soft mode for chunk types that were being converted. |
| 3318 | */ |
| 3319 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3320 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3321 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3322 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3323 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3324 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3325 | |
| 3326 | /* |
| 3327 | * Turn on usage filter if is not already used. The idea is |
| 3328 | * that chunks that we have already balanced should be |
| 3329 | * reasonably full. Don't do it for chunks that are being |
| 3330 | * converted - that will keep us from relocating unconverted |
| 3331 | * (albeit full) chunks. |
| 3332 | */ |
| 3333 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3334 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3335 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3336 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3337 | bctl->data.usage = 90; |
| 3338 | } |
| 3339 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3340 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3341 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3342 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3343 | bctl->sys.usage = 90; |
| 3344 | } |
| 3345 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3346 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3347 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3348 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3349 | bctl->meta.usage = 90; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3354 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3355 | */ |
| 3356 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3357 | { |
| 3358 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3359 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3360 | |
| 3361 | BUG_ON(!fs_info->balance_ctl); |
| 3362 | |
| 3363 | spin_lock(&fs_info->balance_lock); |
| 3364 | fs_info->balance_ctl = NULL; |
| 3365 | spin_unlock(&fs_info->balance_lock); |
| 3366 | |
| 3367 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3368 | ret = del_balance_item(fs_info); |
| 3369 | if (ret) |
| 3370 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3373 | /* |
| 3374 | * Balance filters. Return 1 if chunk should be filtered out |
| 3375 | * (should not be balanced). |
| 3376 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3377 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3378 | struct btrfs_balance_args *bargs) |
| 3379 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3380 | chunk_type = chunk_to_extended(chunk_type) & |
| 3381 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3382 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3383 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3384 | return 0; |
| 3385 | |
| 3386 | return 1; |
| 3387 | } |
| 3388 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3389 | 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] | 3390 | struct btrfs_balance_args *bargs) |
| 3391 | { |
| 3392 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3393 | u64 chunk_used; |
| 3394 | u64 user_thresh_min; |
| 3395 | u64 user_thresh_max; |
| 3396 | int ret = 1; |
| 3397 | |
| 3398 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3399 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3400 | |
| 3401 | if (bargs->usage_min == 0) |
| 3402 | user_thresh_min = 0; |
| 3403 | else |
| 3404 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3405 | bargs->usage_min); |
| 3406 | |
| 3407 | if (bargs->usage_max == 0) |
| 3408 | user_thresh_max = 1; |
| 3409 | else if (bargs->usage_max > 100) |
| 3410 | user_thresh_max = cache->key.offset; |
| 3411 | else |
| 3412 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3413 | bargs->usage_max); |
| 3414 | |
| 3415 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3416 | ret = 0; |
| 3417 | |
| 3418 | btrfs_put_block_group(cache); |
| 3419 | return ret; |
| 3420 | } |
| 3421 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3422 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3423 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3424 | { |
| 3425 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3426 | u64 chunk_used, user_thresh; |
| 3427 | int ret = 1; |
| 3428 | |
| 3429 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3430 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3431 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3432 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3433 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3434 | else if (bargs->usage > 100) |
| 3435 | user_thresh = cache->key.offset; |
| 3436 | else |
| 3437 | user_thresh = div_factor_fine(cache->key.offset, |
| 3438 | bargs->usage); |
| 3439 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3440 | if (chunk_used < user_thresh) |
| 3441 | ret = 0; |
| 3442 | |
| 3443 | btrfs_put_block_group(cache); |
| 3444 | return ret; |
| 3445 | } |
| 3446 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3447 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3448 | struct btrfs_chunk *chunk, |
| 3449 | struct btrfs_balance_args *bargs) |
| 3450 | { |
| 3451 | struct btrfs_stripe *stripe; |
| 3452 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3453 | int i; |
| 3454 | |
| 3455 | for (i = 0; i < num_stripes; i++) { |
| 3456 | stripe = btrfs_stripe_nr(chunk, i); |
| 3457 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3458 | return 0; |
| 3459 | } |
| 3460 | |
| 3461 | return 1; |
| 3462 | } |
| 3463 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3464 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3465 | { |
| 3466 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3467 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3468 | const int nparity = btrfs_raid_array[index].nparity; |
| 3469 | |
| 3470 | if (nparity) |
| 3471 | return num_stripes - nparity; |
| 3472 | else |
| 3473 | return num_stripes / ncopies; |
| 3474 | } |
| 3475 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3476 | /* [pstart, pend) */ |
| 3477 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3478 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3479 | struct btrfs_balance_args *bargs) |
| 3480 | { |
| 3481 | struct btrfs_stripe *stripe; |
| 3482 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3483 | u64 stripe_offset; |
| 3484 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3485 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3486 | int factor; |
| 3487 | int i; |
| 3488 | |
| 3489 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3490 | return 0; |
| 3491 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3492 | type = btrfs_chunk_type(leaf, chunk); |
| 3493 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3494 | |
| 3495 | for (i = 0; i < num_stripes; i++) { |
| 3496 | stripe = btrfs_stripe_nr(chunk, i); |
| 3497 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3498 | continue; |
| 3499 | |
| 3500 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3501 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3502 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3503 | |
| 3504 | if (stripe_offset < bargs->pend && |
| 3505 | stripe_offset + stripe_length > bargs->pstart) |
| 3506 | return 0; |
| 3507 | } |
| 3508 | |
| 3509 | return 1; |
| 3510 | } |
| 3511 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3512 | /* [vstart, vend) */ |
| 3513 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3514 | struct btrfs_chunk *chunk, |
| 3515 | u64 chunk_offset, |
| 3516 | struct btrfs_balance_args *bargs) |
| 3517 | { |
| 3518 | if (chunk_offset < bargs->vend && |
| 3519 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3520 | /* at least part of the chunk is inside this vrange */ |
| 3521 | return 0; |
| 3522 | |
| 3523 | return 1; |
| 3524 | } |
| 3525 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3526 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3527 | struct btrfs_chunk *chunk, |
| 3528 | struct btrfs_balance_args *bargs) |
| 3529 | { |
| 3530 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3531 | |
| 3532 | if (bargs->stripes_min <= num_stripes |
| 3533 | && num_stripes <= bargs->stripes_max) |
| 3534 | return 0; |
| 3535 | |
| 3536 | return 1; |
| 3537 | } |
| 3538 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3539 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3540 | struct btrfs_balance_args *bargs) |
| 3541 | { |
| 3542 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3543 | return 0; |
| 3544 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3545 | chunk_type = chunk_to_extended(chunk_type) & |
| 3546 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3547 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3548 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3549 | return 1; |
| 3550 | |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3554 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3555 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3556 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3557 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3558 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3559 | struct btrfs_balance_args *bargs = NULL; |
| 3560 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3561 | |
| 3562 | /* type filter */ |
| 3563 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3564 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
| 3568 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3569 | bargs = &bctl->data; |
| 3570 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3571 | bargs = &bctl->sys; |
| 3572 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3573 | bargs = &bctl->meta; |
| 3574 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3575 | /* profiles filter */ |
| 3576 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3577 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3578 | return 0; |
| 3579 | } |
| 3580 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3581 | /* usage filter */ |
| 3582 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3583 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3584 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3585 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3586 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3587 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3588 | } |
| 3589 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3590 | /* devid filter */ |
| 3591 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3592 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3593 | return 0; |
| 3594 | } |
| 3595 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3596 | /* drange filter, makes sense only with devid filter */ |
| 3597 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3598 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3599 | return 0; |
| 3600 | } |
| 3601 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3602 | /* vrange filter */ |
| 3603 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3604 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3608 | /* stripes filter */ |
| 3609 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3610 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3614 | /* soft profile changing mode */ |
| 3615 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3616 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3617 | return 0; |
| 3618 | } |
| 3619 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3620 | /* |
| 3621 | * limited by count, must be the last filter |
| 3622 | */ |
| 3623 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3624 | if (bargs->limit == 0) |
| 3625 | return 0; |
| 3626 | else |
| 3627 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3628 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3629 | /* |
| 3630 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3631 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3632 | * about the count of all chunks that satisfy the filters. |
| 3633 | */ |
| 3634 | if (bargs->limit_max == 0) |
| 3635 | return 0; |
| 3636 | else |
| 3637 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3638 | } |
| 3639 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3640 | return 1; |
| 3641 | } |
| 3642 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3643 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3644 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3645 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3646 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3647 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3648 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3649 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3650 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3651 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3652 | struct extent_buffer *leaf; |
| 3653 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3654 | int ret; |
| 3655 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3656 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3657 | /* 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] | 3658 | u64 limit_data = bctl->data.limit; |
| 3659 | u64 limit_meta = bctl->meta.limit; |
| 3660 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3661 | u32 count_data = 0; |
| 3662 | u32 count_meta = 0; |
| 3663 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3664 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3665 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3666 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3667 | if (!path) { |
| 3668 | ret = -ENOMEM; |
| 3669 | goto error; |
| 3670 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3671 | |
| 3672 | /* zero out stat counters */ |
| 3673 | spin_lock(&fs_info->balance_lock); |
| 3674 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3675 | spin_unlock(&fs_info->balance_lock); |
| 3676 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3677 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3678 | /* |
| 3679 | * The single value limit and min/max limits use the same bytes |
| 3680 | * in the |
| 3681 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3682 | bctl->data.limit = limit_data; |
| 3683 | bctl->meta.limit = limit_meta; |
| 3684 | bctl->sys.limit = limit_sys; |
| 3685 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3686 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3687 | key.offset = (u64)-1; |
| 3688 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3689 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3690 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3691 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3692 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3693 | ret = -ECANCELED; |
| 3694 | goto error; |
| 3695 | } |
| 3696 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3697 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3698 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3699 | if (ret < 0) { |
| 3700 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3701 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3702 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3703 | |
| 3704 | /* |
| 3705 | * this shouldn't happen, it means the last relocate |
| 3706 | * failed |
| 3707 | */ |
| 3708 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3709 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3710 | |
| 3711 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3712 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3713 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3714 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3715 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3716 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3717 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3718 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3719 | leaf = path->nodes[0]; |
| 3720 | slot = path->slots[0]; |
| 3721 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3722 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3723 | if (found_key.objectid != key.objectid) { |
| 3724 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3725 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3726 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3727 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3728 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3729 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3730 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3731 | if (!counting) { |
| 3732 | spin_lock(&fs_info->balance_lock); |
| 3733 | bctl->stat.considered++; |
| 3734 | spin_unlock(&fs_info->balance_lock); |
| 3735 | } |
| 3736 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3737 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3738 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3739 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3740 | if (!ret) { |
| 3741 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3742 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3743 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3744 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3745 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3746 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3747 | spin_lock(&fs_info->balance_lock); |
| 3748 | bctl->stat.expected++; |
| 3749 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3750 | |
| 3751 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3752 | count_data++; |
| 3753 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3754 | count_sys++; |
| 3755 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3756 | count_meta++; |
| 3757 | |
| 3758 | goto loop; |
| 3759 | } |
| 3760 | |
| 3761 | /* |
| 3762 | * Apply limit_min filter, no need to check if the LIMITS |
| 3763 | * filter is used, limit_min is 0 by default |
| 3764 | */ |
| 3765 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3766 | count_data < bctl->data.limit_min) |
| 3767 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3768 | count_meta < bctl->meta.limit_min) |
| 3769 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3770 | count_sys < bctl->sys.limit_min)) { |
| 3771 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3772 | goto loop; |
| 3773 | } |
| 3774 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3775 | if (!chunk_reserved) { |
| 3776 | /* |
| 3777 | * We may be relocating the only data chunk we have, |
| 3778 | * which could potentially end up with losing data's |
| 3779 | * raid profile, so lets allocate an empty one in |
| 3780 | * advance. |
| 3781 | */ |
| 3782 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3783 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3784 | if (ret < 0) { |
| 3785 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3786 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3787 | } else if (ret == 1) { |
| 3788 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3789 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3790 | } |
| 3791 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3792 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3793 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3794 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3795 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3796 | } else if (ret == -ETXTBSY) { |
| 3797 | btrfs_info(fs_info, |
| 3798 | "skipping relocation of block group %llu due to active swapfile", |
| 3799 | found_key.offset); |
| 3800 | ret = 0; |
| 3801 | } else if (ret) { |
| 3802 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3803 | } else { |
| 3804 | spin_lock(&fs_info->balance_lock); |
| 3805 | bctl->stat.completed++; |
| 3806 | spin_unlock(&fs_info->balance_lock); |
| 3807 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3808 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3809 | if (found_key.offset == 0) |
| 3810 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3811 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3812 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3813 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3814 | if (counting) { |
| 3815 | btrfs_release_path(path); |
| 3816 | counting = false; |
| 3817 | goto again; |
| 3818 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3819 | error: |
| 3820 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3821 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3822 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3823 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3824 | if (!ret) |
| 3825 | ret = -ENOSPC; |
| 3826 | } |
| 3827 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3828 | return ret; |
| 3829 | } |
| 3830 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3831 | /** |
| 3832 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3833 | * @flags: profile to validate |
| 3834 | * @extended: if true @flags is treated as an extended profile |
| 3835 | */ |
| 3836 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3837 | { |
| 3838 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3839 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3840 | |
| 3841 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3842 | |
| 3843 | /* 1) check that all other bits are zeroed */ |
| 3844 | if (flags & ~mask) |
| 3845 | return 0; |
| 3846 | |
| 3847 | /* 2) see if profile is reduced */ |
| 3848 | if (flags == 0) |
| 3849 | return !extended; /* "0" is valid for usual profiles */ |
| 3850 | |
David Sterba | c149916 | 2019-10-01 19:44:42 +0200 | [diff] [blame] | 3851 | return has_single_bit_set(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3852 | } |
| 3853 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3854 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3855 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3856 | /* cancel requested || normal exit path */ |
| 3857 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3858 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3859 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3862 | /* Non-zero return value signifies invalidity */ |
| 3863 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3864 | u64 allowed) |
| 3865 | { |
| 3866 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3867 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3868 | (bctl_arg->target & ~allowed))); |
| 3869 | } |
| 3870 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3871 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3872 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3873 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3874 | * does not fit into the provided buffer. |
| 3875 | */ |
| 3876 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3877 | u32 size_buf) |
| 3878 | { |
| 3879 | int ret; |
| 3880 | u32 size_bp = size_buf; |
| 3881 | char *bp = buf; |
| 3882 | u64 flags = bargs->flags; |
| 3883 | char tmp_buf[128] = {'\0'}; |
| 3884 | |
| 3885 | if (!flags) |
| 3886 | return; |
| 3887 | |
| 3888 | #define CHECK_APPEND_NOARG(a) \ |
| 3889 | do { \ |
| 3890 | ret = snprintf(bp, size_bp, (a)); \ |
| 3891 | if (ret < 0 || ret >= size_bp) \ |
| 3892 | goto out_overflow; \ |
| 3893 | size_bp -= ret; \ |
| 3894 | bp += ret; \ |
| 3895 | } while (0) |
| 3896 | |
| 3897 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3898 | do { \ |
| 3899 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3900 | if (ret < 0 || ret >= size_bp) \ |
| 3901 | goto out_overflow; \ |
| 3902 | size_bp -= ret; \ |
| 3903 | bp += ret; \ |
| 3904 | } while (0) |
| 3905 | |
| 3906 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3907 | do { \ |
| 3908 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3909 | if (ret < 0 || ret >= size_bp) \ |
| 3910 | goto out_overflow; \ |
| 3911 | size_bp -= ret; \ |
| 3912 | bp += ret; \ |
| 3913 | } while (0) |
| 3914 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3915 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3916 | CHECK_APPEND_1ARG("convert=%s,", |
| 3917 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3918 | |
| 3919 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3920 | CHECK_APPEND_NOARG("soft,"); |
| 3921 | |
| 3922 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3923 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3924 | sizeof(tmp_buf)); |
| 3925 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3926 | } |
| 3927 | |
| 3928 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3929 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3930 | |
| 3931 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3932 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3933 | bargs->usage_min, bargs->usage_max); |
| 3934 | |
| 3935 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3936 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3937 | |
| 3938 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3939 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3940 | bargs->pstart, bargs->pend); |
| 3941 | |
| 3942 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3943 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3944 | bargs->vstart, bargs->vend); |
| 3945 | |
| 3946 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3947 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3948 | |
| 3949 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3950 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3951 | bargs->limit_min, bargs->limit_max); |
| 3952 | |
| 3953 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3954 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3955 | bargs->stripes_min, bargs->stripes_max); |
| 3956 | |
| 3957 | #undef CHECK_APPEND_2ARG |
| 3958 | #undef CHECK_APPEND_1ARG |
| 3959 | #undef CHECK_APPEND_NOARG |
| 3960 | |
| 3961 | out_overflow: |
| 3962 | |
| 3963 | if (size_bp < size_buf) |
| 3964 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3965 | else |
| 3966 | buf[0] = '\0'; |
| 3967 | } |
| 3968 | |
| 3969 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3970 | { |
| 3971 | u32 size_buf = 1024; |
| 3972 | char tmp_buf[192] = {'\0'}; |
| 3973 | char *buf; |
| 3974 | char *bp; |
| 3975 | u32 size_bp = size_buf; |
| 3976 | int ret; |
| 3977 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3978 | |
| 3979 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3980 | if (!buf) |
| 3981 | return; |
| 3982 | |
| 3983 | bp = buf; |
| 3984 | |
| 3985 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3986 | do { \ |
| 3987 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3988 | if (ret < 0 || ret >= size_bp) \ |
| 3989 | goto out_overflow; \ |
| 3990 | size_bp -= ret; \ |
| 3991 | bp += ret; \ |
| 3992 | } while (0) |
| 3993 | |
| 3994 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 3995 | CHECK_APPEND_1ARG("%s", "-f "); |
| 3996 | |
| 3997 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 3998 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 3999 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 4000 | } |
| 4001 | |
| 4002 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 4003 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 4004 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 4005 | } |
| 4006 | |
| 4007 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 4008 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 4009 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 4010 | } |
| 4011 | |
| 4012 | #undef CHECK_APPEND_1ARG |
| 4013 | |
| 4014 | out_overflow: |
| 4015 | |
| 4016 | if (size_bp < size_buf) |
| 4017 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 4018 | btrfs_info(fs_info, "balance: %s %s", |
| 4019 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 4020 | "resume" : "start", buf); |
| 4021 | |
| 4022 | kfree(buf); |
| 4023 | } |
| 4024 | |
| 4025 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4026 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4027 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4028 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 4029 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4030 | struct btrfs_ioctl_balance_args *bargs) |
| 4031 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4032 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4033 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4034 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4035 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 4036 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4037 | unsigned seq; |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4038 | bool reducing_redundancy; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4039 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4040 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4041 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4042 | atomic_read(&fs_info->balance_pause_req) || |
| 4043 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4044 | ret = -EINVAL; |
| 4045 | goto out; |
| 4046 | } |
| 4047 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4048 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 4049 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 4050 | mixed = 1; |
| 4051 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4052 | /* |
| 4053 | * In case of mixed groups both data and meta should be picked, |
| 4054 | * and identical options should be given for both of them. |
| 4055 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4056 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 4057 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4058 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4059 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4060 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4061 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4062 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4063 | ret = -EINVAL; |
| 4064 | goto out; |
| 4065 | } |
| 4066 | } |
| 4067 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4068 | num_devices = btrfs_num_devices(fs_info); |
Qu Wenruo | fab2735 | 2019-09-25 10:13:27 +0800 | [diff] [blame] | 4069 | |
| 4070 | /* |
| 4071 | * SINGLE profile on-disk has no profile bit, but in-memory we have a |
| 4072 | * special bit for it, to make it easier to distinguish. Thus we need |
| 4073 | * to set it manually, or balance would refuse the profile. |
| 4074 | */ |
| 4075 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4076 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 4077 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 4078 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4079 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4080 | if (validate_convert_profile(&bctl->data, allowed)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4081 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4082 | "balance: invalid convert data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4083 | btrfs_bg_type_to_raid_name(bctl->data.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4084 | ret = -EINVAL; |
| 4085 | goto out; |
| 4086 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4087 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4088 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4089 | "balance: invalid convert metadata profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4090 | btrfs_bg_type_to_raid_name(bctl->meta.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4091 | ret = -EINVAL; |
| 4092 | goto out; |
| 4093 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4094 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4095 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4096 | "balance: invalid convert system profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4097 | btrfs_bg_type_to_raid_name(bctl->sys.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4098 | ret = -EINVAL; |
| 4099 | goto out; |
| 4100 | } |
| 4101 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 4102 | /* |
| 4103 | * Allow to reduce metadata or system integrity only if force set for |
| 4104 | * profiles with redundancy (copies, parity) |
| 4105 | */ |
| 4106 | allowed = 0; |
| 4107 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4108 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4109 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4110 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4111 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4112 | do { |
| 4113 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4114 | |
| 4115 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4116 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4117 | !(bctl->sys.target & allowed)) || |
| 4118 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4119 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4120 | !(bctl->meta.target & allowed))) |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4121 | reducing_redundancy = true; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4122 | else |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4123 | reducing_redundancy = false; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4124 | |
| 4125 | /* if we're not converting, the target field is uninitialized */ |
| 4126 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4127 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4128 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4129 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4130 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4131 | |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4132 | if (reducing_redundancy) { |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4133 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4134 | btrfs_info(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4135 | "balance: force reducing metadata redundancy"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4136 | } else { |
| 4137 | btrfs_err(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4138 | "balance: reduces metadata redundancy, use --force if you want this"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4139 | ret = -EINVAL; |
| 4140 | goto out; |
| 4141 | } |
| 4142 | } |
| 4143 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4144 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4145 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4146 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4147 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4148 | btrfs_bg_type_to_raid_name(meta_target), |
| 4149 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 4152 | if (fs_info->send_in_progress) { |
| 4153 | btrfs_warn_rl(fs_info, |
| 4154 | "cannot run balance while send operations are in progress (%d in progress)", |
| 4155 | fs_info->send_in_progress); |
| 4156 | ret = -EAGAIN; |
| 4157 | goto out; |
| 4158 | } |
| 4159 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4160 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4161 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4162 | goto out; |
| 4163 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4164 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4165 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4166 | BUG_ON(fs_info->balance_ctl); |
| 4167 | spin_lock(&fs_info->balance_lock); |
| 4168 | fs_info->balance_ctl = bctl; |
| 4169 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4170 | } else { |
| 4171 | BUG_ON(ret != -EEXIST); |
| 4172 | spin_lock(&fs_info->balance_lock); |
| 4173 | update_balance_args(bctl); |
| 4174 | spin_unlock(&fs_info->balance_lock); |
| 4175 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4176 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4177 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4178 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4179 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4180 | mutex_unlock(&fs_info->balance_mutex); |
| 4181 | |
| 4182 | ret = __btrfs_balance(fs_info); |
| 4183 | |
| 4184 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4185 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4186 | btrfs_info(fs_info, "balance: paused"); |
| 4187 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4188 | btrfs_info(fs_info, "balance: canceled"); |
| 4189 | else |
| 4190 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4191 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4192 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4193 | |
| 4194 | if (bargs) { |
| 4195 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4196 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4197 | } |
| 4198 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4199 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4200 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4201 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4202 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4205 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4206 | |
| 4207 | return ret; |
| 4208 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4209 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4210 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4211 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4212 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4213 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4214 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4215 | return ret; |
| 4216 | } |
| 4217 | |
| 4218 | static int balance_kthread(void *data) |
| 4219 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4220 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4221 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4222 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4223 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4224 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4225 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4226 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4227 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4228 | return ret; |
| 4229 | } |
| 4230 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4231 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4232 | { |
| 4233 | struct task_struct *tsk; |
| 4234 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4235 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4236 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4237 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4238 | return 0; |
| 4239 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4240 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4241 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4242 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4243 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4244 | return 0; |
| 4245 | } |
| 4246 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4247 | /* |
| 4248 | * A ro->rw remount sequence should continue with the paused balance |
| 4249 | * regardless of who pauses it, system or the user as of now, so set |
| 4250 | * the resume flag. |
| 4251 | */ |
| 4252 | spin_lock(&fs_info->balance_lock); |
| 4253 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4254 | spin_unlock(&fs_info->balance_lock); |
| 4255 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4256 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4257 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4258 | } |
| 4259 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4260 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4261 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4262 | struct btrfs_balance_control *bctl; |
| 4263 | struct btrfs_balance_item *item; |
| 4264 | struct btrfs_disk_balance_args disk_bargs; |
| 4265 | struct btrfs_path *path; |
| 4266 | struct extent_buffer *leaf; |
| 4267 | struct btrfs_key key; |
| 4268 | int ret; |
| 4269 | |
| 4270 | path = btrfs_alloc_path(); |
| 4271 | if (!path) |
| 4272 | return -ENOMEM; |
| 4273 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4274 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4275 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4276 | key.offset = 0; |
| 4277 | |
| 4278 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4279 | if (ret < 0) |
| 4280 | goto out; |
| 4281 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4282 | ret = 0; |
| 4283 | goto out; |
| 4284 | } |
| 4285 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4286 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4287 | if (!bctl) { |
| 4288 | ret = -ENOMEM; |
| 4289 | goto out; |
| 4290 | } |
| 4291 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4292 | leaf = path->nodes[0]; |
| 4293 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4294 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4295 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4296 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4297 | |
| 4298 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4299 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4300 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4301 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4302 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4303 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4304 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4305 | /* |
| 4306 | * This should never happen, as the paused balance state is recovered |
| 4307 | * during mount without any chance of other exclusive ops to collide. |
| 4308 | * |
| 4309 | * This gives the exclusive op status to balance and keeps in paused |
| 4310 | * state until user intervention (cancel or umount). If the ownership |
| 4311 | * cannot be assigned, show a message but do not fail. The balance |
| 4312 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4313 | * set up. |
| 4314 | */ |
| 4315 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4316 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4317 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4318 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4319 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4320 | BUG_ON(fs_info->balance_ctl); |
| 4321 | spin_lock(&fs_info->balance_lock); |
| 4322 | fs_info->balance_ctl = bctl; |
| 4323 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4324 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4325 | out: |
| 4326 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4327 | return ret; |
| 4328 | } |
| 4329 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4330 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4331 | { |
| 4332 | int ret = 0; |
| 4333 | |
| 4334 | mutex_lock(&fs_info->balance_mutex); |
| 4335 | if (!fs_info->balance_ctl) { |
| 4336 | mutex_unlock(&fs_info->balance_mutex); |
| 4337 | return -ENOTCONN; |
| 4338 | } |
| 4339 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4340 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4341 | atomic_inc(&fs_info->balance_pause_req); |
| 4342 | mutex_unlock(&fs_info->balance_mutex); |
| 4343 | |
| 4344 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4345 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4346 | |
| 4347 | mutex_lock(&fs_info->balance_mutex); |
| 4348 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4349 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4350 | atomic_dec(&fs_info->balance_pause_req); |
| 4351 | } else { |
| 4352 | ret = -ENOTCONN; |
| 4353 | } |
| 4354 | |
| 4355 | mutex_unlock(&fs_info->balance_mutex); |
| 4356 | return ret; |
| 4357 | } |
| 4358 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4359 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4360 | { |
| 4361 | mutex_lock(&fs_info->balance_mutex); |
| 4362 | if (!fs_info->balance_ctl) { |
| 4363 | mutex_unlock(&fs_info->balance_mutex); |
| 4364 | return -ENOTCONN; |
| 4365 | } |
| 4366 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4367 | /* |
| 4368 | * A paused balance with the item stored on disk can be resumed at |
| 4369 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4370 | * and we must not allow cancelling as it deletes the item. |
| 4371 | */ |
| 4372 | if (sb_rdonly(fs_info->sb)) { |
| 4373 | mutex_unlock(&fs_info->balance_mutex); |
| 4374 | return -EROFS; |
| 4375 | } |
| 4376 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4377 | atomic_inc(&fs_info->balance_cancel_req); |
| 4378 | /* |
| 4379 | * if we are running just wait and return, balance item is |
| 4380 | * deleted in btrfs_balance in this case |
| 4381 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4382 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4383 | mutex_unlock(&fs_info->balance_mutex); |
| 4384 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4385 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4386 | mutex_lock(&fs_info->balance_mutex); |
| 4387 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4388 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4389 | /* |
| 4390 | * Lock released to allow other waiters to continue, we'll |
| 4391 | * reexamine the status again. |
| 4392 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4393 | mutex_lock(&fs_info->balance_mutex); |
| 4394 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4395 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4396 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4397 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4398 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4399 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4402 | BUG_ON(fs_info->balance_ctl || |
| 4403 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4404 | atomic_dec(&fs_info->balance_cancel_req); |
| 4405 | mutex_unlock(&fs_info->balance_mutex); |
| 4406 | return 0; |
| 4407 | } |
| 4408 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4409 | static int btrfs_uuid_scan_kthread(void *data) |
| 4410 | { |
| 4411 | struct btrfs_fs_info *fs_info = data; |
| 4412 | struct btrfs_root *root = fs_info->tree_root; |
| 4413 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4414 | struct btrfs_path *path = NULL; |
| 4415 | int ret = 0; |
| 4416 | struct extent_buffer *eb; |
| 4417 | int slot; |
| 4418 | struct btrfs_root_item root_item; |
| 4419 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4420 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4421 | |
| 4422 | path = btrfs_alloc_path(); |
| 4423 | if (!path) { |
| 4424 | ret = -ENOMEM; |
| 4425 | goto out; |
| 4426 | } |
| 4427 | |
| 4428 | key.objectid = 0; |
| 4429 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4430 | key.offset = 0; |
| 4431 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4432 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4433 | ret = btrfs_search_forward(root, &key, path, |
| 4434 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4435 | if (ret) { |
| 4436 | if (ret > 0) |
| 4437 | ret = 0; |
| 4438 | break; |
| 4439 | } |
| 4440 | |
| 4441 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4442 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4443 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4444 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4445 | goto skip; |
| 4446 | |
| 4447 | eb = path->nodes[0]; |
| 4448 | slot = path->slots[0]; |
| 4449 | item_size = btrfs_item_size_nr(eb, slot); |
| 4450 | if (item_size < sizeof(root_item)) |
| 4451 | goto skip; |
| 4452 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4453 | read_extent_buffer(eb, &root_item, |
| 4454 | btrfs_item_ptr_offset(eb, slot), |
| 4455 | (int)sizeof(root_item)); |
| 4456 | if (btrfs_root_refs(&root_item) == 0) |
| 4457 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4458 | |
| 4459 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4460 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4461 | if (trans) |
| 4462 | goto update_tree; |
| 4463 | |
| 4464 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4465 | /* |
| 4466 | * 1 - subvol uuid item |
| 4467 | * 1 - received_subvol uuid item |
| 4468 | */ |
| 4469 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4470 | if (IS_ERR(trans)) { |
| 4471 | ret = PTR_ERR(trans); |
| 4472 | break; |
| 4473 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4474 | continue; |
| 4475 | } else { |
| 4476 | goto skip; |
| 4477 | } |
| 4478 | update_tree: |
| 4479 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4480 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4481 | BTRFS_UUID_KEY_SUBVOL, |
| 4482 | key.objectid); |
| 4483 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4484 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4485 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4486 | break; |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4491 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4492 | root_item.received_uuid, |
| 4493 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4494 | key.objectid); |
| 4495 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4496 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4497 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4498 | break; |
| 4499 | } |
| 4500 | } |
| 4501 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4502 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4503 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4504 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4505 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4506 | if (ret) |
| 4507 | break; |
| 4508 | } |
| 4509 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4510 | btrfs_release_path(path); |
| 4511 | if (key.offset < (u64)-1) { |
| 4512 | key.offset++; |
| 4513 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4514 | key.offset = 0; |
| 4515 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4516 | } else if (key.objectid < (u64)-1) { |
| 4517 | key.offset = 0; |
| 4518 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4519 | key.objectid++; |
| 4520 | } else { |
| 4521 | break; |
| 4522 | } |
| 4523 | cond_resched(); |
| 4524 | } |
| 4525 | |
| 4526 | out: |
| 4527 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4528 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4529 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4530 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4531 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4532 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4533 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4534 | up(&fs_info->uuid_tree_rescan_sem); |
| 4535 | return 0; |
| 4536 | } |
| 4537 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4538 | /* |
| 4539 | * Callback for btrfs_uuid_tree_iterate(). |
| 4540 | * returns: |
| 4541 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4542 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4543 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4544 | */ |
| 4545 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4546 | u8 *uuid, u8 type, u64 subid) |
| 4547 | { |
| 4548 | struct btrfs_key key; |
| 4549 | int ret = 0; |
| 4550 | struct btrfs_root *subvol_root; |
| 4551 | |
| 4552 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4553 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4554 | goto out; |
| 4555 | |
| 4556 | key.objectid = subid; |
| 4557 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4558 | key.offset = (u64)-1; |
| 4559 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4560 | if (IS_ERR(subvol_root)) { |
| 4561 | ret = PTR_ERR(subvol_root); |
| 4562 | if (ret == -ENOENT) |
| 4563 | ret = 1; |
| 4564 | goto out; |
| 4565 | } |
| 4566 | |
| 4567 | switch (type) { |
| 4568 | case BTRFS_UUID_KEY_SUBVOL: |
| 4569 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4570 | ret = 1; |
| 4571 | break; |
| 4572 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4573 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4574 | BTRFS_UUID_SIZE)) |
| 4575 | ret = 1; |
| 4576 | break; |
| 4577 | } |
| 4578 | |
| 4579 | out: |
| 4580 | return ret; |
| 4581 | } |
| 4582 | |
| 4583 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4584 | { |
| 4585 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4586 | int ret; |
| 4587 | |
| 4588 | /* |
| 4589 | * 1st step is to iterate through the existing UUID tree and |
| 4590 | * to delete all entries that contain outdated data. |
| 4591 | * 2nd step is to add all missing entries to the UUID tree. |
| 4592 | */ |
| 4593 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4594 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4595 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4596 | up(&fs_info->uuid_tree_rescan_sem); |
| 4597 | return ret; |
| 4598 | } |
| 4599 | return btrfs_uuid_scan_kthread(data); |
| 4600 | } |
| 4601 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4602 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4603 | { |
| 4604 | struct btrfs_trans_handle *trans; |
| 4605 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4606 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4607 | struct task_struct *task; |
| 4608 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4609 | |
| 4610 | /* |
| 4611 | * 1 - root node |
| 4612 | * 1 - root item |
| 4613 | */ |
| 4614 | trans = btrfs_start_transaction(tree_root, 2); |
| 4615 | if (IS_ERR(trans)) |
| 4616 | return PTR_ERR(trans); |
| 4617 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4618 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4619 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4620 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4621 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4622 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4623 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | fs_info->uuid_root = uuid_root; |
| 4627 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4628 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4629 | if (ret) |
| 4630 | return ret; |
| 4631 | |
| 4632 | down(&fs_info->uuid_tree_rescan_sem); |
| 4633 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4634 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4635 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4636 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4637 | up(&fs_info->uuid_tree_rescan_sem); |
| 4638 | return PTR_ERR(task); |
| 4639 | } |
| 4640 | |
| 4641 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4642 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4643 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4644 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4645 | { |
| 4646 | struct task_struct *task; |
| 4647 | |
| 4648 | down(&fs_info->uuid_tree_rescan_sem); |
| 4649 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4650 | if (IS_ERR(task)) { |
| 4651 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4652 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4653 | up(&fs_info->uuid_tree_rescan_sem); |
| 4654 | return PTR_ERR(task); |
| 4655 | } |
| 4656 | |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4660 | /* |
| 4661 | * shrinking a device means finding all of the device extents past |
| 4662 | * the new size, and then following the back refs to the chunks. |
| 4663 | * The chunk relocation code actually frees the device extent |
| 4664 | */ |
| 4665 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4666 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4667 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4668 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4669 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4670 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4671 | struct btrfs_path *path; |
| 4672 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4673 | u64 chunk_offset; |
| 4674 | int ret; |
| 4675 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4676 | int failed = 0; |
| 4677 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4678 | struct extent_buffer *l; |
| 4679 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4680 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4681 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4682 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4683 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4684 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4685 | |
| 4686 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4687 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4688 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4689 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4690 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4691 | return -EINVAL; |
| 4692 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4693 | path = btrfs_alloc_path(); |
| 4694 | if (!path) |
| 4695 | return -ENOMEM; |
| 4696 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4697 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4698 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4699 | trans = btrfs_start_transaction(root, 0); |
| 4700 | if (IS_ERR(trans)) { |
| 4701 | btrfs_free_path(path); |
| 4702 | return PTR_ERR(trans); |
| 4703 | } |
| 4704 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4705 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4706 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4707 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4708 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4709 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4710 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4711 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4712 | |
| 4713 | /* |
| 4714 | * Once the device's size has been set to the new size, ensure all |
| 4715 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4716 | * and relocates them accordingly. |
| 4717 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4718 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4719 | mutex_unlock(&fs_info->chunk_mutex); |
| 4720 | ret = btrfs_commit_transaction(trans); |
| 4721 | if (ret) |
| 4722 | goto done; |
| 4723 | } else { |
| 4724 | mutex_unlock(&fs_info->chunk_mutex); |
| 4725 | btrfs_end_transaction(trans); |
| 4726 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4727 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4728 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4729 | key.objectid = device->devid; |
| 4730 | key.offset = (u64)-1; |
| 4731 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4732 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4733 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4734 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4735 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4736 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4737 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4738 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4739 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4740 | |
| 4741 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4742 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4743 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4744 | if (ret < 0) |
| 4745 | goto done; |
| 4746 | if (ret) { |
| 4747 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4748 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4749 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | l = path->nodes[0]; |
| 4753 | slot = path->slots[0]; |
| 4754 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4755 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4756 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4757 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4758 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4759 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4760 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4761 | |
| 4762 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4763 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4764 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4765 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4766 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4767 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4768 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4769 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4770 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4771 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4772 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4773 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4774 | /* |
| 4775 | * We may be relocating the only data chunk we have, |
| 4776 | * which could potentially end up with losing data's |
| 4777 | * raid profile, so lets allocate an empty one in |
| 4778 | * advance. |
| 4779 | */ |
| 4780 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4781 | if (ret < 0) { |
| 4782 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4783 | goto done; |
| 4784 | } |
| 4785 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4786 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4787 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4788 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4789 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4790 | } else if (ret) { |
| 4791 | if (ret == -ETXTBSY) { |
| 4792 | btrfs_warn(fs_info, |
| 4793 | "could not shrink block group %llu due to active swapfile", |
| 4794 | chunk_offset); |
| 4795 | } |
| 4796 | goto done; |
| 4797 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4798 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4799 | |
| 4800 | if (failed && !retried) { |
| 4801 | failed = 0; |
| 4802 | retried = true; |
| 4803 | goto again; |
| 4804 | } else if (failed && retried) { |
| 4805 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4806 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4807 | } |
| 4808 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4809 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4810 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4811 | if (IS_ERR(trans)) { |
| 4812 | ret = PTR_ERR(trans); |
| 4813 | goto done; |
| 4814 | } |
| 4815 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4816 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4817 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4818 | if (list_empty(&device->post_commit_list)) |
| 4819 | list_add_tail(&device->post_commit_list, |
| 4820 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4821 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4822 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4823 | btrfs_set_super_total_bytes(super_copy, |
| 4824 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4825 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4826 | |
| 4827 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4828 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4829 | if (ret < 0) { |
| 4830 | btrfs_abort_transaction(trans, ret); |
| 4831 | btrfs_end_transaction(trans); |
| 4832 | } else { |
| 4833 | ret = btrfs_commit_transaction(trans); |
| 4834 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4835 | done: |
| 4836 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4837 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4838 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4839 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4840 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4841 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4842 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4843 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4844 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4845 | return ret; |
| 4846 | } |
| 4847 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4848 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4849 | struct btrfs_key *key, |
| 4850 | struct btrfs_chunk *chunk, int item_size) |
| 4851 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4852 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4853 | struct btrfs_disk_key disk_key; |
| 4854 | u32 array_size; |
| 4855 | u8 *ptr; |
| 4856 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4857 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4858 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4859 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4860 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4861 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4862 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4863 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4864 | |
| 4865 | ptr = super_copy->sys_chunk_array + array_size; |
| 4866 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4867 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4868 | ptr += sizeof(disk_key); |
| 4869 | memcpy(ptr, chunk, item_size); |
| 4870 | item_size += sizeof(disk_key); |
| 4871 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4872 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4873 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4874 | return 0; |
| 4875 | } |
| 4876 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4877 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4878 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4879 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4880 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4881 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4882 | const struct btrfs_device_info *di_a = a; |
| 4883 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4884 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4885 | if (di_a->max_avail > di_b->max_avail) |
| 4886 | return -1; |
| 4887 | if (di_a->max_avail < di_b->max_avail) |
| 4888 | return 1; |
| 4889 | if (di_a->total_avail > di_b->total_avail) |
| 4890 | return -1; |
| 4891 | if (di_a->total_avail < di_b->total_avail) |
| 4892 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4893 | return 0; |
| 4894 | } |
| 4895 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4896 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4897 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4898 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4899 | return; |
| 4900 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4901 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4902 | } |
| 4903 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4904 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4905 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4906 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4907 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4908 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4909 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4910 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4911 | struct extent_map_tree *em_tree; |
| 4912 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4913 | struct btrfs_device_info *devices_info = NULL; |
| 4914 | u64 total_avail; |
| 4915 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4916 | int data_stripes; /* number of stripes that count for |
| 4917 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4918 | int sub_stripes; /* sub_stripes info for map */ |
| 4919 | int dev_stripes; /* stripes per dev */ |
| 4920 | int devs_max; /* max devs to use */ |
| 4921 | int devs_min; /* min devs needed */ |
| 4922 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4923 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4924 | int nparity; /* number of stripes worth of bytes to |
| 4925 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4926 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4927 | u64 max_stripe_size; |
| 4928 | u64 max_chunk_size; |
| 4929 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4930 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4931 | int ndevs; |
| 4932 | int i; |
| 4933 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4934 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4935 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4936 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4937 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4938 | if (list_empty(&fs_devices->alloc_list)) { |
| 4939 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4940 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4941 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4942 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4943 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4944 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4945 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4946 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4947 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4948 | devs_max = btrfs_raid_array[index].devs_max; |
David Sterba | e3ecdb3 | 2019-05-17 11:43:38 +0200 | [diff] [blame] | 4949 | if (!devs_max) |
| 4950 | devs_max = BTRFS_MAX_DEVS(info); |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4951 | devs_min = btrfs_raid_array[index].devs_min; |
| 4952 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4953 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4954 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4955 | |
| 4956 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4957 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4958 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4959 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4960 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4961 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4962 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4963 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4964 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4965 | max_chunk_size = max_stripe_size; |
| 4966 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4967 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4968 | max_chunk_size = 2 * max_stripe_size; |
Qu Wenruo | c17add7 | 2019-08-28 10:33:12 +0800 | [diff] [blame] | 4969 | devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4970 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4971 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4972 | type); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4973 | BUG(); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4976 | /* We don't want a chunk larger than 10% of writable space */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4977 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4978 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4979 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4980 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4981 | GFP_NOFS); |
| 4982 | if (!devices_info) |
| 4983 | return -ENOMEM; |
| 4984 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4985 | /* |
| 4986 | * in the first pass through the devices list, we gather information |
| 4987 | * about the available holes on each device. |
| 4988 | */ |
| 4989 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4990 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4991 | u64 max_avail; |
| 4992 | u64 dev_offset; |
| 4993 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4994 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4995 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4996 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4997 | continue; |
| 4998 | } |
| 4999 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 5000 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 5001 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 5002 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5003 | continue; |
| 5004 | |
| 5005 | if (device->total_bytes > device->bytes_used) |
| 5006 | total_avail = device->total_bytes - device->bytes_used; |
| 5007 | else |
| 5008 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 5009 | |
| 5010 | /* If there is no space on this device, skip it. */ |
| 5011 | if (total_avail == 0) |
| 5012 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5013 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 5014 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5015 | max_stripe_size * dev_stripes, |
| 5016 | &dev_offset, &max_avail); |
| 5017 | if (ret && ret != -ENOSPC) |
| 5018 | goto error; |
| 5019 | |
| 5020 | if (ret == 0) |
| 5021 | max_avail = max_stripe_size * dev_stripes; |
| 5022 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5023 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 5024 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 5025 | btrfs_debug(info, |
| 5026 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 5027 | __func__, device->devid, max_avail, |
| 5028 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5029 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5030 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5031 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 5032 | if (ndevs == fs_devices->rw_devices) { |
| 5033 | WARN(1, "%s: found more than %llu devices\n", |
| 5034 | __func__, fs_devices->rw_devices); |
| 5035 | break; |
| 5036 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5037 | devices_info[ndevs].dev_offset = dev_offset; |
| 5038 | devices_info[ndevs].max_avail = max_avail; |
| 5039 | devices_info[ndevs].total_avail = total_avail; |
| 5040 | devices_info[ndevs].dev = device; |
| 5041 | ++ndevs; |
| 5042 | } |
| 5043 | |
| 5044 | /* |
| 5045 | * now sort the devices by hole size / available space |
| 5046 | */ |
| 5047 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 5048 | btrfs_cmp_device_info, NULL); |
| 5049 | |
| 5050 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 5051 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5052 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5053 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5054 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5055 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5056 | btrfs_debug(info, |
| 5057 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 5058 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5059 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5060 | goto error; |
| 5061 | } |
| 5062 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 5063 | ndevs = min(ndevs, devs_max); |
| 5064 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5065 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5066 | * The primary goal is to maximize the number of stripes, so use as |
| 5067 | * many devices as possible, even if the stripes are not maximum sized. |
| 5068 | * |
| 5069 | * The DUP profile stores more than one stripe per device, the |
| 5070 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5071 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5072 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5073 | num_stripes = ndevs * dev_stripes; |
| 5074 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5075 | /* |
| 5076 | * this will have to be fixed for RAID1 and RAID10 over |
| 5077 | * more drives |
| 5078 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 5079 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5080 | |
| 5081 | /* |
| 5082 | * Use the number of data stripes to figure out how big this chunk |
| 5083 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5084 | * and compare that answer with the max chunk size. If it's higher, |
| 5085 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5086 | */ |
| 5087 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5088 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5089 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5090 | * then use it, unless it ends up being even bigger than the |
| 5091 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5092 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5093 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 5094 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5095 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5096 | } |
| 5097 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5098 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5099 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5100 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5101 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 5102 | if (!map) { |
| 5103 | ret = -ENOMEM; |
| 5104 | goto error; |
| 5105 | } |
| 5106 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5107 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5108 | for (i = 0; i < ndevs; ++i) { |
| 5109 | for (j = 0; j < dev_stripes; ++j) { |
| 5110 | int s = i * dev_stripes + j; |
| 5111 | map->stripes[s].dev = devices_info[i].dev; |
| 5112 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5113 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5114 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5115 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5116 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5117 | map->io_align = BTRFS_STRIPE_LEN; |
| 5118 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5119 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5120 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5121 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5122 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5123 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5124 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5125 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5126 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5127 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5128 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5129 | ret = -ENOMEM; |
| 5130 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5131 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5132 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5133 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5134 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5135 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5136 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5137 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5138 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5139 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5140 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5141 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5142 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5143 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5144 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5145 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5146 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5147 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5148 | write_unlock(&em_tree->lock); |
| 5149 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5150 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5151 | if (ret) |
| 5152 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5153 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5154 | for (i = 0; i < map->num_stripes; i++) { |
| 5155 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5156 | |
| 5157 | btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size); |
| 5158 | if (list_empty(&dev->post_commit_list)) |
| 5159 | list_add_tail(&dev->post_commit_list, |
| 5160 | &trans->transaction->dev_update_list); |
| 5161 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5162 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 5163 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5164 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5165 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5166 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5167 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5168 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5169 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5170 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5171 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5172 | write_lock(&em_tree->lock); |
| 5173 | remove_extent_mapping(em_tree, em); |
| 5174 | write_unlock(&em_tree->lock); |
| 5175 | |
| 5176 | /* One for our allocation */ |
| 5177 | free_extent_map(em); |
| 5178 | /* One for the tree reference */ |
| 5179 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5180 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5181 | kfree(devices_info); |
| 5182 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5183 | } |
| 5184 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5185 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5186 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5187 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5188 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5189 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5190 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5191 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5192 | struct btrfs_device *device; |
| 5193 | struct btrfs_chunk *chunk; |
| 5194 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5195 | struct extent_map *em; |
| 5196 | struct map_lookup *map; |
| 5197 | size_t item_size; |
| 5198 | u64 dev_offset; |
| 5199 | u64 stripe_size; |
| 5200 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5201 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5202 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5203 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5204 | if (IS_ERR(em)) |
| 5205 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5206 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5207 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5208 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5209 | stripe_size = em->orig_block_len; |
| 5210 | |
| 5211 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5212 | if (!chunk) { |
| 5213 | ret = -ENOMEM; |
| 5214 | goto out; |
| 5215 | } |
| 5216 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5217 | /* |
| 5218 | * Take the device list mutex to prevent races with the final phase of |
| 5219 | * a device replace operation that replaces the device object associated |
| 5220 | * with the map's stripes, because the device object's id can change |
| 5221 | * at any time during that final phase of the device replace operation |
| 5222 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5223 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5224 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5225 | for (i = 0; i < map->num_stripes; i++) { |
| 5226 | device = map->stripes[i].dev; |
| 5227 | dev_offset = map->stripes[i].physical; |
| 5228 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5229 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5230 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5231 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5232 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5233 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5234 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5235 | break; |
| 5236 | } |
| 5237 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5238 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5239 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5240 | } |
| 5241 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5242 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5243 | for (i = 0; i < map->num_stripes; i++) { |
| 5244 | device = map->stripes[i].dev; |
| 5245 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5246 | |
| 5247 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5248 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5249 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5250 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5251 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5252 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5253 | |
| 5254 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5255 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5256 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5257 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5258 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5259 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5260 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5261 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5262 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5263 | |
| 5264 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5265 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5266 | key.offset = chunk_offset; |
| 5267 | |
| 5268 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5269 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5270 | /* |
| 5271 | * TODO: Cleanup of inserted chunk root in case of |
| 5272 | * failure. |
| 5273 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5274 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5275 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5276 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5277 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5278 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5279 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5280 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5284 | * Chunk allocation falls into two parts. The first part does work |
| 5285 | * that makes the new allocated chunk usable, but does not do any operation |
| 5286 | * that modifies the chunk tree. The second part does the work that |
| 5287 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5288 | * bootstrap process of adding storage to a seed btrfs. |
| 5289 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5290 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5291 | { |
| 5292 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5293 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5294 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5295 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5296 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5297 | } |
| 5298 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5299 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5300 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5301 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5302 | u64 chunk_offset; |
| 5303 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5304 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5305 | int ret; |
| 5306 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5307 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5308 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5309 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5310 | if (ret) |
| 5311 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5312 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5313 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5314 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5315 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5316 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5317 | } |
| 5318 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5319 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5320 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5321 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5322 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5323 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5324 | } |
| 5325 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5326 | 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] | 5327 | { |
| 5328 | struct extent_map *em; |
| 5329 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5330 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5331 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5332 | int i; |
| 5333 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5334 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5335 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5336 | return 1; |
| 5337 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5338 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5339 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5340 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5341 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5342 | miss_ndevs++; |
| 5343 | continue; |
| 5344 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5345 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5346 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5347 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5348 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5349 | } |
| 5350 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5351 | |
| 5352 | /* |
| 5353 | * If the number of missing devices is larger than max errors, |
| 5354 | * we can not write the data into that chunk successfully, so |
| 5355 | * set it readonly. |
| 5356 | */ |
| 5357 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5358 | readonly = 1; |
| 5359 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5360 | free_extent_map(em); |
| 5361 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5362 | } |
| 5363 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5364 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5365 | { |
| 5366 | struct extent_map *em; |
| 5367 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5368 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5369 | write_lock(&tree->lock); |
| 5370 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5371 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5372 | remove_extent_mapping(tree, em); |
| 5373 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5374 | if (!em) |
| 5375 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5376 | /* once for us */ |
| 5377 | free_extent_map(em); |
| 5378 | /* once for the tree */ |
| 5379 | free_extent_map(em); |
| 5380 | } |
| 5381 | } |
| 5382 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5383 | 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] | 5384 | { |
| 5385 | struct extent_map *em; |
| 5386 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5387 | int ret; |
| 5388 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5389 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5390 | if (IS_ERR(em)) |
| 5391 | /* |
| 5392 | * We could return errors for these cases, but that could get |
| 5393 | * ugly and we'd probably do the same thing which is just not do |
| 5394 | * anything else and exit, so return 1 so the callers don't try |
| 5395 | * to use other copies. |
| 5396 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5397 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5398 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5399 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5400 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5401 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5402 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5403 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5404 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5405 | ret = 2; |
| 5406 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * There could be two corrupted data stripes, we need |
| 5409 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5410 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5411 | * Fail a stripe at a time on every retry except the |
| 5412 | * stripe under reconstruction. |
| 5413 | */ |
| 5414 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5415 | else |
| 5416 | ret = 1; |
| 5417 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5418 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5419 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5420 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5421 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5422 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5423 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5424 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5425 | return ret; |
| 5426 | } |
| 5427 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5428 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5429 | u64 logical) |
| 5430 | { |
| 5431 | struct extent_map *em; |
| 5432 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5433 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5434 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5435 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5436 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5437 | if (!WARN_ON(IS_ERR(em))) { |
| 5438 | map = em->map_lookup; |
| 5439 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5440 | len = map->stripe_len * nr_data_stripes(map); |
| 5441 | free_extent_map(em); |
| 5442 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5443 | return len; |
| 5444 | } |
| 5445 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5446 | 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] | 5447 | { |
| 5448 | struct extent_map *em; |
| 5449 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5450 | int ret = 0; |
| 5451 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5452 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5453 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5454 | if(!WARN_ON(IS_ERR(em))) { |
| 5455 | map = em->map_lookup; |
| 5456 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5457 | ret = 1; |
| 5458 | free_extent_map(em); |
| 5459 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5460 | return ret; |
| 5461 | } |
| 5462 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5463 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5464 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5465 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5466 | { |
| 5467 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5468 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5469 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5470 | int tolerance; |
| 5471 | struct btrfs_device *srcdev; |
| 5472 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5473 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5474 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5475 | |
| 5476 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5477 | num_stripes = map->sub_stripes; |
| 5478 | else |
| 5479 | num_stripes = map->num_stripes; |
| 5480 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5481 | preferred_mirror = first + current->pid % num_stripes; |
| 5482 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5483 | if (dev_replace_is_ongoing && |
| 5484 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5485 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5486 | srcdev = fs_info->dev_replace.srcdev; |
| 5487 | else |
| 5488 | srcdev = NULL; |
| 5489 | |
| 5490 | /* |
| 5491 | * try to avoid the drive that is the source drive for a |
| 5492 | * dev-replace procedure, only choose it if no other non-missing |
| 5493 | * mirror is available |
| 5494 | */ |
| 5495 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5496 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5497 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5498 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5499 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5500 | if (map->stripes[i].dev->bdev && |
| 5501 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5502 | return i; |
| 5503 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5504 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5505 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5506 | /* we couldn't find one that doesn't fail. Just return something |
| 5507 | * and the io error handling code will clean up eventually |
| 5508 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5509 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5510 | } |
| 5511 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5512 | static inline int parity_smaller(u64 a, u64 b) |
| 5513 | { |
| 5514 | return a > b; |
| 5515 | } |
| 5516 | |
| 5517 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5518 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5519 | { |
| 5520 | struct btrfs_bio_stripe s; |
| 5521 | int i; |
| 5522 | u64 l; |
| 5523 | int again = 1; |
| 5524 | |
| 5525 | while (again) { |
| 5526 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5527 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5528 | if (parity_smaller(bbio->raid_map[i], |
| 5529 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5530 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5531 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5532 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5533 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5534 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5535 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5536 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5537 | again = 1; |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | } |
| 5542 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5543 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5544 | { |
| 5545 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5546 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5547 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5548 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5549 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5550 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5551 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5552 | /* |
| 5553 | * plus the raid_map, which includes both the tgt dev |
| 5554 | * and the stripes |
| 5555 | */ |
| 5556 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5557 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5558 | |
| 5559 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5560 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5561 | |
| 5562 | return bbio; |
| 5563 | } |
| 5564 | |
| 5565 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5566 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5567 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5568 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5572 | { |
| 5573 | if (!bbio) |
| 5574 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5575 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5576 | kfree(bbio); |
| 5577 | } |
| 5578 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5579 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5580 | /* |
| 5581 | * Please note that, discard won't be sent to target device of device |
| 5582 | * replace. |
| 5583 | */ |
| 5584 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5585 | u64 logical, u64 length, |
| 5586 | struct btrfs_bio **bbio_ret) |
| 5587 | { |
| 5588 | struct extent_map *em; |
| 5589 | struct map_lookup *map; |
| 5590 | struct btrfs_bio *bbio; |
| 5591 | u64 offset; |
| 5592 | u64 stripe_nr; |
| 5593 | u64 stripe_nr_end; |
| 5594 | u64 stripe_end_offset; |
| 5595 | u64 stripe_cnt; |
| 5596 | u64 stripe_len; |
| 5597 | u64 stripe_offset; |
| 5598 | u64 num_stripes; |
| 5599 | u32 stripe_index; |
| 5600 | u32 factor = 0; |
| 5601 | u32 sub_stripes = 0; |
| 5602 | u64 stripes_per_dev = 0; |
| 5603 | u32 remaining_stripes = 0; |
| 5604 | u32 last_stripe = 0; |
| 5605 | int ret = 0; |
| 5606 | int i; |
| 5607 | |
| 5608 | /* discard always return a bbio */ |
| 5609 | ASSERT(bbio_ret); |
| 5610 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5611 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5612 | if (IS_ERR(em)) |
| 5613 | return PTR_ERR(em); |
| 5614 | |
| 5615 | map = em->map_lookup; |
| 5616 | /* we don't discard raid56 yet */ |
| 5617 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5618 | ret = -EOPNOTSUPP; |
| 5619 | goto out; |
| 5620 | } |
| 5621 | |
| 5622 | offset = logical - em->start; |
| 5623 | length = min_t(u64, em->len - offset, length); |
| 5624 | |
| 5625 | stripe_len = map->stripe_len; |
| 5626 | /* |
| 5627 | * stripe_nr counts the total number of stripes we have to stride |
| 5628 | * to get to this block |
| 5629 | */ |
| 5630 | stripe_nr = div64_u64(offset, stripe_len); |
| 5631 | |
| 5632 | /* stripe_offset is the offset of this block in its stripe */ |
| 5633 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5634 | |
| 5635 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5636 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5637 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5638 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5639 | (offset + length); |
| 5640 | /* |
| 5641 | * after this, stripe_nr is the number of stripes on this |
| 5642 | * device we have to walk to find the data, and stripe_index is |
| 5643 | * the number of our device in the stripe array |
| 5644 | */ |
| 5645 | num_stripes = 1; |
| 5646 | stripe_index = 0; |
| 5647 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5648 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5649 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5650 | sub_stripes = 1; |
| 5651 | else |
| 5652 | sub_stripes = map->sub_stripes; |
| 5653 | |
| 5654 | factor = map->num_stripes / sub_stripes; |
| 5655 | num_stripes = min_t(u64, map->num_stripes, |
| 5656 | sub_stripes * stripe_cnt); |
| 5657 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5658 | stripe_index *= sub_stripes; |
| 5659 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5660 | &remaining_stripes); |
| 5661 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5662 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5663 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5664 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5665 | num_stripes = map->num_stripes; |
| 5666 | } else { |
| 5667 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5668 | &stripe_index); |
| 5669 | } |
| 5670 | |
| 5671 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5672 | if (!bbio) { |
| 5673 | ret = -ENOMEM; |
| 5674 | goto out; |
| 5675 | } |
| 5676 | |
| 5677 | for (i = 0; i < num_stripes; i++) { |
| 5678 | bbio->stripes[i].physical = |
| 5679 | map->stripes[stripe_index].physical + |
| 5680 | stripe_offset + stripe_nr * map->stripe_len; |
| 5681 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5682 | |
| 5683 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5684 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5685 | bbio->stripes[i].length = stripes_per_dev * |
| 5686 | map->stripe_len; |
| 5687 | |
| 5688 | if (i / sub_stripes < remaining_stripes) |
| 5689 | bbio->stripes[i].length += |
| 5690 | map->stripe_len; |
| 5691 | |
| 5692 | /* |
| 5693 | * Special for the first stripe and |
| 5694 | * the last stripe: |
| 5695 | * |
| 5696 | * |-------|...|-------| |
| 5697 | * |----------| |
| 5698 | * off end_off |
| 5699 | */ |
| 5700 | if (i < sub_stripes) |
| 5701 | bbio->stripes[i].length -= |
| 5702 | stripe_offset; |
| 5703 | |
| 5704 | if (stripe_index >= last_stripe && |
| 5705 | stripe_index <= (last_stripe + |
| 5706 | sub_stripes - 1)) |
| 5707 | bbio->stripes[i].length -= |
| 5708 | stripe_end_offset; |
| 5709 | |
| 5710 | if (i == sub_stripes - 1) |
| 5711 | stripe_offset = 0; |
| 5712 | } else { |
| 5713 | bbio->stripes[i].length = length; |
| 5714 | } |
| 5715 | |
| 5716 | stripe_index++; |
| 5717 | if (stripe_index == map->num_stripes) { |
| 5718 | stripe_index = 0; |
| 5719 | stripe_nr++; |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | *bbio_ret = bbio; |
| 5724 | bbio->map_type = map->type; |
| 5725 | bbio->num_stripes = num_stripes; |
| 5726 | out: |
| 5727 | free_extent_map(em); |
| 5728 | return ret; |
| 5729 | } |
| 5730 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5731 | /* |
| 5732 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5733 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5734 | * left cursor can also be read from the target drive. |
| 5735 | * |
| 5736 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5737 | * array of stripes. |
| 5738 | * For READ, it also needs to be supported using the same mirror number. |
| 5739 | * |
| 5740 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5741 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5742 | * case. |
| 5743 | */ |
| 5744 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5745 | u64 logical, u64 length, |
| 5746 | u64 srcdev_devid, int *mirror_num, |
| 5747 | u64 *physical) |
| 5748 | { |
| 5749 | struct btrfs_bio *bbio = NULL; |
| 5750 | int num_stripes; |
| 5751 | int index_srcdev = 0; |
| 5752 | int found = 0; |
| 5753 | u64 physical_of_found = 0; |
| 5754 | int i; |
| 5755 | int ret = 0; |
| 5756 | |
| 5757 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5758 | logical, &length, &bbio, 0, 0); |
| 5759 | if (ret) { |
| 5760 | ASSERT(bbio == NULL); |
| 5761 | return ret; |
| 5762 | } |
| 5763 | |
| 5764 | num_stripes = bbio->num_stripes; |
| 5765 | if (*mirror_num > num_stripes) { |
| 5766 | /* |
| 5767 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5768 | * that means that the requested area is not left of the left |
| 5769 | * cursor |
| 5770 | */ |
| 5771 | btrfs_put_bbio(bbio); |
| 5772 | return -EIO; |
| 5773 | } |
| 5774 | |
| 5775 | /* |
| 5776 | * process the rest of the function using the mirror_num of the source |
| 5777 | * drive. Therefore look it up first. At the end, patch the device |
| 5778 | * pointer to the one of the target drive. |
| 5779 | */ |
| 5780 | for (i = 0; i < num_stripes; i++) { |
| 5781 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5782 | continue; |
| 5783 | |
| 5784 | /* |
| 5785 | * In case of DUP, in order to keep it simple, only add the |
| 5786 | * mirror with the lowest physical address |
| 5787 | */ |
| 5788 | if (found && |
| 5789 | physical_of_found <= bbio->stripes[i].physical) |
| 5790 | continue; |
| 5791 | |
| 5792 | index_srcdev = i; |
| 5793 | found = 1; |
| 5794 | physical_of_found = bbio->stripes[i].physical; |
| 5795 | } |
| 5796 | |
| 5797 | btrfs_put_bbio(bbio); |
| 5798 | |
| 5799 | ASSERT(found); |
| 5800 | if (!found) |
| 5801 | return -EIO; |
| 5802 | |
| 5803 | *mirror_num = index_srcdev + 1; |
| 5804 | *physical = physical_of_found; |
| 5805 | return ret; |
| 5806 | } |
| 5807 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5808 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5809 | struct btrfs_bio **bbio_ret, |
| 5810 | struct btrfs_dev_replace *dev_replace, |
| 5811 | int *num_stripes_ret, int *max_errors_ret) |
| 5812 | { |
| 5813 | struct btrfs_bio *bbio = *bbio_ret; |
| 5814 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5815 | int tgtdev_indexes = 0; |
| 5816 | int num_stripes = *num_stripes_ret; |
| 5817 | int max_errors = *max_errors_ret; |
| 5818 | int i; |
| 5819 | |
| 5820 | if (op == BTRFS_MAP_WRITE) { |
| 5821 | int index_where_to_add; |
| 5822 | |
| 5823 | /* |
| 5824 | * duplicate the write operations while the dev replace |
| 5825 | * procedure is running. Since the copying of the old disk to |
| 5826 | * the new disk takes place at run time while the filesystem is |
| 5827 | * mounted writable, the regular write operations to the old |
| 5828 | * disk have to be duplicated to go to the new disk as well. |
| 5829 | * |
| 5830 | * Note that device->missing is handled by the caller, and that |
| 5831 | * the write to the old disk is already set up in the stripes |
| 5832 | * array. |
| 5833 | */ |
| 5834 | index_where_to_add = num_stripes; |
| 5835 | for (i = 0; i < num_stripes; i++) { |
| 5836 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5837 | /* write to new disk, too */ |
| 5838 | struct btrfs_bio_stripe *new = |
| 5839 | bbio->stripes + index_where_to_add; |
| 5840 | struct btrfs_bio_stripe *old = |
| 5841 | bbio->stripes + i; |
| 5842 | |
| 5843 | new->physical = old->physical; |
| 5844 | new->length = old->length; |
| 5845 | new->dev = dev_replace->tgtdev; |
| 5846 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5847 | index_where_to_add++; |
| 5848 | max_errors++; |
| 5849 | tgtdev_indexes++; |
| 5850 | } |
| 5851 | } |
| 5852 | num_stripes = index_where_to_add; |
| 5853 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5854 | int index_srcdev = 0; |
| 5855 | int found = 0; |
| 5856 | u64 physical_of_found = 0; |
| 5857 | |
| 5858 | /* |
| 5859 | * During the dev-replace procedure, the target drive can also |
| 5860 | * be used to read data in case it is needed to repair a corrupt |
| 5861 | * block elsewhere. This is possible if the requested area is |
| 5862 | * left of the left cursor. In this area, the target drive is a |
| 5863 | * full copy of the source drive. |
| 5864 | */ |
| 5865 | for (i = 0; i < num_stripes; i++) { |
| 5866 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5867 | /* |
| 5868 | * In case of DUP, in order to keep it simple, |
| 5869 | * only add the mirror with the lowest physical |
| 5870 | * address |
| 5871 | */ |
| 5872 | if (found && |
| 5873 | physical_of_found <= |
| 5874 | bbio->stripes[i].physical) |
| 5875 | continue; |
| 5876 | index_srcdev = i; |
| 5877 | found = 1; |
| 5878 | physical_of_found = bbio->stripes[i].physical; |
| 5879 | } |
| 5880 | } |
| 5881 | if (found) { |
| 5882 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5883 | bbio->stripes + num_stripes; |
| 5884 | |
| 5885 | tgtdev_stripe->physical = physical_of_found; |
| 5886 | tgtdev_stripe->length = |
| 5887 | bbio->stripes[index_srcdev].length; |
| 5888 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5889 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5890 | |
| 5891 | tgtdev_indexes++; |
| 5892 | num_stripes++; |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | *num_stripes_ret = num_stripes; |
| 5897 | *max_errors_ret = max_errors; |
| 5898 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5899 | *bbio_ret = bbio; |
| 5900 | } |
| 5901 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5902 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5903 | { |
| 5904 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5905 | } |
| 5906 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5907 | /* |
| 5908 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5909 | * tuple. This information is used to calculate how big a |
| 5910 | * particular bio can get before it straddles a stripe. |
| 5911 | * |
| 5912 | * @fs_info - the filesystem |
| 5913 | * @logical - address that we want to figure out the geometry of |
| 5914 | * @len - the length of IO we are going to perform, starting at @logical |
| 5915 | * @op - type of operation - write or read |
| 5916 | * @io_geom - pointer used to return values |
| 5917 | * |
| 5918 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5919 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5920 | */ |
| 5921 | int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5922 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5923 | { |
| 5924 | struct extent_map *em; |
| 5925 | struct map_lookup *map; |
| 5926 | u64 offset; |
| 5927 | u64 stripe_offset; |
| 5928 | u64 stripe_nr; |
| 5929 | u64 stripe_len; |
| 5930 | u64 raid56_full_stripe_start = (u64)-1; |
| 5931 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5932 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5933 | |
| 5934 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5935 | |
| 5936 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5937 | if (IS_ERR(em)) |
| 5938 | return PTR_ERR(em); |
| 5939 | |
| 5940 | map = em->map_lookup; |
| 5941 | /* Offset of this logical address in the chunk */ |
| 5942 | offset = logical - em->start; |
| 5943 | /* Len of a stripe in a chunk */ |
| 5944 | stripe_len = map->stripe_len; |
| 5945 | /* Stripe wher this block falls in */ |
| 5946 | stripe_nr = div64_u64(offset, stripe_len); |
| 5947 | /* Offset of stripe in the chunk */ |
| 5948 | stripe_offset = stripe_nr * stripe_len; |
| 5949 | if (offset < stripe_offset) { |
| 5950 | btrfs_crit(fs_info, |
| 5951 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5952 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5953 | ret = -EINVAL; |
| 5954 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | /* stripe_offset is the offset of this block in its stripe */ |
| 5958 | stripe_offset = offset - stripe_offset; |
| 5959 | data_stripes = nr_data_stripes(map); |
| 5960 | |
| 5961 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5962 | u64 max_len = stripe_len - stripe_offset; |
| 5963 | |
| 5964 | /* |
| 5965 | * In case of raid56, we need to know the stripe aligned start |
| 5966 | */ |
| 5967 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5968 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5969 | raid56_full_stripe_start = offset; |
| 5970 | |
| 5971 | /* |
| 5972 | * Allow a write of a full stripe, but make sure we |
| 5973 | * don't allow straddling of stripes |
| 5974 | */ |
| 5975 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5976 | full_stripe_len); |
| 5977 | raid56_full_stripe_start *= full_stripe_len; |
| 5978 | |
| 5979 | /* |
| 5980 | * For writes to RAID[56], allow a full stripeset across |
| 5981 | * all disks. For other RAID types and for RAID[56] |
| 5982 | * reads, just allow a single stripe (on a single disk). |
| 5983 | */ |
| 5984 | if (op == BTRFS_MAP_WRITE) { |
| 5985 | max_len = stripe_len * data_stripes - |
| 5986 | (offset - raid56_full_stripe_start); |
| 5987 | } |
| 5988 | } |
| 5989 | len = min_t(u64, em->len - offset, max_len); |
| 5990 | } else { |
| 5991 | len = em->len - offset; |
| 5992 | } |
| 5993 | |
| 5994 | io_geom->len = len; |
| 5995 | io_geom->offset = offset; |
| 5996 | io_geom->stripe_len = stripe_len; |
| 5997 | io_geom->stripe_nr = stripe_nr; |
| 5998 | io_geom->stripe_offset = stripe_offset; |
| 5999 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 6000 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 6001 | out: |
| 6002 | /* once for us */ |
| 6003 | free_extent_map(em); |
| 6004 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 6005 | } |
| 6006 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6007 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 6008 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6009 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6010 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6011 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6012 | { |
| 6013 | struct extent_map *em; |
| 6014 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6015 | u64 stripe_offset; |
| 6016 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6017 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6018 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6019 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6020 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6021 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6022 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6023 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6024 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6025 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6026 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 6027 | int dev_replace_is_ongoing = 0; |
| 6028 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6029 | int patch_the_first_stripe_for_dev_replace = 0; |
| 6030 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6031 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6032 | struct btrfs_io_geometry geom; |
| 6033 | |
| 6034 | ASSERT(bbio_ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6035 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6036 | if (op == BTRFS_MAP_DISCARD) |
| 6037 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 6038 | *length, bbio_ret); |
| 6039 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6040 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 6041 | if (ret < 0) |
| 6042 | return ret; |
| 6043 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6044 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Dan Carpenter | f113698 | 2019-08-09 17:07:39 +0300 | [diff] [blame] | 6045 | ASSERT(!IS_ERR(em)); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6046 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6047 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6048 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6049 | stripe_len = geom.stripe_len; |
| 6050 | stripe_nr = geom.stripe_nr; |
| 6051 | stripe_offset = geom.stripe_offset; |
| 6052 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6053 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6054 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6055 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6056 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6057 | /* |
| 6058 | * Hold the semaphore for read during the whole operation, write is |
| 6059 | * requested at commit time but must wait. |
| 6060 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6061 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6062 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6063 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6064 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6065 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6066 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 6067 | dev_replace->srcdev->devid, |
| 6068 | &mirror_num, |
| 6069 | &physical_to_patch_in_first_stripe); |
| 6070 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6071 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6072 | else |
| 6073 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6074 | } else if (mirror_num > map->num_stripes) { |
| 6075 | mirror_num = 0; |
| 6076 | } |
| 6077 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6078 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6079 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6080 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6081 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6082 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6083 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6084 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 6085 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6086 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6087 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6088 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6089 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6090 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6091 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6092 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6093 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6094 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6095 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6096 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6097 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6098 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6099 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6100 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6101 | } else { |
| 6102 | mirror_num = 1; |
| 6103 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6104 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6105 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6106 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6107 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6108 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6109 | stripe_index *= map->sub_stripes; |
| 6110 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6111 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6112 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6113 | else if (mirror_num) |
| 6114 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6115 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6116 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6117 | stripe_index = find_live_mirror(fs_info, map, |
| 6118 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6119 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6120 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6121 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6122 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6123 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6124 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6125 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6126 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6127 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6128 | |
| 6129 | /* RAID[56] write or recovery. Return all stripes */ |
| 6130 | num_stripes = map->num_stripes; |
| 6131 | max_errors = nr_parity_stripes(map); |
| 6132 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6133 | *length = map->stripe_len; |
| 6134 | stripe_index = 0; |
| 6135 | stripe_offset = 0; |
| 6136 | } else { |
| 6137 | /* |
| 6138 | * Mirror #0 or #1 means the original data block. |
| 6139 | * Mirror #2 is RAID5 parity block. |
| 6140 | * Mirror #3 is RAID6 Q block. |
| 6141 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6142 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6143 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6144 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6145 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6146 | |
| 6147 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6148 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6149 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6150 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6151 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6152 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6153 | } else { |
| 6154 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6155 | * after this, stripe_nr is the number of stripes on this |
| 6156 | * device we have to walk to find the data, and stripe_index is |
| 6157 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6158 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6159 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6160 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6161 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6162 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6163 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6164 | btrfs_crit(fs_info, |
| 6165 | "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] | 6166 | stripe_index, map->num_stripes); |
| 6167 | ret = -EINVAL; |
| 6168 | goto out; |
| 6169 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6170 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6171 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6172 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6173 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6174 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6175 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6176 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6177 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6178 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6179 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6180 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6181 | if (!bbio) { |
| 6182 | ret = -ENOMEM; |
| 6183 | goto out; |
| 6184 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6185 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6186 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6187 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6188 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6189 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6190 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6191 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6192 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6193 | |
| 6194 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6195 | sizeof(struct btrfs_bio_stripe) * |
| 6196 | num_alloc_stripes + |
| 6197 | sizeof(int) * tgtdev_indexes); |
| 6198 | |
| 6199 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6200 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6201 | |
| 6202 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6203 | tmp = stripe_nr * data_stripes; |
| 6204 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6205 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6206 | em->start + (tmp + i) * map->stripe_len; |
| 6207 | |
| 6208 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6209 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6210 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6211 | RAID6_Q_STRIPE; |
| 6212 | } |
| 6213 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6214 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6215 | for (i = 0; i < num_stripes; i++) { |
| 6216 | bbio->stripes[i].physical = |
| 6217 | map->stripes[stripe_index].physical + |
| 6218 | stripe_offset + |
| 6219 | stripe_nr * map->stripe_len; |
| 6220 | bbio->stripes[i].dev = |
| 6221 | map->stripes[stripe_index].dev; |
| 6222 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6223 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6224 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6225 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6226 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6227 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6228 | if (bbio->raid_map) |
| 6229 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6230 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6231 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6232 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6233 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6234 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6235 | } |
| 6236 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6237 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6238 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6239 | bbio->num_stripes = num_stripes; |
| 6240 | bbio->max_errors = max_errors; |
| 6241 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6242 | |
| 6243 | /* |
| 6244 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6245 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6246 | * available as a mirror |
| 6247 | */ |
| 6248 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6249 | WARN_ON(num_stripes > 1); |
| 6250 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6251 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6252 | bbio->mirror_num = map->num_stripes + 1; |
| 6253 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6254 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6255 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6256 | lockdep_assert_held(&dev_replace->rwsem); |
| 6257 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6258 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6259 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6260 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6261 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6262 | } |
| 6263 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6264 | 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] | 6265 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6266 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6267 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6268 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6269 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6270 | } |
| 6271 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6272 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6273 | 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] | 6274 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6275 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6276 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6277 | 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] | 6278 | } |
| 6279 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6280 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6281 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6282 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6283 | struct extent_map *em; |
| 6284 | struct map_lookup *map; |
| 6285 | u64 *buf; |
| 6286 | u64 bytenr; |
| 6287 | u64 length; |
| 6288 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6289 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6290 | int i, j, nr = 0; |
| 6291 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6292 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6293 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6294 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6295 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6296 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6297 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6298 | rmap_len = map->stripe_len; |
| 6299 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6300 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6301 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6302 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6303 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6304 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6305 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6306 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6307 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6308 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6309 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6310 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6311 | |
| 6312 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6313 | if (map->stripes[i].physical > physical || |
| 6314 | map->stripes[i].physical + length <= physical) |
| 6315 | continue; |
| 6316 | |
| 6317 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6318 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6319 | |
| 6320 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6321 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6322 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6323 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6324 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6325 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6326 | * Alternatively, just use rmap_len below instead of |
| 6327 | * map->stripe_len */ |
| 6328 | |
| 6329 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6330 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6331 | for (j = 0; j < nr; j++) { |
| 6332 | if (buf[j] == bytenr) |
| 6333 | break; |
| 6334 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6335 | if (j == nr) { |
| 6336 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6337 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6338 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6339 | } |
| 6340 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6341 | *logical = buf; |
| 6342 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6343 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6344 | |
| 6345 | free_extent_map(em); |
| 6346 | return 0; |
| 6347 | } |
| 6348 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6349 | 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] | 6350 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6351 | bio->bi_private = bbio->private; |
| 6352 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6353 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6354 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6355 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6356 | } |
| 6357 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6358 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6359 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6360 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6361 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6362 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6363 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6364 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6365 | if (bio->bi_status == BLK_STS_IOERR || |
| 6366 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6367 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6368 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6369 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6370 | |
| 6371 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6372 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6373 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6374 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6375 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6376 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6377 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6378 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6379 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6380 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6381 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6382 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6383 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6384 | } |
| 6385 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6386 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6387 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6388 | is_orig_bio = 1; |
| 6389 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6390 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6391 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6392 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6393 | if (!is_orig_bio) { |
| 6394 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6395 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6396 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6397 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6398 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6399 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6400 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6401 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6402 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6403 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6404 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6405 | /* |
| 6406 | * this bio is actually up to date, we didn't |
| 6407 | * go over the max number of errors |
| 6408 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6409 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6410 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6411 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6412 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6413 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6414 | bio_put(bio); |
| 6415 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6416 | } |
| 6417 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6418 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame^] | 6419 | u64 physical, int dev_nr) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6420 | { |
| 6421 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6422 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6423 | |
| 6424 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6425 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6426 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6427 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6428 | btrfs_debug_in_rcu(fs_info, |
| 6429 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6430 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6431 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6432 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6433 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6434 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6435 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6436 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame^] | 6437 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6438 | } |
| 6439 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6440 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6441 | { |
| 6442 | atomic_inc(&bbio->error); |
| 6443 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6444 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6445 | WARN_ON(bio != bbio->orig_bio); |
| 6446 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6447 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6448 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6449 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6450 | bio->bi_status = BLK_STS_IOERR; |
| 6451 | else |
| 6452 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6453 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6454 | } |
| 6455 | } |
| 6456 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6457 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame^] | 6458 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6459 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6460 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6461 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6462 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6463 | u64 length = 0; |
| 6464 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6465 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6466 | int dev_nr; |
| 6467 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6468 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6469 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6470 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6471 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6472 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6473 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6474 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6475 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6476 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6477 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6478 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6479 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6480 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6481 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6482 | bbio->orig_bio = first_bio; |
| 6483 | bbio->private = first_bio->bi_private; |
| 6484 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6485 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6486 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6487 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6488 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6489 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6490 | /* In this case, map_length has been set to the length of |
| 6491 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6492 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6493 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6494 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6495 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6496 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6497 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6498 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6499 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6500 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6501 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6502 | } |
| 6503 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6504 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6505 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6506 | "mapping failed logical %llu bio len %llu len %llu", |
| 6507 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6508 | BUG(); |
| 6509 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6510 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6511 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6512 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6513 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6514 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6515 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6516 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6517 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6518 | continue; |
| 6519 | } |
| 6520 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6521 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6522 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6523 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6524 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6525 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6526 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame^] | 6527 | dev_nr); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6528 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6529 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6530 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6531 | } |
| 6532 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6533 | /* |
| 6534 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6535 | * return NULL. |
| 6536 | * |
| 6537 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6538 | * only devid is used. |
| 6539 | * |
| 6540 | * If @seed is true, traverse through the seed devices. |
| 6541 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6542 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6543 | u64 devid, u8 *uuid, u8 *fsid, |
| 6544 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6545 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6546 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6547 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6548 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6549 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6550 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6551 | list_for_each_entry(device, &fs_devices->devices, |
| 6552 | dev_list) { |
| 6553 | if (device->devid == devid && |
| 6554 | (!uuid || memcmp(device->uuid, uuid, |
| 6555 | BTRFS_UUID_SIZE) == 0)) |
| 6556 | return device; |
| 6557 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6558 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6559 | if (seed) |
| 6560 | fs_devices = fs_devices->seed; |
| 6561 | else |
| 6562 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6563 | } |
| 6564 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6565 | } |
| 6566 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6567 | 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] | 6568 | u64 devid, u8 *dev_uuid) |
| 6569 | { |
| 6570 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6571 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6572 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6573 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6574 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6575 | |
| 6576 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6577 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6578 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6579 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6580 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6581 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6582 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6583 | return device; |
| 6584 | } |
| 6585 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6586 | /** |
| 6587 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6588 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6589 | * devid is provided (i.e. @devid != NULL). |
| 6590 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6591 | * is generated. |
| 6592 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6593 | * is generated. |
| 6594 | * |
| 6595 | * 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] | 6596 | * 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] | 6597 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6598 | */ |
| 6599 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6600 | const u64 *devid, |
| 6601 | const u8 *uuid) |
| 6602 | { |
| 6603 | struct btrfs_device *dev; |
| 6604 | u64 tmp; |
| 6605 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6606 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6607 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6608 | |
| 6609 | dev = __alloc_device(); |
| 6610 | if (IS_ERR(dev)) |
| 6611 | return dev; |
| 6612 | |
| 6613 | if (devid) |
| 6614 | tmp = *devid; |
| 6615 | else { |
| 6616 | int ret; |
| 6617 | |
| 6618 | ret = find_next_devid(fs_info, &tmp); |
| 6619 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6620 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6621 | return ERR_PTR(ret); |
| 6622 | } |
| 6623 | } |
| 6624 | dev->devid = tmp; |
| 6625 | |
| 6626 | if (uuid) |
| 6627 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6628 | else |
| 6629 | generate_random_uuid(dev->uuid); |
| 6630 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 6631 | btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6632 | |
| 6633 | return dev; |
| 6634 | } |
| 6635 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6636 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6637 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6638 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6639 | if (error) |
| 6640 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6641 | devid, uuid); |
| 6642 | else |
| 6643 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6644 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6645 | } |
| 6646 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6647 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6648 | { |
| 6649 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6650 | int ncopies = btrfs_raid_array[index].ncopies; |
| 6651 | int data_stripes; |
| 6652 | |
| 6653 | switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 6654 | case BTRFS_BLOCK_GROUP_RAID5: |
| 6655 | data_stripes = num_stripes - 1; |
| 6656 | break; |
| 6657 | case BTRFS_BLOCK_GROUP_RAID6: |
| 6658 | data_stripes = num_stripes - 2; |
| 6659 | break; |
| 6660 | default: |
| 6661 | data_stripes = num_stripes / ncopies; |
| 6662 | break; |
| 6663 | } |
| 6664 | return div_u64(chunk_len, data_stripes); |
| 6665 | } |
| 6666 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6667 | static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6668 | struct btrfs_chunk *chunk) |
| 6669 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6670 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6671 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6672 | struct map_lookup *map; |
| 6673 | struct extent_map *em; |
| 6674 | u64 logical; |
| 6675 | u64 length; |
| 6676 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6677 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6678 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6679 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6680 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6681 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6682 | logical = key->offset; |
| 6683 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6684 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6685 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6686 | /* |
| 6687 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6688 | * as chunk item in tree block is already verified by tree-checker. |
| 6689 | */ |
| 6690 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6691 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6692 | if (ret) |
| 6693 | return ret; |
| 6694 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6695 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6696 | read_lock(&map_tree->lock); |
| 6697 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6698 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6699 | |
| 6700 | /* already mapped? */ |
| 6701 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6702 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6703 | return 0; |
| 6704 | } else if (em) { |
| 6705 | free_extent_map(em); |
| 6706 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6707 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6708 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6709 | if (!em) |
| 6710 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6711 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6712 | if (!map) { |
| 6713 | free_extent_map(em); |
| 6714 | return -ENOMEM; |
| 6715 | } |
| 6716 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6717 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6718 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6719 | em->start = logical; |
| 6720 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6721 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6722 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6723 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6724 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6725 | map->num_stripes = num_stripes; |
| 6726 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6727 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6728 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6729 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6730 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6731 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6732 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6733 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6734 | for (i = 0; i < num_stripes; i++) { |
| 6735 | map->stripes[i].physical = |
| 6736 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6737 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6738 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6739 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6740 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6741 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6742 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6743 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6744 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6745 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6746 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6747 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6748 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6749 | if (!map->stripes[i].dev) { |
| 6750 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6751 | add_missing_dev(fs_info->fs_devices, devid, |
| 6752 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6753 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6754 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6755 | btrfs_err(fs_info, |
| 6756 | "failed to init missing dev %llu: %ld", |
| 6757 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6758 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6759 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6760 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6761 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6762 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6763 | &(map->stripes[i].dev->dev_state)); |
| 6764 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6765 | } |
| 6766 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6767 | write_lock(&map_tree->lock); |
| 6768 | ret = add_extent_mapping(map_tree, em, 0); |
| 6769 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6770 | if (ret < 0) { |
| 6771 | btrfs_err(fs_info, |
| 6772 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6773 | em->start, em->len, ret); |
| 6774 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6775 | free_extent_map(em); |
| 6776 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6777 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6778 | } |
| 6779 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6780 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6781 | struct btrfs_dev_item *dev_item, |
| 6782 | struct btrfs_device *device) |
| 6783 | { |
| 6784 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6785 | |
| 6786 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6787 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6788 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6789 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6790 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6791 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6792 | device->type = btrfs_device_type(leaf, dev_item); |
| 6793 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6794 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6795 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6796 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6797 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6798 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6799 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6800 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6801 | } |
| 6802 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6803 | 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] | 6804 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6805 | { |
| 6806 | struct btrfs_fs_devices *fs_devices; |
| 6807 | int ret; |
| 6808 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6809 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6810 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6811 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6812 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6813 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6814 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6815 | return fs_devices; |
| 6816 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6817 | fs_devices = fs_devices->seed; |
| 6818 | } |
| 6819 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6820 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6821 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6822 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6823 | return ERR_PTR(-ENOENT); |
| 6824 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6825 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6826 | if (IS_ERR(fs_devices)) |
| 6827 | return fs_devices; |
| 6828 | |
| 6829 | fs_devices->seeding = 1; |
| 6830 | fs_devices->opened = 1; |
| 6831 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6832 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6833 | |
| 6834 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6835 | if (IS_ERR(fs_devices)) |
| 6836 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6837 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6838 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6839 | if (ret) { |
| 6840 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6841 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6842 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6843 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6844 | |
| 6845 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6846 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6847 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6848 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6849 | goto out; |
| 6850 | } |
| 6851 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6852 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6853 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6854 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6855 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6856 | } |
| 6857 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6858 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6859 | struct btrfs_dev_item *dev_item) |
| 6860 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6861 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6862 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6863 | struct btrfs_device *device; |
| 6864 | u64 devid; |
| 6865 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6866 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6867 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6868 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6869 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6870 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6871 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6872 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6873 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6874 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6875 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6876 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6877 | if (IS_ERR(fs_devices)) |
| 6878 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6879 | } |
| 6880 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6881 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6882 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6883 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6884 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6885 | btrfs_report_missing_device(fs_info, devid, |
| 6886 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6887 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6888 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6889 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6890 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6891 | if (IS_ERR(device)) { |
| 6892 | btrfs_err(fs_info, |
| 6893 | "failed to add missing dev %llu: %ld", |
| 6894 | devid, PTR_ERR(device)); |
| 6895 | return PTR_ERR(device); |
| 6896 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6897 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6898 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6899 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6900 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6901 | btrfs_report_missing_device(fs_info, |
| 6902 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6903 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6904 | } |
| 6905 | btrfs_report_missing_device(fs_info, devid, |
| 6906 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6907 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6908 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6909 | if (!device->bdev && |
| 6910 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6911 | /* |
| 6912 | * this happens when a device that was properly setup |
| 6913 | * in the device info lists suddenly goes bad. |
| 6914 | * device->bdev is NULL, and so we have to set |
| 6915 | * device->missing to one here |
| 6916 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6917 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6918 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6919 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6920 | |
| 6921 | /* Move the device to its own fs_devices */ |
| 6922 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6923 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6924 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6925 | |
| 6926 | list_move(&device->dev_list, &fs_devices->devices); |
| 6927 | device->fs_devices->num_devices--; |
| 6928 | fs_devices->num_devices++; |
| 6929 | |
| 6930 | device->fs_devices->missing_devices--; |
| 6931 | fs_devices->missing_devices++; |
| 6932 | |
| 6933 | device->fs_devices = fs_devices; |
| 6934 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6935 | } |
| 6936 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6937 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6938 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6939 | if (device->generation != |
| 6940 | btrfs_device_generation(leaf, dev_item)) |
| 6941 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6942 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6943 | |
| 6944 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6945 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6946 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6947 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6948 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6949 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6950 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6951 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6952 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6953 | return ret; |
| 6954 | } |
| 6955 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6956 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6957 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6958 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6959 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6960 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6961 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6962 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6963 | u8 *array_ptr; |
| 6964 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6965 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6966 | u32 num_stripes; |
| 6967 | u32 array_size; |
| 6968 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6969 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6970 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6971 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6972 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6973 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6974 | /* |
| 6975 | * This will create extent buffer of nodesize, superblock size is |
| 6976 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6977 | * overallocate but we can keep it as-is, only the first page is used. |
| 6978 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6979 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6980 | if (IS_ERR(sb)) |
| 6981 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6982 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6983 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6984 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6985 | * 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] | 6986 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6987 | * pages up-to-date when the page is larger: extent does not cover the |
| 6988 | * whole page and consequently check_page_uptodate does not find all |
| 6989 | * the page's extents up-to-date (the hole beyond sb), |
| 6990 | * write_extent_buffer then triggers a WARN_ON. |
| 6991 | * |
| 6992 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6993 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6994 | * to silence the warning eg. on PowerPC 64. |
| 6995 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6996 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6997 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6998 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6999 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7000 | array_size = btrfs_super_sys_array_size(super_copy); |
| 7001 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7002 | array_ptr = super_copy->sys_chunk_array; |
| 7003 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 7004 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7005 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7006 | while (cur_offset < array_size) { |
| 7007 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7008 | len = sizeof(*disk_key); |
| 7009 | if (cur_offset + len > array_size) |
| 7010 | goto out_short_read; |
| 7011 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7012 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 7013 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7014 | array_ptr += len; |
| 7015 | sb_array_offset += len; |
| 7016 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7017 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7018 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7019 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7020 | /* |
| 7021 | * At least one btrfs_chunk with one stripe must be |
| 7022 | * present, exact stripe count check comes afterwards |
| 7023 | */ |
| 7024 | len = btrfs_chunk_item_size(1); |
| 7025 | if (cur_offset + len > array_size) |
| 7026 | goto out_short_read; |
| 7027 | |
| 7028 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7029 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7030 | btrfs_err(fs_info, |
| 7031 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 7032 | num_stripes, cur_offset); |
| 7033 | ret = -EIO; |
| 7034 | break; |
| 7035 | } |
| 7036 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7037 | type = btrfs_chunk_type(sb, chunk); |
| 7038 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7039 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 7040 | "invalid chunk type %llu in sys_array at offset %u", |
| 7041 | type, cur_offset); |
| 7042 | ret = -EIO; |
| 7043 | break; |
| 7044 | } |
| 7045 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7046 | len = btrfs_chunk_item_size(num_stripes); |
| 7047 | if (cur_offset + len > array_size) |
| 7048 | goto out_short_read; |
| 7049 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7050 | ret = read_one_chunk(&key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7051 | if (ret) |
| 7052 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7053 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7054 | btrfs_err(fs_info, |
| 7055 | "unexpected item type %u in sys_array at offset %u", |
| 7056 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7057 | ret = -EIO; |
| 7058 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7059 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 7060 | array_ptr += len; |
| 7061 | sb_array_offset += len; |
| 7062 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7063 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7064 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7065 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 7066 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7067 | |
| 7068 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7069 | 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] | 7070 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 7071 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 7072 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 7073 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7074 | } |
| 7075 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7076 | /* |
| 7077 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 7078 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7079 | * If the @failing_dev is specified, it's accounted as missing. |
| 7080 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7081 | * Return true if all chunks meet the minimal RW mount requirements. |
| 7082 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 7083 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7084 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 7085 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7086 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7087 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7088 | struct extent_map *em; |
| 7089 | u64 next_start = 0; |
| 7090 | bool ret = true; |
| 7091 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7092 | read_lock(&map_tree->lock); |
| 7093 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 7094 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7095 | /* No chunk at all? Return false anyway */ |
| 7096 | if (!em) { |
| 7097 | ret = false; |
| 7098 | goto out; |
| 7099 | } |
| 7100 | while (em) { |
| 7101 | struct map_lookup *map; |
| 7102 | int missing = 0; |
| 7103 | int max_tolerated; |
| 7104 | int i; |
| 7105 | |
| 7106 | map = em->map_lookup; |
| 7107 | max_tolerated = |
| 7108 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 7109 | map->type); |
| 7110 | for (i = 0; i < map->num_stripes; i++) { |
| 7111 | struct btrfs_device *dev = map->stripes[i].dev; |
| 7112 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 7113 | if (!dev || !dev->bdev || |
| 7114 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7115 | dev->last_flush_error) |
| 7116 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7117 | else if (failing_dev && failing_dev == dev) |
| 7118 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7119 | } |
| 7120 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7121 | if (!failing_dev) |
| 7122 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7123 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7124 | em->start, missing, max_tolerated); |
| 7125 | free_extent_map(em); |
| 7126 | ret = false; |
| 7127 | goto out; |
| 7128 | } |
| 7129 | next_start = extent_map_end(em); |
| 7130 | free_extent_map(em); |
| 7131 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7132 | read_lock(&map_tree->lock); |
| 7133 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7134 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7135 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7136 | } |
| 7137 | out: |
| 7138 | return ret; |
| 7139 | } |
| 7140 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7141 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7142 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7143 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7144 | struct btrfs_path *path; |
| 7145 | struct extent_buffer *leaf; |
| 7146 | struct btrfs_key key; |
| 7147 | struct btrfs_key found_key; |
| 7148 | int ret; |
| 7149 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7150 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7151 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7152 | path = btrfs_alloc_path(); |
| 7153 | if (!path) |
| 7154 | return -ENOMEM; |
| 7155 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7156 | /* |
| 7157 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7158 | * otherwise we don't need it. |
| 7159 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7160 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7161 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7162 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7163 | /* |
| 7164 | * Read all device items, and then all the chunk items. All |
| 7165 | * device items are found before any chunk item (their object id |
| 7166 | * is smaller than the lowest possible object id for a chunk |
| 7167 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7168 | */ |
| 7169 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7170 | key.offset = 0; |
| 7171 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7172 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7173 | if (ret < 0) |
| 7174 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7175 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7176 | leaf = path->nodes[0]; |
| 7177 | slot = path->slots[0]; |
| 7178 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7179 | ret = btrfs_next_leaf(root, path); |
| 7180 | if (ret == 0) |
| 7181 | continue; |
| 7182 | if (ret < 0) |
| 7183 | goto error; |
| 7184 | break; |
| 7185 | } |
| 7186 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7187 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7188 | struct btrfs_dev_item *dev_item; |
| 7189 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7190 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7191 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7192 | if (ret) |
| 7193 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7194 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7195 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7196 | struct btrfs_chunk *chunk; |
| 7197 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7198 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7199 | if (ret) |
| 7200 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7201 | } |
| 7202 | path->slots[0]++; |
| 7203 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7204 | |
| 7205 | /* |
| 7206 | * After loading chunk tree, we've got all device information, |
| 7207 | * do another round of validation checks. |
| 7208 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7209 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7210 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7211 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7212 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7213 | total_dev); |
| 7214 | ret = -EINVAL; |
| 7215 | goto error; |
| 7216 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7217 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7218 | fs_info->fs_devices->total_rw_bytes) { |
| 7219 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7220 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7221 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7222 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7223 | ret = -EINVAL; |
| 7224 | goto error; |
| 7225 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7226 | ret = 0; |
| 7227 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7228 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7229 | mutex_unlock(&uuid_mutex); |
| 7230 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7231 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7232 | return ret; |
| 7233 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7234 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7235 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7236 | { |
| 7237 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7238 | struct btrfs_device *device; |
| 7239 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7240 | while (fs_devices) { |
| 7241 | mutex_lock(&fs_devices->device_list_mutex); |
| 7242 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7243 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7244 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7245 | |
| 7246 | fs_devices = fs_devices->seed; |
| 7247 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
David Sterba | 1dc990d | 2019-08-21 20:05:32 +0200 | [diff] [blame] | 7250 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
| 7251 | const struct btrfs_dev_stats_item *ptr, |
| 7252 | int index) |
| 7253 | { |
| 7254 | u64 val; |
| 7255 | |
| 7256 | read_extent_buffer(eb, &val, |
| 7257 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7258 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7259 | sizeof(val)); |
| 7260 | return val; |
| 7261 | } |
| 7262 | |
| 7263 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, |
| 7264 | struct btrfs_dev_stats_item *ptr, |
| 7265 | int index, u64 val) |
| 7266 | { |
| 7267 | write_extent_buffer(eb, &val, |
| 7268 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7269 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7270 | sizeof(val)); |
| 7271 | } |
| 7272 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7273 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7274 | { |
| 7275 | struct btrfs_key key; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7276 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7277 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7278 | struct extent_buffer *eb; |
| 7279 | int slot; |
| 7280 | int ret = 0; |
| 7281 | struct btrfs_device *device; |
| 7282 | struct btrfs_path *path = NULL; |
| 7283 | int i; |
| 7284 | |
| 7285 | path = btrfs_alloc_path(); |
Anand Jain | 3b80a984 | 2019-08-21 17:26:32 +0800 | [diff] [blame] | 7286 | if (!path) |
| 7287 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7288 | |
| 7289 | mutex_lock(&fs_devices->device_list_mutex); |
| 7290 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7291 | int item_size; |
| 7292 | struct btrfs_dev_stats_item *ptr; |
| 7293 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7294 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7295 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7296 | key.offset = device->devid; |
| 7297 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7298 | if (ret) { |
Anand Jain | ae4b9b4 | 2019-08-07 16:21:20 +0800 | [diff] [blame] | 7299 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7300 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7301 | device->dev_stats_valid = 1; |
| 7302 | btrfs_release_path(path); |
| 7303 | continue; |
| 7304 | } |
| 7305 | slot = path->slots[0]; |
| 7306 | eb = path->nodes[0]; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7307 | item_size = btrfs_item_size_nr(eb, slot); |
| 7308 | |
| 7309 | ptr = btrfs_item_ptr(eb, slot, |
| 7310 | struct btrfs_dev_stats_item); |
| 7311 | |
| 7312 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7313 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7314 | btrfs_dev_stat_set(device, i, |
| 7315 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7316 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7317 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7318 | } |
| 7319 | |
| 7320 | device->dev_stats_valid = 1; |
| 7321 | btrfs_dev_stat_print_on_load(device); |
| 7322 | btrfs_release_path(path); |
| 7323 | } |
| 7324 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7325 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7326 | btrfs_free_path(path); |
| 7327 | return ret < 0 ? ret : 0; |
| 7328 | } |
| 7329 | |
| 7330 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7331 | struct btrfs_device *device) |
| 7332 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7333 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7334 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7335 | struct btrfs_path *path; |
| 7336 | struct btrfs_key key; |
| 7337 | struct extent_buffer *eb; |
| 7338 | struct btrfs_dev_stats_item *ptr; |
| 7339 | int ret; |
| 7340 | int i; |
| 7341 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7342 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7343 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7344 | key.offset = device->devid; |
| 7345 | |
| 7346 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7347 | if (!path) |
| 7348 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7349 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7350 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7351 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7352 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7353 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7354 | goto out; |
| 7355 | } |
| 7356 | |
| 7357 | if (ret == 0 && |
| 7358 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7359 | /* need to delete old one and insert a new one */ |
| 7360 | ret = btrfs_del_item(trans, dev_root, path); |
| 7361 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7362 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7363 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7364 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7365 | goto out; |
| 7366 | } |
| 7367 | ret = 1; |
| 7368 | } |
| 7369 | |
| 7370 | if (ret == 1) { |
| 7371 | /* need to insert a new item */ |
| 7372 | btrfs_release_path(path); |
| 7373 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7374 | &key, sizeof(*ptr)); |
| 7375 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7376 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7377 | "insert dev_stats item for device %s failed %d", |
| 7378 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7379 | goto out; |
| 7380 | } |
| 7381 | } |
| 7382 | |
| 7383 | eb = path->nodes[0]; |
| 7384 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7385 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7386 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7387 | btrfs_dev_stat_read(device, i)); |
| 7388 | btrfs_mark_buffer_dirty(eb); |
| 7389 | |
| 7390 | out: |
| 7391 | btrfs_free_path(path); |
| 7392 | return ret; |
| 7393 | } |
| 7394 | |
| 7395 | /* |
| 7396 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7397 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7398 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7399 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7400 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7401 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7402 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7403 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7404 | int ret = 0; |
| 7405 | |
| 7406 | mutex_lock(&fs_devices->device_list_mutex); |
| 7407 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7408 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7409 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7410 | continue; |
| 7411 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7412 | |
| 7413 | /* |
| 7414 | * There is a LOAD-LOAD control dependency between the value of |
| 7415 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7416 | * reading the in-memory counters. Such control dependencies |
| 7417 | * require explicit read memory barriers. |
| 7418 | * |
| 7419 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7420 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7421 | * barrier implied by atomic_xchg in |
| 7422 | * btrfs_dev_stats_read_and_reset |
| 7423 | */ |
| 7424 | smp_rmb(); |
| 7425 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7426 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7427 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7428 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7429 | } |
| 7430 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7431 | |
| 7432 | return ret; |
| 7433 | } |
| 7434 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7435 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7436 | { |
| 7437 | btrfs_dev_stat_inc(dev, index); |
| 7438 | btrfs_dev_stat_print_on_error(dev); |
| 7439 | } |
| 7440 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7441 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7442 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7443 | if (!dev->dev_stats_valid) |
| 7444 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7445 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7446 | "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] | 7447 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7448 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7449 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7450 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7451 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7452 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7453 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7454 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7455 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7456 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7457 | int i; |
| 7458 | |
| 7459 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7460 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7461 | break; |
| 7462 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7463 | return; /* all values == 0, suppress message */ |
| 7464 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7465 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7466 | "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] | 7467 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7468 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7469 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7470 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7471 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7472 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7473 | } |
| 7474 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7475 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7476 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7477 | { |
| 7478 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7479 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7480 | int i; |
| 7481 | |
| 7482 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7483 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7484 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7485 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7486 | |
| 7487 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7488 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7489 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7490 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7491 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7492 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7493 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7494 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7495 | if (stats->nr_items > i) |
| 7496 | stats->values[i] = |
| 7497 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7498 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7499 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7500 | } |
| 7501 | } else { |
| 7502 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7503 | if (stats->nr_items > i) |
| 7504 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7505 | } |
| 7506 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7507 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7508 | return 0; |
| 7509 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7510 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7511 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7512 | { |
| 7513 | struct buffer_head *bh; |
| 7514 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7515 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7516 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7517 | if (!bdev) |
| 7518 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7519 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7520 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7521 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7522 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7523 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7524 | continue; |
| 7525 | |
| 7526 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7527 | |
| 7528 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7529 | set_buffer_dirty(bh); |
| 7530 | sync_dirty_buffer(bh); |
| 7531 | brelse(bh); |
| 7532 | } |
| 7533 | |
| 7534 | /* Notify udev that device has changed */ |
| 7535 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7536 | |
| 7537 | /* Update ctime/mtime for device path for libblkid */ |
| 7538 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7539 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7540 | |
| 7541 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7542 | * Update the size and bytes used for each device where it changed. This is |
| 7543 | * delayed since we would otherwise get errors while writing out the |
| 7544 | * superblocks. |
| 7545 | * |
| 7546 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7547 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7548 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7549 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7550 | struct btrfs_device *curr, *next; |
| 7551 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7552 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7553 | |
| 7554 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7555 | return; |
| 7556 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7557 | /* |
| 7558 | * We don't need the device_list_mutex here. This list is owned by the |
| 7559 | * transaction and the transaction must complete before the device is |
| 7560 | * released. |
| 7561 | */ |
| 7562 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7563 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7564 | post_commit_list) { |
| 7565 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7566 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7567 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7568 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7569 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7570 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7571 | |
| 7572 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7573 | { |
| 7574 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7575 | while (fs_devices) { |
| 7576 | fs_devices->fs_info = fs_info; |
| 7577 | fs_devices = fs_devices->seed; |
| 7578 | } |
| 7579 | } |
| 7580 | |
| 7581 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7582 | { |
| 7583 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7584 | while (fs_devices) { |
| 7585 | fs_devices->fs_info = NULL; |
| 7586 | fs_devices = fs_devices->seed; |
| 7587 | } |
| 7588 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7589 | |
| 7590 | /* |
| 7591 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7592 | */ |
| 7593 | int btrfs_bg_type_to_factor(u64 flags) |
| 7594 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7595 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7596 | |
| 7597 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7598 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7599 | |
| 7600 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7601 | |
| 7602 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7603 | u64 chunk_offset, u64 devid, |
| 7604 | u64 physical_offset, u64 physical_len) |
| 7605 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7606 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7607 | struct extent_map *em; |
| 7608 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7609 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7610 | u64 stripe_len; |
| 7611 | bool found = false; |
| 7612 | int ret = 0; |
| 7613 | int i; |
| 7614 | |
| 7615 | read_lock(&em_tree->lock); |
| 7616 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7617 | read_unlock(&em_tree->lock); |
| 7618 | |
| 7619 | if (!em) { |
| 7620 | btrfs_err(fs_info, |
| 7621 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7622 | physical_offset, devid); |
| 7623 | ret = -EUCLEAN; |
| 7624 | goto out; |
| 7625 | } |
| 7626 | |
| 7627 | map = em->map_lookup; |
| 7628 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7629 | if (physical_len != stripe_len) { |
| 7630 | btrfs_err(fs_info, |
| 7631 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7632 | physical_offset, devid, em->start, physical_len, |
| 7633 | stripe_len); |
| 7634 | ret = -EUCLEAN; |
| 7635 | goto out; |
| 7636 | } |
| 7637 | |
| 7638 | for (i = 0; i < map->num_stripes; i++) { |
| 7639 | if (map->stripes[i].dev->devid == devid && |
| 7640 | map->stripes[i].physical == physical_offset) { |
| 7641 | found = true; |
| 7642 | if (map->verified_stripes >= map->num_stripes) { |
| 7643 | btrfs_err(fs_info, |
| 7644 | "too many dev extents for chunk %llu found", |
| 7645 | em->start); |
| 7646 | ret = -EUCLEAN; |
| 7647 | goto out; |
| 7648 | } |
| 7649 | map->verified_stripes++; |
| 7650 | break; |
| 7651 | } |
| 7652 | } |
| 7653 | if (!found) { |
| 7654 | btrfs_err(fs_info, |
| 7655 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7656 | physical_offset, devid); |
| 7657 | ret = -EUCLEAN; |
| 7658 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7659 | |
| 7660 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7661 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7662 | if (!dev) { |
| 7663 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7664 | ret = -EUCLEAN; |
| 7665 | goto out; |
| 7666 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7667 | |
| 7668 | /* It's possible this device is a dummy for seed device */ |
| 7669 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7670 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7671 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7672 | if (!dev) { |
| 7673 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7674 | devid); |
| 7675 | ret = -EUCLEAN; |
| 7676 | goto out; |
| 7677 | } |
| 7678 | } |
| 7679 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7680 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7681 | btrfs_err(fs_info, |
| 7682 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7683 | devid, physical_offset, physical_len, |
| 7684 | dev->disk_total_bytes); |
| 7685 | ret = -EUCLEAN; |
| 7686 | goto out; |
| 7687 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7688 | out: |
| 7689 | free_extent_map(em); |
| 7690 | return ret; |
| 7691 | } |
| 7692 | |
| 7693 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7694 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7695 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7696 | struct extent_map *em; |
| 7697 | struct rb_node *node; |
| 7698 | int ret = 0; |
| 7699 | |
| 7700 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7701 | 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] | 7702 | em = rb_entry(node, struct extent_map, rb_node); |
| 7703 | if (em->map_lookup->num_stripes != |
| 7704 | em->map_lookup->verified_stripes) { |
| 7705 | btrfs_err(fs_info, |
| 7706 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7707 | em->start, em->map_lookup->verified_stripes, |
| 7708 | em->map_lookup->num_stripes); |
| 7709 | ret = -EUCLEAN; |
| 7710 | goto out; |
| 7711 | } |
| 7712 | } |
| 7713 | out: |
| 7714 | read_unlock(&em_tree->lock); |
| 7715 | return ret; |
| 7716 | } |
| 7717 | |
| 7718 | /* |
| 7719 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7720 | * later chunk allocation/free would cause unexpected behavior. |
| 7721 | * |
| 7722 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7723 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7724 | */ |
| 7725 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7726 | { |
| 7727 | struct btrfs_path *path; |
| 7728 | struct btrfs_root *root = fs_info->dev_root; |
| 7729 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7730 | u64 prev_devid = 0; |
| 7731 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7732 | int ret = 0; |
| 7733 | |
| 7734 | key.objectid = 1; |
| 7735 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7736 | key.offset = 0; |
| 7737 | |
| 7738 | path = btrfs_alloc_path(); |
| 7739 | if (!path) |
| 7740 | return -ENOMEM; |
| 7741 | |
| 7742 | path->reada = READA_FORWARD; |
| 7743 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7744 | if (ret < 0) |
| 7745 | goto out; |
| 7746 | |
| 7747 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7748 | ret = btrfs_next_item(root, path); |
| 7749 | if (ret < 0) |
| 7750 | goto out; |
| 7751 | /* No dev extents at all? Not good */ |
| 7752 | if (ret > 0) { |
| 7753 | ret = -EUCLEAN; |
| 7754 | goto out; |
| 7755 | } |
| 7756 | } |
| 7757 | while (1) { |
| 7758 | struct extent_buffer *leaf = path->nodes[0]; |
| 7759 | struct btrfs_dev_extent *dext; |
| 7760 | int slot = path->slots[0]; |
| 7761 | u64 chunk_offset; |
| 7762 | u64 physical_offset; |
| 7763 | u64 physical_len; |
| 7764 | u64 devid; |
| 7765 | |
| 7766 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7767 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7768 | break; |
| 7769 | devid = key.objectid; |
| 7770 | physical_offset = key.offset; |
| 7771 | |
| 7772 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7773 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7774 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7775 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7776 | /* Check if this dev extent overlaps with the previous one */ |
| 7777 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7778 | btrfs_err(fs_info, |
| 7779 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7780 | devid, physical_offset, prev_dev_ext_end); |
| 7781 | ret = -EUCLEAN; |
| 7782 | goto out; |
| 7783 | } |
| 7784 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7785 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7786 | physical_offset, physical_len); |
| 7787 | if (ret < 0) |
| 7788 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7789 | prev_devid = devid; |
| 7790 | prev_dev_ext_end = physical_offset + physical_len; |
| 7791 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7792 | ret = btrfs_next_item(root, path); |
| 7793 | if (ret < 0) |
| 7794 | goto out; |
| 7795 | if (ret > 0) { |
| 7796 | ret = 0; |
| 7797 | break; |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | /* Ensure all chunks have corresponding dev extents */ |
| 7802 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7803 | out: |
| 7804 | btrfs_free_path(path); |
| 7805 | return ret; |
| 7806 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7807 | |
| 7808 | /* |
| 7809 | * Check whether the given block group or device is pinned by any inode being |
| 7810 | * used as a swapfile. |
| 7811 | */ |
| 7812 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7813 | { |
| 7814 | struct btrfs_swapfile_pin *sp; |
| 7815 | struct rb_node *node; |
| 7816 | |
| 7817 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7818 | node = fs_info->swapfile_pins.rb_node; |
| 7819 | while (node) { |
| 7820 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7821 | if (ptr < sp->ptr) |
| 7822 | node = node->rb_left; |
| 7823 | else if (ptr > sp->ptr) |
| 7824 | node = node->rb_right; |
| 7825 | else |
| 7826 | break; |
| 7827 | } |
| 7828 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7829 | return node != NULL; |
| 7830 | } |